diff --git a/gleam.toml b/gleam.toml index 2493151..e652086 100644 --- a/gleam.toml +++ b/gleam.toml @@ -16,7 +16,6 @@ version = "1.0.0" gleam_stdlib = "~> 0.34 or ~> 1.0" gleam_erlang = "~> 0.24" gleam_otp = "~> 0.9" -prng = "~> 3.0" [dev-dependencies] gleeunit = "~> 1.0" diff --git a/manifest.toml b/manifest.toml index 812f40e..fe91f12 100644 --- a/manifest.toml +++ b/manifest.toml @@ -2,12 +2,10 @@ # You typically do not need to edit this file packages = [ - { name = "gleam_bitwise", version = "1.3.1", build_tools = ["gleam"], requirements = [], otp_app = "gleam_bitwise", source = "hex", outer_checksum = "B36E1D3188D7F594C7FD4F43D0D2CE17561DE896202017548578B16FE1FE9EFC" }, { name = "gleam_erlang", version = "0.24.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "26BDB52E61889F56A291CB34167315780EE4AA20961917314446542C90D1C1A0" }, { name = "gleam_otp", version = "0.9.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "5FADBBEC5ECF3F8B6BE91101D432758503192AE2ADBAD5602158977341489F71" }, { name = "gleam_stdlib", version = "0.35.1", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "5443EEB74708454B65650FEBBB1EF5175057D1DEC62AEA9D7C6D96F41DA79152" }, { name = "gleeunit", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D364C87AFEB26BDB4FB8A5ABDE67D635DC9FA52D6AB68416044C35B096C6882D" }, - { name = "prng", version = "3.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_bitwise"], otp_app = "prng", source = "hex", outer_checksum = "C78A80DE41469A0BB1AB3B0B0610CCE5DB70C5659A540E2E0E6C54FA38134290" }, ] [requirements] @@ -15,4 +13,3 @@ gleam_erlang = { version = "~> 0.24" } gleam_otp = { version = "~> 0.9" } gleam_stdlib = { version = "~> 0.34 or ~> 1.0" } gleeunit = { version = "~> 1.0" } -prng = { version = "~> 3.0"} diff --git a/src/supervisors.gleam b/src/supervisors.gleam index 0fd991a..71b01c6 100644 --- a/src/supervisors.gleam +++ b/src/supervisors.gleam @@ -9,7 +9,8 @@ import gleam/erlang/process import supervisors/a_shit_actor.{type Message} as duckduckgoose pub fn main() { - let game = supervisor.worker(duckduckgoose.start) + let parent_subject = process.new_subject() + let game = supervisor.worker(duckduckgoose.start(_, parent_subject)) let children = fn(children) { children @@ -17,5 +18,17 @@ pub fn main() { } let assert Ok(_supervisor_subject) = supervisor.start(children) - // Ok, how in the hell do I get this game subject. + let assert Ok(game_subject) = process.receive(parent_subject, 1000) + + // Good messages, nothing crashes + io.debug(duckduckgoose.duck(game_subject)) + io.debug(duckduckgoose.duck(game_subject)) + io.debug(duckduckgoose.duck(game_subject)) + // // Oh shit, that aint good, our actors gonna crash + // io.debug(duckduckgoose.goose(game_subject)) + + // // No worries, supervisor turned things back on for us + // process.sleep(10_000) + // io.debug(duckduckgoose.duck(game_subject)) + // io.debug(duckduckgoose.duck(game_subject)) } diff --git a/src/supervisors/a_shit_actor.gleam b/src/supervisors/a_shit_actor.gleam index 6e1d83a..de1e208 100644 --- a/src/supervisors/a_shit_actor.gleam +++ b/src/supervisors/a_shit_actor.gleam @@ -4,20 +4,44 @@ import gleam/otp/actor import gleam/erlang/process.{type Subject} -import prng/random +import gleam/function -pub fn start(_input: Nil) -> Result(Subject(Message), actor.StartError) { - actor.start(Nil, handle_message) +pub fn start( + _input: Nil, + parent_subject: Subject(Subject(Message)), +) -> Result(Subject(Message), actor.StartError) { + actor.start_spec(actor.Spec( + init: fn() { + let actor_subject = process.new_subject() + process.send(parent_subject, actor_subject) + actor.Ready( + Nil, + process.selecting( + process.new_selector(), + actor_subject, + function.identity, + ), + ) + }, + init_timeout: 1000, + loop: handle_message, + )) } pub fn shutdown(subject: Subject(Message)) { actor.send(subject, Shutdown) } -pub fn play_game(subject: Subject(Message)) -> String { - let msg_generator = random.weighted(#(0.9, Duck), [#(0.1, Goose)]) - let msg = random.random_sample(msg_generator) - actor.call(subject, msg, 1000) +pub fn duck( + subject: Subject(Message), +) -> Result(String, process.CallError(String)) { + process.try_call(subject, Duck, 1000) +} + +pub fn goose( + subject: Subject(Message), +) -> Result(String, process.CallError(String)) { + process.try_call(subject, Goose, 1000) } pub type Message {