Skip to content

Commit

Permalink
Make sub sleep/sleep-timer/sleep-until multi's
Browse files Browse the repository at this point in the history
  • Loading branch information
lizmat committed Feb 24, 2018
1 parent d3a7d7d commit 4f47386
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/core/Date.pm
Expand Up @@ -215,7 +215,9 @@ multi sub infix:«>»(Date:D $a, Date:D $b) {
$a.daycount > $b.daycount
}

sub sleep($seconds = Inf --> Nil) {
proto sub sleep(|) {*}
multi sub sleep(--> Nil) { sleep(*) }
multi sub sleep($seconds --> Nil) {
# 1e9 seconds is a large enough value that still makes VMs sleep
# larger values cause nqp::sleep() to exit immediatelly (esp. on 32-bit)
if nqp::istype($seconds,Whatever) || $seconds == Inf {
Expand All @@ -232,13 +234,16 @@ sub sleep($seconds = Inf --> Nil) {
}
}

sub sleep-timer($seconds = Inf --> Duration:D) {
proto sub sleep-timer(|) {*}
multi sub sleep-timer(--> Duration:D) { sleep-timer(*) }
multi sub sleep-timer($seconds --> Duration:D) {
my $time1 = now;
sleep($seconds);
Duration.new( ( $seconds - (now - $time1) ) max 0 );
Duration.new( ( $seconds - (now - $time1) ) max 0 )
}

sub sleep-until(Instant() $until --> Bool:D) {
proto sub sleep-until(|) {*}
multi sub sleep-until(Instant() $until --> Bool:D) {
my $seconds = $until - now;
return False if $seconds < 0;

Expand Down

0 comments on commit 4f47386

Please sign in to comment.