1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#![deny(missing_docs)]
#![warn(missing_doc_code_examples)]
//! Message Worker is a library for Rust for the creation of event-listeners using futures and
//! streams. Notably, Message Worker supports non-sync and non-send (i.e. non-thread-safe)
//! contexts within listeners.
//!
//! This is a fairly low-level library that can be used to build a wide-array of stream-processing
//! and event-driven systems. It can even be used to build actor systems!
//!
//! This library must be used in a [tokio](https://tokio.rs/) runtime.
//!
//! The tl;dr is that if you want a worker that accepts a stream of messages/events and does
//! something upon receiving each message asynchronously... this is the library for you!
//! The key function here is `message_worker::[non_]blocking::listen(stream, || ctx, handler)`.
//!
//! The first argument is a [Stream](https://docs.rs/futures-core/0.3.15/futures_core/stream/trait.Stream.html).
//! Streams are basically asynchronous iterators and can be made from many different things including
//! mpsc/broadcast channels.
//!
//! The second argument is a closure that creates the "context" for the worker. Essentially, this is
//! any state you want your worker to have access to. With a [non_blocking](non_blocking) worker it's generally best to
//! use immutable datastructures, like those from [im](https://docs.rs/im/15.0.0/im/) if you need to modify the
//! state. With a [blocking](blocking) worker, you can simply wrap your state in a [`RefCell`](https://doc.rust-lang.org/std/cell/struct.RefCell.html).
//!
//! The third argument is the handler, which is where the magic happens. The handler is the name of a function
//! you declare with the signature `fn(ctx: Arc/Rc<Context>, msg: MessageType) -> Result<Option<Context>, Err>`.
//! If an error is returned the error handler for the worker will run. If `Ok(None)` is returned the
//! worker will continue running as-is. If `Ok(context)` is returned the worker will continue running
//! but the next time it runs it will use the new context in that return value.
//!
//! # Examples
//! ## Printer
//! ```
//! use message_worker::non_blocking::listen;
//! use message_worker::{empty_ctx, EmptyCtx};
//! use std::sync::Arc;
//! use anyhow::Result;
//!
//! let mut rt = tokio::runtime::Runtime::new().unwrap();
//! rt.block_on(async {
//!     // Create our stream
//!     let source = tokio_stream::iter(vec![42, 0xff6900, 1337]);
//!
//!     // Create a listener that prints out each item in the stream
//!     async fn on_item(_ctx: Arc<EmptyCtx>, event: usize) -> Result<Option<EmptyCtx>> {
//!         eprintln!("{}", event);
//!         Ok(None)
//!     }
//!
//!     // Start listening
//!     listen(source, empty_ctx, on_item).await.unwrap();
//!
//!     /* Prints:
//!        42
//!        0xff6900
//!        1337
//!     */
//! })
//! ```
//!
//! ## Two-way communication
//! ```
//! use message_worker::non_blocking::listen;
//! use std::sync::Arc;
//! use anyhow::Result;
//! use tokio_stream::StreamExt;
//! use tokio_stream::wrappers::ReceiverStream;
//!
//! let mut rt = tokio::runtime::Runtime::new().unwrap();
//! rt.block_on(async {
//!     struct BiCtx { output: tokio::sync::mpsc::Sender<usize> }
//!
//!     // Create our stream
//!     let source = tokio_stream::iter(vec![42, 0xff6900, 1337]);
//!
//!     // Create a listener that outputs each item in the stream multiplied by two
//!     async fn on_item(ctx: Arc<BiCtx>, event: usize) -> Result<Option<BiCtx>> {
//!         ctx.output.send(event * 2).await?; // Send the output
//!         Ok(None)
//!     }
//!
//!     // Connect the number stream to `on_item`
//!     let (tx, rx) = tokio::sync::mpsc::channel::<usize>(3);
//!     listen(source, move || BiCtx {
//!         output: tx
//!     }, on_item);
//!
//!     let mut  rx = ReceiverStream::new(rx);
//!     assert_eq!(rx.next().await, Some(84));
//!     assert_eq!(rx.next().await, Some(0x1fed200));
//!     assert_eq!(rx.next().await, Some(2674));
//! })
//! ```
//!
//! ## Ping-pong (Actors)
//! ```no_run
//! use message_worker::non_blocking::listen;
//! use std::sync::Arc;
//! use anyhow::{Result, bail, anyhow};
//! use tokio_stream::wrappers::BroadcastStream;
//! use tokio_stream::StreamExt;
//!
//! #[tokio::main]
//! async fn main() {
//!     struct ActorCtx { output: tokio::sync::broadcast::Sender<Message> }
//!
//!     // Create our messages
//!     #[derive(Debug, Copy, Clone, Eq, PartialEq)]
//!     enum Message { Ping, Pong }
//!
//!
//!     // Create the ping actor
//!     async fn ping_actor(ctx: Arc<ActorCtx>, event: Message) -> Result<Option<ActorCtx>> {
//!         match event {
//!             Message::Ping => bail!("I'm meant to be the pinger!"),
//!             Message::Pong => ctx.output.send(Message::Ping).map_err(|err| anyhow!(err))?
//!         };
//!         Ok(None)
//!     }
//!
//!     // Create the pong actor
//!     async fn pong_actor(ctx: Arc<ActorCtx>, event: Message) -> Result<Option<ActorCtx>> {
//!         match event {
//!             Message::Ping => ctx.output.send(Message::Pong).map_err(|err| anyhow!(err))?,
//!             Message::Pong => bail!("I'm meant to be the ponger!")
//!         };
//!         Ok(None)
//!     }
//!
//!     // Create our initial stream
//!     let initial_ping = tokio_stream::iter(vec![Message::Ping]);
//!
//!     // Connect everything together
//!     let (tx_ping, rx_ping) = tokio::sync::broadcast::channel::<Message>(2);
//!     let (tx_pong, rx_pong) = tokio::sync::broadcast::channel::<Message>(2);
//!     let mut watch_pongs = BroadcastStream::new(tx_ping.clone().subscribe())
//!         .filter(|msg| msg.is_ok())
//!         .map(|msg| msg.unwrap());
//!     let mut watch_pings = BroadcastStream::new(tx_pong.clone().subscribe())
//!         .filter(|msg| msg.is_ok())
//!         .map(|msg| msg.unwrap());
//!
//!     // Start the ping actor
//!     listen(
//!         BroadcastStream::new(rx_ping)
//!             .filter(|msg| msg.is_ok())
//!             .map(|msg| msg.unwrap()),
//!         move || ActorCtx { output: tx_pong },
//!         ping_actor
//!     );
//!
//!     // Start the pong actor
//!     listen(
//!         initial_ping.chain(BroadcastStream::new(rx_pong)
//!             .filter(|msg| msg.is_ok())
//!             .map(|msg| msg.unwrap())),
//!         move || ActorCtx { output: tx_ping },
//!         pong_actor
//!     );
//!
//!     assert_eq!(watch_pings.next().await, Some(Message::Ping));
//!     assert_eq!(watch_pongs.next().await, Some(Message::Pong));
//!     assert_eq!(watch_pings.next().await, Some(Message::Ping));
//!     assert_eq!(watch_pongs.next().await, Some(Message::Pong));
//! }
//! ```
//!
//! ## The Wild Example (calling V8's C++ via Deno within an event listener to run JS)
//! ```
//! use message_worker::blocking::listen;
//! use deno_core::{JsRuntime, RuntimeOptions};
//! use std::rc::Rc;
//! use std::cell::RefCell;
//! use anyhow::Result;
//! use tokio_stream::StreamExt;
//! use tokio_stream::wrappers::ReceiverStream;
//!
//! let mut rt = tokio::runtime::Runtime::new().unwrap();
//! rt.block_on(async {
//!     struct Context {
//!         test_res: tokio::sync::mpsc::Sender<()>,
//!         runtime: JsRuntime
//!     }
//!
//!     let (mut tx, rx) = tokio::sync::mpsc::channel::<()>(1);
//!     let stream = ReceiverStream::new(rx);
//!
//!     let (test_res_tx, mut test_res) = {
//!         let (tx, rx) = tokio::sync::mpsc::channel::<()>(1);
//!         (tx, ReceiverStream::new(rx))
//!     };
//!
//!     async fn mock_handle(ctx: Rc<RefCell<Context>>, _event: ()) -> Result<Option<RefCell<Context>>> {
//!         let mut ctx = (&*ctx).borrow_mut();
//!         let runtime = &mut ctx.runtime;
//!
//!         runtime.execute(
//!             "<test>",
//!             r#"Deno.core.print(`Got a message!\n`);"#
//!         )?;
//!         runtime.run_event_loop().await?;
//!
//!         ctx.test_res.send(()).await?;
//!         Ok(None)
//!     }
//!
//!     listen(stream, move || {
//!         let runtime: JsRuntime = {
//!             let tokio_rt = tokio::runtime::Handle::current();
//!             tokio_rt.block_on(async {
//!                 let local = tokio::task::LocalSet::new();
//!                 local.run_until(async {
//!                     let mut runtime = JsRuntime::new(RuntimeOptions {
//!                         module_loader: Some(Rc::new(deno_core::FsModuleLoader)),
//!                         will_snapshot: false,
//!                         ..RuntimeOptions::default()
//!                     });
//!
//!                     runtime.execute(
//!                         "<test>",
//!                         r#"Deno.core.print(`Starting up the JS runtime via C++ FFI and Deno 🤯\n`);"#
//!                     ).unwrap();
//!                     runtime.run_event_loop().await.unwrap();
//!
//!                     runtime
//!                 }).await
//!             })
//!         };
//!
//!         RefCell::new(Context {
//!             test_res: test_res_tx,
//!             runtime
//!         })
//!     }, mock_handle);
//!     tx.send(()).await.unwrap();
//!
//!     /* Prints:
//!        Starting up the JS runtime via C++ FFI and Deno 🤯
//!        Got a message!
//!     */
//!     assert_eq!(test_res.next().await, Some(()));
//! })
//! ```
//!


