Skip to content

Commit

Permalink
Added debouncing update
Browse files Browse the repository at this point in the history
  • Loading branch information
NonSpicyBurrito committed Oct 13, 2022
1 parent 5ff2f51 commit f1d8e7f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import * as vscode from 'vscode'
import { getDecorations } from './decoration'
import { debounce } from './utils'

export function activate(context: vscode.ExtensionContext) {
let tokenSource: vscode.CancellationTokenSource

let currentDecorations: Awaited<ReturnType<typeof getDecorations>> = []

context.subscriptions.push(
vscode.window.onDidChangeTextEditorSelection(async (event) => {
tokenSource?.cancel()
const updateDecorations = debounce(
async (event: vscode.TextEditorSelectionChangeEvent) => {
tokenSource = new vscode.CancellationTokenSource()

const token = tokenSource.token
Expand All @@ -28,6 +28,14 @@ export function activate(context: vscode.ExtensionContext) {
new vscode.Selection(line, 0, line, 0),
])
)
},
() => 50
)

context.subscriptions.push(
vscode.window.onDidChangeTextEditorSelection((event) => {
tokenSource?.cancel()
updateDecorations(event)
})
)
}
16 changes: 16 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export function debounce<T extends (...args: never[]) => unknown>(
fn: T,
getMs: () => number
) {
let timer: NodeJS.Timeout
return (...args: Parameters<T>) => {
clearTimeout(timer)

const ms = getMs()
if (ms > 0) {
timer = setTimeout(() => fn(...args), getMs())
} else {
fn(...args)
}
}
}

0 comments on commit f1d8e7f

Please sign in to comment.