@clickup/ent-framework
Version:
A PostgreSQL graph-database-alike library with microsharding and row-level security
43 lines • 1.44 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.Registry = void 0;
const misc_1 = require("./misc");
/**
* Represents a container of TObj's that can be created in the container from
* the TData data. If the object corresponding to a particular data already
* exists, it's returned instead of being created.
*/
class Registry {
constructor(options) {
this.options = options;
this.map = new Map();
}
/**
* Computes the key for the data and returns the object corresponding to that
* key if it already exists in the registry. Otherwise, creates a new object,
* adds it to the registry and returns it.
*/
getOrCreate(data) {
const key = this.options.key(data);
let obj = this.map.get(key);
if (!obj) {
obj = this.options.create(data);
this.map.set(key, obj);
}
return [obj, key];
}
/**
* Deletes all objects from the registry except those whose keys are in the
* keepKeys set. For each object, calls an optional end() handler.
*/
async deleteExcept(keepKeys) {
await (0, misc_1.mapJoin)([...this.map], async ([key, obj]) => {
if (!keepKeys.has(key)) {
this.map.delete(key);
await this.options.end?.(obj);
}
});
}
}
exports.Registry = Registry;
//# sourceMappingURL=Registry.js.map
;