Skip to content

Commit 963cd12

Browse files
committed
Rename selector functions
1 parent 075cf5a commit 963cd12

File tree

3 files changed

+130
-116
lines changed

3 files changed

+130
-116
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
functions.
2020
- The `subject_owner` function now returns a result as a named subject may not
2121
have any process registered for that name.
22-
- The `selecting_record*` functions have been replaced by the
23-
`selecting_record` function.
22+
- The `selecting_*` functions have been replaced by the
23+
`select_*` functions.
24+
- The `select*` functions have been replaced by the
25+
`selector_receive*` functions.
2426
- The `ProcessDown` type has been removed.
2527
- The `Down` type has been added.
2628
- The `selecting_monitors` function has been added.

src/gleam/erlang/process.gleam

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import gleam/dynamic/decode
33
import gleam/erlang/atom.{type Atom}
44
import gleam/erlang/port.{type Port}
55
import gleam/erlang/reference.{type Reference}
6-
import gleam/function
76
import gleam/string
87

98
/// A `Pid` (or Process identifier) is a reference to an Erlang process. Each
@@ -227,19 +226,19 @@ pub fn receive_forever(from subject: Subject(message)) -> message
227226
/// A type that enables a process to wait for messages from multiple `Subject`s
228227
/// at the same time, returning whichever message arrives first.
229228
///
230-
/// Used with the `new_selector`, `selecting`, and `select` functions.
229+
/// Used with the `new_selector`, `selector_receive`, and `select*` functions.
231230
///
232231
/// # Examples
233232
///
234233
/// ```gleam
235234
/// let int_subject = new_subject()
236-
/// let float_subject = new_subject()
235+
/// let string_subject = new_subject()
237236
/// send(int_subject, 1)
238237
///
239238
/// let selector =
240239
/// new_selector()
241-
/// |> selecting(int_subject, int.to_string)
242-
/// |> selecting(float_subject, float.to_string)
240+
/// |> select(string_subject)
241+
/// |> select_map(int_subject, int.to_string)
243242
///
244243
/// select(selector, 10)
245244
/// // -> Ok("1")
@@ -254,8 +253,8 @@ pub type Selector(payload)
254253
pub fn new_selector() -> Selector(payload)
255254

