Skip to content

External State

The External State Plugin is an easy way to handle EXTERNAL states from the navigation of a Player flow.

You provide an array of handlers, each specifying a ref to match against, an optional match object for additional properties, and a handlerFunction to run. When Player transitions to an EXTERNAL state, the plugin finds the best matching handler and calls it with the state and the player controllers.

For react, the handler should return the transition to take. For mobile, the handler is passed a callback to trigger the next transition.

To register additional handlers after a plugin instance has been created, add a new ExternalStatePlugin instance to the Player.

An EXTERNAL state in a Player flow looks like this:

{
"navigation": {
"BEGIN": "FLOW_1",
"FLOW_1": {
"startState": "EXT_1",
"EXT_1": {
"state_type": "EXTERNAL",
"ref": "my-action",
"transitions": {
"Next": "END_FWD",
"Prev": "END_BCK"
}
},
"END_FWD": { "state_type": "END", "outcome": "FWD" },
"END_BCK": { "state_type": "END", "outcome": "BCK" }
}
}
}

You can register multiple handlers for the same ref. The most specific match wins:

new ExternalStatePlugin([
// Matches any state with ref: 'my-action'
{ ref: "my-action", handlerFunction: () => "default" },
// Takes precedence when type: 'special' is also present on the state
{
ref: "my-action",
match: { type: "special" },
handlerFunction: () => "special",
},
]);

The plugin captures an ExternalStateError (type: "externalState") via the ErrorController in two cases instead of silently stalling the flow:

  • missing-handler — no registered handler matches the state’s ref.
  • missing-transition-value — a handler ran but returned a falsy value (undefined or "").

metadata.ref is set to the offending state’s ref. Recover via errorTransitions or by tapping errorController.hooks.onError; see Error Handling.

Handlers that throw fail the flow directly and bypass the ErrorController — exceptions are treated as programmer errors, not recoverable content-shape issues.

Terminal window
npm i @player-ui/external-state-plugin
import { ExternalStatePlugin } from "@player-ui/external-state-plugin";
import { Player } from "@player-ui/player";
const player = new Player({
plugins: [
new ExternalStatePlugin([
{
ref: "my-action",
handlerFunction: async (state, options) => {
// options.data and options.expression are available here
return "Next";
},
},
]),
],
});