-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Almost done with a modular PubSub example
- Loading branch information
Showing
5 changed files
with
102 additions
and
52 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
pub type Event { | ||
Event(id: Int, user: String, message: String) | ||
} | ||
|
||
pub fn next(events: List(Event), user: String, message: String) -> Event { | ||
Event(id: next_id(events), user: user, message: message) | ||
} | ||
|
||
fn next_id(chat: List(Event)) -> Int { | ||
case chat { | ||
[] -> 1 | ||
[record, ..] -> record.id + 1 | ||
} | ||
} |
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 characters
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 characters
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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import chat/pubsub | ||
import chat/server | ||
import gleam/erlang/process | ||
import gleam/otp/actor | ||
import gleam/otp/supervisor | ||
|
||
pub type Supervisor = | ||
process.Subject(supervisor.Message) | ||
|
||
pub fn start( | ||
caller: process.Subject(server.Server), | ||
) -> Result(Supervisor, actor.StartError) { | ||
supervisor.start(fn(children) { | ||
children | ||
|> supervisor.add(pubsub.childspec()) | ||
|> supervisor.add(server.childspec(caller)) | ||
}) | ||
} |
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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,59 @@ | ||
import chat/pubsub | ||
import chat/event | ||
import chat/server | ||
import chat/supervisor as chat_supervisor | ||
import gleam/erlang/process | ||
import gleam/list | ||
import gleam/otp/task | ||
|
||
pub fn chat_test() { | ||
// Good Example to start the supervision example maybe is to do it without. | ||
// Another good example is order dependency vs inheritance. | ||
let assert Ok(pubsub) = pubsub.start() | ||
let assert Ok(server) = server.start(pubsub) | ||
// Start the chat's supervision tree and retrieve the server. | ||
let caller: process.Subject(server.Server) = process.new_subject() | ||
let assert Ok(_supervisor) = chat_supervisor.start(caller) | ||
let assert Ok(server) = process.receive(caller, 100) | ||
|
||
let self: pubsub.Client = process.new_subject() | ||
// For this scenario, out of simplicity, the client is the current process. | ||
let client: Client = process.new_subject() | ||
|
||
server.connect(server, self) | ||
// Connect the client so it can receive new messages from the server. | ||
server.connect(server, client) | ||
|
||
server.send(server, "luis", "Hola Juan") | ||
server.send(server, "juan", "Hola Luis, como vas?") | ||
server.send(server, "luis", "Bien! Estas recibiendo mensajes") | ||
task.async(fn() { | ||
// Send messages from another Subject. | ||
server.send(server, "luis", "Hola Juan.") | ||
server.send(server, "juan", "Hola Luis, como vas?") | ||
server.send(server, "luis", "Bien! Recibiendo mensajes.") | ||
}) | ||
|
||
// Client should have received the messages | ||
let assert [ | ||
"luis: Hola Juan", | ||
"luis: Hola Juan.", | ||
"juan: Hola Luis, como vas?", | ||
"luis: Bien! Estas recibiendo mensajes", | ||
] = wait_for_messages(self, []) | ||
"luis: Bien! Recibiendo mensajes.", | ||
] = wait_for_messages(client, []) | ||
} | ||
|
||
// Client helpers | ||
|
||
fn wait_for_messages( | ||
subject: process.Subject(pubsub.Event), | ||
messages: List(String), | ||
) -> List(String) { | ||
case process.receive(subject, 100) { | ||
Ok(event) -> | ||
event | ||
|> build_message() | ||
type Client = | ||
process.Subject(event.Event) | ||
|
||
fn wait_for_messages(client: Client, messages: List(String)) -> List(String) { | ||
let selector = | ||
process.new_selector() | ||
|> process.selecting(client, build_message) | ||
|
||
case process.select(selector, 100) { | ||
Ok(message) -> | ||
message | ||
|> list.prepend(messages, _) | ||
|> wait_for_messages(subject, _) | ||
|> wait_for_messages(client, _) | ||
|
||
Error(Nil) -> | ||
messages | ||
|> list.reverse() | ||
} | ||
} | ||
|
||
fn build_message(event: pubsub.Event) -> String { | ||
fn build_message(event: event.Event) -> String { | ||
event.user <> ": " <> event.message | ||
} |