Created
January 19, 2024 14:23
-
-
Save cowboyd/29e297edc80872c5e17b128f83ad3d28 to your computer and use it in GitHub Desktop.
Effection Request Animation Frame Stream
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
import { createSignal, resource, type Stream } from "./mod.ts"; | |
/** | |
* Consume RAF's as a Stream. | |
* | |
* ## Example | |
* | |
* for (let timestamp of yield* each(requestAnimationFrames)) { | |
* // do work | |
* yield* each.next(); | |
* } | |
*/ | |
export const requestAnimationFrames: Stream<number, never> = resource( | |
function* (provide) { | |
let signal = createSignal<number, never>(); | |
let id = 0; | |
let callback: FrameRequestCallback = (timestamp) => { | |
signal.send(timestamp); | |
id = requestAnimationFrame(callback); | |
}; | |
id = requestAnimationFrame(callback); | |
try { | |
yield* provide(yield* signal); | |
} finally { | |
cancelAnimationFrame(id); | |
} | |
}, | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment