-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
182 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import type { ValueCell } from "./cell"; | ||
|
||
export type Debouncer = <T>(cb: (v: T) => void | Promise<void>, v: T) => void; | ||
|
||
/** | ||
* debouncer creates a debounce function that will execute a callback after a _delay_. | ||
* | ||
* Create with `const debounce = debouncer()` | ||
* and use as `debounce(cb, v, delay)`. | ||
* @param cb callback | ||
* @param v value passed to callback | ||
* @param delay optional delay in ms, default: 750 | ||
*/ | ||
export const debouncer = ( | ||
delay = 750, | ||
working: ValueCell<boolean> | undefined = undefined | ||
): Debouncer => { | ||
// console.log({ setting: delay }); | ||
let timer: ReturnType<typeof setTimeout>; | ||
return <T>(cb: (v: T) => void | Promise<void>, v: T) => { | ||
// console.log({ called: delay }); | ||
if (working !== undefined) working.set(true); | ||
clearTimeout(timer); | ||
timer = setTimeout(async () => { | ||
// console.log({ deb: delay }); | ||
await cb(v); | ||
if (working !== undefined) working.set(false); | ||
}, delay); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import type { AnyCell, MapCell } from "./cell"; | ||
import type { SheetProxy } from "./proxy"; | ||
|
||
export const initialValue = <T>( | ||
proxy: SheetProxy, | ||
v0: T | AnyCell<T>, | ||
v: AnyCell<T>, | ||
name = "initial" | ||
): MapCell<T, true> => { | ||
const cell = proxy.new(v0, name); | ||
v.subscribe((v) => { | ||
// We do not propagate errors yet. | ||
if (v instanceof Error) return; | ||
cell.set(v); | ||
}); | ||
// We fake being a MapCell to prevent setting the cell | ||
// outside of this function. | ||
return cell as unknown as MapCell<T, true>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.