Skip to content
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 flag to run event handlers sequentially #3423

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/UserConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ class UserConfig {
return this.events.emit(eventName, ...args);
}

setEventEmitterMode(mode) {
this.events.setHandlerMode(mode);
}

/*
* Universal getters
*/
Expand Down
21 changes: 20 additions & 1 deletion src/Util/AsyncEventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { EventEmitter } from "node:events";
* It can be used for time measurements during a build.
*/
class AsyncEventEmitter extends EventEmitter {
#handlerMode = "parallel";

// TypeScript slop
constructor(...args) {
super(...args);
Expand All @@ -22,7 +24,20 @@ class AsyncEventEmitter extends EventEmitter {
return [];
}

return Promise.all(listeners.map((listener) => listener.apply(this, args)));
if (this.#handlerMode == "sequential") {
const result = [];
for (const listener of listeners) {
const returnValue = await listener.apply(this, args);
result.push(returnValue);
}
return result;
} else {
return Promise.all(
listeners.map((listener) => {
return listener.apply(this, args);
}),
);
}
}

/**
Expand Down Expand Up @@ -51,6 +66,10 @@ class AsyncEventEmitter extends EventEmitter {

return this.emit.call(this, type, ...argsMap);
}

setHandlerMode(mode) {
this.#handlerMode = mode;
}
}

export default AsyncEventEmitter;
27 changes: 27 additions & 0 deletions test/EleventyTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,33 @@ test("eleventy.before and eleventy.after Event Arguments, directories", async (t
let results = await elev.toJSON();
});

test("eleventy.after fires sequentially setting eventEmitterMode 'sequential'", async (t) => {
let reachFirst;
const firstReached = new Promise(resolve => reachFirst = resolve)
let next;
const firstResult = new Promise(resolve => next = resolve)
let secondCalled = false;
let elev = new Eleventy("./test/noop/", "./test/noop/_site", {
config: function (eleventyConfig) {
eleventyConfig.setEventEmitterMode('sequential')
eleventyConfig.on("eleventy.after", arg => {
reachFirst()
return firstResult;
})
eleventyConfig.on("eleventy.after", arg => {
secondCalled = true;
})
},
});
const resultPromise = elev.toJSON();
await firstReached;
t.is(secondCalled, false)
next()
await 'microtask'
t.is(secondCalled, true)
await resultPromise;
})

test("setInputDirectory config method #1503", async (t) => {
t.plan(5);
let elev = new Eleventy("./test/noop/", "./test/noop/_site", {
Expand Down