@mittwald/kubernetes
Version:
Kubernetes client library
122 lines • 4.16 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CachingLookupStore = exports.InMemoryStore = exports.ObservableStoreDecorator = void 0;
class ObservableStoreDecorator {
constructor(inner) {
this.inner = inner;
this.onStoreHandlers = [];
this.onRemoveHandlers = [];
this.onSyncedHandlers = [];
}
onStoredOrUpdated(fn) {
this.onStoreHandlers.push(fn);
}
onRemoved(fn) {
this.onRemoveHandlers.push(fn);
}
onSynced(fn) {
this.onSyncedHandlers.push(fn);
}
get(namespace, name) {
return this.inner.get(namespace, name);
}
pull(obj) {
return __awaiter(this, void 0, void 0, function* () {
yield this.inner.pull(obj);
yield Promise.all(this.onRemoveHandlers.map((h) => h(obj)));
});
}
store(obj) {
return __awaiter(this, void 0, void 0, function* () {
yield this.inner.store(obj);
yield Promise.all(this.onStoreHandlers.map((h) => h(obj)));
});
}
sync(objs) {
return __awaiter(this, void 0, void 0, function* () {
yield this.inner.sync(objs);
yield Promise.all(this.onSyncedHandlers.map((h) => h(objs)));
});
}
}
exports.ObservableStoreDecorator = ObservableStoreDecorator;
class InMemoryStore {
constructor() {
this.objects = new Map();
}
store(obj) {
this.objects.set(`${obj.metadata.namespace}/${obj.metadata.name}`, obj);
}
pull(obj) {
this.objects.delete(`${obj.metadata.namespace}/${obj.metadata.name}`);
}
sync(objs) {
return __awaiter(this, void 0, void 0, function* () {
this.objects = new Map();
yield Promise.all(objs.map((o) => this.store(o)));
});
}
get(namespace, name) {
return this.objects.get(`${namespace}/${name}`);
}
}
exports.InMemoryStore = InMemoryStore;
class CachingLookupStore {
constructor(api, expirationSeconds = 3600) {
this.api = api;
this.expirationSeconds = expirationSeconds;
this.cache = new Map();
}
storeInMap(obj, map) {
const { namespace, name } = obj.metadata;
const key = `${namespace}/${name}`;
const exp = new Date();
exp.setSeconds(exp.getSeconds() + this.expirationSeconds);
map.set(key, {
entry: obj,
until: exp,
});
}
store(obj) {
this.storeInMap(obj, this.cache);
}
sync(objs) {
const newCache = new Map();
for (const obj of objs) {
this.storeInMap(obj, newCache);
}
this.cache = newCache;
}
get(namespace, name) {
return __awaiter(this, void 0, void 0, function* () {
const key = `${namespace}/${name}`;
if (this.cache.has(key)) {
const entry = this.cache.get(key);
if (entry.until > new Date()) {
return entry.entry;
}
}
const result = yield this.api.namespace(namespace).get(name);
if (result) {
this.store(result);
}
return result;
});
}
pull(obj) {
const { namespace, name } = obj.metadata;
const key = `${namespace}/${name}`;
this.cache.delete(key);
}
}
exports.CachingLookupStore = CachingLookupStore;
//# sourceMappingURL=store.js.map