Here's something I wrote for guifa a couple of days ago. I am thinking of turning it into a simple Rakuish event loop for my projects.
Is this a fairly decent implementation, or is this just NIH for cron?
Please share your thoughts.
my %suppliers;
our %shifts is export;
my %times;
my $master = DateTime.now;
class TimeEvent {
has $.time is built;
}
my $minute-time = $master;
for <minute hour day week month year> {
%suppliers{$_} = Supplier::Preserving.new;
%shifts{$_} = -> { %suppliers{$_}.Supply };
%times{$_} = $master;
start loop {
await Promise.at(
Instant.from-posix: (
%times{$_} = %times{$_}.later( |("{ $_ }" => 1) ).truncated-to($_)
).posix
);
%suppliers{$_}.emit: TimeEvent.new( time => %times{$_} );
}
}
react {
whenever %shifts<minute>() -> $e {
CATCH { default { .message.say } }
say "Minute shifted for event { $e.gist }";
}
}
The
start loop
. Though it would look a little weird. :) BTW, another solution is to have a single looping thread and useSupply.interval
. The whole point is to minimize hidden usages of our otherwise limited thread pool.