From adabbe0418df9daef40bef1244819fc7ff8af83e Mon Sep 17 00:00:00 2001 From: TudbuT Date: Mon, 10 Apr 2023 02:26:21 +0200 Subject: [PATCH] feat: add messaging, fix: allow tabs, fix: add =<0-5> methods to arrays =<0-5> is the counterpart to the normal <0-5> methods used for easier indexing into the first few indexes to allow arrays to be used like tuples --- messaging.spl | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/runtime.rs | 1 + src/std_fns.rs | 1 + src/stdlib.rs | 1 + std.spl | 15 +++++++++++++++ test.spl | 13 +++++++++++++ 6 files changed, 75 insertions(+) create mode 100644 messaging.spl diff --git a/messaging.spl b/messaging.spl new file mode 100644 index 0000000..4bbd7ac --- /dev/null +++ b/messaging.spl @@ -0,0 +1,44 @@ +"messaging bus, aka event bus" + +construct messaging namespace { + Message + Bus +} + +construct messaging:Message { + name + content + ; + construct { this | with name content this ; + name this:=name + content this:=content + this + } +} + +construct messaging:Bus { + subscribers + ; + construct { this | with this ; + MicroMap:new this:=subscribers + this + } + subscribe { | with message callable this ; + def entry message this:subscribers:get-or-create-entry =entry + entry:1 null eq if { + List:new entry:=1 + } + callable entry:1:push + } + publish { | with message this ; + message gettype messaging:Message eq not if { + message null messaging:Message:new =message + } + def entry message:name this:subscribers:get =entry + entry null eq not if { + { | with it ; + message it call + } entry:foreach + } + } +} diff --git a/src/runtime.rs b/src/runtime.rs index cb5049e..0166ad6 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -1082,6 +1082,7 @@ pub fn find_in_splpath(path: &str) -> Result { "iter.spl" => Err(stdlib::ITER.to_owned()), "http.spl" => Err(stdlib::HTTP.to_owned()), "stream.spl" => Err(stdlib::STREAM.to_owned()), + "messaging.spl" => Err(stdlib::MESSAGING.to_owned()), _ => Ok(path.to_owned()), } } diff --git a/src/std_fns.rs b/src/std_fns.rs index 980151b..95a0cd9 100644 --- a/src/std_fns.rs +++ b/src/std_fns.rs @@ -613,6 +613,7 @@ pub fn import(stack: &mut Stack) -> OError { "iter.spl" => Some(stdlib::ITER), "http.spl" => Some(stdlib::HTTP), "stream.spl" => Some(stdlib::STREAM), + "messaging.spl" => Some(stdlib::MESSAGING), _ => None, }; if let Some(x) = s.strip_prefix('#') { diff --git a/src/stdlib.rs b/src/stdlib.rs index 6b5bdd5..4b32444 100644 --- a/src/stdlib.rs +++ b/src/stdlib.rs @@ -3,3 +3,4 @@ pub const NET: &str = include_str!("../net.spl"); pub const ITER: &str = include_str!("../iter.spl"); pub const HTTP: &str = include_str!("../http.spl"); pub const STREAM: &str = include_str!("../stream.spl"); +pub const MESSAGING: &str = include_str!("../messaging.spl"); diff --git a/std.spl b/std.spl index 6113350..ed5c459 100644 --- a/std.spl +++ b/std.spl @@ -111,6 +111,21 @@ construct _array-ext { 4 { any | with this ; 4 this:get } + =0 { | with this ; + 0 this:set; + } + =1 { | with this ; + 1 this:set; + } + =2 { | with this ; + 2 this:set; + } + =3 { | with this ; + 3 this:set; + } + =4 { | with this ; + 4 this:set; + } } include _array-ext in array "#iter.spl" import diff --git a/test.spl b/test.spl index 22e66cf..31ecf04 100644 --- a/test.spl +++ b/test.spl @@ -1,6 +1,7 @@ "#stream.spl" import "#http.spl" import +"#messaging.spl" import "SPL tester" =program-name @@ -138,7 +139,19 @@ func main { int | with args ; } with { with e ; e:message println } + "" println + "testing messages" println + def bus messaging:Bus:new =bus + bus:subscribe <{ "testmsg1" { | with message ; message:name print " called1 1" println } } + bus:subscribe <{ "testmsg1" { | with message ; message:name print " called1 2" println } } + bus:subscribe <{ "testmsg2" { | with message ; message:name print " called2 1" println } } + bus:subscribe <{ "testmsg2" { | with message ; message:name print " called2 2" println } } + "testmsg1" bus:publish + "testmsg2" bus:publish + "testmsg1" bus:publish + "testmsg3" bus:publish + 100 }