Skip to content

Instantly share code, notes, and snippets.

@cowboyd
Created January 19, 2024 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cowboyd/29e297edc80872c5e17b128f83ad3d28 to your computer and use it in GitHub Desktop.
Save cowboyd/29e297edc80872c5e17b128f83ad3d28 to your computer and use it in GitHub Desktop.
Effection Request Animation Frame Stream
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