Last active
January 27, 2019 05:33
Star
You must be signed in to star a gist
Revisions
-
araraloren revised this gist
Jan 27, 2019 . 2 changed files with 19 additions and 11 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,7 @@ unit class Song::Pluggable is export; state $threadlock = Lock::Async.new; has $.file; has $!lock; has @!plugins; @@ -24,11 +26,15 @@ method plugins() { } method lock() { $threadlock.protect: { $!lock.lock(); } } method unlock() { $threadlock.protect: { $!lock.unlock(); } } method find(Str $name) { @@ -56,14 +62,16 @@ method !append-plugin-to-config(Str $name, Bool $enable) { } method !make-plugin-info(Str $name, Bool $enable) { $threadlock.protect: { Info.new( enable => $enable, plugin => do { ((try require ::($name)) === Nil) ?? Any !! ::($name) }, name => $name, installed => !((try require ::($name)) === Nil), ) } } class Info { 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 charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ use Song::Pluggable; my @threads; for ^5 { @threads.push( start { given Song::Pluggable.new(file => "plugin.json") { -
araraloren created this gist
Jan 26, 2019 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ unit module Song::Command::List; 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,78 @@ unit class Song::Pluggable is export; has $.file; has $!lock; has @!plugins; submethod TWEAK() { $!lock = $!file.IO.dirname.IO.add(".lock").open(:create, :rw); } class Info { ... } method load() { my %config = Rakudo::Internals::JSON.from-json(slurp($!file)); @!plugins = []; for @(%config<plugins>) -> $plugin { @!plugins.push(self!make-plugin-info($plugin<name>.Str, $plugin<enable>.Bool)); } self; } method plugins() { @!plugins; } method lock() { $!lock.lock(); self; } method unlock() { $!lock.unlock(); self; } method find(Str $name) { @!plugins.grep(*.name eq $name); } method exists(Str $name) { @!plugins.grep(*.name eq $name).elems > 0; } method register(Str $name, Bool $enable) { if self.exists($name) { die "The plugin is already existed: $name"; } else { @!plugins.push($name, $enable); self!append-plugin-to-config($name, $enable); } } method !append-plugin-to-config(Str $name, Bool $enable) { my %config = Rakudo::Internals::JSON.from-json(slurp($!file)); %config<plugins>.push(%{ name => $name, enable => $enable }); spurt($!file, Rakudo::Internals::JSON.to-json(%config)); } method !make-plugin-info(Str $name, Bool $enable) { Info.new( enable => $enable, plugin => do { ((try require ::($name)) === Nil) ?? Any !! ::($name) }, name => $name, installed => !((try require ::($name)) === Nil), ) } class Info { has $.enable; has $.plugin; has $.installed; has $.name; method get-instance(*%_) { $!plugin.new(|%_); } } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,20 @@ { "plugins" : [ { "name" : "Song::Command::Install", "enable" : true }, { "name" : "Song::Command::Remove", "enable" : true }, { "name" : "Song::Command::List", "enable" : true }, { "name" : "Song::Command::Update", "enable" : true } ] } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,27 @@ use Song::Pluggable; my @threads; for ^2 { @threads.push( start { given Song::Pluggable.new(file => "plugin.json") { say "IN THREAD {$*THREAD.id}: ", .gist, .WHICH; .lock(); .load(); .unlock(); say "IN THREAD {$*THREAD.id}: ", .gist for .plugins(); .lock(); try .register('Song::Command::Update', True); .unlock(); say "IN THREAD {$*THREAD.id} LOAD AGAIN"; .lock(); .load(); .unlock(); say "AGAIN, IN THREAD {$*THREAD.id}: ", .gist for .plugins(); } } ) } await @threads;