SimpleFactory

Struct SimpleFactory 

Source
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

  1. Attach: Configure the factory function at build time
  2. Get: Request the node during graph construction
  3. Initialize: First get() call executes the factory function
  4. 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>

Source

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
            })
    });
Source

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 instance

Trait Implementations§

Source§

impl<'a, T: Clone + 'static> Clone for SimpleFactory<'a, T>

Source§

fn clone(&self) -> SimpleFactory<'a, T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a, T: 'static> Default for SimpleFactory<'a, T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'a, T> Freeze for SimpleFactory<'a, T>

§

impl<'a, T> !RefUnwindSafe for SimpleFactory<'a, T>

§

impl<'a, T> !Send for SimpleFactory<'a, T>

§

impl<'a, T> !Sync for SimpleFactory<'a, T>

§

impl<'a, T> Unpin for SimpleFactory<'a, T>

§

impl<'a, T> !UnwindSafe for SimpleFactory<'a, T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.