|
| 1 | +import gleam/erlang/process |
| 2 | +import gleam/erlang/reference |
| 3 | + |
| 4 | +pub fn send(subject: process.Subject(message), message: message) -> Nil { |
| 5 | + let assert Ok(#(pid, ref)) = unwrap_subject(subject) |
| 6 | + as "Named subjects are currently unsupported in gleam_manifold.send()" |
| 7 | + manifold_send_subject(pid, #(ref, message)) |
| 8 | + Nil |
| 9 | +} |
| 10 | + |
| 11 | +/// You almost certainly do not want to use this. |
| 12 | +/// Absolutely prefer to use the type-safe variant of |
| 13 | +/// this function `manifold.send(subject, message)` |
| 14 | +pub fn send_pid(pid: process.Pid, message: message) -> Nil { |
| 15 | + manifold_send_raw(pid, message) |
| 16 | + Nil |
| 17 | +} |
| 18 | + |
| 19 | +/// Call Manifold.send with a list of pids as the first argument, as |
| 20 | +/// is supported in Manifold |
| 21 | +/// |
| 22 | +/// Sending multi does not support sending to subjects, because |
| 23 | +/// subjects themselves EACH have a unique tag along with them. The |
| 24 | +/// subject expects the tag to be included in the message itself, so |
| 25 | +/// this would mean that all subjects need to have the same tag, which is |
| 26 | +/// impossible because they are unique. You would have to use something |
| 27 | +/// like `process.select_other` to take advantage of receiving values |
| 28 | +/// with this function still within Gleam. |
| 29 | +pub fn send_multi_pid(pids: List(process.Pid), message: message) -> Nil { |
| 30 | + manifold_send_multi(pids, message) |
| 31 | + Nil |
| 32 | +} |
| 33 | + |
| 34 | +type DoNotLeak |
| 35 | + |
| 36 | +@external(erlang, "gleam_manifold_ffi", "unwrap_subject") |
| 37 | +fn unwrap_subject( |
| 38 | + subject: process.Subject(message), |
| 39 | +) -> Result(#(process.Pid, reference.Reference), Nil) |
| 40 | + |
| 41 | +@external(erlang, "Elixir.Manifold", "send") |
| 42 | +fn manifold_send_subject( |
| 43 | + pid: process.Pid, |
| 44 | + message: #(reference.Reference, message), |
| 45 | +) -> DoNotLeak |
| 46 | + |
| 47 | +@external(erlang, "Elixir.Manifold", "send") |
| 48 | +fn manifold_send_raw(pid: process.Pid, message: message) -> DoNotLeak |
| 49 | + |
| 50 | +@external(erlang, "Elixir.Manifold", "send") |
| 51 | +fn manifold_send_multi(pid: List(process.Pid), message: message) -> DoNotLeak |
0 commit comments