Skip to content

MiddlewareContext

The context object passed to middleware execute functions. Provides access to the current state, helper functions, and configuration.

const middleware: Middleware = {
name: 'validation',
execute: (context, next, cancel) => {
// Check if any nodes were added
if (context.helpers.anyNodesAdded()) {
console.log('Nodes added:', context.state.nodes);
}
// Access configuration
console.log('Cell size:', context.config.background.cellSize);
// Check what action triggered this
if (context.modelActionType === 'addNodes') {
// Validate new nodes
const isValid = validateNodes(context.state.nodes);
if (!isValid) {
cancel(); // Block the operation
return;
}
}
next(); // Continue to next middleware
}
};

actionStateManager: ActionStateManager

Manager for action states (resizing, linking, etc.)


config: FlowConfig

The current diagram configuration


edgeRoutingManager: EdgeRoutingManager

Manager for edge routing algorithms


edgesMap: Map<string, Edge<object>>

Map for quick edge lookup by ID. Contains the current state after previous middleware processing. Use this to access edges by ID instead of iterating through state.edges.


environment: EnvironmentInfo

Environment information (browser, rendering engine, etc.)


helpers: MiddlewareHelpers

Helper functions to check what changed (tracks all cumulative changes from the initial action and all previous middlewares)


history: MiddlewareHistoryUpdate[]

All state updates from previous middlewares in the chain


initialEdgesMap: Map<string, Edge<object>>

The initial edges map before any modifications (before the initial action and before any middleware modifications). Use this to compare state before and after all modifications. Common usage: Access removed edge instances that no longer exist in edgesMap.


initialNodesMap: Map<string, Node>

The initial nodes map before any modifications (before the initial action and before any middleware modifications). Use this to compare state before and after all modifications. Common usage: Access removed node instances that no longer exist in nodesMap.


initialState: FlowState

The state before any modifications (before the initial action and before any middleware modifications)


initialUpdate: FlowStateUpdate

The initial state update that triggered the middleware chain. Middlewares can add their own updates to the state, so this may not contain all modifications that will be applied. Use helpers to get actual knowledge about all changes.


modelActionType: ModelActionType

The action that triggered the middleware execution


nodesMap: Map<string, Node>

Map for quick node lookup by ID. Contains the current state after previous middleware processing. Use this to access nodes by ID instead of iterating through state.nodes.


state: FlowState

The current state (includes the initial modification and all changes from previous middlewares)