toggles
Version:
A collection of React hooks for toggles with dynamic noun and verb APIs
106 lines (105 loc) • 4.12 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.globalNouns = exports.GlobalNounStore = void 0;
var nouns_1 = require("./nouns");
var GlobalNounStore = /** @class */ (function () {
function GlobalNounStore() {
this.toggles = new Map();
this.cleanupInterval = null;
this.TTL = 5 * 60 * 1000; // 5 minutes Time To Live
this.startCleanupTimer();
}
GlobalNounStore.prototype.startCleanupTimer = function () {
var _this = this;
if (typeof setInterval !== 'undefined') {
this.cleanupInterval = setInterval(function () {
_this.cleanup();
}, 60 * 1000); // Run cleanup every minute
}
};
GlobalNounStore.prototype.cleanup = function () {
var _this = this;
var now = Date.now();
var toDelete = [];
this.toggles.forEach(function (entry, name) {
// Only cleanup if no active references and TTL exceeded
if (entry.refCount === 0 && now - entry.lastAccessed > _this.TTL) {
toDelete.push(name);
}
});
toDelete.forEach(function (name) { return _this.toggles.delete(name); });
};
GlobalNounStore.prototype.get = function (name, initialValue) {
if (initialValue === void 0) { initialValue = false; }
var entry = this.toggles.get(name);
if (!entry) {
entry = {
noun: null,
state: initialValue,
subscribers: new Set(),
refCount: 0,
lastAccessed: Date.now(),
initialValue: initialValue,
initializationStack: new Error().stack
};
entry.noun = (0, nouns_1.createNounFromState)(name, function () { return entry.state; }, function (newState) {
entry.state = newState;
entry.subscribers.forEach(function (callback) { return callback(); });
});
this.toggles.set(name, entry);
}
else {
// Check for initial value conflict
if (entry.initialValue !== undefined && entry.initialValue !== initialValue) {
console.error("Global toggle \"".concat(name, "\" initialized with conflicting values!\n") +
"First initialization: ".concat(entry.initialValue, "\n") +
"".concat(entry.initializationStack, "\n\n") +
"Second initialization: ".concat(initialValue, "\n") +
"".concat(new Error().stack));
}
}
entry.lastAccessed = Date.now();
return entry.noun;
};
GlobalNounStore.prototype.acquire = function (name) {
var entry = this.toggles.get(name);
if (entry) {
entry.refCount++;
}
};
GlobalNounStore.prototype.release = function (name) {
var entry = this.toggles.get(name);
if (entry) {
entry.refCount = Math.max(0, entry.refCount - 1);
}
};
GlobalNounStore.prototype.subscribe = function (name, callback) {
var entry = this.toggles.get(name);
if (!entry) {
throw new Error("Toggle \"".concat(name, "\" not found in global nouns"));
}
entry.subscribers.add(callback);
return function () { return entry.subscribers.delete(callback); };
};
GlobalNounStore.prototype.clear = function () {
if (this.cleanupInterval) {
clearInterval(this.cleanupInterval);
this.cleanupInterval = null;
}
this.toggles.clear();
this.startCleanupTimer();
};
GlobalNounStore.prototype.destroy = function () {
if (this.cleanupInterval) {
clearInterval(this.cleanupInterval);
this.cleanupInterval = null;
}
this.toggles.clear();
};
GlobalNounStore.prototype.has = function (name) {
return this.toggles.has(name);
};
return GlobalNounStore;
}());
exports.GlobalNounStore = GlobalNounStore;
exports.globalNouns = new GlobalNounStore();
;