/// Listeners that perform CPU intensive/blocking tasks or work with non-threadsafe data
pub mod blocking;
/// Listeners that don't block and work with threadsafe (`Sync` + `Send`) data.
pub mod non_blocking;

/// The type of the empty context (unit)
pub type EmptyCtx = ();

/// A predefined context for listeners that don't need any state.
#[inline]
pub const fn empty_ctx() -> () {}

#[cfg(test)]
mod tests {

    #[tokio::test]
    async fn ping_pong() {
        use crate::non_blocking::listen;
        use anyhow::{Result, bail, anyhow};
        use tokio_stream::wrappers::BroadcastStream;
        use tokio_stream::StreamExt;
        use std::sync::Arc;

        struct ActorCtx { output: tokio::sync::broadcast::Sender<Message> }

        // Create our messages
        #[derive(Debug, Copy, Clone, Eq, PartialEq)]
        enum Message { Ping, Pong }


        // Create the ping actor
        async fn ping_actor(ctx: Arc<ActorCtx>, event: Message) -> Result<Option<ActorCtx>> {
            match event {
                Message::Ping => bail!("I'm meant to be the pinger!"),
                Message::Pong => ctx.output.send(Message::Ping).map_err(|err| anyhow!(err))?
            };
            Ok(None)
        }

        // Create the pong actor
        async fn pong_actor(ctx: Arc<ActorCtx>, event: Message) -> Result<Option<ActorCtx>> {
            match event {
                Message::Ping => ctx.output.send(Message::Pong).map_err(|err| anyhow!(err))?,
                Message::Pong => bail!("I'm meant to be the ponger!")
            };
            Ok(None)
        }

        // Create our initial stream
        let initial_ping = tokio_stream::iter(vec![Message::Ping]);

        // Connect everything together
        let (tx_ping, rx_ping) = tokio::sync::broadcast::channel::<Message>(2);
        let (tx_pong, rx_pong) = tokio::sync::broadcast::channel::<Message>(2);
        let mut watch_pongs = BroadcastStream::new(tx_ping.clone().subscribe())
            .filter(|msg| msg.is_ok())
            .map(|msg| msg.unwrap());
        let mut watch_pings = BroadcastStream::new(tx_pong.clone().subscribe())
            .filter(|msg| msg.is_ok())
            .map(|msg| msg.unwrap());

        // Start the ping actor
        listen(
            BroadcastStream::new(rx_ping)
                .filter(|msg| msg.is_ok())
                .map(|msg| msg.unwrap()),
            move || ActorCtx { output: tx_pong },
            ping_actor
        );

        // Start the pong actor
        listen(
            initial_ping.chain(BroadcastStream::new(rx_pong)
                .filter(|msg| msg.is_ok())
                .map(|msg| msg.unwrap())),
            move || ActorCtx { output: tx_ping },
            pong_actor
        );

        assert_eq!(watch_pings.next().await, Some(Message::Ping));
        assert_eq!(watch_pongs.next().await, Some(Message::Pong));
        assert_eq!(watch_pings.next().await, Some(Message::Ping));
        assert_eq!(watch_pongs.next().await, Some(Message::Pong));
    }
}