Skip to content

Instantly share code, notes, and snippets.

@ab5tract
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ab5tract/2c618fa8accc70464952 to your computer and use it in GitHub Desktop.
Save ab5tract/2c618fa8accc70464952 to your computer and use it in GitHub Desktop.
framerate printing
class Draw {
has $!supply;
has $!chan;
has $!interval = 1;
method new( $interval ) {
self.bless(:$interval);
}
submethod BUILD(:$!interval) {
$!chan = Channel.new;
$!supply = Supply.interval($!interval);
}
method init {
start {
my $closed = $!chan.closed;
my @q;
my $timer = $!supply.tap( -> $s { say $s ~ " " ~ @q.join(" "); @q = @(); },
done => { say "announcing finishedness" } );
loop {
if $!chan.poll -> $v {
@q.push($v);
} elsif $closed {
say "finished!";
last;
}
}
}
}
method enqueue($s) {
$!chan.send( $s );
}
method shutdown {
$!chan.close;
$!supply.done;
}
}
my $draw = Draw.new(0.5);
$draw.init;
my $c = 0;
while $c < 20 {
say "\n\n>>>> PASS $c\n";
await do for ^30 -> $x {
start {
my $delay = 5.rand;
sleep $delay;
$draw.enqueue("P<$c>:T<$x>:D<$delay>");
}
}
$c++;
}
$draw.shutdown;
@tony-o
Copy link

tony-o commented May 26, 2015

#!/usr/bin/env perl6

class Draw {
    has $!supply;
    has $!chan;
    has @!q;

    method new {
        self.bless;
    }

    submethod BUILD {
        $!chan = Channel.new;
        $!supply = Supply.interval(1);
    }

    method init {
        say "initialiazing";
        my $timer = $!supply.tap( -> $x { 
            @!q.push($x); 
            $!chan.send(@!q.join(' ')), @!q = @() if @!q.elems == 10; 
            say "time" 
        }, done => { 
            say "announcing finishedness" 
        });

        start {
            loop {
                my $q = $!chan.receive;
                my $closing;
                say $q;
            }
        }
    }

    method enqueue($s) {
        $!chan.send(@($s));
    }

    method shutdown {
        $!chan.close;
        $!supply.done;
    }
}

my $draw = Draw.new;
$draw.init;

my $s = Supply.interval(0.1);
my $t = $s.tap( -> $v { 
  $draw.enqueue("$v") 
});
sleep 5;
$draw.shutdown;

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