UNPKG

@stnekroman/tstools

Version:

Set of handy tools for TypeScript development

21 lines (20 loc) 686 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SingletonGuardError = void 0; exports.SingletonGuard = SingletonGuard; class SingletonGuardError extends Error { } exports.SingletonGuardError = SingletonGuardError; function SingletonGuard() { let instanceRefCount = 0; return (ctr) => { function newConstructor(...args) { if (++instanceRefCount > 1) { throw new SingletonGuardError(`SingletonGuard: class ${ctr.name} cannot have more then one instance`); } return new ctr(...args); } newConstructor.prototype = ctr.prototype; return newConstructor; }; }