Initial commit (from f5274cbdce4ae7c9e4b937dcdf95ac70ae436d5f)
This commit is contained in:
commit
28ccc3db2d
13974 changed files with 2618436 additions and 0 deletions
50
node_modules/onetime/index.js
generated
vendored
Normal file
50
node_modules/onetime/index.js
generated
vendored
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
'use strict';
|
||||
const mimicFn = require('mimic-fn');
|
||||
|
||||
const calledFunctions = new WeakMap();
|
||||
|
||||
const oneTime = (fn, options = {}) => {
|
||||
if (typeof fn !== 'function') {
|
||||
throw new TypeError('Expected a function');
|
||||
}
|
||||
|
||||
let ret;
|
||||
let isCalled = false;
|
||||
let callCount = 0;
|
||||
const functionName = fn.displayName || fn.name || '<anonymous>';
|
||||
|
||||
const onetime = function (...args) {
|
||||
calledFunctions.set(onetime, ++callCount);
|
||||
|
||||
if (isCalled) {
|
||||
if (options.throw === true) {
|
||||
throw new Error(`Function \`${functionName}\` can only be called once`);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
isCalled = true;
|
||||
ret = fn.apply(this, args);
|
||||
fn = null;
|
||||
|
||||
return ret;
|
||||
};
|
||||
|
||||
mimicFn(onetime, fn);
|
||||
calledFunctions.set(onetime, callCount);
|
||||
|
||||
return onetime;
|
||||
};
|
||||
|
||||
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);
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue