mirror of
https://github.com/github/codeql-action.git
synced 2025-12-27 01:30:10 +08:00
* Bump @ava/typescript from 3.0.1 to 4.0.0 Bumps [@ava/typescript](https://github.com/avajs/typescript) from 3.0.1 to 4.0.0. - [Release notes](https://github.com/avajs/typescript/releases) - [Commits](https://github.com/avajs/typescript/compare/v3.0.1...v4.0.0) --- updated-dependencies: - dependency-name: "@ava/typescript" dependency-type: direct:development update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> * Update checked-in dependencies --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions@github.com>
42 lines
1022 B
JavaScript
42 lines
1022 B
JavaScript
import mimicFunction from 'mimic-fn';
|
|
|
|
const calledFunctions = new WeakMap();
|
|
|
|
const onetime = (function_, options = {}) => {
|
|
if (typeof function_ !== 'function') {
|
|
throw new TypeError('Expected a function');
|
|
}
|
|
|
|
let returnValue;
|
|
let callCount = 0;
|
|
const functionName = function_.displayName || function_.name || '<anonymous>';
|
|
|
|
const onetime = function (...arguments_) {
|
|
calledFunctions.set(onetime, ++callCount);
|
|
|
|
if (callCount === 1) {
|
|
returnValue = function_.apply(this, arguments_);
|
|
function_ = null;
|
|
} else if (options.throw === true) {
|
|
throw new Error(`Function \`${functionName}\` can only be called once`);
|
|
}
|
|
|
|
return returnValue;
|
|
};
|
|
|
|
mimicFunction(onetime, function_);
|
|
calledFunctions.set(onetime, callCount);
|
|
|
|
return onetime;
|
|
};
|
|
|
|
onetime.callCount = function_ => {
|
|
if (!calledFunctions.has(function_)) {
|
|
throw new Error(`The given function \`${function_.name}\` is not wrapped by the \`onetime\` package`);
|
|
}
|
|
|
|
return calledFunctions.get(function_);
|
|
};
|
|
|
|
export default onetime;
|