-
Notifications
You must be signed in to change notification settings - Fork 147
feat(raf): add frameloop utils. #824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
royeden
wants to merge
4
commits into
solidjs-community:main
Choose a base branch
from
royeden:feat/raf/frameloop-management
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+551
−6
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d005f2e
feat(raf): add frameloop utils.
royeden 1a8b2e8
feat(raf): Addressed PR comments, renamed new functions and added doc…
royeden 95c5838
Merge branch 'solidjs-community:main' into feat/raf/frameloop-management
royeden 74ab832
Merge branch 'solidjs-community:main' into feat/raf/frameloop-management
royeden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| import { type MaybeAccessor, noop } from "@solid-primitives/utils"; | ||
| import { createHydratableSingletonRoot } from "@solid-primitives/rootless"; | ||
| import { ReactiveSet } from "@solid-primitives/set"; | ||
| import { access, type MaybeAccessor, noop } from "@solid-primitives/utils"; | ||
| import { createSignal, createMemo, type Accessor, onCleanup } from "solid-js"; | ||
| import { isServer } from "solid-js/web"; | ||
|
|
||
|
|
@@ -23,7 +25,7 @@ function createRAF( | |
| return [() => false, noop, noop]; | ||
| } | ||
| const [running, setRunning] = createSignal(false); | ||
| let requestID = 0; | ||
| let requestID: number | null = null; | ||
|
|
||
| const loop: FrameRequestCallback = timeStamp => { | ||
| requestID = requestAnimationFrame(loop); | ||
|
|
@@ -36,7 +38,116 @@ function createRAF( | |
| }; | ||
| const stop = () => { | ||
| setRunning(false); | ||
| cancelAnimationFrame(requestID); | ||
| if (requestID !== null) cancelAnimationFrame(requestID); | ||
| }; | ||
|
|
||
| onCleanup(stop); | ||
| return [running, start, stop]; | ||
| } | ||
|
|
||
| /** | ||
| * Returns an advanced primitive factory function (that has an API similar to `createRAF`) to handle multiple animation frame callbacks in a single batched `requestAnimationFrame`, avoiding the overhead of scheduling multiple animation frames outside of a batch and making them all sync on the same delta. | ||
|
||
| * | ||
| * This is a [singleton root](https://github.com/solidjs-community/solid-primitives/tree/main/packages/rootless#createSingletonRoot) primitive. | ||
| * | ||
| * @returns Returns a factory function that works like `createRAF` but handles all scheduling in the same frame batch and optionally automatically starts and stops the global loop. | ||
| * ```ts | ||
| * (callback: FrameRequestCallback, automatic?: boolean) => [queued: Accessor<boolean>, queue: VoidFunction, dequeue: VoidFunction, running: Accessor<boolean>, start: VoidFunction, stop: VoidFunction] | ||
| * ``` | ||
| * | ||
| * @example | ||
| * const createScheduledFrame = useFrameloop(); | ||
| * | ||
| * const [queued, queue, dequeue, running, start, stop] = createScheduledFrame(() => { | ||
| * el.style.transform = "translateX(...)" | ||
| * }); | ||
| */ | ||
| const useFrameloop = createHydratableSingletonRoot< | ||
| ( | ||
| callback: FrameRequestCallback, | ||
| automatic?: MaybeAccessor<boolean>, | ||
| ) => [ | ||
| queued: Accessor<boolean>, | ||
| queue: VoidFunction, | ||
royeden marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| dequeue: VoidFunction, | ||
| running: Accessor<boolean>, | ||
| start: VoidFunction, | ||
| stop: VoidFunction, | ||
| ] | ||
| >(() => { | ||
| if (isServer) return () => [() => false, noop, noop, () => false, noop, noop]; | ||
|
|
||
| const frameCallbacks = new ReactiveSet<FrameRequestCallback>(); | ||
|
|
||
| const [running, start, stop] = createRAF(delta => { | ||
| frameCallbacks.forEach(frameCallback => { | ||
| frameCallback(delta); | ||
| }); | ||
| }); | ||
|
|
||
| return function createFrame(callback: FrameRequestCallback, automatic = false) { | ||
| const queued = () => frameCallbacks.has(callback); | ||
| const queue = () => { | ||
| frameCallbacks.add(callback); | ||
| if (access(automatic) && !running()) start(); | ||
| }; | ||
| const dequeue = () => { | ||
| frameCallbacks.delete(callback); | ||
| if (running() && frameCallbacks.size === 0) stop(); | ||
| }; | ||
|
|
||
| onCleanup(dequeue); | ||
| return [queued, queue, dequeue, running, start, stop]; | ||
| }; | ||
| }); | ||
|
|
||
| /** | ||
| * An advanced primitive creating reactive scheduled frameloops, for example [motion's frame util](https://motion.dev/docs/frame), that are automatically disposed onCleanup. | ||
| * | ||
| * The idea behind this is for more complex use cases, where you need scheduling and want to avoid potential issues arising from running more than one `requestAnimationFrame`. | ||
| * | ||
| * @see https://github.com/solidjs-community/solid-primitives/tree/main/packages/raf#createScheduledFrameloop | ||
royeden marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * @param schedule The function that receives the callback and handles scheduling the frameloop | ||
| * @param cancel The function that cancels the scheduled callback | ||
| * @param callback The callback to run each scheduled frame | ||
| * @returns Returns a signal if currently running as well as start and stop methods | ||
| * ```ts | ||
| * [running: Accessor<boolean>, start: VoidFunction, stop: VoidFunction] | ||
| * ``` | ||
| * | ||
| * @example | ||
| * import { type FrameData, cancelFrame, frame } from "motion"; | ||
| * | ||
| * const [running, start, stop] = createScheduledFrameloop( | ||
| * callback => frame.update(callback, true), | ||
| * cancelFrame, | ||
| * (data: FrameData) => { | ||
| * // Do something with the data.delta during the `update` phase. | ||
| * }, | ||
| * ); | ||
| */ | ||
| function createScheduledFrameloop< | ||
| RequestID extends NonNullable<unknown>, | ||
| Callback extends (...args: Array<any>) => any, | ||
| >( | ||
| schedule: (callback: Callback) => RequestID, | ||
| cancel: (requestID: RequestID) => void, | ||
| callback: Callback, | ||
| ): [running: Accessor<boolean>, start: VoidFunction, stop: VoidFunction] { | ||
| if (isServer) { | ||
| return [() => false, noop, noop]; | ||
| } | ||
| const [running, setRunning] = createSignal(false); | ||
| let requestID: RequestID | null = null; | ||
|
|
||
| const start = () => { | ||
| if (running()) return; | ||
| setRunning(true); | ||
| requestID = schedule(callback); | ||
| }; | ||
| const stop = () => { | ||
| setRunning(false); | ||
| if (requestID !== null) cancel(requestID); | ||
| }; | ||
|
|
||
| onCleanup(stop); | ||
|
|
@@ -131,4 +242,11 @@ function createMs(fps: MaybeAccessor<number>, limit?: MaybeAccessor<number>): Ms | |
| return Object.assign(ms, { reset, running, start, stop }); | ||
| } | ||
|
|
||
| export { createMs, createRAF, createRAF as default, targetFPS }; | ||
| export { | ||
| createMs, | ||
| createRAF, | ||
| createRAF as default, | ||
| createScheduledFrameloop, | ||
| targetFPS, | ||
| useFrameloop, | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to add your primitives to primitive.list.