once: add debug

This commit is contained in:
Girish Ramakrishnan
2022-11-05 15:36:07 +01:00
parent f2d25ff2fd
commit 5af1bbfb3c
+6 -1
View File
@@ -2,10 +2,15 @@
exports = module.exports = once;
const debug = require('debug')('box:once');
// https://github.com/isaacs/once/blob/main/LICENSE (ISC)
function once (fn) {
const f = function () {
if (f.called) return f.value;
if (f.called) {
debug(`${f.name} was already called, returning previous return value`);
return f.value;
}
f.called = true;
return f.value = fn.apply(this, arguments);
};