@commodo/fields-storage
Version:
We're working hard to get all the docs in order. New articles will be added daily.
82 lines (59 loc) • 1.59 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _name = require("@commodo/name");
var _StoragePoolEntry = _interopRequireDefault(require("./StoragePoolEntry"));
class StoragePool {
constructor() {
this.pool = {};
}
getPool() {
return this.pool;
}
add(model) {
const namespace = (0, _name.getName)(model);
if (!this.getPool()[namespace]) {
this.getPool()[namespace] = {};
}
this.getPool()[namespace][model.id] = new _StoragePoolEntry.default(model);
return this;
}
has(model, id) {
const namespace = (0, _name.getName)(model);
if (!this.getPool()[namespace]) {
return false;
}
const modelId = id || model.id;
return typeof this.getPool()[namespace][modelId] !== "undefined";
}
remove(model) {
const namespace = (0, _name.getName)(model);
if (!this.getPool()[namespace]) {
return this;
}
delete this.getPool()[namespace][model.id];
return this;
}
get(model, id) {
const namespace = (0, _name.getName)(model);
if (!this.getPool()[namespace]) {
return undefined;
}
const modelId = id || model.id;
const poolEntry = this.getPool()[namespace][modelId];
if (poolEntry) {
return poolEntry.getModel();
}
return undefined;
}
flush() {
this.pool = {};
return this;
}
}
var _default = StoragePool;
exports.default = _default;
//# sourceMappingURL=StoragePool.js.map