object-rewrite
Advanced tools
Comparing version 10.0.3 to 10.0.4
@@ -144,2 +144,3 @@ import assert from 'assert'; | ||
schema, | ||
isAsync: fn[Symbol.toStringTag] === 'AsyncFunction', | ||
onInit: (context, logger) => handleCb({ | ||
@@ -146,0 +147,0 @@ type: 'init', |
@@ -56,7 +56,8 @@ import assert from 'assert'; | ||
assert(context instanceof Object && !Array.isArray(context)); | ||
const { promises } = injectRewriter(input, { | ||
const { promises, sync } = injectRewriter(input, { | ||
injectMap: initPluginMap(injectMap, input, context, logger), | ||
promises: [] | ||
promises: [], | ||
sync: [] | ||
}); | ||
return promises; | ||
return { promises, sync }; | ||
}; | ||
@@ -78,4 +79,5 @@ const rewriteEnd = (input, context) => { | ||
const context = { ...context_, ...initContext }; | ||
const promises = rewriteStart(input, context); | ||
const { sync, promises } = rewriteStart(input, context); | ||
assert(promises.length === 0, 'Please use rewriteAsync() for async logic'); | ||
sync.map((f) => f()); | ||
rewriteEnd(input, context); | ||
@@ -85,4 +87,5 @@ }, | ||
const context = { ...context_, ...initContext }; | ||
const promises = rewriteStart(input, context); | ||
const { sync, promises } = rewriteStart(input, context); | ||
await Promise.all(promises.map((p) => p())); | ||
sync.map((f) => f()); | ||
rewriteEnd(input, context); | ||
@@ -89,0 +92,0 @@ } |
@@ -27,2 +27,3 @@ import assert from 'assert'; | ||
const promises = []; | ||
const sync = []; | ||
plugins.forEach((plugin) => { | ||
@@ -36,12 +37,15 @@ const exec = (r) => { | ||
}; | ||
const result = plugin.fn(kwargs); | ||
if (result instanceof Promise) { | ||
promises.push(async () => exec(await result)); | ||
if (plugin.self.meta.isAsync) { | ||
promises.push(async () => { | ||
const r = await plugin.fn(kwargs); | ||
return exec(r); | ||
}); | ||
} else { | ||
exec(result); | ||
sync.push(() => exec(plugin.fn(kwargs))); | ||
} | ||
}); | ||
context.promises.push(...promises); | ||
context.sync.push(...sync); | ||
return true; | ||
} | ||
}); |
{ | ||
"name": "object-rewrite", | ||
"type": "module", | ||
"version": "10.0.3", | ||
"version": "10.0.4", | ||
"description": "Rewrite Object(s) in place using plugins.", | ||
@@ -6,0 +6,0 @@ "main": "lib/index.js", |
28868
565