gingersnap 0.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (251) hide show
  1. package/_virtual/_tslib.cjs +102 -0
  2. package/_virtual/_tslib.mjs +93 -0
  3. package/data/AtomicValue.d.ts +15 -0
  4. package/data/bus.cjs +179 -0
  5. package/data/bus.d.ts +72 -0
  6. package/data/bus.mjs +177 -0
  7. package/data/decoders/index.d.ts +4 -0
  8. package/data/decoders/json.cjs +12 -0
  9. package/data/decoders/json.d.ts +7 -0
  10. package/data/decoders/json.mjs +10 -0
  11. package/data/decoders/msgpack.cjs +14 -0
  12. package/data/decoders/msgpack.d.ts +7 -0
  13. package/data/decoders/msgpack.mjs +12 -0
  14. package/data/decoders/protobuf.d.ts +13 -0
  15. package/data/decoders/string.cjs +12 -0
  16. package/data/decoders/string.d.ts +7 -0
  17. package/data/decoders/string.mjs +10 -0
  18. package/data/decoders/type.d.ts +7 -0
  19. package/data/decoders.cjs +11 -0
  20. package/data/decoders.mjs +3 -0
  21. package/data/model/credentials.cjs +114 -0
  22. package/data/model/credentials.d.ts +61 -0
  23. package/data/model/credentials.mjs +109 -0
  24. package/data/model/extra.cjs +88 -0
  25. package/data/model/extra.d.ts +5 -0
  26. package/data/model/extra.mjs +66 -0
  27. package/data/model/index.d.ts +6 -0
  28. package/data/model/model.cjs +541 -0
  29. package/data/model/model.d.ts +207 -0
  30. package/data/model/model.mjs +518 -0
  31. package/data/model/plugin.cjs +96 -0
  32. package/data/model/plugin.d.ts +38 -0
  33. package/data/model/plugin.mjs +72 -0
  34. package/data/model/property.cjs +310 -0
  35. package/data/model/property.d.ts +83 -0
  36. package/data/model/property.mjs +278 -0
  37. package/data/model/types.cjs +32 -0
  38. package/data/model/types.d.ts +28 -0
  39. package/data/model/types.mjs +32 -0
  40. package/data/model.cjs +43 -0
  41. package/data/model.mjs +6 -0
  42. package/data/signal.cjs +47 -0
  43. package/data/signal.d.ts +17 -0
  44. package/data/signal.mjs +45 -0
  45. package/data/store/index.d.ts +4 -0
  46. package/data/store/json.cjs +58 -0
  47. package/data/store/json.d.ts +18 -0
  48. package/data/store/json.mjs +56 -0
  49. package/data/store/manager.cjs +206 -0
  50. package/data/store/manager.d.ts +44 -0
  51. package/data/store/manager.mjs +203 -0
  52. package/data/store/web.cjs +92 -0
  53. package/data/store/web.d.ts +25 -0
  54. package/data/store/web.mjs +89 -0
  55. package/data/value.cjs +66 -0
  56. package/data/value.mjs +64 -0
  57. package/data-structures/array/CyclicalList.cjs +57 -0
  58. package/data-structures/array/CyclicalList.d.ts +12 -0
  59. package/data-structures/array/CyclicalList.mjs +55 -0
  60. package/data-structures/array/Pair.cjs +30 -0
  61. package/data-structures/array/Pair.d.ts +17 -0
  62. package/data-structures/array/Pair.mjs +27 -0
  63. package/data-structures/array/Stack.cjs +96 -0
  64. package/data-structures/array/Stack.d.ts +41 -0
  65. package/data-structures/array/Stack.mjs +94 -0
  66. package/data-structures/array/Triple.cjs +34 -0
  67. package/data-structures/array/Triple.d.ts +19 -0
  68. package/data-structures/array/Triple.mjs +31 -0
  69. package/data-structures/array/Tuple.d.ts +8 -0
  70. package/data-structures/array/index.d.ts +5 -0
  71. package/data-structures/array.cjs +15 -0
  72. package/data-structures/array.mjs +4 -0
  73. package/data-structures/object/BufferQueue.cjs +98 -0
  74. package/data-structures/object/BufferQueue.d.ts +21 -0
  75. package/data-structures/object/BufferQueue.mjs +77 -0
  76. package/data-structures/object/CyclicalObject.cjs +155 -0
  77. package/data-structures/object/CyclicalObject.d.ts +74 -0
  78. package/data-structures/object/CyclicalObject.mjs +134 -0
  79. package/data-structures/object/Queue.cjs +178 -0
  80. package/data-structures/object/Queue.d.ts +31 -0
  81. package/data-structures/object/Queue.mjs +176 -0
  82. package/data-structures/object/SimpleQueue.cjs +53 -0
  83. package/data-structures/object/SimpleQueue.d.ts +13 -0
  84. package/data-structures/object/SimpleQueue.mjs +51 -0
  85. package/data-structures/object/TimeableObject.cjs +73 -0
  86. package/data-structures/object/TimeableObject.d.ts +30 -0
  87. package/data-structures/object/TimeableObject.mjs +71 -0
  88. package/data-structures/object/WaitableObject.cjs +61 -0
  89. package/data-structures/object/WaitableObject.d.ts +29 -0
  90. package/data-structures/object/WaitableObject.mjs +59 -0
  91. package/data-structures/object/WatchableObject.cjs +251 -0
  92. package/data-structures/object/WatchableObject.d.ts +88 -0
  93. package/data-structures/object/WatchableObject.mjs +230 -0
  94. package/data-structures/object/index.d.ts +7 -0
  95. package/data-structures/object.cjs +23 -0
  96. package/data-structures/object.mjs +7 -0
  97. package/errors/AbortError.cjs +9 -0
  98. package/errors/AbortError.d.ts +5 -0
  99. package/errors/AbortError.mjs +7 -0
  100. package/errors/CallExecutionError.cjs +13 -0
  101. package/errors/CallExecutionError.d.ts +7 -0
  102. package/errors/CallExecutionError.mjs +11 -0
  103. package/errors/FutureCancelled.cjs +11 -0
  104. package/errors/FutureCancelled.d.ts +6 -0
  105. package/errors/FutureCancelled.mjs +9 -0
  106. package/errors/FutureError.cjs +11 -0
  107. package/errors/FutureError.d.ts +6 -0
  108. package/errors/FutureError.mjs +9 -0
  109. package/errors/IllegalArgumentsError.cjs +13 -0
  110. package/errors/IllegalArgumentsError.d.ts +7 -0
  111. package/errors/IllegalArgumentsError.mjs +11 -0
  112. package/errors/IllegalOperationError.cjs +6 -0
  113. package/errors/IllegalOperationError.d.ts +2 -0
  114. package/errors/IllegalOperationError.mjs +4 -0
  115. package/errors/InvalidType.cjs +6 -0
  116. package/errors/InvalidType.d.ts +2 -0
  117. package/errors/InvalidType.mjs +4 -0
  118. package/errors/InvalidValue.cjs +11 -0
  119. package/errors/InvalidValue.d.ts +4 -0
  120. package/errors/InvalidValue.mjs +9 -0
  121. package/errors/MissingArgumentsError.cjs +13 -0
  122. package/errors/MissingArgumentsError.d.ts +7 -0
  123. package/errors/MissingArgumentsError.mjs +11 -0
  124. package/errors/NetworkError.cjs +13 -0
  125. package/errors/NetworkError.d.ts +8 -0
  126. package/errors/NetworkError.mjs +11 -0
  127. package/errors/NoSuchElement.cjs +6 -0
  128. package/errors/NoSuchElement.d.ts +2 -0
  129. package/errors/NoSuchElement.mjs +4 -0
  130. package/errors/NotImplemented.cjs +9 -0
  131. package/errors/NotImplemented.d.ts +5 -0
  132. package/errors/NotImplemented.mjs +7 -0
  133. package/errors/ParsingError.cjs +10 -0
  134. package/errors/ParsingError.d.ts +5 -0
  135. package/errors/ParsingError.mjs +8 -0
  136. package/errors/QueueEmptyError.cjs +6 -0
  137. package/errors/QueueEmptyError.d.ts +2 -0
  138. package/errors/QueueEmptyError.mjs +4 -0
  139. package/errors/QueueFullError.cjs +6 -0
  140. package/errors/QueueFullError.d.ts +2 -0
  141. package/errors/QueueFullError.mjs +4 -0
  142. package/errors/StackEmptyError.cjs +6 -0
  143. package/errors/StackEmptyError.d.ts +2 -0
  144. package/errors/StackEmptyError.mjs +4 -0
  145. package/errors/StackFullError.cjs +6 -0
  146. package/errors/StackFullError.d.ts +2 -0
  147. package/errors/StackFullError.mjs +4 -0
  148. package/errors/StreamEnded.cjs +6 -0
  149. package/errors/StreamEnded.d.ts +2 -0
  150. package/errors/StreamEnded.mjs +4 -0
  151. package/errors/TimeoutError.cjs +11 -0
  152. package/errors/TimeoutError.d.ts +6 -0
  153. package/errors/TimeoutError.mjs +9 -0
  154. package/errors/ValueDestroyedError.cjs +6 -0
  155. package/errors/ValueDestroyedError.d.ts +2 -0
  156. package/errors/ValueDestroyedError.mjs +4 -0
  157. package/errors/VerificationError.cjs +9 -0
  158. package/errors/VerificationError.d.ts +5 -0
  159. package/errors/VerificationError.mjs +7 -0
  160. package/errors/index.d.ts +18 -0
  161. package/errors.cjs +41 -0
  162. package/errors.mjs +18 -0
  163. package/files.cjs +201 -0
  164. package/files.d.ts +31 -0
  165. package/files.mjs +180 -0
  166. package/functools/index.d.ts +31 -0
  167. package/functools.cjs +371 -0
  168. package/functools.mjs +334 -0
  169. package/future/future.cjs +570 -0
  170. package/future/future.d.ts +208 -0
  171. package/future/future.mjs +548 -0
  172. package/future/group.cjs +180 -0
  173. package/future/group.d.ts +82 -0
  174. package/future/group.mjs +159 -0
  175. package/future/index.d.ts +3 -0
  176. package/future/result.cjs +19 -0
  177. package/future/result.d.ts +18 -0
  178. package/future/result.mjs +17 -0
  179. package/future.cjs +11 -0
  180. package/future.mjs +3 -0
  181. package/managers/context.d.ts +8 -0
  182. package/managers/index.d.ts +1 -0
  183. package/managers.cjs +2 -0
  184. package/managers.mjs +1 -0
  185. package/mocks.cjs +301 -0
  186. package/mocks.d.ts +125 -0
  187. package/mocks.mjs +293 -0
  188. package/networking/EventSourceService.cjs +104 -0
  189. package/networking/EventSourceService.d.ts +11 -0
  190. package/networking/EventSourceService.mjs +83 -0
  191. package/networking/NetworkService.cjs +394 -0
  192. package/networking/NetworkService.d.ts +97 -0
  193. package/networking/NetworkService.mjs +373 -0
  194. package/networking/SocketService.cjs +218 -0
  195. package/networking/SocketService.d.ts +23 -0
  196. package/networking/SocketService.mjs +197 -0
  197. package/networking/decorators/index.d.ts +4 -0
  198. package/networking/decorators/options.cjs +289 -0
  199. package/networking/decorators/options.d.ts +148 -0
  200. package/networking/decorators/options.mjs +242 -0
  201. package/networking/decorators/requests.cjs +80 -0
  202. package/networking/decorators/requests.d.ts +34 -0
  203. package/networking/decorators/requests.mjs +52 -0
  204. package/networking/decorators/socket.cjs +81 -0
  205. package/networking/decorators/socket.d.ts +13 -0
  206. package/networking/decorators/socket.mjs +51 -0
  207. package/networking/http.cjs +69 -0
  208. package/networking/http.d.ts +66 -0
  209. package/networking/http.mjs +69 -0
  210. package/networking/index.d.ts +39 -0
  211. package/networking/request.cjs +18 -0
  212. package/networking/request.d.ts +9 -0
  213. package/networking/request.mjs +16 -0
  214. package/networking/types.cjs +37 -0
  215. package/networking/types.d.ts +132 -0
  216. package/networking/types.mjs +35 -0
  217. package/networking.cjs +91 -0
  218. package/networking.mjs +33 -0
  219. package/package.json +1 -0
  220. package/reflection/injector.cjs +43 -0
  221. package/reflection/injector.d.ts +3 -0
  222. package/reflection/injector.mjs +40 -0
  223. package/socket.cjs +239 -0
  224. package/socket.d.ts +65 -0
  225. package/socket.mjs +237 -0
  226. package/store.cjs +12 -0
  227. package/store.mjs +3 -0
  228. package/stream/call.cjs +157 -0
  229. package/stream/call.d.ts +66 -0
  230. package/stream/call.mjs +154 -0
  231. package/stream/collector.cjs +217 -0
  232. package/stream/collector.d.ts +14 -0
  233. package/stream/collector.mjs +215 -0
  234. package/stream/index.d.ts +257 -0
  235. package/stream/observable.cjs +110 -0
  236. package/stream/observable.d.ts +65 -0
  237. package/stream/observable.mjs +106 -0
  238. package/stream/state.cjs +10 -0
  239. package/stream/state.d.ts +5 -0
  240. package/stream/state.mjs +8 -0
  241. package/stream.cjs +1153 -0
  242. package/stream.mjs +1132 -0
  243. package/synchronize.cjs +106 -0
  244. package/synchronize.d.ts +54 -0
  245. package/synchronize.mjs +103 -0
  246. package/typing/types.d.ts +24 -0
  247. package/typing.cjs +2 -0
  248. package/typing.mjs +1 -0
  249. package/utils/parser.cjs +37 -0
  250. package/utils/parser.d.ts +7 -0
  251. package/utils/parser.mjs +16 -0
