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: make log filter async friendly by adding logAsync #3141

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
22 changes: 20 additions & 2 deletions src/defaultConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,26 @@ export default function (config) {
});

config.addFilter("log", (input, ...messages) => {
console.log(input, ...messages);
return input;
const allEntries = [input, ...messages];
if(allEntries.some(entry => entry instanceof Promise)) {
// If any entry is a Promise, we switch to async bahavior.
// Async bahvior means that we return a Promise and await all entries before logging them to
// avoid logs of "Promise {pending}".
// After all entries are resolved and logged, the returned Promise resolves to the input value.

// additional Promise wrapper to only resolve to the original input value
return new Promise((res, rej) => {
Promise.all(allEntries).then(resolvedEntries => {
console.log(...resolvedEntries);
// We can resolve to the original input argument safely here.
res(input);
}).catch(e => rej(e))
})
} else {
// if all entries are sync, log them normally and return `input`
console.log(...allEntries);
return input;
}
});

config.addFilter("getCollectionItemIndex", function (collection, pageOverride) {
Expand Down