Skip to content

//workflow/com.squareup.workflow1/WorkflowInterceptor

WorkflowInterceptor

[jvm] interface 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](../-stateful-workflow/index.md), 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](-workflow-session/index.md) object that can be queried for information about the workflow being intercepted.

All methods have default no-op implementations.

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 an “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.

Types

Name Summary
RenderContextInterceptor [jvm]
Content
interface RenderContextInterceptor<P, S, O>
More info
Provides hooks for intercepting calls to a BaseRenderContext, to be used from onRender.


WorkflowSession [jvm]
Content
interface WorkflowSession
More info
Information about the session of a workflow in the runtime that a WorkflowInterceptor method is intercepting.


Functions

Name Summary
onInitialState [jvm]
Content
open fun <P, S> onInitialState(props: P, snapshot: Snapshot?, proceed: (P, Snapshot?) -> S, session: WorkflowInterceptor.WorkflowSession): S
More info
Intercepts calls to StatefulWorkflow.initialState.


onPropsChanged [jvm]
Content
open fun <P, S> onPropsChanged(old: P, new: P, state: S, proceed: (P, P, S) -> S, session: WorkflowInterceptor.WorkflowSession): S
More info
Intercepts calls to StatefulWorkflow.onPropsChanged.


onRender [jvm]
Content
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
More info
Intercepts calls to StatefulWorkflow.render.


onSessionStarted [jvm]
Content
open fun onSessionStarted(workflowScope: CoroutineScope, session: WorkflowInterceptor.WorkflowSession)
More info
Called when the session is starting, before onInitialState.


onSnapshotState [jvm]
Content
open fun <S> onSnapshotState(state: S, proceed: (S) -> Snapshot?, session: WorkflowInterceptor.WorkflowSession): Snapshot?
More info
Intercepts calls to StatefulWorkflow.snapshotState.


Inheritors

Name
SimpleLoggingWorkflowInterceptor
NoopWorkflowInterceptor
RenderIdempotencyChecker