2020-01-28 13:07:56 +08:00
|
|
|
'use strict';
|
|
|
|
const mimicFn = require('mimic-fn');
|
|
|
|
|
2020-03-31 20:40:00 +08:00
|
|
|
const calledFunctions = new WeakMap();
|
2020-01-28 13:07:56 +08:00
|
|
|
|
2020-03-31 20:40:00 +08:00
|
|
|
const oneTime = (fn, options = {}) => {
|
2020-01-28 13:07:56 +08:00
|
|
|
if (typeof fn !== 'function') {
|
|
|
|
throw new TypeError('Expected a function');
|
|
|
|
}
|
|
|
|
|
2020-03-07 11:45:40 +08:00
|
|
|
let ret;
|
2020-03-31 20:40:00 +08:00
|
|
|
let isCalled = false;
|
|
|
|
let callCount = 0;
|
|
|
|
const functionName = fn.displayName || fn.name || '<anonymous>';
|
2020-01-28 13:07:56 +08:00
|
|
|
|
2020-03-31 20:40:00 +08:00
|
|
|
const onetime = function (...args) {
|
|
|
|
calledFunctions.set(onetime, ++callCount);
|
|
|
|
|
|
|
|
if (isCalled) {
|
|
|
|
if (options.throw === true) {
|
|
|
|
throw new Error(`Function \`${functionName}\` can only be called once`);
|
2020-01-28 13:07:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:40:00 +08:00
|
|
|
isCalled = true;
|
|
|
|
ret = fn.apply(this, args);
|
2020-01-28 13:07:56 +08:00
|
|
|
fn = null;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
mimicFn(onetime, fn);
|
2020-03-31 20:40:00 +08:00
|
|
|
calledFunctions.set(onetime, callCount);
|
2020-01-28 13:07:56 +08:00
|
|
|
|
|
|
|
return onetime;
|
|
|
|
};
|
2020-03-31 20:40:00 +08:00
|
|
|
|
|
|
|
module.exports = oneTime;
|
|
|
|
// TODO: Remove this for the next major release
|
|
|
|
module.exports.default = oneTime;
|
|
|
|
|
|
|
|
module.exports.callCount = fn => {
|
|
|
|
if (!calledFunctions.has(fn)) {
|
|
|
|
throw new Error(`The given function \`${fn.name}\` is not wrapped by the \`onetime\` package`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return calledFunctions.get(fn);
|
|
|
|
};
|