UNPKG

3.27 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11Object.defineProperty(exports, "__esModule", { value: true });
12class ObservableStoreDecorator {
13 constructor(inner) {
14 this.inner = inner;
15 this.onStoreHandlers = [];
16 this.onRemoveHandlers = [];
17 this.onSyncedHandlers = [];
18 }
19 onStoredOrUpdated(fn) {
20 this.onStoreHandlers.push(fn);
21 }
22 onRemoved(fn) {
23 this.onRemoveHandlers.push(fn);
24 }
25 onSynced(fn) {
26 this.onSyncedHandlers.push(fn);
27 }
28 get(namespace, name) {
29 return this.inner.get(namespace, name);
30 }
31 pull(obj) {
32 this.inner.pull(obj);
33 this.onRemoveHandlers.forEach(fn => fn(obj));
34 }
35 store(obj) {
36 this.inner.store(obj);
37 this.onStoreHandlers.forEach(fn => fn(obj));
38 }
39 sync(objs) {
40 this.inner.sync(objs);
41 this.onSyncedHandlers.forEach(fn => fn(objs));
42 }
43}
44exports.ObservableStoreDecorator = ObservableStoreDecorator;
45class InMemoryStore {
46 constructor() {
47 this.objects = new Map();
48 }
49 store(obj) {
50 this.objects.set(`${obj.metadata.namespace}/${obj.metadata.name}`, obj);
51 }
52 pull(obj) {
53 this.objects.delete(`${obj.metadata.namespace}/${obj.metadata.name}`);
54 }
55 sync(objs) {
56 this.objects = new Map();
57 objs.forEach(o => this.store(o));
58 }
59 get(namespace, name) {
60 return __awaiter(this, void 0, void 0, function* () {
61 return this.objects.get(`${namespace}/${name}`);
62 });
63 }
64}
65exports.InMemoryStore = InMemoryStore;
66class CachingLookupStore {
67 constructor(api) {
68 this.api = api;
69 this.cache = new Map();
70 }
71 store(obj) {
72 // no-op
73 }
74 sync(objs) {
75 // no-op
76 }
77 get(namespace, name) {
78 return __awaiter(this, void 0, void 0, function* () {
79 const key = `${namespace}/${name}`;
80 if (this.cache.has(key)) {
81 const entry = this.cache.get(key);
82 if (entry.until > new Date()) {
83 return entry.entry;
84 }
85 }
86 const result = yield this.api.namespace(namespace).get(name);
87 if (result) {
88 const exp = new Date();
89 exp.setSeconds(exp.getSeconds() + 3600);
90 this.cache.set(key, {
91 entry: result,
92 until: exp,
93 });
94 }
95 return result;
96 });
97 }
98 pull(obj) {
99 // no-op
100 }
101}
102exports.CachingLookupStore = CachingLookupStore;
103//# sourceMappingURL=store.js.map
\No newline at end of file