Skip to content

Commit

Permalink
Make changes required to be compatible with gleam-lang/erlang/gleam-l…
Browse files Browse the repository at this point in the history
  • Loading branch information
sbergen committed Dec 3, 2024
1 parent a510de5 commit 1bec7d0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 26 deletions.
31 changes: 13 additions & 18 deletions src/gleam/otp/actor.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,11 @@ pub type Next(message, state) {

/// Stop handling messages and shut down.
///
Stop(ExitReason)
Stop

/// Stop handling messages with an abnormal reason.
///
StopAbnormal(reason: String)
}

pub fn continue(state: state) -> Next(message, state) {
Expand Down Expand Up @@ -255,17 +259,6 @@ pub type Spec(state, msg) {
)
}

// TODO: Check needed functionality here to be OTP compatible
fn exit_process(reason: ExitReason) -> ExitReason {
case reason {
Abnormal(reason) -> process.send_abnormal_exit(process.self(), reason)
Killed -> process.kill(process.self())
_ -> Nil
}

reason
}

fn receive_message(self: Self(state, msg)) -> Message(msg) {
let selector = case self.mode {
// When suspended we only respond to system messages
Expand Down Expand Up @@ -320,7 +313,7 @@ fn process_status_info(self: Self(state, msg)) -> StatusInfo {
)
}

fn loop(self: Self(state, msg)) -> ExitReason {
fn loop(self: Self(state, msg)) -> Nil {
case receive_message(self) {
// An OTP system message. This is handled by the actor for the programmer,
// behind the scenes.
Expand Down Expand Up @@ -358,7 +351,10 @@ fn loop(self: Self(state, msg)) -> ExitReason {
// subject or some other messsage that the programmer's selector expects.
Message(msg) ->
case self.message_handler(msg, self.state) {
Stop(reason) -> exit_process(reason)
Stop -> Nil

StopAbnormal(reason) ->
process.send_abnormal_exit(process.self(), reason)

Continue(state: state, selector: new_selector) -> {
let selector =
Expand All @@ -379,7 +375,7 @@ fn log_warning(a: Charlist, b: List(Charlist)) -> Nil
fn initialise_actor(
spec: Spec(state, msg),
ack: Subject(Result(Subject(msg), ExitReason)),
) -> ExitReason {
) -> Nil {
// This is the main subject for the actor, the one that the actor.start
// functions return.
// Once the actor has been initialised this will be sent to the parent for
Expand Down Expand Up @@ -411,8 +407,7 @@ fn initialise_actor(

// The init failed. Send the reason back to the parent, but exit normally.
Failed(reason) -> {
process.send(ack, Error(Abnormal(reason)))
exit_process(process.Normal)
process.send(ack, Error(Abnormal(dynamic.from(reason))))
}
}
}
Expand All @@ -426,7 +421,7 @@ fn init_selector(subject, selector) {
pub type StartError {
InitTimeout
InitFailed(ExitReason)
InitCrashed(Dynamic)
InitCrashed(ExitReason)
}

/// The result of starting a Gleam actor.
Expand Down
4 changes: 2 additions & 2 deletions src/gleam/otp/supervisor.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,9 @@ fn handle_exit(pid: Pid, state: State(a)) -> actor.Next(Message, State(a)) {
actor.continue(state)
}
Error(TooManyRestarts) ->
actor.Stop(process.Abnormal(
actor.StopAbnormal(
"Child processes restarted too many times within allowed period",
))
)
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/gleam/otp/actor_documentation_example_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn handle_message(
case message {
// For the `Shutdown` message we return the `actor.Stop` value, which causes
// the actor to discard any remaining messages and stop.
Shutdown -> actor.Stop(process.Normal)
Shutdown -> actor.Stop

// For the `Push` message we add the new element to the stack and return
// `actor.Continue` with this new stack, causing the actor to process any
Expand Down
9 changes: 4 additions & 5 deletions test/gleam/otp/actor_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ pub fn abnormal_exit_can_be_trapped_test() {

// Make an actor exit with an abnormal reason
let assert Ok(subject) =
actor.start(Nil, fn(_, _) { actor.Stop(process.Abnormal("reason")) })
actor.start(Nil, fn(_, _) { actor.StopAbnormal("reason") })
process.send(subject, Nil)

let trapped_reason = process.select(exits, 10)
Expand All @@ -225,7 +225,7 @@ pub fn abnormal_exit_can_be_trapped_test() {
|> should.equal(
Ok(process.ExitMessage(
process.subject_owner(subject),
process.Abnormal("reason"),
process.Abnormal(dynamic.from("reason")),
)),
)
}
Expand All @@ -237,9 +237,8 @@ pub fn killed_exit_can_be_trapped_test() {
|> process.selecting_trapped_exits(function.identity)

// Make an actor exit with a killed reason
let assert Ok(subject) =
actor.start(Nil, fn(_, _) { actor.Stop(process.Killed) })
process.send(subject, Nil)
let assert Ok(subject) = actor.start(Nil, fn(_, _) { actor.continue(Nil) })
process.kill(process.subject_owner(subject))

let trapped_reason = process.select(exits, 10)

Expand Down

0 comments on commit 1bec7d0

Please sign in to comment.