Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement index feature for @supply
This allows the more/done/quit to determine which supply they were called for.
  • Loading branch information
lizmat committed Apr 20, 2014
1 parent b1ace93 commit 33ea6e5
Showing 1 changed file with 37 additions and 18 deletions.
55 changes: 37 additions & 18 deletions src/core/Supply.pm
Expand Up @@ -114,7 +114,7 @@ sub on(&setup) {
submethod BUILD(:&!setup) { }

method !add_source(
$source, $lock, :&more, :&done is copy, :&quit is copy
$source, $lock, $index, :&more, :&done is copy, :&quit is copy
) {
unless defined &more {
X::Supply::On::NoMore.new.throw;
Expand All @@ -125,36 +125,53 @@ sub on(&setup) {
unless defined &quit {
&quit = -> $ex { self.quit($ex) }
}
$source.tap(
-> \val {
$lock.protect({ more(val) });
CATCH { default { self.quit($_) } }
},
done => {
$lock.protect({ done() });
CATCH { default { self.quit($_) } }
},
quit => -> $ex {
$lock.protect({ quit($ex) });
CATCH { default { self.quit($_) } }
});
if $index.defined {
$source.tap(
-> \val {
$lock.protect({ more(val,$index) });
CATCH { default { self.quit($_) } }
},
done => {
$lock.protect({ done($index) });
CATCH { default { self.quit($_) } }
},
quit => -> $ex {
$lock.protect({ quit($ex,$index) });
CATCH { default { self.quit($_) } }
});
}
else {
$source.tap(
-> \val {
$lock.protect({ more(val) });
CATCH { default { self.quit($_) } }
},
done => {
$lock.protect({ done() });
CATCH { default { self.quit($_) } }
},
quit => -> $ex {
$lock.protect({ quit($ex) });
CATCH { default { self.quit($_) } }
});
}
}

method tap(|c) {
my $sub = self.Supply::tap(|c);
my @tappers = &!setup(self);
my $lock = Lock.new;

sub add ($source, $what) {
sub add ($source, $what, $index?) {
unless $source ~~ Supply {
X::Supply::On::BadSetup.new.throw;
}
given $what {
when EnumMap {
self!add_source($source, $lock, |$what);
self!add_source($source, $lock, $index, |$what);
}
when Callable {
self!add_source($source, $lock, more => $what);
self!add_source($source, $lock, $index, more => $what);
}
default {
X::Supply::On::BadSetup.new.throw;
Expand All @@ -169,7 +186,9 @@ sub on(&setup) {
given $tap.key {
when Positional {
my $todo := $tap.value;
add( $_, $todo ) for .list;
for .list.kv -> $index, $supply {
add( $supply, $todo, $index );
}
}
when Supply {
add( $_, $tap.value );
Expand Down

0 comments on commit 33ea6e5

Please sign in to comment.