256255
/// Receive a message that has been sent to current process using any of the
257-
/// `Subject`s that have been added to the `Selector` with the `selecting`
258-
/// function.
256+
/// `Subject`s that have been added to the `Selector` with the `select*`
257+
/// functions.
259258
///
260259
/// If there is not an existing message for the `Selector` in the process'
261260
/// mailbox or one does not arrive `within` the permitted timeout then the
@@ -266,12 +265,12 @@ pub fn new_selector() -> Selector(payload)
266265
/// with it then it will not receive a message.
267266
///
268267
/// To wait forever for the next message rather than for a limited amount of
269-
/// time see the `select_forever` function.
268+
/// time see the `selector_receive_forever` function.
270269
///
271270
/// The `within` parameter specifies the timeout duration in milliseconds.
272271
///
273272
@external(erlang, "gleam_erlang_ffi", "select")
274-
pub fn select(
273+
pub fn selector_receive(
275274
from from: Selector(payload),
276275
within within: Int,
277276
) -> Result(payload, Nil)
@@ -280,7 +279,7 @@ pub fn select(
280279
/// arrive rather than timing out after a specified amount of time.
281280
///
282281
@external(erlang, "gleam_erlang_ffi", "select")
283-
pub fn select_forever(from from: Selector(payload)) -> payload
282+
pub fn selector_receive_forever(from from: Selector(payload)) -> payload
284283

285284
/// Add a transformation function to a selector. When a message is received
286285
/// using this selector the transformation function is applied to the message.
@@ -314,7 +313,7 @@ pub type ExitReason {
314313
/// sent to the process when a linked process exits the process must call the
315314
/// `trap_exit` beforehand.
316315
///
317-
pub fn selecting_trapped_exits(
316+
pub fn select_trapped_exits(
318317
selector: Selector(a),
319318
handler: fn(ExitMessage) -> a,
320319
) -> Selector(a) {
@@ -336,6 +335,21 @@ pub fn selecting_trapped_exits(
336335
@external(erlang, "gleam_erlang_ffi", "flush_messages")
337336
pub fn flush_messages() -> Nil
338337

338+
/// Add a new `Subject` to the `Selector` so that its messages can be selected
339+
/// from the receiver process inbox.
340+
///
341+
/// See `select_map` to add subjects of a different message type.
342+
//
343+
344+
/// See `deselect` to remove a subject from a selector.
345+
///
346+
pub fn select(
347+
selector: Selector(payload),
348+
for subject: Subject(payload),
349+
) -> Selector(payload) {
350+
select_map(selector, subject, fn(x) { x })
351+
}
352+
339353
/// Add a new `Subject` to the `Selector` so that its messages can be selected
340354
/// from the receiver process inbox.
341355
///
@@ -345,9 +359,9 @@ pub fn flush_messages() -> Nil
345359
/// message types. If you do not wish to transform the incoming messages in any
346360
/// way then the `identity` function can be given.
347361
///
348-
/// See `deselecting` to remove a subject from a selector.
362+
/// See `deselect` to remove a subject from a selector.
349363
///
350-
pub fn selecting(
364+
pub fn select_map(
351365
selector: Selector(payload),
352366
for subject: Subject(message),
353367
mapping transform: fn(message) -> payload,
@@ -362,7 +376,7 @@ pub fn selecting(
362376
/// Remove a new `Subject` from the `Selector` so that its messages will not be
363377
/// selected from the receiver process inbox.
364378
///
365-
pub fn deselecting(
379+
pub fn deselect(
366380
selector: Selector(payload),
367381
for subject: Subject(message),
368382
) -> Selector(payload) {
@@ -375,15 +389,15 @@ pub fn deselecting(
375389
/// Add a handler to a selector for tuple messages with a given tag in the
376390
/// first position followed by a given number of fields.
377391
///
378-
/// Typically you want to use the `selecting` function with a `Subject` instead,
392+
/// Typically you want to use the `select` function with a `Subject` instead,
379393
/// but this function may be useful if you need to receive messages sent from
380394
/// other BEAM languages that do not use the `Subject` type.
381395
///
382396
/// This will not select messages sent via a subject even if the message has
383397
/// the same tag in the first position. This is because when a message is sent
384398
/// via a subject a new tag is used that is unique and specific to that subject.
385399
///
386-
pub fn selecting_record(
400+
pub fn select_record(
387401
selector: Selector(payload),
388402
tag tag: tag,
389403
fields arity: Int,
@@ -403,7 +417,7 @@ type AnythingSelectorTag {
403417
/// is handled, or when you need to handle messages from other BEAM languages
404418
/// which do not use subjects or record format messages.
405419
///
406-
pub fn selecting_anything(
420+
pub fn select_anything(
407421
selector: Selector(payload),
408422
mapping handler: fn(Dynamic) -> payload,
409423
) -> Selector(payload) {
@@ -469,7 +483,7 @@ pub type Down {
469483
/// be received.
470484
///
471485
/// The down message can be received with a selector and the
472-
/// `selecting_monitors` function.
486+
/// `select_monitors` function.
473487
///
474488
/// The process can be demonitored with the `demonitor_process` function.
475489
///
@@ -479,28 +493,28 @@ pub fn monitor(pid: Pid) -> Monitor {
479493

480494
/// Select for a message sent for a given monitor.
481495
///
482-
/// Each monitor handler added to a selector has a selecting performance cost,
483-
/// so prefer [`selecting_monitors`](#selecting_monitors) if you are selecting
496+
/// Each monitor handler added to a selector has a select performance cost,
497+
/// so prefer [`select_monitors`](#select_monitors) if you are select
484498
/// for multiple monitors.
485499
///
486500
/// The handler can be removed from the selector later using
487-
/// [`deselecting_specific_monitor`](#deselecting_specific_monitor).
501+
/// [`deselect_specific_monitor`](#deselect_specific_monitor).
488502
///
489-
pub fn selecting_specific_monitor(
503+
pub fn select_specific_monitor(
490504
selector: Selector(payload),
491505
monitor: Monitor,
492506
mapping: fn(Down) -> payload,
493-
) -> Selector(payload) {
507+
) {
494508
insert_selector_handler(selector, monitor, mapping)
495509
}
496510

497-
/// Select for any messages sent for any monitors set up by the selecting process.
511+
/// Select for any messages sent for any monitors set up by the select process.
498512
///
499513
/// If you want to select for a specific message then use
500-
/// [`selecting_specific_monitor`](#selecting_specific_monitor), but this
514+
/// [`select_specific_monitor`](#select_specific_monitor), but this
501515
/// function is preferred if you need to select for multiple monitors.
502516
///
503-
pub fn selecting_monitors(
517+
pub fn select_monitors(
504518
selector: Selector(payload),
505519
mapping: fn(Down) -> payload,
506520
) -> Selector(payload) {
@@ -530,11 +544,11 @@ pub fn demonitor_process(monitor monitor: Monitor) -> Nil {
530544
fn erlang_demonitor_process(monitor: Monitor) -> DoNotLeak
531545

532546
/// Remove a `Monitor` from a `Selector` prevoiusly added by
533-
/// [`selecting_specific_monitor`](#selecting_specific_monitor). If
547+
/// [`select_specific_monitor`](#select_specific_monitor). If
534548
/// the `Monitor` is not in the `Selector` it will be returned
535549
/// unchanged.
536550
///
537-
pub fn deselecting_specific_monitor(
551+
pub fn deselect_specific_monitor(
538552
selector: Selector(payload),
539553
monitor: Monitor,
540554
) -> Selector(payload) {
@@ -560,8 +574,8 @@ fn perform_call(
560574
// Await a reply or handle failure modes (timeout, process down, etc)
561575
let reply =
562576
new_selector()
563-
|> selecting(reply_subject, function.identity)
564-
|> selecting_specific_monitor(monitor, fn(down) {
577+
|> select(reply_subject)
578+
|> select_specific_monitor(monitor, fn(down) {
565579
panic as { "callee exited: " <> string.inspect(down) }
566580
})
567581
|> run_selector
@@ -636,7 +650,7 @@ pub fn call(
636650
waiting timeout: Int,
637651
sending make_request: fn(Subject(reply)) -> message,
638652
) -> reply {
639-
perform_call(subject, make_request, select(_, timeout))
653+
perform_call(subject, make_request, selector_receive(_, timeout))
640654
}
641655

642656
/// Send a message to a process and wait for a reply.
@@ -652,7 +666,7 @@ pub fn call_forever(
652666
subject: Subject(message),
653667
make_request: fn(Subject(reply)) -> message,
654668
) -> reply {
655-
perform_call(subject, make_request, fn(s) { Ok(select_forever(s)) })
669+
perform_call(subject, make_request, fn(s) { Ok(selector_receive_forever(s)) })
656670
}
657671

658672
/// Creates a link between the calling process and another process.
@@ -779,7 +793,7 @@ pub fn send_abnormal_exit(pid: Pid, reason: anything) -> Nil {
779793
///
780794
/// When trapping exits (after this function is called) if a linked process
781795
/// crashes an exit message is sent to the process instead. These messages can
782-
/// be handled with the `selecting_trapped_exits` function.
796+
/// be handled with the `select_trapped_exits` function.
783797
///
784798
@external(erlang, "gleam_erlang_ffi", "trap_exits")
785799
pub fn trap_exits(a: Bool) -> Nil

0 commit comments

Comments
 (0)