Add ability to listen (tap) the input
By doing something like
```
fn input_output_loop(input_output: Subject(List(String))) -> Nil {
let output = process.receive_forever(input_output)
echo output
input_output_loop(input_output)
}
```
This commit is contained in:
@@ -9,8 +9,13 @@ import mpv/control.{type Control}
|
||||
import tcp/reason
|
||||
import tcp/tcp.{type Socket}
|
||||
|
||||
type State(socket, inject_input, exit) {
|
||||
State(socket: Socket, inject_input: Subject(Key), exit: Subject(Nil))
|
||||
type State(socket, inject_input, tap_input, exit) {
|
||||
State(
|
||||
socket: Socket,
|
||||
inject_input: Subject(Key),
|
||||
tap_input: Subject(List(String)),
|
||||
exit: Subject(Nil),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn new(exit: Subject(Nil)) -> Result(Nil, String) {
|
||||
@@ -26,8 +31,11 @@ pub fn new(exit: Subject(Nil)) -> Result(Nil, String) {
|
||||
let inject_input_name = process.new_name("inject_input")
|
||||
let inject_input = process.named_subject(inject_input_name)
|
||||
|
||||
let tap_input_name = process.new_name("tap_input")
|
||||
let tap_input = process.named_subject(tap_input_name)
|
||||
|
||||
case
|
||||
actor.new(State(socket, inject_input, exit))
|
||||
actor.new(State(socket, inject_input, tap_input, exit))
|
||||
|> actor.on_message(handle_message)
|
||||
|> actor.start
|
||||
{
|
||||
@@ -41,7 +49,7 @@ pub fn new(exit: Subject(Nil)) -> Result(Nil, String) {
|
||||
let assert Ok(_) =
|
||||
process.register(process.self(), inject_input_name)
|
||||
|
||||
read_input(data, inject_input)
|
||||
read_input(data, inject_input, tap_input)
|
||||
})
|
||||
|
||||
Ok(Nil)
|
||||
@@ -52,9 +60,9 @@ pub fn new(exit: Subject(Nil)) -> Result(Nil, String) {
|
||||
}
|
||||
|
||||
fn handle_message(
|
||||
state: State(socket, inject, exit),
|
||||
state: State(socket, inject, input_output, exit),
|
||||
control: Control,
|
||||
) -> actor.Next(State(socket, inject, exit), Control) {
|
||||
) -> actor.Next(State(socket, inject, input_output, exit), Control) {
|
||||
case control {
|
||||
control.TogglePlayPause -> {
|
||||
echo "toggling play/pause"
|
||||
@@ -83,16 +91,20 @@ fn handle_message(
|
||||
/// messages to `inject_input` which will initialize the "input to key" sequence.
|
||||
/// This is useful to ultimately create a `Control` without the user having to
|
||||
/// input all of the character(s) needed.
|
||||
fn read_input(subject: Subject(Control), inject_input: Subject(Key)) -> Nil {
|
||||
fn read_input(
|
||||
subject: Subject(Control),
|
||||
inject_input: Subject(Key),
|
||||
tap_input: Subject(List(String)),
|
||||
) -> Nil {
|
||||
let buffer = case process.receive(inject_input, 1) {
|
||||
Ok(key.Continue(buffer)) -> buffer
|
||||
Ok(_) | Error(_) -> []
|
||||
}
|
||||
|
||||
let _ =
|
||||
key.read_input_until_key(buffer)
|
||||
key.read_input_until_key(buffer, tap_input)
|
||||
|> control.from_key
|
||||
|> result.map(process.send(subject, _))
|
||||
|
||||
read_input(subject, inject_input)
|
||||
read_input(subject, inject_input, tap_input)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user