Socket
Socket
Sign inDemoInstall

@wordpress/hooks

Package Overview
Dependencies
Maintainers
0
Versions
162
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@wordpress/hooks - npm Package Compare versions

Comparing version 4.8.1 to 4.8.2

5

build-module/createCurrentHook.js

@@ -13,5 +13,6 @@ /**

return function currentHook() {
var _hooksStore$__current;
var _currentArray$at$name;
const hooksStore = hooks[storeKey];
return (_hooksStore$__current = hooksStore.__current[hooksStore.__current.length - 1]?.name) !== null && _hooksStore$__current !== void 0 ? _hooksStore$__current : null;
const currentArray = Array.from(hooksStore.__current);
return (_currentArray$at$name = currentArray.at(-1)?.name) !== null && _currentArray$at$name !== void 0 ? _currentArray$at$name : null;
};

@@ -18,0 +19,0 @@ }

6

build-module/createDoingHook.js

@@ -27,7 +27,7 @@ /**

if ('undefined' === typeof hookName) {
return 'undefined' !== typeof hooksStore.__current[0];
return hooksStore.__current.size > 0;
}
// Return the __current hook.
return hooksStore.__current[0] ? hookName === hooksStore.__current[0].name : false;
// Find if the `hookName` hook is in `__current`.
return Array.from(hooksStore.__current).some(hook => hook.name === hookName);
};

@@ -34,0 +34,0 @@ }

@@ -23,7 +23,7 @@ /**

this.actions = Object.create(null);
this.actions.__current = [];
this.actions.__current = new Set();
/** @type {import('.').Store} filters */
this.filters = Object.create(null);
this.filters.__current = [];
this.filters.__current = new Set();
this.addAction = createAddHook(this, 'actions');

@@ -37,4 +37,6 @@ this.addFilter = createAddHook(this, 'filters');

this.removeAllFilters = createRemoveHook(this, 'filters', true);
this.doAction = createRunHook(this, 'actions');
this.applyFilters = createRunHook(this, 'filters', true);
this.doAction = createRunHook(this, 'actions', false, false);
this.doActionAsync = createRunHook(this, 'actions', false, true);
this.applyFilters = createRunHook(this, 'filters', true, false);
this.applyFiltersAsync = createRunHook(this, 'filters', true, true);
this.currentAction = createCurrentHook(this, 'actions');

@@ -41,0 +43,0 @@ this.currentFilter = createCurrentHook(this, 'filters');

