WorkflowInterceptor

Provides hooks into the workflow runtime that can be used to instrument or modify the behavior of workflows.

This interface's methods mirror the methods of StatefulWorkflow. It also has one additional method, onSessionStarted, that is notified when a workflow is started. Each method returns the same thing as the corresponding method on StatefulWorkflow, and receives the same parameters as well as two extra parameters:

  • proceed – A function that exactly mirrors the corresponding function on StatefulWorkflow, accepting the same parameters and returning the same thing. An interceptor can call this function to run the actual workflow, but it may also decide to not call it at all, or call it multiple times.

  • session – A WorkflowSession object that can be queried for information about the workflow being intercepted. Note that this object carries parent information. So we can use the session object to determine if we are the root Workflow if session.parent == null.

All methods have default no-op implementations.

On Profiling

Note that the WorkflowInterceptor's methods will call the actual methods with the proceed function. This means that we have hooks before and after the actual method making it very straightforward to trace/measure the timing of any part of any workflow, or of the whole tree.

Workflow sessions

A single workflow may be rendered by different parents at the same time, or the same parent at different, disjoint times. Each continuous sequence of renderings of a particular workflow type, with the same key passed to BaseRenderContext.renderChild, is called a "session" of that workflow. The workflow's StatefulWorkflow.initialState method will be called at the start of the session, and its state will be maintained by the runtime until the session is finished. Each session is identified by the WorkflowSession object passed into the corresponding method in a WorkflowInterceptor.

In addition to the WorkflowIdentifier of the type of the workflow being rendered, this object also knows the key used to render the workflow and the WorkflowSession of the parent workflow that is rendering it.

Each session is also assigned a numerical ID that uniquely identifies the session over the life of the entire runtime. This value will remain constant as long as the workflow's parent is rendering it, and then it will never be used again. If this workflow stops being rendered, and then starts again, the value will be different.

Inheritors

Types

Link copied to clipboard

Provides hooks for intercepting calls to a BaseRenderContext, to be used from onRender.

Link copied to clipboard
interface WorkflowSession

Information about the session of a workflow in the runtime that a WorkflowInterceptor method is intercepting.

Functions

Link copied to clipboard
open fun <P, S> onInitialState(props: P, snapshot: Snapshot?, workflowScope: CoroutineScope, proceed: (P, Snapshot?, CoroutineScope) -> S, session: WorkflowInterceptor.WorkflowSession): S

Intercepts calls to StatefulWorkflow.initialState.

Link copied to clipboard
open fun <P, S> onPropsChanged(old: P, new: P, state: S, proceed: (P, P, S) -> S, session: WorkflowInterceptor.WorkflowSession): S

Intercepts calls to StatefulWorkflow.onPropsChanged.

Link copied to clipboard
open fun <P, S, O, R> onRender(renderProps: P, renderState: S, context: BaseRenderContext<P, S, O>, proceed: (P, S, WorkflowInterceptor.RenderContextInterceptor<P, S, O>?) -> R, session: WorkflowInterceptor.WorkflowSession): R

Intercepts calls to StatefulWorkflow.render.

Link copied to clipboard

Intercept a full rendering pass which involves rendering then snapshotting the workflow tree. This is useful for tracing purposes.

Link copied to clipboard
open fun onSessionStarted(workflowScope: CoroutineScope, session: WorkflowInterceptor.WorkflowSession)

Called when the session is starting, before onInitialState.

Link copied to clipboard
open fun <S> onSnapshotState(state: S, proceed: (S) -> Snapshot?, session: WorkflowInterceptor.WorkflowSession): Snapshot?

Intercepts calls to StatefulWorkflow.snapshotState.

Link copied to clipboard

Intercept calls to StatefulWorkflow.snapshotState including the children calls. This is useful to intercept a rendering + snapshot traversal for tracing purposes.