@@ -0,0 +1,206 @@
1
+ 'use strict';
2
+
3
+ var _tslib = require('../../_virtual/_tslib.cjs');
4
+ var WatchableObject = require('../../data-structures/object/WatchableObject.cjs');
5
+ require('object-hash');
6
+ require('ramda');
7
+ var stream = require('../../stream.cjs');
8
+ var future = require('../../future/future.cjs');
9
+ require('uuid');
10
+ var Pair = require('../../data-structures/array/Pair.cjs');
11
+ var model = require('../model/model.cjs');
12
+ var property = require('../model/property.cjs');
13
+ var types = require('../model/types.cjs');
14
+ require('../model/credentials.cjs');
15
+
16
+ class PersistedCachedRecord extends model.Model {
17
+ setValue(v) {
18
+ this.value = v;
19
+ }
20
+ }
21
+ _tslib.__decorate([
22
+ property.Field("lastModifiedOn"),
23
+ _tslib.__metadata("design:type", Number)
24
+ ], PersistedCachedRecord.prototype, "lastModifiedOn", void 0);
25
+ _tslib.__decorate([
26
+ property.Field("value"),
27
+ _tslib.__metadata("design:type", Function),
28
+ _tslib.__metadata("design:paramtypes", [Object]),
29
+ _tslib.__metadata("design:returntype", void 0)
30
+ ], PersistedCachedRecord.prototype, "setValue", null);
31
+ class Cache {
32
+ constructor(decode, encode, persistStore, maxSize, expiryPeriod) {
33
+ this.decode = decode;
34
+ this.encode = encode;
35
+ this.persistStore = persistStore;
36
+ this.expiryPeriod = expiryPeriod;
37
+ this.memStore = new WatchableObject.WatchableObject(maxSize, expiryPeriod);
38
+ if (persistStore) {
39
+ this.memStore.onDelete((k) => {
40
+ persistStore.remove(this.getTransformedKey(k)).schedule();
41
+ });
42
+ this.memStore.onSet((k, v) => _tslib.__awaiter(this, void 0, void 0, function* () {
43
+ const record = new PersistedCachedRecord();
44
+ const result = this.encode(v);
45
+ if (result instanceof Promise || result instanceof future.Future) {
46
+ record.value = yield result;
47
+ }
48
+ else {
49
+ record.value = result;
50
+ }
51
+ record.lastModifiedOn = Date.now();
52
+ persistStore.set(this.getTransformedKey(k), record.json()).schedule();
53
+ }));
54
+ this.memStore.onClear(() => {
55
+ persistStore.clear().schedule();
56
+ });
57
+ }
58
+ }
59
+ get(key) {
60
+ return future.Future.of((resolve, reject, signal) => {
61
+ const value = this.memStore.get(key);
62
+ if (value === undefined && this.persistStore) {
63
+ const transformedKey = this.getTransformedKey(key);
64
+ resolve(this.persistStore
65
+ .get(transformedKey)
66
+ .thenApply(({ value }) => {
67
+ if (value) {
68
+ const val = PersistedCachedRecord.fromString(value, types.DataFormat.JSON);
69
+ val.value = this.decode(val.value);
70
+ return val;
71
+ }
72
+ })
73
+ .thenApply(({ value, signal }) => _tslib.__awaiter(this, void 0, void 0, function* () {
74
+ if (value) {
75
+ if (this.expiryPeriod) {
76
+ const remainingTime = Date.now() - value.lastModifiedOn - future.calculatePeriodValue(this.expiryPeriod);
77
+ if (remainingTime <= 0) {
78
+ yield this.persistStore.remove(transformedKey).registerSignal(signal);
79
+ return;
80
+ }
81
+ this.memStore.set(key, value.value, { milliseconds: remainingTime });
82
+ }
83
+ else {
84
+ this.memStore.set(key, value.value);
85
+ }
86
+ }
87
+ return value === null || value === void 0 ? void 0 : value.value;
88
+ }))
89
+ .registerSignal(signal));
90
+ }
91
+ resolve(value);
92
+ });
93
+ }
94
+ getSync(key) {
95
+ return this.memStore.get(key);
96
+ }
97
+ set(key, value) {
98
+ this.memStore.set(key, value);
99
+ }
100
+ remove(key) {
101
+ this.memStore.delete(key);
102
+ }
103
+ keys() {
104
+ var _a, _b;
105
+ return ((_b = (_a = this.persistStore) === null || _a === void 0 ? void 0 : _a.keys().map(this.getInversedKey)) !== null && _b !== void 0 ? _b : stream.Stream.of(this.memStore.keys()));
106
+ }
107
+ keysSync() {
108
+ return this.memStore.keys();
109
+ }
110
+ values() {
111
+ var _a, _b;
112
+ return ((_b = (_a = this.persistStore) === null || _a === void 0 ? void 0 : _a.values().map((v) => PersistedCachedRecord.fromString(v, types.DataFormat.JSON)).map((v) => v.value)) !== null && _b !== void 0 ? _b : stream.Stream.of(this.valuesSync()));
113
+ }
114
+ valuesSync() {
115
+ return this.memStore.values(true);
116
+ }
117
+ clear() {
118
+ this.memStore.clear();
119
+ }
120
+ [Symbol.asyncIterator]() {
121
+ const stream = this.keys()
122
+ .map((k) => this.get(k).thenApply(({ value }) => Pair.pair(k, value)))
123
+ .filter((v) => v.second !== undefined);
124
+ return stream[Symbol.asyncIterator]();
125
+ }
126
+ getTransformedKey(key) {
127
+ let transformedKey;
128
+ switch (typeof key) {
129
+ case "string":
130
+ transformedKey = "<string>" + key;
131
+ break;
132
+ case "number":
133
+ transformedKey = `<number>${key}`;
134
+ break;
135
+ case "boolean":
136
+ transformedKey = `<boolean>${key}`;
137
+ break;
138
+ case "object":
139
+ if (key instanceof Date) {
140
+ transformedKey = "<iso>" + key.toISOString();
141
+ break;
142
+ }
143
+ else if (Array.isArray(key)) {
144
+ transformedKey = "<json>" + JSON.stringify(key);
145
+ break;
146
+ }
147
+ default:
148
+ throw new Error("invalid key");
149
+ }
150
+ return transformedKey;
151
+ }
152
+ getInversedKey(transformedKey) {
153
+ let key;
154
+ const rawKey = transformedKey.substring(8);
155
+ switch (transformedKey.substring(0, 8)) {
156
+ case "<string>":
157
+ key = rawKey;
158
+ break;
159
+ case "<number>":
160
+ key = Number(rawKey);
161
+ break;
162
+ case "<bool >":
163
+ key = (rawKey === "true");
164
+ break;
165
+ case "<date >":
166
+ key = new Date(rawKey);
167
+ break;
168
+ case "<json >":
169
+ key = JSON.parse(rawKey);
170
+ break;
171
+ default:
172
+ throw new Error("invalid key");
173
+ }
174
+ return key;
175
+ }
176
+ }
177
+ class CacheManager {
178
+ constructor(persistStore, maxSize, expiryPeriod) {
179
+ this.persistStore = persistStore;
180
+ this.maxSize = maxSize;
181
+ this.expiryPeriod = expiryPeriod;
182
+ this.memStores = new Map();
183
+ }
184
+ createCache(name, persist, encoder, decoder, maxSize, expiryPeriod) {
185
+ if (persist && (!encoder || !decoder)) {
186
+ throw new Error("encoder and decoder required for persisting data");
187
+ }
188
+ if (persist && !this.persistStore) {
189
+ throw new Error("store required for persisting data");
190
+ }
191
+ const cache = new Cache(decoder, encoder, persist ? this.persistStore : undefined, maxSize !== null && maxSize !== void 0 ? maxSize : this.maxSize, expiryPeriod !== null && expiryPeriod !== void 0 ? expiryPeriod : this.expiryPeriod);
192
+ this.memStores.set(name, cache);
193
+ return cache;
194
+ }
195
+ getCache(name) {
196
+ return this.memStores.get(name);
197
+ }
198
+ removeCache(name) {
199
+ const cache = this.memStores.get(name);
200
+ this.memStores.delete(name);
201
+ cache === null || cache === void 0 ? void 0 : cache.clear();
202
+ }
203
+ }
204
+
205
+ exports.Cache = Cache;
206
+ exports.CacheManager = CacheManager;
@@ -0,0 +1,44 @@
1
+ import { Future, WaitPeriod } from "../../future";
2
+ import { Pair } from "../../data-structures/array";
3
+ import { Stream } from "../../stream";
4
+ export interface PersistenceStore {
5
+ open: () => Future<void>;
6
+ close: () => Future<void>;
7
+ get: (key: string) => Future<string | undefined>;
8
+ set: (key: string, value: string) => Future<void>;
9
+ remove: (key: string) => Future<void>;
10
+ keys: () => Stream<string>;
11
+ values: () => Stream<string>;
12
+ length: () => Future<number>;
13
+ clear: () => Future<void>;
14
+ }
15
+ export declare class Cache<T, K> implements AsyncIterable<Pair<T, K>> {
16
+ private readonly decode;
17
+ private readonly encode;
18
+ private readonly persistStore?;
19
+ private readonly expiryPeriod?;
20
+ constructor(decode: (v: string) => K, encode: (v: K) => string | Future<string> | Promise<string>, persistStore?: PersistenceStore, maxSize?: number, expiryPeriod?: WaitPeriod);
21
+ private readonly memStore;
22
+ get(key: T): Future<K | undefined>;
23
+ getSync(key: T): K;
24
+ set(key: T, value: K): void;
25
+ remove(key: T): void;
26
+ keys(): Stream<T>;
27
+ keysSync(): IterableIterator<T>;
28
+ values(): Stream<K>;
29
+ valuesSync(): K[];
30
+ clear(): void;
31
+ [Symbol.asyncIterator](): AsyncGenerator<Pair<T, K>, any, unknown>;
32
+ private getTransformedKey;
33
+ private getInversedKey;
34
+ }
35
+ export declare class CacheManager {
36
+ private readonly persistStore?;
37
+ private readonly maxSize?;
38
+ private readonly expiryPeriod?;
39
+ private readonly memStores;
40
+ constructor(persistStore?: PersistenceStore, maxSize?: number, expiryPeriod?: WaitPeriod);
41
+ createCache<T, K>(name: string, persist: boolean, encoder?: (v: K) => string | Future<string> | Promise<string>, decoder?: (v: string) => K, maxSize?: number, expiryPeriod?: WaitPeriod): Cache<T, K>;
42
+ getCache<T, K>(name: string): Cache<T, K>;
43
+ removeCache(name: string): void;
44
+ }
@@ -0,0 +1,203 @@
1
+ import { __decorate, __metadata, __awaiter } from '../../_virtual/_tslib.mjs';
2
+ import { WatchableObject } from '../../data-structures/object/WatchableObject.mjs';
3
+ import 'object-hash';
4
+ import 'ramda';
5
+ import { Stream } from '../../stream.mjs';
6
+ import { Future, calculatePeriodValue } from '../../future/future.mjs';
7
+ import 'uuid';
8
+ import { pair } from '../../data-structures/array/Pair.mjs';
9
+ import { Model } from '../model/model.mjs';
10
+ import { Field } from '../model/property.mjs';
11
+ import { DataFormat } from '../model/types.mjs';
12
+ import '../model/credentials.mjs';
13
+
14
+ class PersistedCachedRecord extends Model {
15
+ setValue(v) {
16
+ this.value = v;
17
+ }
18
+ }
19
+ __decorate([
20
+ Field("lastModifiedOn"),
21
+ __metadata("design:type", Number)
22
+ ], PersistedCachedRecord.prototype, "lastModifiedOn", void 0);
23
+ __decorate([
24
+ Field("value"),
25
+ __metadata("design:type", Function),
26
+ __metadata("design:paramtypes", [Object]),
27
+ __metadata("design:returntype", void 0)
28
+ ], PersistedCachedRecord.prototype, "setValue", null);
29
+ class Cache {
30
+ constructor(decode, encode, persistStore, maxSize, expiryPeriod) {
31
+ this.decode = decode;
32
+ this.encode = encode;
33
+ this.persistStore = persistStore;
34
+ this.expiryPeriod = expiryPeriod;
35
+ this.memStore = new WatchableObject(maxSize, expiryPeriod);
36
+ if (persistStore) {
37
+ this.memStore.onDelete((k) => {
38
+ persistStore.remove(this.getTransformedKey(k)).schedule();
39
+ });
40
+ this.memStore.onSet((k, v) => __awaiter(this, void 0, void 0, function* () {
41
+ const record = new PersistedCachedRecord();
42
+ const result = this.encode(v);
43
+ if (result instanceof Promise || result instanceof Future) {
44
+ record.value = yield result;
45
+ }
46
+ else {
47
+ record.value = result;
48
+ }
49
+ record.lastModifiedOn = Date.now();
50
+ persistStore.set(this.getTransformedKey(k), record.json()).schedule();
51
+ }));
52
+ this.memStore.onClear(() => {
53
+ persistStore.clear().schedule();
54
+ });
55
+ }
56
+ }
57
+ get(key) {
58
+ return Future.of((resolve, reject, signal) => {
59
+ const value = this.memStore.get(key);
60
+ if (value === undefined && this.persistStore) {
61
+ const transformedKey = this.getTransformedKey(key);
62
+ resolve(this.persistStore
63
+ .get(transformedKey)
64
+ .thenApply(({ value }) => {
65
+ if (value) {
66
+ const val = PersistedCachedRecord.fromString(value, DataFormat.JSON);
67
+ val.value = this.decode(val.value);
68
+ return val;
69
+ }
70
+ })
71
+ .thenApply(({ value, signal }) => __awaiter(this, void 0, void 0, function* () {
72
+ if (value) {
73
+ if (this.expiryPeriod) {
74
+ const remainingTime = Date.now() - value.lastModifiedOn - calculatePeriodValue(this.expiryPeriod);
75
+ if (remainingTime <= 0) {
76
+ yield this.persistStore.remove(transformedKey).registerSignal(signal);
77
+ return;
78
+ }
79
+ this.memStore.set(key, value.value, { milliseconds: remainingTime });
80
+ }
81
+ else {
82
+ this.memStore.set(key, value.value);
83
+ }
84
+ }
85
+ return value === null || value === void 0 ? void 0 : value.value;
86
+ }))
87
+ .registerSignal(signal));
88
+ }
89
+ resolve(value);
90
+ });
91
+ }
92
+ getSync(key) {
93
+ return this.memStore.get(key);
94
+ }
95
+ set(key, value) {
96
+ this.memStore.set(key, value);
97
+ }
98
+ remove(key) {
99
+ this.memStore.delete(key);
100
+ }
101
+ keys() {
102
+ var _a, _b;
103
+ return ((_b = (_a = this.persistStore) === null || _a === void 0 ? void 0 : _a.keys().map(this.getInversedKey)) !== null && _b !== void 0 ? _b : Stream.of(this.memStore.keys()));
104
+ }
105
+ keysSync() {
106
+ return this.memStore.keys();
107
+ }
108
+ values() {
109
+ var _a, _b;
110
+ return ((_b = (_a = this.persistStore) === null || _a === void 0 ? void 0 : _a.values().map((v) => PersistedCachedRecord.fromString(v, DataFormat.JSON)).map((v) => v.value)) !== null && _b !== void 0 ? _b : Stream.of(this.valuesSync()));
111
+ }
112
+ valuesSync() {
113
+ return this.memStore.values(true);
114
+ }
115
+ clear() {
116
+ this.memStore.clear();
117
+ }
118
+ [Symbol.asyncIterator]() {
119
+ const stream = this.keys()
120
+ .map((k) => this.get(k).thenApply(({ value }) => pair(k, value)))
121
+ .filter((v) => v.second !== undefined);
122
+ return stream[Symbol.asyncIterator]();
123
+ }
124
+ getTransformedKey(key) {
125
+ let transformedKey;
126
+ switch (typeof key) {
127
+ case "string":
128
+ transformedKey = "<string>" + key;
129
+ break;
130
+ case "number":
131
+ transformedKey = `<number>${key}`;
132
+ break;
133
+ case "boolean":
134
+ transformedKey = `<boolean>${key}`;
135
+ break;
136
+ case "object":
137
+ if (key instanceof Date) {
138
+ transformedKey = "<iso>" + key.toISOString();
139
+ break;
140
+ }
141
+ else if (Array.isArray(key)) {
142
+ transformedKey = "<json>" + JSON.stringify(key);
143
+ break;
144
+ }
145
+ default:
146
+ throw new Error("invalid key");
147
+ }
148
+ return transformedKey;
149
+ }
150
+ getInversedKey(transformedKey) {
151
+ let key;
152
+ const rawKey = transformedKey.substring(8);
153
+ switch (transformedKey.substring(0, 8)) {
154
+ case "<string>":
155
+ key = rawKey;
156
+ break;
157
+ case "<number>":
158
+ key = Number(rawKey);
159
+ break;
160
+ case "<bool >":
161
+ key = (rawKey === "true");
162
+ break;
163
+ case "<date >":
164
+ key = new Date(rawKey);
165
+ break;
166
+ case "<json >":
167
+ key = JSON.parse(rawKey);
168
+ break;
169
+ default:
170
+ throw new Error("invalid key");
171
+ }
172
+ return key;
173
+ }
174
+ }
175
+ class CacheManager {
176
+ constructor(persistStore, maxSize, expiryPeriod) {
177
+ this.persistStore = persistStore;
178
+ this.maxSize = maxSize;
179
+ this.expiryPeriod = expiryPeriod;
180
+ this.memStores = new Map();
181
+ }
182
+ createCache(name, persist, encoder, decoder, maxSize, expiryPeriod) {
183
+ if (persist && (!encoder || !decoder)) {
184
+ throw new Error("encoder and decoder required for persisting data");
185
+ }
186
+ if (persist && !this.persistStore) {
187
+ throw new Error("store required for persisting data");
188
+ }
189
+ const cache = new Cache(decoder, encoder, persist ? this.persistStore : undefined, maxSize !== null && maxSize !== void 0 ? maxSize : this.maxSize, expiryPeriod !== null && expiryPeriod !== void 0 ? expiryPeriod : this.expiryPeriod);
190
+ this.memStores.set(name, cache);
191
+ return cache;
192
+ }
193
+ getCache(name) {
194
+ return this.memStores.get(name);
195
+ }
196
+ removeCache(name) {
197
+ const cache = this.memStores.get(name);
198
+ this.memStores.delete(name);
199
+ cache === null || cache === void 0 ? void 0 : cache.clear();
200
+ }
201
+ }
202
+
203
+ export { Cache, CacheManager };
@@ -0,0 +1,92 @@
1
+ 'use strict';
2
+
3
+ var future = require('../../future/future.cjs');
4
+ require('ramda');
5
+ var stream = require('../../stream.cjs');
6
+ var stream_state = require('../../stream/state.cjs');
7
+
8
+ class LocalWebStore {
9
+ close() {
10
+ return future.Future.completed(null);
11
+ }
12
+ open() {
13
+ return future.Future.completed(null);
14
+ }
15
+ clear() {
16
+ return undefined;
17
+ }
18
+ get(key) {
19
+ return future.Future.of((resolve) => resolve(localStorage.getItem(key)));
20
+ }
21
+ keys() {
22
+ let index = -1;
23
+ return new stream.Stream(() => {
24
+ if (++index >= localStorage.length) {
25
+ return new stream_state.ExecutorState(true);
26
+ }
27
+ return localStorage.key(index);
28
+ });
29
+ }
30
+ length() {
31
+ return future.Future.of((resolve) => resolve(localStorage.length));
32
+ }
33
+ remove(key) {
34
+ return future.Future.of((resolve) => {
35
+ localStorage.removeItem(key);
36
+ resolve();
37
+ });
38
+ }
39
+ set(key, value) {
40
+ return future.Future.of((resolve) => {
41
+ localStorage.setItem(key, value);
42
+ resolve();
43
+ });
44
+ }
45
+ values() {
46
+ return this.keys().map((key) => localStorage.getItem(key));
47
+ }
48
+ }
49
+ class SessionWebStore {
50
+ close() {
51
+ return future.Future.completed(null);
52
+ }
53
+ open() {
54
+ return future.Future.completed(null);
55
+ }
56
+ clear() {
57
+ return undefined;
58
+ }
59
+ get(key) {
60
+ return future.Future.of((resolve) => resolve(sessionStorage.getItem(key)));
61
+ }
62
+ keys() {
63
+ let index = -1;
64
+ return new stream.Stream(() => {
65
+ if (++index >= sessionStorage.length) {
66
+ return new stream_state.ExecutorState(true);
67
+ }
68
+ return sessionStorage.key(index);
69
+ });
70
+ }
71
+ length() {
72
+ return future.Future.of((resolve) => resolve(sessionStorage.length));
73
+ }
74
+ remove(key) {
75
+ return future.Future.of((resolve) => {
76
+ sessionStorage.removeItem(key);
77
+ resolve();
78
+ });
79
+ }
80
+ set(key, value) {
81
+ return future.Future.of((resolve) => {
82
+ sessionStorage.setItem(key, value);
83
+ resolve();
84
+ });
85
+ }
86
+ values() {
87
+ return this.keys().map((key) => sessionStorage.getItem(key));
88
+ }
89
+ }
90
+
91
+ exports.LocalWebStore = LocalWebStore;
92
+ exports.SessionWebStore = SessionWebStore;
@@ -0,0 +1,25 @@
1
+ import { PersistenceStore } from "./manager";
2
+ import { Future } from "../../future";
3
+ import { Stream } from "../../stream";
4
+ export declare class LocalWebStore implements PersistenceStore {
5
+ close(): Future<void>;
6
+ open(): Future<void>;
7
+ clear(): Future<void>;
8
+ get(key: string): Future<string | undefined>;
9
+ keys(): Stream<string>;
10
+ length(): Future<number>;
11
+ remove(key: string): Future<void>;
12
+ set(key: string, value: string): Future<void>;
13
+ values(): Stream<string>;
14
+ }
15
+ export declare class SessionWebStore implements PersistenceStore {
16
+ close(): Future<void>;
17
+ open(): Future<void>;
18
+ clear(): Future<void>;
19
+ get(key: string): Future<string | undefined>;
20
+ keys(): Stream<string>;
21
+ length(): Future<number>;
22
+ remove(key: string): Future<void>;
23
+ set(key: string, value: string): Future<void>;
24
+ values(): Stream<string>;
25
+ }
@@ -0,0 +1,89 @@
1
+ import { Future } from '../../future/future.mjs';
2
+ import 'ramda';
3
+ import { Stream } from '../../stream.mjs';
4
+ import { ExecutorState } from '../../stream/state.mjs';
5
+
6
+ class LocalWebStore {
7
+ close() {
8
+ return Future.completed(null);
9
+ }
10
+ open() {
11
+ return Future.completed(null);
12
+ }
13
+ clear() {
14
+ return undefined;
15
+ }
16
+ get(key) {
17
+ return Future.of((resolve) => resolve(localStorage.getItem(key)));
18
+ }
19
+ keys() {
20
+ let index = -1;
21
+ return new Stream(() => {
22
+ if (++index >= localStorage.length) {
23
+ return new ExecutorState(true);
24
+ }
25
+ return localStorage.key(index);
26
+ });
27
+ }
28
+ length() {
29
+ return Future.of((resolve) => resolve(localStorage.length));
30
+ }
31
+ remove(key) {
32
+ return Future.of((resolve) => {
33
+ localStorage.removeItem(key);
34
+ resolve();
35
+ });
36
+ }
37
+ set(key, value) {
38
+ return Future.of((resolve) => {
39
+ localStorage.setItem(key, value);
40
+ resolve();
41
+ });
42
+ }
43
+ values() {
44
+ return this.keys().map((key) => localStorage.getItem(key));
45
+ }
46
+ }
47
+ class SessionWebStore {
48
+ close() {
49
+ return Future.completed(null);
50
+ }
51
+ open() {
52
+ return Future.completed(null);
53
+ }
54
+ clear() {
55
+ return undefined;
56
+ }
57
+ get(key) {
58
+ return Future.of((resolve) => resolve(sessionStorage.getItem(key)));
59
+ }
60
+ keys() {
61
+ let index = -1;
62
+ return new Stream(() => {
63
+ if (++index >= sessionStorage.length) {
64
+ return new ExecutorState(true);
65
+ }
66
+ return sessionStorage.key(index);
67
+ });
68
+ }
69
+ length() {
70
+ return Future.of((resolve) => resolve(sessionStorage.length));
71
+ }
72
+ remove(key) {
73
+ return Future.of((resolve) => {
74
+ sessionStorage.removeItem(key);
75
+ resolve();
76
+ });
77
+ }
78
+ set(key, value) {
79
+ return Future.of((resolve) => {
80
+ sessionStorage.setItem(key, value);
81
+ resolve();
82
+ });
83
+ }
84
+ values() {
85
+ return this.keys().map((key) => sessionStorage.getItem(key));
86
+ }
87
+ }
88
+
89
+ export { LocalWebStore, SessionWebStore };