@@ -6,11 +6,11 @@ /**

*
* @param {import('.').Hooks} hooks Hooks instance.
* @param {import('.').Hooks} hooks Hooks instance.
* @param {import('.').StoreKey} storeKey
* @param {boolean} [returnFirstArg=false] Whether each hook callback is expected to
* return its first argument.
* @param {boolean} returnFirstArg Whether each hook callback is expected to return its first argument.
* @param {boolean} async Whether the hook callback should be run asynchronously
*
* @return {(hookName:string, ...args: unknown[]) => undefined|unknown} Function that runs hook callbacks.
*/
function createRunHook(hooks, storeKey, returnFirstArg = false) {
return function runHooks(hookName, ...args) {
function createRunHook(hooks, storeKey, returnFirstArg, async) {
return function runHook(hookName, ...args) {
const hooksStore = hooks[storeKey];

@@ -40,16 +40,37 @@ if (!hooksStore[hookName]) {

};
hooksStore.__current.push(hookInfo);
while (hookInfo.currentIndex < handlers.length) {
const handler = handlers[hookInfo.currentIndex];
const result = handler.callback.apply(null, args);
if (returnFirstArg) {
args[0] = result;
async function asyncRunner() {
try {
hooksStore.__current.add(hookInfo);
let result = returnFirstArg ? args[0] : undefined;
while (hookInfo.currentIndex < handlers.length) {
const handler = handlers[hookInfo.currentIndex];
result = await handler.callback.apply(null, args);
if (returnFirstArg) {
args[0] = result;
}
hookInfo.currentIndex++;
}
return returnFirstArg ? result : undefined;
} finally {
hooksStore.__current.delete(hookInfo);
}
hookInfo.currentIndex++;
}
hooksStore.__current.pop();
if (returnFirstArg) {
return args[0];
function syncRunner() {
try {
hooksStore.__current.add(hookInfo);
let result = returnFirstArg ? args[0] : undefined;
while (hookInfo.currentIndex < handlers.length) {
const handler = handlers[hookInfo.currentIndex];
result = handler.callback.apply(null, args);
if (returnFirstArg) {
args[0] = result;
}
hookInfo.currentIndex++;
}
return returnFirstArg ? result : undefined;
} finally {
hooksStore.__current.delete(hookInfo);
}
}
return undefined;
return (async ? asyncRunner : syncRunner)();
};

@@ -56,0 +77,0 @@ }

@@ -28,3 +28,3 @@ /**

/**
* @typedef {Record<string, Hook> & {__current: Current[]}} Store
* @typedef {Record<string, Hook> & {__current: Set<Current>}} Store
*/

@@ -51,3 +51,5 @@

doAction,
doActionAsync,
applyFilters,
applyFiltersAsync,
currentAction,

@@ -62,3 +64,3 @@ currentFilter,

} = defaultHooks;
export { createHooks, addAction, addFilter, removeAction, removeFilter, hasAction, hasFilter, removeAllActions, removeAllFilters, doAction, applyFilters, currentAction, currentFilter, doingAction, doingFilter, didAction, didFilter, actions, filters };
export { createHooks, addAction, addFilter, removeAction, removeFilter, hasAction, hasFilter, removeAllActions, removeAllFilters, doAction, doActionAsync, applyFilters, applyFiltersAsync, currentAction, currentFilter, doingAction, doingFilter, didAction, didFilter, actions, filters };
//# sourceMappingURL=index.js.map

@@ -22,3 +22,5 @@ /**

doAction: (hookName: string, ...args: unknown[]) => undefined | unknown;
doActionAsync: (hookName: string, ...args: unknown[]) => undefined | unknown;
applyFilters: (hookName: string, ...args: unknown[]) => undefined | unknown;
applyFiltersAsync: (hookName: string, ...args: unknown[]) => undefined | unknown;
currentAction: () => string | null;

@@ -25,0 +27,0 @@ currentFilter: () => string | null;

@@ -7,10 +7,10 @@ export default createRunHook;

*
* @param {import('.').Hooks} hooks Hooks instance.
* @param {import('.').Hooks} hooks Hooks instance.
* @param {import('.').StoreKey} storeKey
* @param {boolean} [returnFirstArg=false] Whether each hook callback is expected to
* return its first argument.
* @param {boolean} returnFirstArg Whether each hook callback is expected to return its first argument.
* @param {boolean} async Whether the hook callback should be run asynchronously
*
* @return {(hookName:string, ...args: unknown[]) => undefined|unknown} Function that runs hook callbacks.
*/
declare function createRunHook(hooks: import(".").Hooks, storeKey: import(".").StoreKey, returnFirstArg?: boolean | undefined): (hookName: string, ...args: unknown[]) => undefined | unknown;
declare function createRunHook(hooks: import(".").Hooks, storeKey: import(".").StoreKey, returnFirstArg: boolean, async: boolean): (hookName: string, ...args: unknown[]) => undefined | unknown;
//# sourceMappingURL=createRunHook.d.ts.map

@@ -19,3 +19,3 @@ /** @typedef {(...args: any[])=>any} Callback */

/**
* @typedef {Record<string, Hook> & {__current: Current[]}} Store
* @typedef {Record<string, Hook> & {__current: Set<Current>}} Store
*/

@@ -65,3 +65,3 @@ /**

export type Store = Record<string, Hook> & {
__current: Current[];
__current: Set<Current>;
};

@@ -80,3 +80,5 @@ export type StoreKey = "actions" | "filters";

export const doAction: (hookName: string, ...args: unknown[]) => undefined | unknown;
export const doActionAsync: (hookName: string, ...args: unknown[]) => undefined | unknown;
export const applyFilters: (hookName: string, ...args: unknown[]) => undefined | unknown;
export const applyFiltersAsync: (hookName: string, ...args: unknown[]) => undefined | unknown;
export const currentAction: () => string | null;

@@ -83,0 +85,0 @@ export const currentFilter: () => string | null;

@@ -19,5 +19,6 @@ "use strict";

return function currentHook() {
var _hooksStore$__current;
var _currentArray$at$name;
const hooksStore = hooks[storeKey];
return (_hooksStore$__current = hooksStore.__current[hooksStore.__current.length - 1]?.name) !== null && _hooksStore$__current !== void 0 ? _hooksStore$__current : null;
const currentArray = Array.from(hooksStore.__current);
return (_currentArray$at$name = currentArray.at(-1)?.name) !== null && _currentArray$at$name !== void 0 ? _currentArray$at$name : null;
};

@@ -24,0 +25,0 @@ }

@@ -33,7 +33,7 @@ "use strict";

if ('undefined' === typeof hookName) {
return 'undefined' !== typeof hooksStore.__current[0];
return hooksStore.__current.size > 0;
}
// Return the __current hook.
return hooksStore.__current[0] ? hookName === hooksStore.__current[0].name : false;
// Find if the `hookName` hook is in `__current`.
return Array.from(hooksStore.__current).some(hook => hook.name === hookName);
};

@@ -40,0 +40,0 @@ }

@@ -30,7 +30,7 @@ "use strict";

this.actions = Object.create(null);
this.actions.__current = [];
this.actions.__current = new Set();
/** @type {import('.').Store} filters */
this.filters = Object.create(null);
this.filters.__current = [];
this.filters.__current = new Set();
this.addAction = (0, _createAddHook.default)(this, 'actions');

@@ -44,4 +44,6 @@ this.addFilter = (0, _createAddHook.default)(this, 'filters');

this.removeAllFilters = (0, _createRemoveHook.default)(this, 'filters', true);
this.doAction = (0, _createRunHook.default)(this, 'actions');
this.applyFilters = (0, _createRunHook.default)(this, 'filters', true);
this.doAction = (0, _createRunHook.default)(this, 'actions', false, false);
this.doActionAsync = (0, _createRunHook.default)(this, 'actions', false, true);
this.applyFilters = (0, _createRunHook.default)(this, 'filters', true, false);
this.applyFiltersAsync = (0, _createRunHook.default)(this, 'filters', true, true);
this.currentAction = (0, _createCurrentHook.default)(this, 'actions');

@@ -48,0 +50,0 @@ this.currentFilter = (0, _createCurrentHook.default)(this, 'filters');

@@ -12,11 +12,11 @@ "use strict";

*
* @param {import('.').Hooks} hooks Hooks instance.
* @param {import('.').Hooks} hooks Hooks instance.
* @param {import('.').StoreKey} storeKey
* @param {boolean} [returnFirstArg=false] Whether each hook callback is expected to
* return its first argument.
* @param {boolean} returnFirstArg Whether each hook callback is expected to return its first argument.
* @param {boolean} async Whether the hook callback should be run asynchronously
*
* @return {(hookName:string, ...args: unknown[]) => undefined|unknown} Function that runs hook callbacks.
*/
function createRunHook(hooks, storeKey, returnFirstArg = false) {
return function runHooks(hookName, ...args) {
function createRunHook(hooks, storeKey, returnFirstArg, async) {
return function runHook(hookName, ...args) {
const hooksStore = hooks[storeKey];

@@ -46,16 +46,37 @@ if (!hooksStore[hookName]) {

};
hooksStore.__current.push(hookInfo);
while (hookInfo.currentIndex < handlers.length) {
const handler = handlers[hookInfo.currentIndex];
const result = handler.callback.apply(null, args);
if (returnFirstArg) {
args[0] = result;
async function asyncRunner() {
try {
hooksStore.__current.add(hookInfo);
let result = returnFirstArg ? args[0] : undefined;
while (hookInfo.currentIndex < handlers.length) {
const handler = handlers[hookInfo.currentIndex];
result = await handler.callback.apply(null, args);
if (returnFirstArg) {
args[0] = result;
}
hookInfo.currentIndex++;
}
return returnFirstArg ? result : undefined;
} finally {
hooksStore.__current.delete(hookInfo);
}
hookInfo.currentIndex++;
}
hooksStore.__current.pop();
if (returnFirstArg) {
return args[0];
function syncRunner() {
try {
hooksStore.__current.add(hookInfo);
let result = returnFirstArg ? args[0] : undefined;
while (hookInfo.currentIndex < handlers.length) {
const handler = handlers[hookInfo.currentIndex];
result = handler.callback.apply(null, args);
if (returnFirstArg) {
args[0] = result;
}
hookInfo.currentIndex++;
}
return returnFirstArg ? result : undefined;
} finally {
hooksStore.__current.delete(hookInfo);
}
}
return undefined;
return (async ? asyncRunner : syncRunner)();
};

@@ -62,0 +83,0 @@ }

@@ -7,3 +7,3 @@ "use strict";

});
exports.applyFilters = exports.addFilter = exports.addAction = exports.actions = void 0;
exports.applyFiltersAsync = exports.applyFilters = exports.addFilter = exports.addAction = exports.actions = void 0;
Object.defineProperty(exports, "createHooks", {

@@ -15,3 +15,3 @@ enumerable: true,

});
exports.removeFilter = exports.removeAllFilters = exports.removeAllActions = exports.removeAction = exports.hasFilter = exports.hasAction = exports.filters = exports.doingFilter = exports.doingAction = exports.doAction = exports.didFilter = exports.didAction = exports.defaultHooks = exports.currentFilter = exports.currentAction = void 0;
exports.removeFilter = exports.removeAllFilters = exports.removeAllActions = exports.removeAction = exports.hasFilter = exports.hasAction = exports.filters = exports.doingFilter = exports.doingAction = exports.doActionAsync = exports.doAction = exports.didFilter = exports.didAction = exports.defaultHooks = exports.currentFilter = exports.currentAction = void 0;
var _createHooks = _interopRequireDefault(require("./createHooks"));

@@ -44,3 +44,3 @@ /**

/**
* @typedef {Record<string, Hook> & {__current: Current[]}} Store
* @typedef {Record<string, Hook> & {__current: Set<Current>}} Store
*/

@@ -67,3 +67,5 @@

doAction,
doActionAsync,
applyFilters,
applyFiltersAsync,
currentAction,

@@ -86,3 +88,5 @@ currentFilter,

exports.currentAction = currentAction;
exports.applyFiltersAsync = applyFiltersAsync;
exports.applyFilters = applyFilters;
exports.doActionAsync = doActionAsync;
exports.doAction = doAction;

@@ -89,0 +93,0 @@ exports.removeAllFilters = removeAllFilters;

@@ -5,2 +5,6 @@ <!-- Learn how to maintain this file at https://github.com/WordPress/gutenberg/tree/HEAD/packages#maintaining-changelogs. -->

### New Features
- added new `doActionAsync` and `applyFiltersAsync` functions to run hooks in async mode ([#64204](https://github.com/WordPress/gutenberg/pull/64204)).
## 4.8.0 (2024-09-19)

@@ -7,0 +11,0 @@

{
"name": "@wordpress/hooks",
"version": "4.8.1",
"version": "4.8.2",
"description": "WordPress hooks library.",

@@ -35,3 +35,3 @@ "author": "The WordPress Contributors",

},
"gitHead": "cf707c1f25a2716e310cc8f9afcc8554405c79ac"
"gitHead": "6187079697e13c3292eb098d6338523a6676c6e8"
}

@@ -44,3 +44,5 @@ # Hooks

- `doAction( 'hookName', arg1, arg2, moreArgs, finalArg )`
- `doActionAsync( 'hookName', arg1, arg2, moreArgs, finalArg )`
- `applyFilters( 'hookName', content, arg1, arg2, moreArgs, finalArg )`
- `applyFiltersAsync( 'hookName', content, arg1, arg2, moreArgs, finalArg )`
- `doingAction( 'hookName' )`

@@ -47,0 +49,0 @@ - `doingFilter( 'hookName' )`

@@ -14,7 +14,4 @@ /**

const hooksStore = hooks[ storeKey ];
return (
hooksStore.__current[ hooksStore.__current.length - 1 ]?.name ??
null
);
const currentArray = Array.from( hooksStore.__current );
return currentArray.at( -1 )?.name ?? null;
};

@@ -21,0 +18,0 @@ }

@@ -27,9 +27,9 @@ /**

if ( 'undefined' === typeof hookName ) {
return 'undefined' !== typeof hooksStore.__current[ 0 ];
return hooksStore.__current.size > 0;
}
// Return the __current hook.
return hooksStore.__current[ 0 ]
? hookName === hooksStore.__current[ 0 ].name
: false;
// Find if the `hookName` hook is in `__current`.
return Array.from( hooksStore.__current ).some(
( hook ) => hook.name === hookName
);
};

@@ -36,0 +36,0 @@ }

@@ -23,7 +23,7 @@ /**

this.actions = Object.create( null );
this.actions.__current = [];
this.actions.__current = new Set();
/** @type {import('.').Store} filters */
this.filters = Object.create( null );
this.filters.__current = [];
this.filters.__current = new Set();

@@ -38,4 +38,6 @@ this.addAction = createAddHook( this, 'actions' );

this.removeAllFilters = createRemoveHook( this, 'filters', true );
this.doAction = createRunHook( this, 'actions' );
this.applyFilters = createRunHook( this, 'filters', true );
this.doAction = createRunHook( this, 'actions', false, false );
this.doActionAsync = createRunHook( this, 'actions', false, true );
this.applyFilters = createRunHook( this, 'filters', true, false );
this.applyFiltersAsync = createRunHook( this, 'filters', true, true );
this.currentAction = createCurrentHook( this, 'actions' );

@@ -42,0 +44,0 @@ this.currentFilter = createCurrentHook( this, 'filters' );

@@ -6,11 +6,11 @@ /**

*
* @param {import('.').Hooks} hooks Hooks instance.
* @param {import('.').Hooks} hooks Hooks instance.
* @param {import('.').StoreKey} storeKey
* @param {boolean} [returnFirstArg=false] Whether each hook callback is expected to
* return its first argument.
* @param {boolean} returnFirstArg Whether each hook callback is expected to return its first argument.
* @param {boolean} async Whether the hook callback should be run asynchronously
*
* @return {(hookName:string, ...args: unknown[]) => undefined|unknown} Function that runs hook callbacks.
*/
function createRunHook( hooks, storeKey, returnFirstArg = false ) {
return function runHooks( hookName, ...args ) {
function createRunHook( hooks, storeKey, returnFirstArg, async ) {
return function runHook( hookName, ...args ) {
const hooksStore = hooks[ storeKey ];

@@ -46,22 +46,39 @@

hooksStore.__current.push( hookInfo );
while ( hookInfo.currentIndex < handlers.length ) {
const handler = handlers[ hookInfo.currentIndex ];
const result = handler.callback.apply( null, args );
if ( returnFirstArg ) {
args[ 0 ] = result;
async function asyncRunner() {
try {
hooksStore.__current.add( hookInfo );
let result = returnFirstArg ? args[ 0 ] : undefined;
while ( hookInfo.currentIndex < handlers.length ) {
const handler = handlers[ hookInfo.currentIndex ];
result = await handler.callback.apply( null, args );
if ( returnFirstArg ) {
args[ 0 ] = result;
}
hookInfo.currentIndex++;
}
return returnFirstArg ? result : undefined;
} finally {
hooksStore.__current.delete( hookInfo );
}
hookInfo.currentIndex++;
}
hooksStore.__current.pop();
if ( returnFirstArg ) {
return args[ 0 ];
function syncRunner() {
try {
hooksStore.__current.add( hookInfo );
let result = returnFirstArg ? args[ 0 ] : undefined;
while ( hookInfo.currentIndex < handlers.length ) {
const handler = handlers[ hookInfo.currentIndex ];
result = handler.callback.apply( null, args );
if ( returnFirstArg ) {
args[ 0 ] = result;
}
hookInfo.currentIndex++;
}
return returnFirstArg ? result : undefined;
} finally {
hooksStore.__current.delete( hookInfo );
}
}
return undefined;
return ( async ? asyncRunner : syncRunner )();
};

@@ -68,0 +85,0 @@ }

@@ -28,3 +28,3 @@ /**

/**
* @typedef {Record<string, Hook> & {__current: Current[]}} Store
* @typedef {Record<string, Hook> & {__current: Set<Current>}} Store
*/

@@ -52,3 +52,5 @@

doAction,
doActionAsync,
applyFilters,
applyFiltersAsync,
currentAction,

@@ -75,3 +77,5 @@ currentFilter,

doAction,
doActionAsync,
applyFilters,
applyFiltersAsync,
currentAction,

@@ -78,0 +82,0 @@ currentFilter,

@@ -15,3 +15,5 @@ /**

doAction,
doActionAsync,
applyFilters,
applyFiltersAsync,
currentAction,

@@ -947,1 +949,149 @@ currentFilter,

} );
describe( 'async filter', () => {
test( 'runs all registered handlers', async () => {
addFilter( 'test.async.filter', 'callback_plus1', ( value ) => {
return new Promise( ( r ) =>
setTimeout( () => r( value + 1 ), 10 )
);
} );
addFilter( 'test.async.filter', 'callback_times2', ( value ) => {
return new Promise( ( r ) =>
setTimeout( () => r( value * 2 ), 10 )
);
} );
expect( await applyFiltersAsync( 'test.async.filter', 2 ) ).toBe( 6 );
} );
test( 'aborts when handler throws an error', async () => {
const sqrt = jest.fn( async ( value ) => {
if ( value < 0 ) {
throw new Error( 'cannot pass negative value to sqrt' );
}
return Math.sqrt( value );
} );
const plus1 = jest.fn( async ( value ) => {
return value + 1;
} );
addFilter( 'test.async.filter', 'callback_sqrt', sqrt );
addFilter( 'test.async.filter', 'callback_plus1', plus1 );
await expect(
applyFiltersAsync( 'test.async.filter', -1 )
).rejects.toThrow( 'cannot pass negative value to sqrt' );
expect( sqrt ).toHaveBeenCalledTimes( 1 );
expect( plus1 ).not.toHaveBeenCalled();
} );
test( 'is correctly tracked by doingFilter and didFilter', async () => {
addFilter( 'test.async.filter', 'callback_doing', async ( value ) => {
await new Promise( ( r ) => setTimeout( () => r(), 10 ) );
expect( doingFilter( 'test.async.filter' ) ).toBe( true );
return value;
} );
expect( doingFilter( 'test.async.filter' ) ).toBe( false );
expect( didFilter( 'test.async.filter' ) ).toBe( 0 );
await applyFiltersAsync( 'test.async.filter', 0 );
expect( doingFilter( 'test.async.filter' ) ).toBe( false );
expect( didFilter( 'test.async.filter' ) ).toBe( 1 );
} );
test( 'is correctly tracked when multiple filters run at once', async () => {
addFilter( 'test.async.filter1', 'callback_doing', async ( value ) => {
await new Promise( ( r ) => setTimeout( () => r(), 10 ) );
expect( doingFilter( 'test.async.filter1' ) ).toBe( true );
await new Promise( ( r ) => setTimeout( () => r(), 10 ) );
return value;
} );
addFilter( 'test.async.filter2', 'callback_doing', async ( value ) => {
await new Promise( ( r ) => setTimeout( () => r(), 10 ) );
expect( doingFilter( 'test.async.filter2' ) ).toBe( true );
await new Promise( ( r ) => setTimeout( () => r(), 10 ) );
return value;
} );
await Promise.all( [
applyFiltersAsync( 'test.async.filter1', 0 ),
applyFiltersAsync( 'test.async.filter2', 0 ),
] );
} );
} );
describe( 'async action', () => {
test( 'runs all registered handlers sequentially', async () => {
const outputs = [];
const action1 = async () => {
outputs.push( 1 );
await new Promise( ( r ) => setTimeout( () => r(), 10 ) );
outputs.push( 2 );
};
const action2 = async () => {
outputs.push( 3 );
await new Promise( ( r ) => setTimeout( () => r(), 10 ) );
outputs.push( 4 );
};
addAction( 'test.async.action', 'action1', action1 );
addAction( 'test.async.action', 'action2', action2 );
await doActionAsync( 'test.async.action' );
expect( outputs ).toEqual( [ 1, 2, 3, 4 ] );
} );
test( 'aborts when handler throws an error', async () => {
const outputs = [];
const action1 = async () => {
throw new Error( 'aborting' );
};
const action2 = async () => {
outputs.push( 3 );
await new Promise( ( r ) => setTimeout( () => r(), 10 ) );
outputs.push( 4 );
};
addAction( 'test.async.action', 'action1', action1 );
addAction( 'test.async.action', 'action2', action2 );
await expect( doActionAsync( 'test.async.action' ) ).rejects.toThrow(
'aborting'
);
expect( outputs ).toEqual( [] );
} );
test( 'is correctly tracked by doingAction and didAction', async () => {
addAction( 'test.async.action', 'callback_doing', async () => {
await new Promise( ( r ) => setTimeout( () => r(), 10 ) );
expect( doingAction( 'test.async.action' ) ).toBe( true );
} );
expect( doingAction( 'test.async.action' ) ).toBe( false );
expect( didAction( 'test.async.action' ) ).toBe( 0 );
await doActionAsync( 'test.async.action', 0 );
expect( doingAction( 'test.async.action' ) ).toBe( false );
expect( didAction( 'test.async.action' ) ).toBe( 1 );
} );
test( 'is correctly tracked when multiple actions run at once', async () => {
addAction( 'test.async.action1', 'callback_doing', async () => {
await new Promise( ( r ) => setTimeout( () => r(), 10 ) );
expect( doingAction( 'test.async.action1' ) ).toBe( true );
await new Promise( ( r ) => setTimeout( () => r(), 10 ) );
} );
addAction( 'test.async.action2', 'callback_doing', async () => {
await new Promise( ( r ) => setTimeout( () => r(), 10 ) );
expect( doingAction( 'test.async.action2' ) ).toBe( true );
await new Promise( ( r ) => setTimeout( () => r(), 10 ) );
} );
await Promise.all( [
doActionAsync( 'test.async.action1', 0 ),
doActionAsync( 'test.async.action2', 0 ),
] );
} );
} );

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc