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

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 implementation
addToCart.replace((data) => {
  // Custom logic
});

// Restore addToCart implementation
addToCart.restore();

// Reset addToCart implementation, discarding mutations attached with useBefore
addToCart.reset();

// Prepend data flow mutation
addToCart.useBefore((data) => {
  // Change data of action
  return mutableActions.next(data);
});

Change data passed into pre-defined actions

Mutable actions, available in @shopgate/engage/core package:

  • mutableActions
    • next (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

Mutable actions, available in @shopgate/engage/core package:

  • mutableActions
    • skipRest (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();
});
List of Engage mutable actions