apply abstract method
dynamic
apply(})
Executes a State Transition or arbitrary logic via the Command Pattern gateway.
apply serves as the primary enforcement mechanism for a Cell, bridging the gap between compile-time governance and runtime Integrity Gate validation.
When to use
- Somatic State Transitions: When you need to perform direct mutations or execute business logic that is whitelisted in modifiable.
- Transaction Orchestration: When performing multiple, related updates that must succeed or fail as a single atomic unit using an ApplyTransactionScope.
- Compensating Transactions: Defining reversal logic (
compensate) for SAGA-like patterns or complex state recovery. - Dynamic Command Injection: Executing logic that is determined at runtime but must still pass through the node's security perimeter.
How it works
- Capability Scrutiny: The method verifies if the provided
functionis present in the modifiable manifest. - Integrity Gate Validation: The request triggers a Reciprocal Handshake with the cell's TestCell. The intent is evaluated against the node's security invariants and administrative mandates.
- Causal Anchor: A new internal stimulus is synthesized, carrying the Causal Provenance (trace ID, timestamp, and operational context).
- Execution & Propagation: If authorized, the function is executed within
the requested
txscope. The result is then broadcast through the Egress Gateway to all downstream receptors. - Compensate Registration: If a
compensatefunction is provided, it is registered to be invoked if the transaction fails or requires rollback.
Non‑obvious
- Short-Circuit Rejection: If the Integrity Gate rejects the action,
the method returns
nullimmediately without triggering any reactive waves. - Transaction Isolation: Operations within an ApplyTransactionScope prevent "Glitch" states by ensuring downstream observers only see the final, committed transition.
- Flyweight Strategy: The framework utilizes Record-based storage for command metadata to minimize heap pressure during high-frequency mutations.
- Causal Lineage: Even failed attempts are recorded in the system audit log (if enabled), preserving the trace of the rejected stimulus.
// Define a whitelisted mutation
void increment(int amount) => value += amount;
// Execute via the Command Pattern gateway
final result = myCell.apply(
increment,
positionalArguments: [5],
compensate: (int amount) => value -= amount,
);
if (result == null) {
print('Action rejected by Integrity Gate');
}
Parameters:
function: The Command Anchor. A reference to the whitelisted function to be executed.positionalArguments: Optional arguments passed to the function by index.namedArguments: Optional arguments passed to the function by Symbol keys.tx: The Transaction Scope. An optional handle to group multiple operations into an atomic wave.compensate: The Reversal Logic. A function executed if the transaction needs to be rolled back.compensatePositional: Arguments for the compensation function.compensateNamed: Named arguments for the compensation function.compensateCell: An optional target cell for the compensation logic.
Returns:
The result of the executed function if authorized and validated; otherwise null.
See Also:
- modifiable: The capability manifest defining authorized actions.
- TestCell: The Integrity Gate implementation.
- ApplyTransactionScope: The coordinator for atomic multi-step updates.
- Example: See
example/atomic_multi_update.dartfor a walkthrough of bank transfer logic and multi-node consistency.
Implementation
dynamic apply(
Function function, {
List? positionalArguments,
Map<Symbol, dynamic>? namedArguments,
ApplyTransactionScope? tx,
Function? compensate,
List? compensatePositional,
Map<Symbol, dynamic>? compensateNamed,
Cell? compensateCell,
});