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

Dispatch method for repeating jobs with interval #299

Open
postcasio opened this issue Feb 18, 2019 · 5 comments
Open

Dispatch method for repeating jobs with interval #299

postcasio opened this issue Feb 18, 2019 · 5 comments

Comments

@postcasio
Copy link
Contributor

Currently the only way to create repeating dispatch jobs is with onUpdate or onRender. It would be useful to have an additional type, similar to onUpdate, that would allow repeating jobs with a specific interval.

for example:

Dispatch.every(5, callback);

Would behave similarly to onUpdate, but would be called every 5 frames instead of every frame.

@fatcerberus
Copy link
Member

You can fake this using Sphere.sleep because Dispatch supports async functions:

Dispatch.onUpdate(async () => {
    /* do stuffs */
    await Sphere.sleep(5);
});

This works because if the callback returns a promise, the engine will consider the job to be busy until after the promise resolves.

That said, might still be worth having this facility built in.

@rhuanjl
Copy link
Contributor

rhuanjl commented Feb 19, 2019

You can do this even more simply with:

function repeatJob(job, frameCount)
{
  job();
  Sphere.sleep(frameCount).then(()=>{repeatJob(job, frameCount);});
}

Only problem with that is that cancelling it would be tricky - though that could be done with a small extra trick.

@fatcerberus
Copy link
Member

I don’t know if I’d consider that version simpler... and it also has subtle timing differences in that my version runs during the update phase while yours runs the first job immediately and subsequent iterations in the tick phase (because that’s when promise reactions fire).

Also the cancellation thing, like you already said.

@fatcerberus
Copy link
Member

I thought about and I think I’m going to implement this. It seems like a common enough use case for e.g. animations and such that it would be worth having an easily-discoverable built-in function for it.

@fatcerberus
Copy link
Member

fatcerberus commented May 8, 2019

I started to implement this locally but it turns out there's no code in the engine for handling recurring jobs that fire every N frames - you can either have a recurring job that fires every single frame, or a one-time job that fires once N frames from now. You'd think that wouldn't pose a problem, but it does because one-time jobs are internally handled a bit differently than recurring ones--they go into a separate queue and everything.

I'll need to refactor things a bit internally to support this. Nothing too difficult, just a bit tedious is all.

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

No branches or pull requests

3 participants