Skip to content

Commit

Permalink
feat(extensions): added state reset method to action extension
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewcourtice committed Sep 6, 2021
1 parent 7af6778 commit 9deba20
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
30 changes: 19 additions & 11 deletions extensions/action/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,16 @@ export default function actionsExtension<TState extends BaseState>() {

_store.write('$action-init', SENDER, state => state.$actions = {}, true);

function registerAction(name: string) {
_store.register('actions', name, () => undefined);
_store.write('$action-register', SENDER, state => {
state.$actions[name] = {
runCount: 0,
instances: new Map<symbol, unknown>(),
};
}, true);
function setActionState(state: TState & ActionStoreState, name: string) {
state.$actions[name] = {
runCount: 0,
instances: new Map<symbol, unknown>(),
};
}

function clearActionRunCount(name: string) {
_store.write('$action-clear-run-count', SENDER, state => state.$actions[name].runCount = 0);
function registerAction(name: string) {
_store.register('actions', name, () => _store.state.$actions[name]);
_store.write('$action-register', SENDER, state => setActionState(state, name), true);
}

function incrementRunCount(name: string) {
Expand Down Expand Up @@ -155,12 +153,22 @@ export default function actionsExtension<TState extends BaseState>() {
}, controller);
}

function resetActionState(name?: string | string[]) {
const names = ([] as string[]).concat(name || Object.keys(_store.state.$actions));

_store.write('$action-reset-state', SENDER, state => names.forEach(name => {
if (name in state.$actions) {
setActionState(state, name);
}
}));
}

return {
action,
hasActionRun,
isActionRunning,
whenActionIdle,
clearActionRunCount,
resetActionState,
};
};
}
20 changes: 20 additions & 0 deletions extensions/action/test/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,24 @@ describe('Actions Extension', () => {
expect(hasConcurrentFailed).toBe(false);
});

test('Handles action resetting', async () => {
const {
loadUserInfo,
loadUserInfoName,
} = instance;

const {
hasActionRun,
resetActionState,
} = instance.store;

expect(hasActionRun(loadUserInfoName)).toBe(false);

await loadUserInfo();

expect(hasActionRun(loadUserInfoName)).toBe(true);
resetActionState();
expect(hasActionRun(loadUserInfoName)).toBe(false);
});

});

0 comments on commit 9deba20

Please sign in to comment.