Skip to content

MiddlewareContext

since v0.8.0

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 actions triggered this (supports transactions with multiple actions)
if (context.modelActionTypes.includes('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.


modelActionTypes: ModelActionTypes

All action types that triggered the middleware execution. For transactions, this contains the transaction name followed by all action types from commands executed within the transaction. For single commands outside transactions, this is a single-element array.

// For a transaction named 'batchUpdate' with addNodes and moveViewport commands:
// modelActionTypes = ['batchUpdate', 'addNodes', 'moveViewport']
// For a single command outside a transaction:
// modelActionTypes = ['addNodes']

0.9.0


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)