Add create-ticker function
Implements the `create-ticker` function, which allows for the creation of a ticker stream based on a provided condition closure. This function is crucial for synchronizing pipeline execution based on specific events or conditions.
This commit is contained in:
+56
-1
@@ -341,6 +341,62 @@ pub fn register(env: &Environment) {
|
||||
Value::Object(Rc::new(stream_node))
|
||||
},
|
||||
);
|
||||
|
||||
// (create-ticker condition-closure) -> StreamNode
|
||||
let ticker_generators = env.pipeline_generators.clone();
|
||||
let globals = env.global_values.clone();
|
||||
|
||||
env.register_native_fn(
|
||||
"create-ticker",
|
||||
StaticType::Function(Box::new(Signature {
|
||||
params: StaticType::Tuple(vec![StaticType::Any]), // Expects a closure
|
||||
ret: StaticType::Any, // Returns StreamNode
|
||||
})),
|
||||
Purity::Impure,
|
||||
move |args: std::vec::Vec<Value>| {
|
||||
if args.len() != 1 {
|
||||
panic!("create-ticker expects exactly 1 argument (the condition closure)");
|
||||
}
|
||||
|
||||
let closure_obj = if let Value::Object(obj) = &args[0] {
|
||||
obj.clone()
|
||||
} else {
|
||||
panic!("create-ticker expects a closure as its argument");
|
||||
};
|
||||
|
||||
// 1. Create the RootStream
|
||||
let root_stream = Rc::new(RootStream::new());
|
||||
let stream_node = StreamNode { inner: root_stream.clone() };
|
||||
|
||||
// 2. Setup isolated VM
|
||||
let mut ticker_vm = crate::ast::vm::VM::new(globals.clone());
|
||||
let my_closure = closure_obj.clone();
|
||||
|
||||
// 3. Create generator
|
||||
let generator = move || -> bool {
|
||||
match ticker_vm.run_with_args(my_closure.clone(), vec![]) {
|
||||
Ok(Value::Bool(b)) => {
|
||||
if b {
|
||||
// Ticker pulses with a simple `true` value or `Void`
|
||||
// We use true here so the pipe receives something tangible.
|
||||
root_stream.tick(Value::Bool(true));
|
||||
true
|
||||
} else {
|
||||
false // Exhausted
|
||||
}
|
||||
}
|
||||
Ok(_) => panic!("create-ticker closure must return a boolean"),
|
||||
Err(e) => panic!("create-ticker closure execution failed: {}", e),
|
||||
}
|
||||
};
|
||||
|
||||
// 4. Register the generator
|
||||
ticker_generators.borrow_mut().push(Box::new(generator));
|
||||
|
||||
// 5. Return stream
|
||||
Value::Object(Rc::new(stream_node))
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -379,7 +435,6 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_barrier_sync() {
|
||||
let root = RootStream::new();
|
||||
// Pipe with 2 inputs
|
||||
let pipe = Rc::new(RefCell::new(PipeStream::new("barrier-pipe".to_string(), 2, None)));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user