Files
react-native-devtools-frontend/node_modules/onetime/index.js
T
Tim van der Lippe c8f6ffd7c3 Update ESLint to 6.8.0
Also update the PRESUBMIT linter configuration to make sure
it actually runs the linter when updating ESLint. Otherwise,
it would not properly do the full linter check.

R=jacktfranklin@chromium.org
DISABLE_THIRD_PARTY_CHECK=Update ESLint

Bug: 1068145
Change-Id: Ibf2de156366dc64c90ee1aced5537d2038e7a268
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2137384
Commit-Queue: Tim van der Lippe <tvanderlippe@chromium.org>
Reviewed-by: Jack Franklin <jacktfranklin@chromium.org>
2020-04-06 13:52:18 +00:00

51 lines
1.0 KiB
JavaScript

'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);
};