Skip to content

Commit ee43611

Browse files
authored
Hook and callback changes (#359)
* Fixes a typo with Array.prototype.includes in aHook * Styling: changed let and var to const * Add test to validate multiple filter-file aHooks
1 parent b23c97b commit ee43611

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/Eventizer.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ export default class extends EventTarget {
322322
// console.log(`[event] Fire hook "${name}"${args.length ? ' with ' + args.length + ' arguments' : ''}`);
323323
value = callback.apply(this, args);
324324
if (name === 'file-added' && value === false) {
325-
console.warn('In Flow.js 3.x, file-added event is an action rather than a fitler. return value is ignored but removing the `file` property allows to skip an enqueued file.');
325+
console.warn('In Flow.js 3.x, file-added event is an action rather than a filter. Return value is ignored but removing the `file` property allows to skip an enqueued file.');
326326
}
327327

328328
if (isFilter) {
@@ -347,17 +347,18 @@ export default class extends EventTarget {
347347
* @return {mixed} In the case of *actions*: The first argument (possibly modified by hooks).
348348
*/
349349
async aHook(name, ...args) {
350-
let calls = this._asyncHooks[name] || [],
351-
isFilter = this.isFilter(name);
350+
const calls = this._asyncHooks[name] || [],
351+
isFilter = this.isFilter(name);
352352

353353
if (! calls.length) {
354354
return isFilter ? true : args[0];
355355
}
356356

357357
// console.log(`[event] Fire ${calls.length} async hook for "${name}"${args.length ? ' with ' + args.length + ' arguments' : ''}`);
358-
var returns = await Promise.all(calls.map(e => e.apply(this, args)));
358+
const returns = await Promise.all(calls.map(e => e.apply(this, args)));
359359

360360
this.emitCatchAll(name, ...args);
361-
return isFilter ? returns.include(false) : returns;
361+
362+
return isFilter ? !returns.includes(false) : returns;
362363
}
363364
}

test/fileAddSpec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,29 @@ describe('fileAdd event', function() {
8787
expect(valid).toBeTruthy();
8888
});
8989

90+
it('should validate multiple filter-file aHooks', async function() {
91+
const customFunction = jasmine.createSpy('fn');
92+
flow.on('filter-file', async () => {
93+
customFunction();
94+
return true;
95+
});
96+
flow.on('filter-file', async () => {
97+
customFunction();
98+
return false; // a single hook returning false should prevail
99+
});
100+
flow.on('filter-file', async () => {
101+
customFunction();
102+
return true;
103+
});
104+
let valid = false;
105+
flow.on('files-added', (files) => {
106+
valid = files.length === 0;
107+
});
108+
await flow.asyncAddFile(new Blob(['file part']));
109+
expect(valid).toBeTruthy();
110+
expect(customFunction).toHaveBeenCalledTimes(3);
111+
});
112+
90113
describe('async/sync hooks', function () {
91114
beforeAll(function() {
92115
jasmine.getEnv().addReporter({

0 commit comments

Comments
 (0)