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
use core::fmt;
use std::sync::{Arc, Mutex};
use crate::{scheduler::Runtime, ActModule, ActPlugin};
#[derive(Clone)]
pub struct Extender {
runtime: Arc<Runtime>,
plugins: Arc<Mutex<Vec<Box<dyn ActPlugin>>>>,
}
impl fmt::Debug for Extender {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Extender").finish()
}
}
impl Extender {
pub(crate) fn new(runtime: &Arc<Runtime>) -> Self {
Self {
runtime: runtime.clone(),
plugins: Arc::new(Mutex::new(Vec::new())),
}
}
/// register module
///
/// ## Example
/// ```no_run
/// use acts_next::Engine;
/// mod test_module {
/// use acts_next::{ActModule, Result};
/// #[derive(Clone)]
/// pub struct TestModule;
/// impl ActModule for TestModule {
/// fn init<'a>(&self, _ctx: &rquickjs::Ctx<'a>) -> Result<()> {
/// Ok(())
/// }
/// }
/// }
/// let engine = Engine::new();
/// let module = test_module::TestModule;
/// engine.extender().register_module(&module);
/// ```
pub fn register_module<T: ActModule + Clone + 'static>(&self, module: &T) {
self.runtime.env().register_module(module)
}
/// register plugin
///
/// ## Example
///
/// ```no_run
/// use acts_next::{ActPlugin, Message, Engine, Workflow};
///
/// #[derive(Clone)]
/// struct TestPlugin;
/// impl TestPlugin {
/// fn new() -> Self {
/// Self
/// }
/// }
/// impl ActPlugin for TestPlugin {
/// fn on_init(&self, engine: &Engine) {
/// println!("TestPlugin");
/// engine.channel().on_start(|e| {});
/// engine.channel().on_complete(|e| {});
/// engine.channel().on_message(|e| {});
/// }
/// }
/// let engine = Engine::new();
/// engine.extender().register_plugin(&TestPlugin::new());
/// ```
pub fn register_plugin<T: ActPlugin + 'static + Clone>(&self, plugin: &T) {
let mut plugins = self.plugins.lock().unwrap();
plugins.push(Box::new(plugin.clone()));
}
pub fn plugins(&self) -> Arc<Mutex<Vec<Box<dyn ActPlugin>>>> {
self.plugins.clone()
}
}