singleton-proxy
Version:
A lightweight TypeScript singleton implementation using Proxy pattern
21 lines (20 loc) • 618 B
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.singleton = singleton;
function singleton(target) {
let instance = null;
return new Proxy(target, {
construct(target, args) {
if (!instance) {
instance = new target(...args);
}
return instance;
},
apply(target, thisArg, args) {
if (!instance) {
instance = target.apply(thisArg, args);
}
return instance !== null && instance !== void 0 ? instance : target.apply(thisArg, args);
}
});
}
;