We haven’t covered the standard library yet, but for now, here are the basics: std is a globally available module, there to help you interact with the runtime.

You create flows using std.flow. It takes two parameters:

  1. Name of the flow
  2. An instance of a class

std.flow lets you go through the looking glass. It lets you provide a new state machine to the runtime, such that when it ticks, it manages this newly given state machine as well.

FlowExecutor

A standard flow, lazily evaluated.


When you don’t provide a schedule to std.flow and call a FlowFunction, you get a FlowExecutor. This is a handle that encapsulates work.

A FlowExecutor doesn’t start its work unless explcitly told to do so. It has two function through which you can start execution:

interface FlowExecutor<RT, M> {
	run(): void;
	await(): RT;
}

We will look at await in next section.
The type argument RT captures the return type of the FlowFunction that spawned it, M we can ignore for now.

Given a FlowExecutor is a handle, we can do this:

class Main {
	hello() {
		const flow = std.flow<Main>("Print!", this).world("Hello");
		this.print(flow); //We didn't call 'run' on flow, but gave it to 'print'.
	}

	world(arg: string) {
		std.log(`${arg} World!`);
	}

	// This takes a flow, and simply runs it.
	print(flow: std.FlowExecutor<void, Main['world']>) {
		flow.run();
	}
}

Try this code in the playground to see what happens!

You can pass around flows like values, but be mindful of type arguments. They are essential to creating the call graph.

Scheduled Flows

A scheduled flow, eagerly evaluated, run immediately.

You can provide a schedule to std.flow to get a ScheduledFlow. A schedule is an object and it can have of the following keys:

after
number

Start the flow after given number of ticks have passed.

every
number

Start a flow every time given number of ticks pass.

A ScheduledFlow is evaluated immediately, and it begins its work in the same tick it’s created.
Check out the playground to see how we can emulate a polling job using it.