What Are Mutable Actions
You can use actions to control your application and data flow. See pre-defined Engage Actions.
The pre-defined actions have mutable behavior.
Using mutable behavior and/or mutable actions, you can:
- Change or replace pre-defined actions
- Change data passed into pre-defined actions
- Conditionally change behavior of pre-defined actions
Change or replace pre-defined actions
Section titled “Change or replace pre-defined actions”Pre-defined Engage actions with mutable behavior have internal functions to set / replace action execution flow.
replace(replace original action implementation)restore(restore original action)reset(reset action to original state, discarding attached mutations)useBefore(prepend data flow mutation)
All functions can be called with optional arguments, passed to the next mutation.
import { mutableActions } from '@shopgate/engage/core';import { addToCart } from '@shopgate/engage/cart';
// Replace addToCart implementationaddToCart.replace((data) => { // Custom logic});
// Restore addToCart implementationaddToCart.restore();
// Reset addToCart implementation, discarding mutations attached with useBeforeaddToCart.reset();
// Prepend data flow mutationaddToCart.useBefore((data) => { // Change data of action return mutableActions.next(data);});Change data passed into pre-defined actions
Section titled “Change data passed into pre-defined actions”Mutable actions, available in @shopgate/engage/core package:
mutableActionsnext(call next mutation with changed data)
import { mutableActions } from '@shopgate/engage/core';import { addToCart } from '@shopgate/engage/cart';
addToCart.useBefore((data) => { // Call next with changed data return mutableActions.next(data);});Conditionally change behavior of pre-defined actions
Section titled “Conditionally change behavior of pre-defined actions”Mutable actions, available in @shopgate/engage/core package:
mutableActionsskipRest(immediately call original action with given arguments, skipping the rest of mutations)stop(stop action execution)
import { mutableActions } from '@shopgate/engage/core';import { addToCart } from '@shopgate/engage/cart';
addToCart.useBefore((data) => { // Call original, skipping the rest return mutableActions.skipRest();
// Stop action execution return mutableActions.stop();});
