Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

How to Build a Workflow

Note

This chapter will be about building workflows using the native Rust API. That means writing Rust code to build the workflow, which will be compiled and embeded in an application. If you are instead interested in building workflows using JSON diagrams, then you can skip ahead to the JSON Diagrams chapter.

Spawning

You can spawn a workflow anywhere that you can access Bevy Commands through the trait SpawnWorkflowExt. This can be done while setting up your application or during runtime.

This is an example of spawning an input/output (i/o) workflow—a workflow that doesn’t have any output streams, just one input message and one final output message:

let workflow: Service<Request, Response> = commands.spawn_io_workflow(
    |scope: Scope<Request, Response>, builder: &mut Builder| {
        builder.connect(scope.start, scope.terminate);
    }
);

Notice some key details:

  • The output of spawn_io_workflow is a Service. This is what you will use to refer to the workflow after it has been spawned.
  • The input argument of spawn_io_workflow is a closure.
  • The input arguments of the closure are a Scope and a Builder.
  • The generic parameters of the Scope match those of the Service.
  • The Scope has an input and a terminate field. These represent the input and output of the overall workflow, and therefore match the request and response type of the Service. In this case Request and Response are actually aliases of the same type.
  • The Builder can make a connection between an output and an input.

Very often the Rust compiler can infer the generic types of the service and scope, so the above example can usually be reduced to:

let workflow = commands.spawn_io_workflow(
    |scope, builder| {
        builder.connect(scope.start, scope.terminate);
    }
);

The spawn_io_workflow command exists to make this inference easy and concise. When streams are used this kind of inference will not work as easily. This will be discussed more in the Output Streams section. In the meantime, move on to the next page to see how to put nodes into your workflow.