New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a utility function to execute hooks serially #814
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
'use strict' | ||
|
||
const pify = require('pify') | ||
|
||
module.exports = { | ||
promisifyHooks: function promisifyHooks (hooks, args) { | ||
if (!hooks || !Array.isArray(hooks)) { | ||
return Promise.resolve() | ||
} | ||
|
||
return Promise.all(hooks.map(hookFn => pify(hookFn).apply(this, args))) | ||
}, | ||
serialHooks: function serialHooks (hooks) { | ||
return function () { | ||
const args = Array.prototype.splice.call(arguments, 0, arguments.length - 1) | ||
const done = arguments[arguments.length - 1] | ||
let result = Promise.resolve() | ||
for (const hook of hooks) { | ||
result = result.then(() => hook.apply(this, args)) | ||
} | ||
|
||
return result.then(() => done()) // eslint-disable-line promise/no-callback-in-promise | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
'use strict' | ||
|
||
const config = require('./config.json') | ||
const hooks = require('../hooks') | ||
const packager = require('..') | ||
const test = require('ava') | ||
const util = require('./_util') | ||
|
||
function createHookTest (hookName) { | ||
|
@@ -32,3 +34,49 @@ function createHookTest (hookName) { | |
createHookTest('afterCopy') | ||
createHookTest('afterPrune') | ||
createHookTest('afterExtract') | ||
|
||
test('promisifyHooks executes functions in parallel', t => { | ||
let output = '0' | ||
const testHooks = [ | ||
done => { output += ' 1'; done() }, | ||
done => { setTimeout(() => { output += ' 2'; done() }, 1000) }, | ||
done => { output += ' 3'; done() }, | ||
done => { setTimeout(() => { output += ' 4'; done() }, 1000) }, | ||
done => { output += ' 5'; done() }, | ||
done => { setTimeout(() => { output += ' 6'; done() }, 1000) }, | ||
done => { output += ' 7'; done() }, | ||
done => { setTimeout(() => { output += ' 8'; done() }, 1000) }, | ||
done => { output += ' 9'; done() }, | ||
done => { setTimeout(() => { output += ' 10'; done() }, 1000) } | ||
] | ||
|
||
return hooks.promisifyHooks(testHooks) | ||
.then(() => t.not(output, '0 1 2 3 4 5 6 7 8 9 10', 'should not be in sequential order')) | ||
}) | ||
|
||
test('serialHooks executes functions serially', t => { | ||
let output = '0' | ||
const timeoutFunc = number => { | ||
return () => new Promise(resolve => { // eslint-disable-line promise/avoid-new | ||
setTimeout(() => { | ||
output += ` ${number}` | ||
resolve() | ||
}, 1000) | ||
}) | ||
} | ||
const testHooks = [ | ||
() => { output += ' 1'; return Promise.resolve() }, | ||
timeoutFunc(2), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use this in the above test as well |
||
() => { output += ' 3'; return Promise.resolve() }, | ||
timeoutFunc(4), | ||
() => { output += ' 5'; return Promise.resolve() }, | ||
timeoutFunc(6), | ||
() => { output += ' 7'; return Promise.resolve() }, | ||
timeoutFunc(8), | ||
() => { output += ' 9'; return Promise.resolve() }, | ||
timeoutFunc(10) | ||
] | ||
|
||
return hooks.serialHooks(testHooks)(() => output) | ||
.then(result => t.is(result, '0 1 2 3 4 5 6 7 8 9 10', 'should be in sequential order')) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this helper takes in the timeout as well we can remove more code duplication below for the hooks that instantly resolve
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 9dcc68c