Skip to content

Commit 7e96509

Browse files
committed
Improve call, document
1 parent d8a8761 commit 7e96509

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
- The `selecting_process_down` function has been removed.
3030
- The `deselecting_process_down` function has been removed.
3131
- The `Abnormal` variant of the `ExitReason` type now holds a `Dynamic`.
32+
- The argument ordering and labels of `call` have changed.
3233
- In the `gleam/erlang/node` module:
3334
- The `to_atom` function has been removed.
3435
- The `send` function has been removed.

src/gleam/erlang/process.gleam

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,10 @@ pub fn deselecting(
379379
/// but this function may be useful if you need to receive messages sent from
380380
/// other BEAM languages that do not use the `Subject` type.
381381
///
382+
/// This will not select messages sent via a subject even if the message has
383+
/// the same tag in the first position. This is because went a message is send
384+
/// via a subject a new tag is used that is unique and specific to that subject.
385+
///
382386
pub fn selecting_record(
383387
selector: Selector(payload),
384388
tag tag: tag,
@@ -574,23 +578,71 @@ fn perform_call(
574578
/// Send a message to a process and wait a given number of milliseconds for a
575579
/// reply.
576580
///
581+
/// ## Panics
582+
///
577583
/// This function will panic under the following circumstances:
578584
/// - The callee process exited prior to sending a reply.
579585
/// - The callee process did not send a reply within the permitted amount of
580586
/// time.
581587
/// - The subject is a named subject but no process is registered with that
582588
/// name.
583589
///
590+
/// ## Examples
591+
///
592+
/// ```gleam
593+
/// pub type Message {
594+
/// // This message variant is to be used with `call`.
595+
/// // The `reply` field contains a subject that the reply message will be
596+
/// // sent over.
597+
/// SayHello(reply_to: Subject(String), name: String)
598+
/// }
599+
///
600+
/// // Typically we make public functions that hide the details of a process'
601+
/// // message-based API.
602+
/// pub fn say_hello(subject: Subject(Message), name: String) -> String {
603+
/// // The `SayHello` message constructor is given _partially applied_ with
604+
/// // all the arguments except the reply subject, which will be supplied by
605+
/// // the `call` function itself before sending the message.
606+
/// process.call(subject, 100, SayHello(_, name))
607+
/// }
608+
///
609+
/// // This is the message handling logic used by the process that owns the
610+
/// // subject, and so receives the messages. In a real project it would be
611+
/// // within a process or some higher level abstraction like an actor, but for
612+
/// // this demonstration that has been omitted.
613+
/// pub fn handle_message(message: Message) -> Nil {
614+
/// case message {
615+
/// SayHello(reply:, name:) -> {
616+
/// let data = "Hello, " <> name <> "!"
617+
/// // The reply subject is used to send the response back.
618+
/// // If the receiver process does not sent a reply in time then the
619+
/// // caller will crash.
620+
/// process.send(reply, data)
621+
/// }
622+
/// }
623+
/// }
624+
///
625+
/// // Here is what it looks like using the functional API to call the process.
626+
/// pub fn run(subject: Subject(Message)) {
627+
/// say_hello(subject, "Lucy")
628+
/// // -> "Hello, Lucy!"
629+
/// say_hello(subject, "Nubi")
630+
/// // -> "Hello, Nubi!"
631+
/// }
632+
/// ```
633+
///
584634
pub fn call(
585635
subject: Subject(message),
586-
make_request: fn(Subject(reply)) -> message,
587-
within timeout: Int,
636+
waiting timeout: Int,
637+
sending make_request: fn(Subject(reply)) -> message,
588638
) -> reply {
589639
perform_call(subject, make_request, select(_, timeout))
590640
}
591641

592642
/// Send a message to a process and wait for a reply.
593643
///
644+
/// # Panics
645+
///
594646
/// This function will panic under the following circumstances:
595647
/// - The callee process exited prior to sending a reply.
596648
/// - The subject is a named subject but no process is registered with that

test/gleam/erlang/process_test.gleam

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ pub fn call_test() {
283283
let assert Ok(call_subject) = process.receive(parent_subject, 50)
284284

285285
// Call the child process and get a response.
286-
let assert 2 = process.call(call_subject, fn(subject) { #(1, subject) }, 50)
286+
let assert 2 = process.call(call_subject, 50, fn(subject) { #(1, subject) })
287287
}
288288

289289
pub fn call_forever_test() {

0 commit comments

Comments
 (0)