Skip to content
LogoLogo

Actions

An action is a function that the SDK runs by its key. An action comes from one of two places:

  • Your application registers a handler in createClient({ actions }).
  • Your project publishes the action. Its handler sends the visitor's answers to Picked, and the workflow of the action runs there.

A handler from your application replaces a published action that has the same key. The workflow then does not start.

Response

The value that a handler returns is the response of the action. A handler can return any value, or a promise of any value.

The response of a published action is the payload of the Action response node in its workflow. A workflow that has no Action response node gives undefined.

Outcome and branch

Handler outcomeBranch the flow takes
Throws, or its promise rejectsThe failure connection
Resolves to any value, and that includes falseThe success connection
No handler has the key of the actionThe failure connection

In the flow builder, the failure connection starts at the red handle of the action node. In an experience that you make in code, onFail makes the failure connection and onSuccess makes the success connection.

An action node can also carry one plain connection, with no success or failure branch. The flow then continues through it on either outcome. An action node that has no connection for its outcome stops the flow on that node.

An action does not interrupt your visitor. The current step stays on the screen while an async action runs. While the action runs, the flow ignores a request to go to the next step or to a specified step. The outcome of the action then chooses the branch. If your visitor goes back before the action completes, the flow stays on that step. The signal that the handler receives then stops.

Success field and failure field

An action can have a success field and a failure field. You set them on the action, so every action node that runs the action uses the same fields. When the action completes on an action node, the SDK writes to these fields before the flow continues:

Handler outcomeSuccess fieldFailure field
Resolves to a valueThe valuenull
Throws, or its promise rejectsnull{ code, message }

code is the code string of the error. If the error has no code string, code is the name of the error. If the error has no name string, code is "ERROR". message is the message string of the error. If the error has no message string, message is the error as text.

The SDK changes each value to the type of its field:

Field typeValue that the field gets
ObjectThe value as JSON
TextA string as it is. Other values as JSON text.
NumberA number. Other values, NaN and Infinity give null.
Booleanfalse if the value is false, 0, "" or NaN. true for all other values.
MultipleAn array. The SDK puts a single value in an array, and changes each item. If one item does not change to the type, the field gets null.

A value of null or undefined gives null. In an Object or Text field, a value that JSON cannot show, for example a function, gives null. The SDK does not use the validation of the field. Later steps and conditions read the values from client.responses.

The SDK does not write to the fields when:

  • Your visitor goes back before the action completes.
  • No handler has the key of the action. The flow then takes the failure connection.

Published actions

A published action waits for the response of its workflow. It stops after 30 seconds.

CauseThe action rejects with
Picked refuses the requestAn Error that names the HTTP status
Picked refuses the request with a 429 status 4 timesRateLimitError
Picked does not accept the request in 25 secondsA TimeoutError
Picked accepts the request, but gives no run to wait forAn Error that says that no run is available
The workflow fails before it sends a responseActionRunFailedError
Picked refuses a request for the response with a 4xx status other than 429An Error that names the HTTP status
No response in 30 secondsActionResponseTimeoutError
Your code stops the wait with a signalThe reason of the signal

When Picked refuses a request with a 429 status, the SDK waits about 1 second and sends the request again. It does this 3 times at most. The time limit of the request includes these waits.

A request for the response can fail because of a network error or a timeout. It can also fail because of a 429 or 5xx status, or a body that the SDK cannot read. The SDK then sends the request again. It waits 1 second after the first failure, and two times longer after each failure that follows. If the requests continue to fail, the 30-second limit stops the wait with ActionResponseTimeoutError.

To identify ActionRunFailedError, ActionResponseTimeoutError and RateLimitError with instanceof, import them from @picked/client/plugins/dynamic-experiences. The name of each error is the same as the name that you import.

Run an action from code

APIReturnsRejects
client.actions.run(key, options)A promise of the responseYes, with the error of the action
useAction(key, options) (React)For an action that starts a workflow: { run, status, response, error }. For other actions: the action as a function.run: No. The function: it returns or throws what the action returns or throws.
getAction(key, options) (Svelte)A store of the same valueThe same as useAction
useActionResult(key, options) (React)The last result of your own actionNo
getActionResult(key, options) (Svelte)A store of the last result of your own actionNo
client.actions.getActionBinding(key, options)A store of the value that useAction and getAction giveThe same as useAction
client.actions.getActionResult(key, options)A store of the last result of your own action, which useActionResult and getActionResult readNo

Type generation names the actions that start a workflow (WorkflowActionKeys). Without generated types, useAction and getAction give the action as a function. For each key, the value also includes run, status, response and error.

The handler receives the arguments of a call to the function in args. It also receives the arguments of run(...args) in args.

status is idle, running, succeeded or failed, for the last run. When a run starts, response and error are undefined. run(...args) resolves to { status: 'succeeded', response } or to { status: 'failed', error }.

The SDK keeps one run state for each key and experience. All the components that use the same key and the same experience share that run state, and they show the same last run. When one of them calls run, all of them show that run.

useActionResult and getActionResult run your own action again when a response, a field or the handler of the action changes. They also run it when a component that reads the result starts. In React, the action runs when the component mounts, and when an <Activity> shows the component again. In Svelte, it runs each time a component calls getActionResult. A change that occurs outside Picked, for example a login, then shows in a component that starts after the change.

The components that read the same key and the same experience share one result. The action runs one time for each change. It also runs one time for the components that React renders together. React can stop while it renders and continue later, for example in a transition. Then the components that React renders after it continues run the action again.

The result is always from the run that started last. If that run throws, the result is undefined. For a published action they give undefined and do not send a request. The types refuse the key of an action that starts a workflow.

A result for an experience follows the changes only while client.experienceInstances contains that experience. When client.experienceInstances adds the experience, the action runs if a response, a field or the handler changed after the last run.

The options of client.actions.run:

OptionTypeDescription
argsunknown[]The values that the handler receives in args.
experienceExperienceInstanceThe experience that the handler receives as context.experience.
signalAbortSignalStops the wait. The handler receives a signal that stops too.
timeoutMsnumberStops the wait after this many milliseconds, with a TimeoutError.

useAction and getAction take experience only.

Other methods of client.actions

MethodDescription
refreshActionResult(key, options)Runs your own action again for the result that getActionResult gives. It does nothing until a component or your code reads that result. The calls in one task start one run.
isPublished(key)Returns true when the handler of the key is a published action.
unregister(key)Removes the handler of the key. If the key has no handler, nothing changes.

refreshActionResult and getActionResult take experience only.

Rule logic

A rule reads the response of an action with { "invoke": [{ "var": "actions.<key>" }] }. A response that is a string, a number, a Boolean, null, or an array or object of these reaches the rule. A different response reaches the rule as undefined.

A rule runs the action each time the SDK evaluates the rule.

A condition node can have a rule that runs an action. While the condition node waits for that rule, the flow ignores a request to go to the next step or to a specified step. The result of the rule then chooses the branch. If your visitor goes back before the rule has a result, the flow ignores the result.