pub struct SimpleFactory<'a, T: 'static>(/* private fields */);Expand description
A dependency injection factory for creating single-instance wsnl with lazy initialization.
SimpleFactory<T> provides build-time configuration for wsnl that should only
be created once per factory instance. Uses OnceCell for thread-safe lazy
initialization - the factory function is called exactly once on the first
get() request, and subsequent calls return the cached node.
§Use Cases
- Singleton services: Database connections, configuration managers, loggers
- Expensive initialization: Nodes that load large datasets or establish connections
- Shared resources: Nodes that multiple parts of the graph need to reference
- Environment abstraction: Different implementations based on build-time configuration
§Example: Configuration Service
// Build-time: Configure different config sources
let config_factory = match environment {
Environment::Production => SimpleFactory::default()
.attach(|executor| {
NodeBuilder::new(ProductionConfig::load())
.with_name("prod_config".to_string())
.build(executor, |config, ctx| {
// Handle config reload requests
Control::Broadcast
})
}),
Environment::Test => SimpleFactory::default()
.attach(|executor| {
NodeBuilder::new(MockConfig::default())
.with_name("test_config".to_string())
.build(executor, |config, ctx| Control::Unchanged)
}),
};
// Runtime: Multiple wsnl can depend on the same config instance
let config_node = config_factory.get(&mut executor);
let processor1 = NodeBuilder::new(DataProcessor::new())
.observer_of(&config_node) // Read config but don't react to changes
.build(&mut executor, process_data);
let processor2 = NodeBuilder::new(AnotherProcessor::new())
.observer_of(&config_node) // Same config instance
.build(&mut executor, process_other_data);§Lifecycle
- Attach: Configure the factory function at build time
- Get: Request the node during graph construction
- Initialize: First
get()call executes the factory function - Reuse: All subsequent
get()calls return the same node instance
§Comparison with KeyedFactory
SimpleFactory: One node per factory instance (singleton pattern)KeyedFactory: One node per unique key (multi-instance with caching)
Choose SimpleFactory when you need exactly one instance of a particular
node type in your graph.
Implementations§
Source§impl<'a, T: 'static> SimpleFactory<'a, T>
impl<'a, T: 'static> SimpleFactory<'a, T>
Sourcepub fn attach(self, f: impl FnOnce(&mut Executor) -> Node<T> + 'a) -> Self
pub fn attach(self, f: impl FnOnce(&mut Executor) -> Node<T> + 'a) -> Self
Configures the factory function that creates the node.
The factory function receives full access to the executor for node creation.
This function will be called exactly once on the first get() request.
§Panics
Panics if a factory function has already been attached.
§Example
use wavelet::prelude::*;
let factory = SimpleFactory::default()
.attach(|executor| {
NodeBuilder::new(0)
.on_init(|executor, timer, idx| {
// Set up a recurring heartbeat
executor.yield_driver().yield_now(idx);
})
.build(executor, |_, ctx| {
Control::Broadcast
})
});Sourcepub fn get(&self, executor: &mut Executor) -> Node<T>
pub fn get(&self, executor: &mut Executor) -> Node<T>
Creates or retrieves the cached node instance.
On the first call, executes the factory function to create the node. All subsequent calls return the same node instance.
§Panics
Panics if no factory function has been attached.
§Example
let logger1 = logger_factory.get(&mut executor);
let logger2 = logger_factory.get(&mut executor);
// logger1 and logger2 are the same node instanceTrait Implementations§
Source§impl<'a, T: Clone + 'static> Clone for SimpleFactory<'a, T>
impl<'a, T: Clone + 'static> Clone for SimpleFactory<'a, T>
Source§fn clone(&self) -> SimpleFactory<'a, T>
fn clone(&self) -> SimpleFactory<'a, T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more