Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
@harlem/extension-action
Advanced tools
This is the official action extension for Harlem. This extension adds asynchronous action capabilities to your store. Some of the features of this extension are:
isActionRunning
and hasActionRun
)Follow the steps below to get started using the action extension.
Before installing this extension make sure you have installed @harlem/core
.
yarn add @harlem/extension-action
# or
npm install @harlem/extension-action
To get started simply register this extension with the store you wish to extend.
import actionExtension from '@harlem/extension-action';
import {
createStore
} from '@harlem/core';
const STATE = {
firstName: 'Jane',
lastName: 'Smith'
};
const {
state,
getter,
mutation,
action,
hasActionRun,
isActionRunning,
hasActionFailed,
getActionErrors,
whenActionIdle,
resetActionState,
abortAction,
onBeforeAction,
onAfterAction,
onActionSuccess,
onActionError,
} = createStore('example', STATE, {
extensions: [
actionExtension()
]
});
The action extension adds several new methods to the store instance (highlighted above).
An action can be defined the same way you define any other core functionality (eg. getter
, mutation
etc.).
export default action('load-user-data', async (id: number, mutate, controller) => {
const userData = await fetch(`/api/user-data/${id}`, {
signal: controller.signal
});
mutate(state => Object.assign(state.details.user, userData));
});
The action implementation is very similar to the core mutation
method with some minor differences - The body of the action method is async, includes a mutate
method and controller
parameter.
You can also return data from the action body.
The mutate method supplied to the action body is the equivilent of defining a mutation with the same name and no payload. This is a convenient alternative to defining a separate mutation.
The only parameter supplied to the mutate
callback is a writable version of state.
The controller parameter supplied to the action body is an instance of an AbortController
. The controller is used as a cancellation token for handling when the action is cancelled. This is particularly useful for terminating fetch
requests in actions.
The third argument to the action body is an options object.
export default action('load-user-data', async (id: number, mutate, controller) => {
...
}, {
parallel: true,
autoClearErrors: true
});
boolean
- indicates whether this action allows multiple instances running in parallel. This is set to false
by default meaning any instance of this action that starts while another is running will cause the already running action to abort.boolean
- indicates whether any currently stored errors for this action should be cleared upon a new instance starting. Default is true
.To call an action simply import it and call it with the payload (if a payload type is defined).
import {
loadUserData
} from './actions';
async function runAction() {
await loadUserData(85);
}
There are 2 ways to cancel a running action:
The direct method is simply calling the action and then calling the abort
method. Each time an action is called it returns an instance of a Task
class. The Task
class is an extension of the in-built Promise
class that adds an abort
method you can use to terminate the action.
async function runAction() {
const task = loadUserData(85);
setTimeout(() => task.abort(), 1000);
await task;
}
The indirect method is using the helper method on the store to cancel the action by name.
abortAction('load-user-data');
Cancelling the task will throw an ActionAbortError
. It is recommended to wrap actions you intend on cancelling in a try/catch
statement to handle this.
Using nested actions is as simple as calling any other action(s) within the body of the current action. However, to handle cancellation through nesting, the parent controller
needs to be passed down to the nested instance(s).
import {
childAction1,
childAction2,
} from './child-actions';
export default action('parent-action', async (id: number, mutate, controller) => {
await Promise.all([
childAction1('payload', controller),
childAction2('payload', controller),
]);
});
This extension provides a set of helper methods for checking the status of actions. Similar to cancelling an action, there are 2 ways to check whether an action is running:
The direct method is simply awaiting the task that is returned from calling the action:
async function runAction() {
await loadUserData(85);
}
The indirect method is using the helper methods on the store to check whether the action is currently running or has run at all.
To check whether the action is currently running use the isActionRunning
method:
const isRunning = computed(() => isActionRunning('load-user-data'));
To check whether the action has run at all use the isActionRunning
method:
const isRunning = computed(() => hasActionRun('load-user-data'));
To respond to when an action becomes idle use the whenActionIdle
method:
await whenActionIdle('load-user-data');
All of these methods accept an optional predicate function as the second argument. The predicate function is used to check the status of a particular instance of that action. for example, say you call the same action with 2 different payloads:
loadUserData(85);
loadUserData(88);
const isFirstActionRunning = isActionRunning('load-user-data', payload => payload === 85);
The predicate function is called with the payload for each instance of the action currently running. The predicate function must return a boolean.
A few methods are available for handling errors that occur within actions.
To check whether an action has failed use the hasActionFailed
method:
const hasFailed = computed(() => hasActionFailed('load-user-data'));
To get a list of errors for this action use the getActionErrors
method:
const errors = getActionErrors('load-user-data');
The list of errors is an array of objects with an id: symbol
property and a error: unknown
property. The id is the unique identifier for the instance this error occurred on.
Action triggers are used the same way mutation triggers are with the only difference being using the action name as opposed to the mutation name. See the triggers documentation for more details.
FAQs
The official action extension for Harlem
The npm package @harlem/extension-action receives a total of 9 weekly downloads. As such, @harlem/extension-action popularity was classified as not popular.
We found that @harlem/extension-action demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.