Files
hello-world-javascript-action/node_modules/before-after-hook/lib/add.js

47 lines
1004 B
JavaScript
Raw Normal View History

2022-04-05 10:05:12 +10:00
module.exports = addHook;
2019-08-06 21:05:52 -07:00
2022-04-05 10:05:12 +10:00
function addHook(state, kind, name, hook) {
var orig = hook;
2019-08-06 21:05:52 -07:00
if (!state.registry[name]) {
2022-04-05 10:05:12 +10:00
state.registry[name] = [];
2019-08-06 21:05:52 -07:00
}
2022-04-05 10:05:12 +10:00
if (kind === "before") {
2019-08-06 21:05:52 -07:00
hook = function (method, options) {
return Promise.resolve()
.then(orig.bind(null, options))
2022-04-05 10:05:12 +10:00
.then(method.bind(null, options));
};
2019-08-06 21:05:52 -07:00
}
2022-04-05 10:05:12 +10:00
if (kind === "after") {
2019-08-06 21:05:52 -07:00
hook = function (method, options) {
2022-04-05 10:05:12 +10:00
var result;
2019-08-06 21:05:52 -07:00
return Promise.resolve()
.then(method.bind(null, options))
.then(function (result_) {
2022-04-05 10:05:12 +10:00
result = result_;
return orig(result, options);
2019-08-06 21:05:52 -07:00
})
.then(function () {
2022-04-05 10:05:12 +10:00
return result;
});
};
2019-08-06 21:05:52 -07:00
}
2022-04-05 10:05:12 +10:00
if (kind === "error") {
2019-08-06 21:05:52 -07:00
hook = function (method, options) {
return Promise.resolve()
.then(method.bind(null, options))
.catch(function (error) {
2022-04-05 10:05:12 +10:00
return orig(error, options);
});
};
2019-08-06 21:05:52 -07:00
}
state.registry[name].push({
hook: hook,
2022-04-05 10:05:12 +10:00
orig: orig,
});
2019-08-06 21:05:52 -07:00
}