Skip to content

Commit

Permalink
More progressgit add .
Browse files Browse the repository at this point in the history
  • Loading branch information
bcpeinhardt committed Feb 28, 2024
1 parent 5ec7de3 commit f729a94
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 13 deletions.
1 change: 0 additions & 1 deletion gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
3 changes: 0 additions & 3 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@
# 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]
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"}
17 changes: 15 additions & 2 deletions src/supervisors.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,26 @@ 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
|> supervisor.add(game)
}

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))
}
38 changes: 31 additions & 7 deletions src/supervisors/a_shit_actor.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f729a94

Please sign in to comment.