Skip to content

Commit

Permalink
Merge pull request #13277 from strapi/fix/hooks-invalid-spread
Browse files Browse the repository at this point in the history
fix: hook getter spread make handlers invalid
  • Loading branch information
alexandrebodin committed May 11, 2022
2 parents 91bfab5 + d184161 commit 488f701
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/core/utils/lib/__tests__/hooks.test.js
Expand Up @@ -8,7 +8,7 @@ describe('Hooks Module', () => {
test(`It's possible to create a hook that has all the needed methods`, () => {
const hook = hooks.internals.createHook();

expect(hook).toHaveProperty('handlers', expect.any(Array));
expect(hook).toHaveProperty('getHandlers', expect.any(Function));
expect(hook).toHaveProperty('register', expect.any(Function));
expect(hook).toHaveProperty('delete', expect.any(Function));
expect(hook).toHaveProperty('call', expect.any(Function));
Expand Down
10 changes: 6 additions & 4 deletions packages/core/utils/lib/hooks.js
Expand Up @@ -20,17 +20,19 @@ const createHook = () => {
};

return {
get handlers() {
getHandlers() {
return state.handlers;
},

register(handler) {
state.handlers.push(handler);

return this;
},

delete(handler) {
state.handlers = remove(eq(handler), state.handlers);

return this;
},

Expand All @@ -49,7 +51,7 @@ const createAsyncSeriesHook = () => ({
...createHook(),

async call(context) {
for (const handler of this.handlers) {
for (const handler of this.getHandlers()) {
await handler(context);
}
},
Expand All @@ -66,7 +68,7 @@ const createAsyncSeriesWaterfallHook = () => ({
async call(param) {
let res = param;

for (const handler of this.handlers) {
for (const handler of this.getHandlers()) {
res = await handler(res);
}

Expand All @@ -83,7 +85,7 @@ const createAsyncParallelHook = () => ({
...createHook(),

async call(context) {
const promises = this.handlers.map(handler => handler(cloneDeep(context)));
const promises = this.getHandlers().map(handler => handler(cloneDeep(context)));

return Promise.all(promises);
},
Expand Down

0 comments on commit 488f701

Please sign in to comment.