Skip to content
Open
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
38 changes: 16 additions & 22 deletions WrappedPlugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,35 @@ const genWrappedFunc = ({
// to complete compilation. If this gets invoked and not the subsequent
// call, then our data will be inaccurate, sadly
addEndEvent();
const normalArgMap = a => wrap(a, pluginName, smp);
let ret;
if (endType === "wrapDone")
ret = func.apply(
context,
args.map(a => wrap(a, pluginName, smp, addEndEvent))
args.map((a) => wrap(a, pluginName, smp, addEndEvent))
);
else if (endType === "async") {
const argsButLast = args.slice(0, args.length - 1);
const callback = args[args.length - 1];
ret = func.apply(
context,
argsButLast.map(normalArgMap).concat((...callbackArgs) => {
argsButLast.concat((...callbackArgs) => {
addEndEvent();
callback(...callbackArgs);
})
);
} else if (endType === "promise")
ret = func.apply(context, args.map(normalArgMap)).then(promiseArg => {
ret = func.apply(context, args).then((promiseArg) => {
addEndEvent();
return promiseArg;
});
else ret = func.apply(context, args.map(normalArgMap));
else ret = func.apply(context, args);
addEndEvent();

return ret;
};

const genPluginMethod = (orig, pluginName, smp, type) =>
function(method, func) {
function (method, func) {
const timeEventName = pluginName + "/" + type + "/" + method;
const wrappedFunc = genWrappedFunc({
func,
Expand All @@ -70,7 +69,7 @@ const genPluginMethod = (orig, pluginName, smp, type) =>
};

const wrapTap = (tap, pluginName, smp, type, method) =>
function(id, func) {
function (id, func) {
const timeEventName = pluginName + "/" + type + "/" + method;
const wrappedFunc = genWrappedFunc({
func,
Expand All @@ -83,7 +82,7 @@ const wrapTap = (tap, pluginName, smp, type, method) =>
};

const wrapTapAsync = (tapAsync, pluginName, smp, type, method) =>
function(id, func) {
function (id, func) {
const timeEventName = pluginName + "/" + type + "/" + method;
const wrappedFunc = genWrappedFunc({
func,
Expand All @@ -97,7 +96,7 @@ const wrapTapAsync = (tapAsync, pluginName, smp, type, method) =>
};

const wrapTapPromise = (tapPromise, pluginName, smp, type, method) =>
function(id, func) {
function (id, func) {
const timeEventName = pluginName + "/" + type + "/" + method;
const wrappedFunc = genWrappedFunc({
func,
Expand All @@ -115,12 +114,12 @@ const wrapHooks = (orig, pluginName, smp, type) => {
const hooks = orig.hooks;
if (!hooks) return hooks;
const prevWrapped = wrappedHooks.find(
w =>
(w) =>
w.pluginName === pluginName && (w.orig === hooks || w.wrapped === hooks)
);
if (prevWrapped) return prevWrapped.wrapped;

const genProxy = method => {
const genProxy = (method) => {
const proxy = new Proxy(hooks[method], {
get: (target, property) => {
const raw = Reflect.get(target, property);
Expand Down Expand Up @@ -170,7 +169,8 @@ const construcNamesToWrap = [
const wrappedObjs = [];
const findWrappedObj = (orig, pluginName) => {
const prevWrapped = wrappedObjs.find(
w => w.pluginName === pluginName && (w.orig === orig || w.wrapped === orig)
(w) =>
w.pluginName === pluginName && (w.orig === orig || w.wrapped === orig)
);
if (prevWrapped) return prevWrapped.wrapped;
};
Expand All @@ -179,15 +179,15 @@ const wrap = (orig, pluginName, smp, addEndEvent) => {
const prevWrapped = findWrappedObj(orig, pluginName);
if (prevWrapped) return prevWrapped;

const getOrigConstrucName = target =>
const getOrigConstrucName = (target) =>
target && target.constructor && target.constructor.name;
const getShouldWrap = target => {
const getShouldWrap = (target) => {
const origConstrucName = getOrigConstrucName(target);
return construcNamesToWrap.includes(origConstrucName);
};
const shouldWrap = getShouldWrap(orig);
const shouldSoftWrap = Object.keys(orig)
.map(k => orig[k])
.map((k) => orig[k])
.some(getShouldWrap);

let wrappedReturn;
Expand All @@ -196,7 +196,7 @@ const wrap = (orig, pluginName, smp, addEndEvent) => {
const vanillaFunc = orig.name === "next";
wrappedReturn =
vanillaFunc && addEndEvent
? function() {
? function () {
// do this before calling the callback, since the callback can start
// the next plugin step
addEndEvent();
Expand Down Expand Up @@ -238,12 +238,6 @@ const wrap = (orig, pluginName, smp, addEndEvent) => {
Object.defineProperty(ret, "name", {
value: raw.name,
});
const funcProxy = new Proxy(ret, {
get: (target, property) => {
return raw[property];
},
});
return funcProxy;
}

return raw;
Expand Down
Loading