Attribute Macro conflagrate::dependency

source · []
#[dependency]
Expand description

Defines a dependency that can be accessed from any node in the graph.

Dependencies are resources shared amongst all nodes in a graph. They can be interfaces to external processes or collections of data shared between disparate parts of the graph. Basically anything a node needs that isn’t provided directly by the preceding node should be provided as a dependency.

Providers

Async functions decorated with #[dependency] macro construct a dependency that shares the name of the function. The first time a node that declares that dependency is executed in a graph, the graph will call the provider function and store the returned object in the dependency cache. The cache is a hash map whose keys are the names of the provider functions and the values are the provided objects.

Shared Resources

Dependencies are only created the first time they are needed. Every node that names the same dependency gets the same object. Because multiple nodes can be running simultaneously on separate threads, nodes only receive the dependency as an immutable reference. If you need mutable access to the dependency, wrap it in a tokio::sync::Mutex.

Examples

use conflagrate::{dependency, graph, nodetype};
use tokio::sync::Mutex;

#[dependency]
async fn messages() -> Mutex<Vec<String>> {
    Mutex::<Vec<String>>::new(Vec::<String>::new())
}

#[nodetype]
pub async fn StoreMessage(messages: &Mutex<Vec<String>>) {
    messages.lock().await.push(String::from("Hello mutable storage!"));
}

#[nodetype]
async fn StoreAnotherMessage(messages: &Mutex<Vec<String>>) {
    messages.lock().await.push(String::from("Here's another message!"));
}

#[nodetype]
pub async fn PrintMessages(messages: &Mutex<Vec<String>>) {
    for msg in messages.lock().await.iter() {
        println!("{}", msg);
    }
}

graph!{
    digraph MessageGraph {
        store[type=StoreMessage, start=true];
        store_another[type=StoreAnotherMessage];
        print[type=PrintMessages];

        store -> store_another -> print;
    }
}

fn main() {
    MessageGraph::run(());
}

See Also

  • nodetype – Macro for associating a function with a type of node. Reference types in input arguments are interpreted to be dependencies.