Clean up subscribe a bit, add once

This commit is contained in:
Jon Staab
2024-05-17 13:57:49 -07:00
parent 42b3b8b5e7
commit 69b8bb3b54
3 changed files with 61 additions and 42 deletions
+11
View File
@@ -254,6 +254,17 @@ export const chunks = <T>(n: number, xs: T[]) => {
return result
}
export const once = (f: (...args: any) => void) => {
let called = false
return (...args: any) => {
if (!called) {
called = true
f(...args)
}
}
}
export const batch = <T>(t: number, f: (xs: T[]) => void) => {
const xs: T[] = []
const cb = throttle(t, () => xs.length > 0 && f(xs.splice(0)))