-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
45 additions
and
69 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,5 @@ | ||
# Changelog | ||
|
||
## v1.0.0 - 2024-01-02 | ||
|
||
- Initial release. |
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 |
---|---|---|
@@ -1,64 +1,33 @@ | ||
import gleam/erlang/process.{ | ||
type Pid, type ProcessMonitor, type Selector, type Subject, | ||
} | ||
import gleam/erlang/process.{type Pid, type ProcessMonitor, type Selector} | ||
import gleam/dict.{type Dict} | ||
import gleam/list | ||
|
||
// TODO: document | ||
pub opaque type ProcessWaiter { | ||
ProcessWaiter(pids: List(Pid), monitors: Dict(Pid, ProcessMonitor)) | ||
} | ||
|
||
// TODO: document | ||
pub fn new() -> ProcessWaiter { | ||
ProcessWaiter([], dict.new()) | ||
} | ||
|
||
// TODO: document | ||
pub fn add_pid(waiter: ProcessWaiter, pid: Pid) -> ProcessWaiter { | ||
ProcessWaiter(..waiter, pids: [pid, ..waiter.pids]) | ||
} | ||
|
||
// TODO: document | ||
pub fn add_subject(waiter: ProcessWaiter, subject: Subject(a)) -> ProcessWaiter { | ||
add_pid(waiter, process.subject_owner(subject)) | ||
} | ||
|
||
// TODO: document | ||
pub fn wait_forever(waiter: ProcessWaiter) -> Result(Nil, ProcessWaiter) { | ||
let waiter = convert_pids_to_monitors(waiter) | ||
let selector = selector_for_monitors(waiter.monitors) | ||
wait_forever_loop(waiter, selector) | ||
/// Wait for a list of processes to exit. | ||
/// | ||
pub fn await_forever(pids: List(Pid)) -> Nil { | ||
let monitors = | ||
pids | ||
|> list.map(fn(pid) { #(pid, process.monitor_process(pid)) }) | ||
|> dict.from_list | ||
let selector = selector_for_monitors(monitors) | ||
await_forever_loop(monitors, selector) | ||
} | ||
|
||
fn wait_forever_loop( | ||
waiter: ProcessWaiter, | ||
fn await_forever_loop( | ||
pids: Dict(Pid, ProcessMonitor), | ||
selector: Selector(Pid), | ||
) -> Result(Nil, ProcessWaiter) { | ||
) -> Nil { | ||
let pid = process.select_forever(selector) | ||
let waiter = remove_monitor(waiter, pid) | ||
case dict.size(waiter.monitors) { | ||
0 -> Ok(Nil) | ||
_ -> wait_forever_loop(waiter, selector) | ||
let pids = dict.delete(pids, pid) | ||
case dict.size(pids) { | ||
0 -> Nil | ||
_ -> await_forever_loop(pids, selector) | ||
} | ||
} | ||
|
||
fn remove_monitor(waiter: ProcessWaiter, pid: Pid) -> ProcessWaiter { | ||
let monitors = dict.delete(waiter.monitors, pid) | ||
ProcessWaiter(..waiter, monitors: monitors) | ||
} | ||
|
||
fn selector_for_monitors(monitors: Dict(Pid, ProcessMonitor)) -> Selector(Pid) { | ||
dict.values(monitors) | ||
|> list.fold(process.new_selector(), fn(selector, monitor) { | ||
process.selecting_process_down(selector, monitor, fn(msg) { msg.pid }) | ||
}) | ||
} | ||
|
||
fn convert_pids_to_monitors(waiter: ProcessWaiter) -> ProcessWaiter { | ||
let monitors = | ||
waiter.pids | ||
|> list.map(fn(pid) { #(pid, process.monitor_process(pid)) }) | ||
|> dict.from_list | ||
ProcessWaiter(monitors: monitors, pids: []) | ||
} |
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