From d95533428f2ec19c98c979bb92f49ff082391cd5 Mon Sep 17 00:00:00 2001 From: Brad Cornes Date: Fri, 21 Aug 2020 13:44:24 +0100 Subject: [PATCH] Update color decorator debounce --- package-lock.json | 12 ++++++++++++ package.json | 2 ++ src/lib/registerColorDecorator.ts | 10 ++-------- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3617ee8..f79d333 100755 --- a/package-lock.json +++ b/package-lock.json @@ -1019,6 +1019,12 @@ "integrity": "sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ==", "dev": true }, + "@types/debounce": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/@types/debounce/-/debounce-1.2.0.tgz", + "integrity": "sha512-bWG5wapaWgbss9E238T0R6bfo5Fh3OkeoSt245CM7JJwVwpw6MEBCbIxLq5z8KzsE3uJhzcIuQkyiZmzV3M/Dw==", + "dev": true + }, "@types/graceful-fs": { "version": "4.1.3", "resolved": "https://registry.npmjs.org/@types/graceful-fs/-/graceful-fs-4.1.3.tgz", @@ -1960,6 +1966,12 @@ "integrity": "sha512-8P1cDi8ebZyDxUyUprBXwidoEtiQAawYPGvpfb+Dg0G6JrQ+VozwOmm91xYC0vAv1+0VmLehEPb+isg4BGUFfA==", "dev": true }, + "debounce": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/debounce/-/debounce-1.2.0.tgz", + "integrity": "sha512-mYtLl1xfZLi1m4RtQYlZgJUNQjl4ZxVnHzIR8nLLgi4q1YT8o/WM+MK/f8yfcc9s5Ir5zRaPZyZU6xs1Syoocg==", + "dev": true + }, "debug": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz", diff --git a/package.json b/package.json index f7d84e0..7815ce1 100755 --- a/package.json +++ b/package.json @@ -173,6 +173,7 @@ }, "devDependencies": { "@ctrl/tinycolor": "^3.1.0", + "@types/debounce": "^1.2.0", "@types/mocha": "^5.2.0", "@types/moo": "^0.5.3", "@types/node": "^13.9.3", @@ -182,6 +183,7 @@ "chokidar": "^3.3.1", "concurrently": "^5.1.0", "css.escape": "^1.5.1", + "debounce": "^1.2.0", "detect-indent": "^6.0.0", "dlv": "^1.1.3", "dset": "^2.0.1", diff --git a/src/lib/registerColorDecorator.ts b/src/lib/registerColorDecorator.ts index b365b8f..66d6767 100644 --- a/src/lib/registerColorDecorator.ts +++ b/src/lib/registerColorDecorator.ts @@ -1,6 +1,7 @@ import { window, workspace, ExtensionContext, TextEditor } from 'vscode' import { NotificationEmitter } from './emitter' import { LanguageClient } from 'vscode-languageclient' +import debounce from 'debounce' const colorDecorationType = window.createTextEditorDecorationType({ before: { @@ -28,7 +29,6 @@ export function registerColorDecorator( emitter: NotificationEmitter ) { let activeEditor = window.activeTextEditor - let timeout: NodeJS.Timer | undefined = undefined async function updateDecorations() { return updateDecorationsInEditor(activeEditor) @@ -76,13 +76,7 @@ export function registerColorDecorator( ) } - function triggerUpdateDecorations() { - if (timeout) { - clearTimeout(timeout) - timeout = undefined - } - timeout = setTimeout(updateDecorations, 500) - } + const triggerUpdateDecorations = debounce(updateDecorations, 200) if (activeEditor) { triggerUpdateDecorations()