-
-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- add SyncRAF class and syncRAF() factory - deprecate sidechainPartitionRAF() - add tests
- Loading branch information
1 parent
2bf4094
commit 3c17520
Showing
5 changed files
with
115 additions
and
0 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
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,76 @@ | ||
import { isNode } from "@thi.ng/checks/is-node"; | ||
import { State, type ISubscribable, type CommonOpts } from "./api.js"; | ||
import { Subscription } from "./subscription.js"; | ||
|
||
export class SyncRAF<T> extends Subscription<T, T> { | ||
queued?: T; | ||
raf?: number | NodeJS.Timeout; | ||
|
||
constructor(opts?: Partial<CommonOpts>) { | ||
super(undefined, opts); | ||
} | ||
|
||
next(x: T) { | ||
if (this.state >= State.DONE) return; | ||
this.queued = x; | ||
if (!this.raf) { | ||
const update = () => { | ||
if (this.state < State.DONE) super.next(this.queued!); | ||
this._clean(); | ||
}; | ||
this.raf = isNode() | ||
? setTimeout(update, 16) | ||
: requestAnimationFrame(update); | ||
} | ||
} | ||
|
||
done() { | ||
this._clean(); | ||
super.done(); | ||
} | ||
|
||
error(e: any) { | ||
this._clean(); | ||
return super.error(e); | ||
} | ||
|
||
protected _clean() { | ||
if (this.raf) { | ||
isNode() | ||
? clearTimeout(this.raf) | ||
: cancelAnimationFrame(<number>this.raf); | ||
} | ||
this.raf = this.queued = undefined; | ||
} | ||
} | ||
|
||
/** | ||
* Similar to (in in effect the same as the now deprecated) | ||
* {@link sidechainPartitionRAF}, however more performant & lightweight. | ||
* Synchronizes downstream processing w/ `requestAnimationFrame()`. The returned | ||
* subscription delays & debounces any high frequency intra-frame input values | ||
* and passes only most recent one downstream during next RAF event processing. | ||
* | ||
* This example uses thi.ng/atom as state container. Also see {@link fromAtom}. | ||
* | ||
* @example | ||
* ```ts | ||
* const atom = defAtom("alice"); | ||
* | ||
* // any changes to the atom will only be received by this subscription | ||
* // during next RAF update cycle | ||
* syncRAF(fromAtom(atom)).subscribe({ | ||
* next({ name }) { document.body.innerText = name; } | ||
* }); | ||
* | ||
* // trigger update | ||
* atom.reset("bob"); | ||
* ``` | ||
* | ||
* @param src - | ||
* @param opts - | ||
*/ | ||
export const syncRAF = <T>( | ||
parent: ISubscribable<T>, | ||
opts?: Partial<CommonOpts> | ||
) => parent.subscribe(new SyncRAF<T>(opts)); |
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,33 @@ | ||
import { group } from "@thi.ng/testament"; | ||
import * as assert from "assert"; | ||
import { syncRAF, stream, State } from "../src/index.js"; | ||
|
||
group("syncRAF", { | ||
basic: ({ done }) => { | ||
const a = stream(); | ||
const a2 = syncRAF(a); | ||
a.next(1); | ||
assert.strictEqual(a.deref(), 1); | ||
assert.strictEqual(a2.deref(), undefined); | ||
setTimeout(() => { | ||
assert.strictEqual(a.deref(), 2); | ||
assert.strictEqual(a2.deref(), 2); | ||
done(); | ||
}, 20); | ||
a.next(2); | ||
}, | ||
|
||
"early done": ({ done }) => { | ||
const a = stream(); | ||
const a2 = syncRAF(a); | ||
a.next(1); | ||
a.done(); | ||
setTimeout(() => { | ||
assert.strictEqual(a.getState(), State.UNSUBSCRIBED); | ||
assert.strictEqual(a.deref(), undefined); | ||
assert.strictEqual(a2.getState(), State.UNSUBSCRIBED); | ||
assert.strictEqual(a2.deref(), undefined); | ||
done(); | ||
}, 20); | ||
}, | ||
}); |