Skip to content

Instantly share code, notes, and snippets.

View ds300's full-sized avatar
πŸ³οΈβ€πŸŒˆ

David Sheldrick ds300

πŸ³οΈβ€πŸŒˆ
View GitHub Profile
{
"vim.easymotion": true,
"vim.leader": "<space>",
"files.autoSave": "afterDelay",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": ["source.addMissingImports"],
"vim.visualModeKeyBindingsNonRecursive": [
{
"before": ["<leader>", "g"],
"commands": ["editor.action.smartSelect.grow"]
@ds300
ds300 / usePoller.ts
Created September 15, 2023 09:52
usePoller.ts
import { useCallback, useEffect, useMemo } from 'react'
export class Poller<T> {
private interval: ReturnType<typeof setInterval> | null = null
constructor(
private readonly callback: (arg: { didCancelRef: { current: boolean } }) => T,
private readonly intervalMs: number
) {}
readonly didCancelRef = { current: false }
start() {
class Stats {
periods = 0
totals = {} as Record<string, number>
starts = {} as Record<string, number>
start(name: string) {
this.starts[name] = performance.now()
}
end(name: string) {
if (!this.starts[name]) throw new Error(`No start for ${name}`)