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,570 @@
1
+ 'use strict';
2
+
3
+ var _tslib = require('../_virtual/_tslib.cjs');
4
+ var FutureCancelled = require('../errors/FutureCancelled.cjs');
5
+ var FutureError = require('../errors/FutureError.cjs');
6
+ var InvalidValue = require('../errors/InvalidValue.cjs');
7
+ var TimeoutError = require('../errors/TimeoutError.cjs');
8
+ var R = require('ramda');
9
+ var SimpleQueue = require('../data-structures/object/SimpleQueue.cjs');
10
+ var uuid = require('uuid');
11
+ var result = require('./result.cjs');
12
+ var stream = require('../stream.cjs');
13
+
14
+ function _interopNamespaceDefault(e) {
15
+ var n = Object.create(null);
16
+ if (e) {
17
+ Object.keys(e).forEach(function (k) {
18
+ if (k !== 'default') {
19
+ var d = Object.getOwnPropertyDescriptor(e, k);
20
+ Object.defineProperty(n, k, d.get ? d : {
21
+ enumerable: true,
22
+ get: function () { return e[k]; }
23
+ });
24
+ }
25
+ });
26
+ }
27
+ n.default = e;
28
+ return Object.freeze(n);
29
+ }
30
+
31
+ var R__namespace = /*#__PURE__*/_interopNamespaceDefault(R);
32
+
33
+ const calculatePeriodValue = (period) => {
34
+ var _a, _b, _c, _d;
35
+ return ((_a = period.hours) !== null && _a !== void 0 ? _a : 0) * 60 * 60 * 1000 +
36
+ ((_b = period.minutes) !== null && _b !== void 0 ? _b : 0) * 60 * 1000 +
37
+ ((_c = period.seconds) !== null && _c !== void 0 ? _c : 0) * 1000 +
38
+ ((_d = period.milliseconds) !== null && _d !== void 0 ? _d : 0);
39
+ };
40
+ /**
41
+ * Represents an eventual result of some asynchronous operation. Futures are quite similar to Promise, and can be
42
+ * awaited as they are **Thenable** objects
43
+ */
44
+ class Future {
45
+ constructor(executor, signal) {
46
+ this.guid = uuid.v4();
47
+ this.executor = executor;
48
+ this.defaultSignal = signal !== null && signal !== void 0 ? signal : new AbortController().signal;
49
+ this.signals = new Set([this.defaultSignal]);
50
+ this.fulfilled = false;
51
+ this.isRunning = false;
52
+ this.processors = new SimpleQueue.SimpleQueue();
53
+ this.finallyHandlers = new SimpleQueue.SimpleQueue();
54
+ }
55
+ /**
56
+ * Creates a future that wraps the given future, and prevents it from being cancellable on the call to "cancel" method
57
+ * @param future
58
+ */
59
+ static shield(future) {
60
+ return Future.of((resolve, reject) => {
61
+ future
62
+ .thenApply(({ value }) => resolve(value))
63
+ .catch(reject)
64
+ .schedule();
65
+ });
66
+ }
67
+ /**
68
+ * Creates a future from the value provided
69
+ * @param value an Executor function (callback for the new Future constructor)
70
+ * @param signal Abort Signal to establish the future with. If not provided, the future will only have the default
71
+ * signals
72
+ */
73
+ static of(value, signal) {
74
+ return new Future(value, signal);
75
+ }
76
+ /**
77
+ * Wraps a promise as a future
78
+ * @param value
79
+ */
80
+ static wrap(value) {
81
+ return new Future((resolve, reject) => {
82
+ value.then(resolve).catch(reject);
83
+ });
84
+ }
85
+ /**
86
+ * Waits for the given future to complete. If not completed within the given timeframe, the given future is cancelled
87
+ * @param value
88
+ * @param timeout wait period object or the number of milliseconds to wait
89
+ */
90
+ static waitFor(value, timeout) {
91
+ const timeableFuture = Future.sleep(timeout)
92
+ .registerSignal(value.defaultSignal)
93
+ .catch(() => { });
94
+ return Future.of((resolve, reject) => Promise.race([timeableFuture, value.run()])
95
+ .then((v) => {
96
+ if (value.done) {
97
+ timeableFuture.cancel();
98
+ resolve(v);
99
+ return;
100
+ }
101
+ value.cancel();
102
+ reject(new TimeoutError.TimeoutError());
103
+ })
104
+ .catch((error) => {
105
+ if (!timeableFuture.failed) {
106
+ timeableFuture.cancel();
107
+ }
108
+ reject(error);
109
+ }), value.defaultSignal);
110
+ }
111
+ /**
112
+ * Sleeps for the given period. If period is a number, then it's the sleep duration in seconds
113
+ * @param period
114
+ * @param signal
115
+ */
116
+ static sleep(period, signal) {
117
+ return new Future((resolve, reject, futureSignal) => {
118
+ const totalTime = typeof period === "number" ? period * 1000 : calculatePeriodValue(period);
119
+ const timer = setTimeout(resolve, totalTime);
120
+ futureSignal.addEventListener("abort", () => {
121
+ clearTimeout(timer);
122
+ reject(new FutureCancelled.FutureCancelled());
123
+ }, { once: true });
124
+ }, signal);
125
+ }
126
+ /**
127
+ * Returns a future that completes with the given value.
128
+ * Important Note:
129
+ * Passing a Future or Stream to this function will execute it once and yield the result.
130
+ * @param value
131
+ */
132
+ static completed(value) {
133
+ return new Future((resolve) => resolve(value));
134
+ }
135
+ /**
136
+ * Returns a future that fails with the given value
137
+ * @param value
138
+ */
139
+ static exceptionally(value) {
140
+ return new Future((_, reject) => reject(value));
141
+ }
142
+ /**
143
+ * Returns the first completed or failed. If this is cancelled, then all futures provided will also be cancelled
144
+ * @param futures list of futures
145
+ * @param signal optinal abort signal
146
+ */
147
+ static firstCompleted(futures, signal) {
148
+ return Future.of((resolve, reject, signal) => {
149
+ futures.forEach((future) => {
150
+ future.registerSignal(signal);
151
+ });
152
+ Promise.race(futures)
153
+ .then(resolve)
154
+ .catch(reject)
155
+ .finally(() => {
156
+ futures.forEach((future) => {
157
+ future.unregisterSignal(signal);
158
+ });
159
+ });
160
+ }, signal);
161
+ }
162
+ /**
163
+ * Awaits all futures completion, and collects the results in an array. If this future is cancelled, then all provided
164
+ * futures will also be cancelled.
165
+ * @param futures list of futures
166
+ * @param signal optional abort signal
167
+ */
168
+ static collect(futures, signal) {
169
+ return Future.of((resolve, reject, signal) => {
170
+ futures.forEach((future) => {
171
+ future.registerSignal(signal);
172
+ });
173
+ Promise.all(futures.map((f) => f.run()))
174
+ .then(resolve)
175
+ .catch(reject)
176
+ .finally(() => {
177
+ futures.forEach((future) => {
178
+ future.unregisterSignal(signal);
179
+ });
180
+ });
181
+ }, signal);
182
+ }
183
+ /**
184
+ * Awaits futures finishing all possible executions, and returns the same list of futures. Whether each future
185
+ * completed or failed, the future is returned in the array
186
+ * @param futures
187
+ */
188
+ static collectSettled(futures) {
189
+ return Future.of((resolve, reject, signal) => {
190
+ futures.forEach((future) => {
191
+ future.registerSignal(signal);
192
+ });
193
+ Promise.allSettled(futures.map((f) => f.run()))
194
+ .then(() => resolve(futures))
195
+ .catch(reject)
196
+ .finally(() => {
197
+ futures.forEach((future) => {
198
+ future.unregisterSignal(signal);
199
+ });
200
+ });
201
+ });
202
+ }
203
+ /**
204
+ * Creates a future that schedules the provided functor at the given frequency intervals
205
+ * @param functor function to be executed at specified intervals
206
+ * @param frequency interval used to schedule the functor's call
207
+ */
208
+ static periodic(functor, frequency) {
209
+ return Future.of((resolve, reject, signal) => {
210
+ // eslint-disable-next-line @typescript-eslint/no-misused-promises
211
+ const cancel = setInterval(() => _tslib.__awaiter(this, void 0, void 0, function* () {
212
+ const result = functor(signal);
213
+ if (result instanceof Promise || result instanceof Future) {
214
+ yield result;
215
+ }
216
+ }), calculatePeriodValue(frequency));
217
+ signal.addEventListener("abort", () => {
218
+ clearInterval(cancel);
219
+ resolve(undefined);
220
+ }, { once: true });
221
+ });
222
+ }
223
+ /**
224
+ * Unique ID of the future
225
+ */
226
+ get id() {
227
+ return this.guid;
228
+ }
229
+ /**
230
+ * Changes the future ID
231
+ * @param value
232
+ */
233
+ set id(value) {
234
+ this.guid = value;
235
+ }
236
+ /**
237
+ * Checks if the future completed. A completed future is one that ran successfully without termination.
238
+ */
239
+ get done() {
240
+ return this.fulfilled;
241
+ }
242
+ /**
243
+ * Checks if the future failed
244
+ */
245
+ get failed() {
246
+ return this.error instanceof Error;
247
+ }
248
+ /**
249
+ * Retrieves the result of the future, if there is any
250
+ */
251
+ get result() {
252
+ return this.completedResult;
253
+ }
254
+ /**
255
+ * Checks if the future is running
256
+ */
257
+ get running() {
258
+ return this.isRunning;
259
+ }
260
+ /**
261
+ * Retrieves the error if any occurred
262
+ */
263
+ get error() {
264
+ return this.failureResult;
265
+ }
266
+ /**
267
+ * Convert the future to a stream
268
+ */
269
+ get stream() {
270
+ return stream.Stream.of(this);
271
+ }
272
+ /**
273
+ * Registers additional signal to the current future
274
+ * @param signal
275
+ */
276
+ registerSignal(signal) {
277
+ if (!this.done && !this.failed) {
278
+ this.signals.add(signal);
279
+ if (this.signalRegisteredCallback) {
280
+ this.signalRegisteredCallback(signal);
281
+ }
282
+ }
283
+ return this;
284
+ }
285
+ /**
286
+ * Removes the given signal from the future
287
+ * @param signal
288
+ */
289
+ unregisterSignal(signal) {
290
+ if (signal === this.defaultSignal) {
291
+ throw new InvalidValue.InvalidValue("Cannot deregister default signal");
292
+ }
293
+ this.signals.delete(signal);
294
+ return this;
295
+ }
296
+ /**
297
+ * Runs the future in the background
298
+ */
299
+ schedule() {
300
+ this.run().catch(() => { });
301
+ return this;
302
+ }
303
+ /**
304
+ * Launches the future
305
+ */
306
+ run() {
307
+ return this.__run__();
308
+ }
309
+ /**
310
+ * Cancels the future if it is currently running
311
+ */
312
+ cancel() {
313
+ if (this.running) {
314
+ this.cancelWithSignal(this.defaultSignal);
315
+ }
316
+ }
317
+ /**
318
+ * Used to chain additional steps to be executed. This creates a new future
319
+ * @param callback
320
+ * @param clone whether the future should be cloned or not.Defaults to true
321
+ */
322
+ thenApply(callback, clone = false) {
323
+ if (this.running) {
324
+ const newFuture = Future.wrap(this.underLyingPromise)
325
+ .thenApply(callback)
326
+ .registerSignal(this.defaultSignal);
327
+ this.signals.forEach((signal) => newFuture.registerSignal(signal));
328
+ return newFuture;
329
+ }
330
+ else if (this.done) {
331
+ const newFuture = Future.wrap(this.underLyingPromise).thenApply(callback);
332
+ return newFuture;
333
+ }
334
+ const newFuture = (clone ? this.clone() : this);
335
+ newFuture.processors.enqueue({ success: callback });
336
+ return newFuture;
337
+ }
338
+ /**
339
+ * Used to run callback irrespective of the future completing or failing
340
+ * @param callback
341
+ * @param clone whether the future should be cloned or not.Defaults to true
342
+ */
343
+ finally(callback, clone = false) {
344
+ const newFuture = clone ? this.clone() : this;
345
+ newFuture.finallyHandlers.enqueue(callback);
346
+ return newFuture;
347
+ }
348
+ /**
349
+ * Handles any error that occurs in the previous steps inside the future
350
+ * @param callback
351
+ * @param clone whether the future should be cloned or not.Defaults to true
352
+ */
353
+ catch(callback, clone = false) {
354
+ const newFuture = (clone ? this.clone() : this);
355
+ newFuture.processors.enqueue({ failure: callback });
356
+ return newFuture;
357
+ }
358
+ /**
359
+ * Clones the current future
360
+ */
361
+ clone() {
362
+ const future = new Future(this.executor, this.defaultSignal);
363
+ future.signals = new Set(this.signals);
364
+ future.defaultSignal = this.defaultSignal;
365
+ future.processors = this.processors.clone();
366
+ future.fulfilled = this.fulfilled;
367
+ return future;
368
+ }
369
+ /**
370
+ * Internal. Only to be called by the internal JS runtime during await
371
+ * @private
372
+ */
373
+ then(onFulfilled, onRejected) {
374
+ return this.__run__(onFulfilled, onRejected);
375
+ }
376
+ /**
377
+ * Executes the future. Should only be triggered by the JS runtime via then(), or the user via run()
378
+ * @param onFulfilled
379
+ * @param onRejected
380
+ * @private
381
+ */
382
+ __run__(onFulfilled, onRejected) {
383
+ if (this.underLyingPromise) {
384
+ return this.underLyingPromise
385
+ .then((v) => {
386
+ onFulfilled === null || onFulfilled === void 0 ? void 0 : onFulfilled(v);
387
+ return v;
388
+ })
389
+ .catch((error) => {
390
+ if (!(error instanceof Error)) {
391
+ error = new FutureError.FutureError(error);
392
+ }
393
+ onRejected === null || onRejected === void 0 ? void 0 : onRejected(error);
394
+ throw error;
395
+ });
396
+ }
397
+ this.isRunning = true;
398
+ const resolvePromise = (promise) => {
399
+ return promise
400
+ .then((result$1) => _tslib.__awaiter(this, void 0, void 0, function* () {
401
+ while (!this.processors.empty) {
402
+ const { success } = this.processors.dequeue();
403
+ if (!success)
404
+ continue;
405
+ try {
406
+ result$1 = yield this.postResultProcessing(success(new result.FutureResult(result$1, this.defaultSignal)));
407
+ }
408
+ catch (error) {
409
+ const handler = this.findFailureHandler();
410
+ if (!handler) {
411
+ throw error;
412
+ }
413
+ result$1 = yield this.postResultProcessing(handler(error instanceof Error ? error : new Error(String(error))));
414
+ }
415
+ }
416
+ return result$1;
417
+ }))
418
+ .then((v) => {
419
+ onFulfilled === null || onFulfilled === void 0 ? void 0 : onFulfilled(v);
420
+ return v;
421
+ })
422
+ .catch((error) => _tslib.__awaiter(this, void 0, void 0, function* () {
423
+ const handler = this.findFailureHandler();
424
+ if (!handler) {
425
+ if (!(error instanceof Error)) {
426
+ error = new FutureError.FutureError(error);
427
+ }
428
+ onRejected === null || onRejected === void 0 ? void 0 : onRejected(error);
429
+ throw error;
430
+ }
431
+ try {
432
+ const result = yield this.postResultProcessing(handler(error));
433
+ return yield resolvePromise(Promise.resolve(result));
434
+ }
435
+ catch (e) {
436
+ onRejected === null || onRejected === void 0 ? void 0 : onRejected(e);
437
+ throw e;
438
+ }
439
+ }));
440
+ };
441
+ this.underLyingPromise = resolvePromise(new Promise((resolve, reject) => {
442
+ if (Array.from(this.signals).some((v) => v.aborted)) {
443
+ reject(new FutureCancelled.FutureCancelled());
444
+ return;
445
+ }
446
+ let rejected = false;
447
+ const timeouts = [];
448
+ const abort = R__namespace.once(() => this.cancelWithSignal(this.defaultSignal));
449
+ const resolver = (v) => {
450
+ if (v instanceof Promise) {
451
+ v.then((v) => {
452
+ this.fulfilled = true;
453
+ resolve(v);
454
+ }).catch(reject);
455
+ }
456
+ else if (v instanceof Future) {
457
+ v.run()
458
+ .then((v) => {
459
+ this.fulfilled = true;
460
+ resolve(v);
461
+ })
462
+ .catch(reject);
463
+ }
464
+ else if (v instanceof result.FutureResult) {
465
+ this.fulfilled = true;
466
+ resolve(v.value);
467
+ }
468
+ else {
469
+ this.fulfilled = true;
470
+ resolve(v);
471
+ }
472
+ };
473
+ const rejecter = (reason) => {
474
+ rejected = true;
475
+ timeouts.forEach((v) => clearTimeout(v));
476
+ reject(reason);
477
+ };
478
+ this.signalRegisteredCallback = (v) => v.addEventListener("abort", () => {
479
+ abort();
480
+ if (!rejected)
481
+ timeouts.push(setTimeout(() => reject(new FutureCancelled.FutureCancelled()), 1000));
482
+ }, { once: true });
483
+ this.signals.forEach((v) => {
484
+ v.addEventListener("abort", () => {
485
+ abort();
486
+ if (!rejected) {
487
+ timeouts.push(setTimeout(() => reject(new FutureCancelled.FutureCancelled()), 1000));
488
+ }
489
+ }, { once: true });
490
+ });
491
+ const result$1 = this.executor(resolver, rejecter, this.defaultSignal);
492
+ if (result$1 instanceof Promise) {
493
+ result$1
494
+ .then(() => {
495
+ if (!this.fulfilled) {
496
+ this.fulfilled = true;
497
+ resolve(null);
498
+ }
499
+ })
500
+ .catch(rejecter);
501
+ }
502
+ }))
503
+ .then((v) => {
504
+ this.completedResult = v;
505
+ return v;
506
+ })
507
+ .catch((e) => {
508
+ this.failureResult = e instanceof Error ? e : new Error(String(e));
509
+ throw this.failureResult;
510
+ })
511
+ .finally(() => {
512
+ this.finallyHandlers.forEach((handler) => handler(this));
513
+ })
514
+ .catch((e) => {
515
+ this.failureResult = e instanceof Error ? e : new Error(String(e));
516
+ throw this.failureResult;
517
+ })
518
+ .finally(() => {
519
+ this.signals = new Set();
520
+ this.isRunning = false;
521
+ });
522
+ return this.underLyingPromise;
523
+ }
524
+ /**
525
+ * Fires the abort signal
526
+ * @param signal
527
+ * @private
528
+ */
529
+ cancelWithSignal(signal) {
530
+ if (!signal.aborted) {
531
+ signal.dispatchEvent(new CustomEvent("abort"));
532
+ }
533
+ }
534
+ /**
535
+ * Checks if any handler exist to process errors
536
+ * @private
537
+ */
538
+ findFailureHandler() {
539
+ while (!this.processors.empty) {
540
+ const { failure } = this.processors.dequeue();
541
+ if (failure)
542
+ return failure;
543
+ }
544
+ }
545
+ /**
546
+ * Nested processing of a future result
547
+ * @param result
548
+ * @private
549
+ */
550
+ postResultProcessing(result$1) {
551
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
552
+ if (result$1 instanceof result.FutureResult) {
553
+ result$1 = result$1.value;
554
+ }
555
+ else if (result$1 instanceof Future) {
556
+ result$1 = yield result$1.registerSignal(this.defaultSignal).run();
557
+ }
558
+ else if (result$1 instanceof Promise) {
559
+ result$1 = yield result$1;
560
+ }
561
+ else if (result$1 instanceof stream.Stream) {
562
+ result$1 = yield result$1.execute();
563
+ }
564
+ return result$1;
565
+ });
566
+ }
567
+ }
568
+
569
+ exports.Future = Future;
570
+ exports.calculatePeriodValue = calculatePeriodValue;