UNPKG

4.05 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 return __awaiter(this, void 0, void 0, function* () {
33 yield this.inner.pull(obj);
34 yield Promise.all(this.onRemoveHandlers.map(h => h(obj)));
35 });
36 }
37 store(obj) {
38 return __awaiter(this, void 0, void 0, function* () {
39 yield this.inner.store(obj);
40 yield Promise.all(this.onStoreHandlers.map(h => h(obj)));
41 });
42 }
43 sync(objs) {
44 return __awaiter(this, void 0, void 0, function* () {
45 yield this.inner.sync(objs);
46 yield Promise.all(this.onSyncedHandlers.map(h => h(objs)));
47 });
48 }
49}
50exports.ObservableStoreDecorator = ObservableStoreDecorator;
51class InMemoryStore {
52 constructor() {
53 this.objects = new Map();
54 }
55 store(obj) {
56 return __awaiter(this, void 0, void 0, function* () {
57 this.objects.set(`${obj.metadata.namespace}/${obj.metadata.name}`, obj);
58 });
59 }
60 pull(obj) {
61 return __awaiter(this, void 0, void 0, function* () {
62 this.objects.delete(`${obj.metadata.namespace}/${obj.metadata.name}`);
63 });
64 }
65 sync(objs) {
66 return __awaiter(this, void 0, void 0, function* () {
67 this.objects = new Map();
68 yield Promise.all(objs.map(o => this.store(o)));
69 });
70 }
71 get(namespace, name) {
72 return __awaiter(this, void 0, void 0, function* () {
73 return this.objects.get(`${namespace}/${name}`);
74 });
75 }
76}
77exports.InMemoryStore = InMemoryStore;
78class CachingLookupStore {
79 constructor(api) {
80 this.api = api;
81 this.cache = new Map();
82 }
83 store(obj) {
84 return __awaiter(this, void 0, void 0, function* () {
85 // no-op
86 });
87 }
88 sync(objs) {
89 return __awaiter(this, void 0, void 0, function* () {
90 // no-op
91 });
92 }
93 get(namespace, name) {
94 return __awaiter(this, void 0, void 0, function* () {
95 const key = `${namespace}/${name}`;
96 if (this.cache.has(key)) {
97 const entry = this.cache.get(key);
98 if (entry.until > new Date()) {
99 return entry.entry;
100 }
101 }
102 const result = yield this.api.namespace(namespace).get(name);
103 if (result) {
104 const exp = new Date();
105 exp.setSeconds(exp.getSeconds() + 3600);
106 this.cache.set(key, {
107 entry: result,
108 until: exp,
109 });
110 }
111 return result;
112 });
113 }
114 pull(obj) {
115 return __awaiter(this, void 0, void 0, function* () {
116 // no-op
117 });
118 }
119}
120exports.CachingLookupStore = CachingLookupStore;
121//# sourceMappingURL=store.js.map
\No newline at end of file