Push an action at the top of a Clock's scheduler

Hello sweet people!

I was wondering if it would be possible to push one action to the top of a Clock's scheduler:

t = TempoClock.default;

//Can the second one be executed before the first?
//maybe with like a "force:true" argument?
(
t.sched(1, {"first".postln});
t.sched(1, {"hey, i wanna be first!".postln});
//t.sched(1, {"hey, i wanna be first!".postln}, force:true); //??
)

//Dirty fix, 0.0000000000000569 is the smallest number I could find
(
t.sched(1, {"first".postln});
t.sched(1 - 0.0000000000000569, {"hey, i wanna be first!".postln});
)

Of course I know that to do so I should execute the second .sched first, but this is a very simplified example from a much more complex nested Class situation.

I think a non-dirty solution would be to maintain your own queue of pending actions, instead of relying on the clock to manage the order.

That is, if you have a data structure that stores, for each task, a wake-up time, a priority number and the task, then your object could pop all the tasks with the same time and sort them by priority order before executing them.

It’s not necessarily easy but it would work.

Otherwise, the only way to force order on a clock is to give the tasks different times (as you did).

hjh

I see, thanks! Also, is there any specific reason that I found 0.0000000000000569 to be the smallest valid number (is it close to a Clock's smallest tick?) ? Can I rely on it being platform independent?

I wouldn’t rely on it. I think the specific “smallest tick” will depend on the current time value, because precision shifts with the order of magnitude, so if you let it run for awhile, the “smallest tick” will get longer.

But even if you multiply that by a thousand, you still won’t be able to perceive the gap.

hjh

1 Like