Created
June 12, 2014 09:22
-
-
Save anonymous/2de67108870c2dfdba3f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
role Toy::Thing[Str $noun] { | |
has $.parent ;#= die "no orphans"; | |
has %.descendent; # must do Toy::Thing but saying so blows up perl | |
my $.noun = $noun or die "needs name"; | |
method perform (Any $context) { ... } | |
method spawn (Toy::Thing :$child) { | |
return $child.new(parent => self); | |
} | |
method lookup (Str $path) { | |
# perl5ish | |
my ($here, $there) = $path.split('/',2); | |
if %.descendent{$here}:exists { | |
if $there ~~ Str { | |
return %.descendent{$here}.lookup($there); | |
} else { | |
return %.descendent{$here}; | |
} | |
} else { | |
...; # blow up spectacularly; | |
} | |
} | |
} | |
#use Toy::Thing; | |
class Toy::Context { | |
has Toy::Thing $!root = die "no context root"; | |
submethod BUILD(Toy::Thing :$!root) { } | |
method root { $!root } | |
method lookup(Str $path) { | |
if $path eq ''|'/' { | |
return $.root; | |
} elsif $path ~~ /^\// { | |
$.root.lookup($path.substr(1)); | |
} | |
} | |
method attach(Pair $entry) { | |
my ($to, $name) = $entry.key.flip.split('/',2).reverse>>.flip; | |
say "attach $entry.key() ($to, $name)"; | |
my $at = self.lookup($to); | |
say "found ", $at; | |
$at.descendent{$name} = $entry.value; | |
} | |
} | |
#use Toy::Thing; | |
#use Toy::Context; | |
class Toy does Toy::Thing["Toy::Main"] { | |
has Channel $!Q = Channel.new; | |
my $*SCHEDULER = ThreadPoolScheduler.new; | |
method push ($any) { | |
$!Q.send($any); | |
} | |
method go { | |
my $context = Toy::Context.new(root => self); | |
self.perform($context); | |
} | |
method perform(Toy::Context $context) { | |
say "Performing main"; | |
$*SCHEDULER.cue({ | |
for $!Q.list -> $msg { | |
say "next..."; | |
last if $msg ~~ Cool && $msg == "quit"; | |
say "Performing $msg.value() at $msg.key()"; | |
if $msg.value.perform($context) { | |
$context.attach($msg); | |
} else { | |
## ...; | |
} | |
say "still here!"; | |
# say self.descendent<foo>.filename; | |
say self; | |
} | |
say "stop"; | |
}) | |
} | |
method query { | |
say $*SCHEDULER.loads ~ " loads."; | |
} | |
} | |
#use Toy; | |
#use Toy::Thing; | |
#use Toy::Context; | |
my $a = Toy.new; | |
say "go"; | |
$a.go; | |
class CreateFile does Toy::Thing["File"] { | |
has $.filename = "foo";# = die "Need filename"; | |
method perform (Toy::Context $context) { | |
say "Examine $.filename in $context!"; | |
} | |
}; | |
say "test"; | |
my $t = $a.spawn(child => CreateFile); | |
$a.push("/foo" => $t); | |
say "sleep"; | |
sleep 3; | |
#say "exit ", $a.descendent<foo>.filename; | |
say "exit ", $a; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment