Skip to content
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

feat: add onFrame lifecycle function #14594

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

feat: add onFrame lifecycle function #14594

wants to merge 2 commits into from

Conversation

Rich-Harris
Copy link
Member

Just an idle thought I had while washing the dishes: should we have an onFrame function? It would mean for example that this demo could be written like this:

$effect(() => {
  const context = canvas.getContext('2d');

  onFrame(() => {
    paint(context, Date.now());
  });
});

Alternatively we might want to have some sort of reactive time primitive (other than SvelteDate) so that it could be used in deriveds too. Either way not wedded to it, just an idle thought that I thought was worth a PR

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.
  • If this PR changes code within packages/svelte/src, add a changeset (npx changeset).

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Copy link

changeset-bot bot commented Dec 6, 2024

🦋 Changeset detected

Latest commit: 0060198

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Rich-Harris
Copy link
Member Author

preview: https://svelte-dev-git-preview-svelte-14594-svelte.vercel.app/

this is an automated message

Copy link
Contributor

github-actions bot commented Dec 6, 2024

Playground

pnpm add https://pkg.pr.new/svelte@14594

1 similar comment
Copy link
Contributor

github-actions bot commented Dec 6, 2024

Playground

pnpm add https://pkg.pr.new/svelte@14594

@dummdidumm
Copy link
Member

While it's nice, this does feel more of a thing for a utility or animation library. I wouldn't call it a lifecycle function at least and expose it through svelte/reactivity instead.

@ottomated
Copy link
Contributor

Adjacent to #12441

@trueadm
Copy link
Contributor

trueadm commented Dec 12, 2024

Adjacent to #12441

I'm not sure it is at all. You definitely don't want to be updating the UI with Date.now() every frame, that's going to put a lot of pressure on the runtime for no real benefit. I'd imagine you'd only want to update the time in relation to your UI requirements, such as every second or minute. This is very much a user-space problem rather than a problem that Svelte should even attempt to solve.

* @param {() => NotFunction<T> | Promise<NotFunction<T>> | (() => any)} fn
* @returns {void}
*/
export function onFrame(fn) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been thinking more about this and I think its the name I don't like. Maybe we could call it onAnimationFrame as this is really where you're meant to handle scheduling of animation related things. I worry about onFrame as it feels very close to onMount in that people will just want something to happen on the next frame, but in reality it continuously happens.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants