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.
- package/_virtual/_tslib.cjs +102 -0
- package/_virtual/_tslib.mjs +93 -0
- package/data/AtomicValue.d.ts +15 -0
- package/data/bus.cjs +179 -0
- package/data/bus.d.ts +72 -0
- package/data/bus.mjs +177 -0
- package/data/decoders/index.d.ts +4 -0
- package/data/decoders/json.cjs +12 -0
- package/data/decoders/json.d.ts +7 -0
- package/data/decoders/json.mjs +10 -0
- package/data/decoders/msgpack.cjs +14 -0
- package/data/decoders/msgpack.d.ts +7 -0
- package/data/decoders/msgpack.mjs +12 -0
- package/data/decoders/protobuf.d.ts +13 -0
- package/data/decoders/string.cjs +12 -0
- package/data/decoders/string.d.ts +7 -0
- package/data/decoders/string.mjs +10 -0
- package/data/decoders/type.d.ts +7 -0
- package/data/decoders.cjs +11 -0
- package/data/decoders.mjs +3 -0
- package/data/model/credentials.cjs +114 -0
- package/data/model/credentials.d.ts +61 -0
- package/data/model/credentials.mjs +109 -0
- package/data/model/extra.cjs +88 -0
- package/data/model/extra.d.ts +5 -0
- package/data/model/extra.mjs +66 -0
- package/data/model/index.d.ts +6 -0
- package/data/model/model.cjs +541 -0
- package/data/model/model.d.ts +207 -0
- package/data/model/model.mjs +518 -0
- package/data/model/plugin.cjs +96 -0
- package/data/model/plugin.d.ts +38 -0
- package/data/model/plugin.mjs +72 -0
- package/data/model/property.cjs +310 -0
- package/data/model/property.d.ts +83 -0
- package/data/model/property.mjs +278 -0
- package/data/model/types.cjs +32 -0
- package/data/model/types.d.ts +28 -0
- package/data/model/types.mjs +32 -0
- package/data/model.cjs +43 -0
- package/data/model.mjs +6 -0
- package/data/signal.cjs +47 -0
- package/data/signal.d.ts +17 -0
- package/data/signal.mjs +45 -0
- package/data/store/index.d.ts +4 -0
- package/data/store/json.cjs +58 -0
- package/data/store/json.d.ts +18 -0
- package/data/store/json.mjs +56 -0
- package/data/store/manager.cjs +206 -0
- package/data/store/manager.d.ts +44 -0
- package/data/store/manager.mjs +203 -0
- package/data/store/web.cjs +92 -0
- package/data/store/web.d.ts +25 -0
- package/data/store/web.mjs +89 -0
- package/data/value.cjs +66 -0
- package/data/value.mjs +64 -0
- package/data-structures/array/CyclicalList.cjs +57 -0
- package/data-structures/array/CyclicalList.d.ts +12 -0
- package/data-structures/array/CyclicalList.mjs +55 -0
- package/data-structures/array/Pair.cjs +30 -0
- package/data-structures/array/Pair.d.ts +17 -0
- package/data-structures/array/Pair.mjs +27 -0
- package/data-structures/array/Stack.cjs +96 -0
- package/data-structures/array/Stack.d.ts +41 -0
- package/data-structures/array/Stack.mjs +94 -0
- package/data-structures/array/Triple.cjs +34 -0
- package/data-structures/array/Triple.d.ts +19 -0
- package/data-structures/array/Triple.mjs +31 -0
- package/data-structures/array/Tuple.d.ts +8 -0
- package/data-structures/array/index.d.ts +5 -0
- package/data-structures/array.cjs +15 -0
- package/data-structures/array.mjs +4 -0
- package/data-structures/object/BufferQueue.cjs +98 -0
- package/data-structures/object/BufferQueue.d.ts +21 -0
- package/data-structures/object/BufferQueue.mjs +77 -0
- package/data-structures/object/CyclicalObject.cjs +155 -0
- package/data-structures/object/CyclicalObject.d.ts +74 -0
- package/data-structures/object/CyclicalObject.mjs +134 -0
- package/data-structures/object/Queue.cjs +178 -0
- package/data-structures/object/Queue.d.ts +31 -0
- package/data-structures/object/Queue.mjs +176 -0
- package/data-structures/object/SimpleQueue.cjs +53 -0
- package/data-structures/object/SimpleQueue.d.ts +13 -0
- package/data-structures/object/SimpleQueue.mjs +51 -0
- package/data-structures/object/TimeableObject.cjs +73 -0
- package/data-structures/object/TimeableObject.d.ts +30 -0
- package/data-structures/object/TimeableObject.mjs +71 -0
- package/data-structures/object/WaitableObject.cjs +61 -0
- package/data-structures/object/WaitableObject.d.ts +29 -0
- package/data-structures/object/WaitableObject.mjs +59 -0
- package/data-structures/object/WatchableObject.cjs +251 -0
- package/data-structures/object/WatchableObject.d.ts +88 -0
- package/data-structures/object/WatchableObject.mjs +230 -0
- package/data-structures/object/index.d.ts +7 -0
- package/data-structures/object.cjs +23 -0
- package/data-structures/object.mjs +7 -0
- package/errors/AbortError.cjs +9 -0
- package/errors/AbortError.d.ts +5 -0
- package/errors/AbortError.mjs +7 -0
- package/errors/CallExecutionError.cjs +13 -0
- package/errors/CallExecutionError.d.ts +7 -0
- package/errors/CallExecutionError.mjs +11 -0
- package/errors/FutureCancelled.cjs +11 -0
- package/errors/FutureCancelled.d.ts +6 -0
- package/errors/FutureCancelled.mjs +9 -0
- package/errors/FutureError.cjs +11 -0
- package/errors/FutureError.d.ts +6 -0
- package/errors/FutureError.mjs +9 -0
- package/errors/IllegalArgumentsError.cjs +13 -0
- package/errors/IllegalArgumentsError.d.ts +7 -0
- package/errors/IllegalArgumentsError.mjs +11 -0
- package/errors/IllegalOperationError.cjs +6 -0
- package/errors/IllegalOperationError.d.ts +2 -0
- package/errors/IllegalOperationError.mjs +4 -0
- package/errors/InvalidType.cjs +6 -0
- package/errors/InvalidType.d.ts +2 -0
- package/errors/InvalidType.mjs +4 -0
- package/errors/InvalidValue.cjs +11 -0
- package/errors/InvalidValue.d.ts +4 -0
- package/errors/InvalidValue.mjs +9 -0
- package/errors/MissingArgumentsError.cjs +13 -0
- package/errors/MissingArgumentsError.d.ts +7 -0
- package/errors/MissingArgumentsError.mjs +11 -0
- package/errors/NetworkError.cjs +13 -0
- package/errors/NetworkError.d.ts +8 -0
- package/errors/NetworkError.mjs +11 -0
- package/errors/NoSuchElement.cjs +6 -0
- package/errors/NoSuchElement.d.ts +2 -0
- package/errors/NoSuchElement.mjs +4 -0
- package/errors/NotImplemented.cjs +9 -0
- package/errors/NotImplemented.d.ts +5 -0
- package/errors/NotImplemented.mjs +7 -0
- package/errors/ParsingError.cjs +10 -0
- package/errors/ParsingError.d.ts +5 -0
- package/errors/ParsingError.mjs +8 -0
- package/errors/QueueEmptyError.cjs +6 -0
- package/errors/QueueEmptyError.d.ts +2 -0
- package/errors/QueueEmptyError.mjs +4 -0
- package/errors/QueueFullError.cjs +6 -0
- package/errors/QueueFullError.d.ts +2 -0
- package/errors/QueueFullError.mjs +4 -0
- package/errors/StackEmptyError.cjs +6 -0
- package/errors/StackEmptyError.d.ts +2 -0
- package/errors/StackEmptyError.mjs +4 -0
- package/errors/StackFullError.cjs +6 -0
- package/errors/StackFullError.d.ts +2 -0
- package/errors/StackFullError.mjs +4 -0
- package/errors/StreamEnded.cjs +6 -0
- package/errors/StreamEnded.d.ts +2 -0
- package/errors/StreamEnded.mjs +4 -0
- package/errors/TimeoutError.cjs +11 -0
- package/errors/TimeoutError.d.ts +6 -0
- package/errors/TimeoutError.mjs +9 -0
- package/errors/ValueDestroyedError.cjs +6 -0
- package/errors/ValueDestroyedError.d.ts +2 -0
- package/errors/ValueDestroyedError.mjs +4 -0
- package/errors/VerificationError.cjs +9 -0
- package/errors/VerificationError.d.ts +5 -0
- package/errors/VerificationError.mjs +7 -0
- package/errors/index.d.ts +18 -0
- package/errors.cjs +41 -0
- package/errors.mjs +18 -0
- package/files.cjs +201 -0
- package/files.d.ts +31 -0
- package/files.mjs +180 -0
- package/functools/index.d.ts +31 -0
- package/functools.cjs +371 -0
- package/functools.mjs +334 -0
- package/future/future.cjs +570 -0
- package/future/future.d.ts +208 -0
- package/future/future.mjs +548 -0
- package/future/group.cjs +180 -0
- package/future/group.d.ts +82 -0
- package/future/group.mjs +159 -0
- package/future/index.d.ts +3 -0
- package/future/result.cjs +19 -0
- package/future/result.d.ts +18 -0
- package/future/result.mjs +17 -0
- package/future.cjs +11 -0
- package/future.mjs +3 -0
- package/managers/context.d.ts +8 -0
- package/managers/index.d.ts +1 -0
- package/managers.cjs +2 -0
- package/managers.mjs +1 -0
- package/mocks.cjs +301 -0
- package/mocks.d.ts +125 -0
- package/mocks.mjs +293 -0
- package/networking/EventSourceService.cjs +104 -0
- package/networking/EventSourceService.d.ts +11 -0
- package/networking/EventSourceService.mjs +83 -0
- package/networking/NetworkService.cjs +394 -0
- package/networking/NetworkService.d.ts +97 -0
- package/networking/NetworkService.mjs +373 -0
- package/networking/SocketService.cjs +218 -0
- package/networking/SocketService.d.ts +23 -0
- package/networking/SocketService.mjs +197 -0
- package/networking/decorators/index.d.ts +4 -0
- package/networking/decorators/options.cjs +289 -0
- package/networking/decorators/options.d.ts +148 -0
- package/networking/decorators/options.mjs +242 -0
- package/networking/decorators/requests.cjs +80 -0
- package/networking/decorators/requests.d.ts +34 -0
- package/networking/decorators/requests.mjs +52 -0
- package/networking/decorators/socket.cjs +81 -0
- package/networking/decorators/socket.d.ts +13 -0
- package/networking/decorators/socket.mjs +51 -0
- package/networking/http.cjs +69 -0
- package/networking/http.d.ts +66 -0
- package/networking/http.mjs +69 -0
- package/networking/index.d.ts +39 -0
- package/networking/request.cjs +18 -0
- package/networking/request.d.ts +9 -0
- package/networking/request.mjs +16 -0
- package/networking/types.cjs +37 -0
- package/networking/types.d.ts +132 -0
- package/networking/types.mjs +35 -0
- package/networking.cjs +91 -0
- package/networking.mjs +33 -0
- package/package.json +1 -0
- package/reflection/injector.cjs +43 -0
- package/reflection/injector.d.ts +3 -0
- package/reflection/injector.mjs +40 -0
- package/socket.cjs +239 -0
- package/socket.d.ts +65 -0
- package/socket.mjs +237 -0
- package/store.cjs +12 -0
- package/store.mjs +3 -0
- package/stream/call.cjs +157 -0
- package/stream/call.d.ts +66 -0
- package/stream/call.mjs +154 -0
- package/stream/collector.cjs +217 -0
- package/stream/collector.d.ts +14 -0
- package/stream/collector.mjs +215 -0
- package/stream/index.d.ts +257 -0
- package/stream/observable.cjs +110 -0
- package/stream/observable.d.ts +65 -0
- package/stream/observable.mjs +106 -0
- package/stream/state.cjs +10 -0
- package/stream/state.d.ts +5 -0
- package/stream/state.mjs +8 -0
- package/stream.cjs +1153 -0
- package/stream.mjs +1132 -0
- package/synchronize.cjs +106 -0
- package/synchronize.d.ts +54 -0
- package/synchronize.mjs +103 -0
- package/typing/types.d.ts +24 -0
- package/typing.cjs +2 -0
- package/typing.mjs +1 -0
- package/utils/parser.cjs +37 -0
- package/utils/parser.d.ts +7 -0
- package/utils/parser.mjs +16 -0
package/future/group.cjs
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var R = require('ramda');
|
|
4
|
+
var future = require('./future.cjs');
|
|
5
|
+
var synchronize = require('../synchronize.cjs');
|
|
6
|
+
var stream = require('../stream.cjs');
|
|
7
|
+
var stream_state = require('../stream/state.cjs');
|
|
8
|
+
|
|
9
|
+
function _interopNamespaceDefault(e) {
|
|
10
|
+
var n = Object.create(null);
|
|
11
|
+
if (e) {
|
|
12
|
+
Object.keys(e).forEach(function (k) {
|
|
13
|
+
if (k !== 'default') {
|
|
14
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
15
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
get: function () { return e[k]; }
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
n.default = e;
|
|
23
|
+
return Object.freeze(n);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
var R__namespace = /*#__PURE__*/_interopNamespaceDefault(R);
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Groups one or more futures for collective operations
|
|
30
|
+
*/
|
|
31
|
+
class FutureGroup {
|
|
32
|
+
constructor() {
|
|
33
|
+
this.futures = [];
|
|
34
|
+
this.closedEvent = new synchronize.FutureEvent();
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Check if the group can accept more futures
|
|
38
|
+
*/
|
|
39
|
+
get open() {
|
|
40
|
+
return !this.closedEvent.isSet;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Total number of futures that completed
|
|
44
|
+
*/
|
|
45
|
+
get totalCompleted() {
|
|
46
|
+
return this.futures.reduce((total, future) => total + (future.done ? 1 : 0), 0);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Total number of futures that failed
|
|
50
|
+
*/
|
|
51
|
+
get totalFailed() {
|
|
52
|
+
return this.futures.reduce((total, future) => total + (future.error ? 1 : 0), 0);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Total number of futures current executing
|
|
56
|
+
*/
|
|
57
|
+
get totalRunning() {
|
|
58
|
+
return this.futures.reduce((total, future) => total + (future.running ? 1 : 0), 0);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Adds a future to the group
|
|
62
|
+
* @param future
|
|
63
|
+
*/
|
|
64
|
+
add(future) {
|
|
65
|
+
this.futures.push(future);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Marks the group as closed, indicating that all the necessary futures to monitor have been added
|
|
69
|
+
*/
|
|
70
|
+
done() {
|
|
71
|
+
this.closedEvent.set();
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Waits for all the futures in the group to complete
|
|
75
|
+
*/
|
|
76
|
+
allCompleted() {
|
|
77
|
+
return this.closedEvent
|
|
78
|
+
.wait()
|
|
79
|
+
.thenApply(() => future.Future.collect(this.futures))
|
|
80
|
+
.thenApply(() => this);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Waits for all the futures to complete and collect the results in an array
|
|
84
|
+
*/
|
|
85
|
+
collect() {
|
|
86
|
+
return this.closedEvent.wait().thenApply(() => future.Future.collect(this.futures));
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Waits for the first future to complete
|
|
90
|
+
*/
|
|
91
|
+
firstCompleted() {
|
|
92
|
+
return this.asCompleted().future;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Waits for the first future to fail
|
|
96
|
+
*/
|
|
97
|
+
firstFailed() {
|
|
98
|
+
return this.asFailed().future;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Stream of all the futures that failed
|
|
102
|
+
*/
|
|
103
|
+
asFailed() {
|
|
104
|
+
const pendingPromises = [];
|
|
105
|
+
const lookup = { pendingPromises };
|
|
106
|
+
const createPromises = R__namespace.once(() => {
|
|
107
|
+
lookup.pendingPromises = this.futures.map((future, index) => {
|
|
108
|
+
return future
|
|
109
|
+
.run()
|
|
110
|
+
.then(() => ({ index }))
|
|
111
|
+
.catch(() => ({ future, index }));
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
return this.streamPendingFutures(lookup, createPromises);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Stream of all the futures that completed
|
|
118
|
+
*/
|
|
119
|
+
asCompleted() {
|
|
120
|
+
const pendingPromises = [];
|
|
121
|
+
const lookup = { pendingPromises };
|
|
122
|
+
const createPromises = R__namespace.once(() => {
|
|
123
|
+
lookup.pendingPromises = this.futures.map((future, index) => {
|
|
124
|
+
return future
|
|
125
|
+
.run()
|
|
126
|
+
.then(() => ({ index, future }))
|
|
127
|
+
.catch(() => ({ index }));
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
return this.streamPendingFutures(lookup, createPromises);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Stream of all the futures either completed or failed
|
|
134
|
+
*/
|
|
135
|
+
asSettled() {
|
|
136
|
+
const pendingPromises = [];
|
|
137
|
+
const lookup = { pendingPromises };
|
|
138
|
+
const createPromises = R__namespace.once(() => {
|
|
139
|
+
lookup.pendingPromises = this.futures.map((future, index) => {
|
|
140
|
+
return future
|
|
141
|
+
.run()
|
|
142
|
+
.then(() => ({ index, future }))
|
|
143
|
+
.catch(() => ({ index, future }));
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
return this.streamPendingFutures(lookup, createPromises);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Waits for all the futures to run and either complete or fail
|
|
150
|
+
*/
|
|
151
|
+
allSettled() {
|
|
152
|
+
return this.closedEvent
|
|
153
|
+
.wait()
|
|
154
|
+
.thenApply(() => future.Future.collectSettled(this.futures))
|
|
155
|
+
.thenApply(() => this);
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Cancels every running future that is in the group
|
|
159
|
+
*/
|
|
160
|
+
cancel() {
|
|
161
|
+
this.futures.forEach((future) => future.cancel());
|
|
162
|
+
}
|
|
163
|
+
streamPendingFutures(lookup, setup) {
|
|
164
|
+
return new stream.Stream(() => {
|
|
165
|
+
setup();
|
|
166
|
+
if (!lookup.pendingPromises.length) {
|
|
167
|
+
return new stream_state.ExecutorState(true);
|
|
168
|
+
}
|
|
169
|
+
return this.closedEvent.wait().thenApply(() => Promise.any(lookup.pendingPromises).then((result) => {
|
|
170
|
+
lookup.pendingPromises = lookup.pendingPromises.filter((_, index) => index !== result.index);
|
|
171
|
+
if (result.future) {
|
|
172
|
+
return { future: result.future };
|
|
173
|
+
}
|
|
174
|
+
return undefined;
|
|
175
|
+
}));
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
exports.FutureGroup = FutureGroup;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { Future } from "./future";
|
|
2
|
+
import { Stream } from "../stream";
|
|
3
|
+
/**
|
|
4
|
+
* Groups one or more futures for collective operations
|
|
5
|
+
*/
|
|
6
|
+
export declare class FutureGroup<T = any> {
|
|
7
|
+
readonly futures: Array<Future<T>>;
|
|
8
|
+
private readonly closedEvent;
|
|
9
|
+
constructor();
|
|
10
|
+
/**
|
|
11
|
+
* Check if the group can accept more futures
|
|
12
|
+
*/
|
|
13
|
+
get open(): boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Total number of futures that completed
|
|
16
|
+
*/
|
|
17
|
+
get totalCompleted(): number;
|
|
18
|
+
/**
|
|
19
|
+
* Total number of futures that failed
|
|
20
|
+
*/
|
|
21
|
+
get totalFailed(): number;
|
|
22
|
+
/**
|
|
23
|
+
* Total number of futures current executing
|
|
24
|
+
*/
|
|
25
|
+
get totalRunning(): number;
|
|
26
|
+
/**
|
|
27
|
+
* Adds a future to the group
|
|
28
|
+
* @param future
|
|
29
|
+
*/
|
|
30
|
+
add(future: Future<T>): void;
|
|
31
|
+
/**
|
|
32
|
+
* Marks the group as closed, indicating that all the necessary futures to monitor have been added
|
|
33
|
+
*/
|
|
34
|
+
done(): void;
|
|
35
|
+
/**
|
|
36
|
+
* Waits for all the futures in the group to complete
|
|
37
|
+
*/
|
|
38
|
+
allCompleted(): Future<FutureGroup>;
|
|
39
|
+
/**
|
|
40
|
+
* Waits for all the futures to complete and collect the results in an array
|
|
41
|
+
*/
|
|
42
|
+
collect(): Future<T[]>;
|
|
43
|
+
/**
|
|
44
|
+
* Waits for the first future to complete
|
|
45
|
+
*/
|
|
46
|
+
firstCompleted(): Future<{
|
|
47
|
+
future: Future<T>;
|
|
48
|
+
}>;
|
|
49
|
+
/**
|
|
50
|
+
* Waits for the first future to fail
|
|
51
|
+
*/
|
|
52
|
+
firstFailed(): Future<{
|
|
53
|
+
future: Future<T>;
|
|
54
|
+
}>;
|
|
55
|
+
/**
|
|
56
|
+
* Stream of all the futures that failed
|
|
57
|
+
*/
|
|
58
|
+
asFailed(): Stream<{
|
|
59
|
+
future: Future<T>;
|
|
60
|
+
}>;
|
|
61
|
+
/**
|
|
62
|
+
* Stream of all the futures that completed
|
|
63
|
+
*/
|
|
64
|
+
asCompleted(): Stream<{
|
|
65
|
+
future: Future<T>;
|
|
66
|
+
}>;
|
|
67
|
+
/**
|
|
68
|
+
* Stream of all the futures either completed or failed
|
|
69
|
+
*/
|
|
70
|
+
asSettled(): Stream<{
|
|
71
|
+
future: Future<T>;
|
|
72
|
+
}>;
|
|
73
|
+
/**
|
|
74
|
+
* Waits for all the futures to run and either complete or fail
|
|
75
|
+
*/
|
|
76
|
+
allSettled(): Future<FutureGroup>;
|
|
77
|
+
/**
|
|
78
|
+
* Cancels every running future that is in the group
|
|
79
|
+
*/
|
|
80
|
+
cancel(): void;
|
|
81
|
+
private streamPendingFutures;
|
|
82
|
+
}
|
package/future/group.mjs
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import * as R from 'ramda';
|
|
2
|
+
import { Future } from './future.mjs';
|
|
3
|
+
import { FutureEvent } from '../synchronize.mjs';
|
|
4
|
+
import { Stream } from '../stream.mjs';
|
|
5
|
+
import { ExecutorState } from '../stream/state.mjs';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Groups one or more futures for collective operations
|
|
9
|
+
*/
|
|
10
|
+
class FutureGroup {
|
|
11
|
+
constructor() {
|
|
12
|
+
this.futures = [];
|
|
13
|
+
this.closedEvent = new FutureEvent();
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Check if the group can accept more futures
|
|
17
|
+
*/
|
|
18
|
+
get open() {
|
|
19
|
+
return !this.closedEvent.isSet;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Total number of futures that completed
|
|
23
|
+
*/
|
|
24
|
+
get totalCompleted() {
|
|
25
|
+
return this.futures.reduce((total, future) => total + (future.done ? 1 : 0), 0);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Total number of futures that failed
|
|
29
|
+
*/
|
|
30
|
+
get totalFailed() {
|
|
31
|
+
return this.futures.reduce((total, future) => total + (future.error ? 1 : 0), 0);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Total number of futures current executing
|
|
35
|
+
*/
|
|
36
|
+
get totalRunning() {
|
|
37
|
+
return this.futures.reduce((total, future) => total + (future.running ? 1 : 0), 0);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Adds a future to the group
|
|
41
|
+
* @param future
|
|
42
|
+
*/
|
|
43
|
+
add(future) {
|
|
44
|
+
this.futures.push(future);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Marks the group as closed, indicating that all the necessary futures to monitor have been added
|
|
48
|
+
*/
|
|
49
|
+
done() {
|
|
50
|
+
this.closedEvent.set();
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Waits for all the futures in the group to complete
|
|
54
|
+
*/
|
|
55
|
+
allCompleted() {
|
|
56
|
+
return this.closedEvent
|
|
57
|
+
.wait()
|
|
58
|
+
.thenApply(() => Future.collect(this.futures))
|
|
59
|
+
.thenApply(() => this);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Waits for all the futures to complete and collect the results in an array
|
|
63
|
+
*/
|
|
64
|
+
collect() {
|
|
65
|
+
return this.closedEvent.wait().thenApply(() => Future.collect(this.futures));
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Waits for the first future to complete
|
|
69
|
+
*/
|
|
70
|
+
firstCompleted() {
|
|
71
|
+
return this.asCompleted().future;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Waits for the first future to fail
|
|
75
|
+
*/
|
|
76
|
+
firstFailed() {
|
|
77
|
+
return this.asFailed().future;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Stream of all the futures that failed
|
|
81
|
+
*/
|
|
82
|
+
asFailed() {
|
|
83
|
+
const pendingPromises = [];
|
|
84
|
+
const lookup = { pendingPromises };
|
|
85
|
+
const createPromises = R.once(() => {
|
|
86
|
+
lookup.pendingPromises = this.futures.map((future, index) => {
|
|
87
|
+
return future
|
|
88
|
+
.run()
|
|
89
|
+
.then(() => ({ index }))
|
|
90
|
+
.catch(() => ({ future, index }));
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
return this.streamPendingFutures(lookup, createPromises);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Stream of all the futures that completed
|
|
97
|
+
*/
|
|
98
|
+
asCompleted() {
|
|
99
|
+
const pendingPromises = [];
|
|
100
|
+
const lookup = { pendingPromises };
|
|
101
|
+
const createPromises = R.once(() => {
|
|
102
|
+
lookup.pendingPromises = this.futures.map((future, index) => {
|
|
103
|
+
return future
|
|
104
|
+
.run()
|
|
105
|
+
.then(() => ({ index, future }))
|
|
106
|
+
.catch(() => ({ index }));
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
return this.streamPendingFutures(lookup, createPromises);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Stream of all the futures either completed or failed
|
|
113
|
+
*/
|
|
114
|
+
asSettled() {
|
|
115
|
+
const pendingPromises = [];
|
|
116
|
+
const lookup = { pendingPromises };
|
|
117
|
+
const createPromises = R.once(() => {
|
|
118
|
+
lookup.pendingPromises = this.futures.map((future, index) => {
|
|
119
|
+
return future
|
|
120
|
+
.run()
|
|
121
|
+
.then(() => ({ index, future }))
|
|
122
|
+
.catch(() => ({ index, future }));
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
return this.streamPendingFutures(lookup, createPromises);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Waits for all the futures to run and either complete or fail
|
|
129
|
+
*/
|
|
130
|
+
allSettled() {
|
|
131
|
+
return this.closedEvent
|
|
132
|
+
.wait()
|
|
133
|
+
.thenApply(() => Future.collectSettled(this.futures))
|
|
134
|
+
.thenApply(() => this);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Cancels every running future that is in the group
|
|
138
|
+
*/
|
|
139
|
+
cancel() {
|
|
140
|
+
this.futures.forEach((future) => future.cancel());
|
|
141
|
+
}
|
|
142
|
+
streamPendingFutures(lookup, setup) {
|
|
143
|
+
return new Stream(() => {
|
|
144
|
+
setup();
|
|
145
|
+
if (!lookup.pendingPromises.length) {
|
|
146
|
+
return new ExecutorState(true);
|
|
147
|
+
}
|
|
148
|
+
return this.closedEvent.wait().thenApply(() => Promise.any(lookup.pendingPromises).then((result) => {
|
|
149
|
+
lookup.pendingPromises = lookup.pendingPromises.filter((_, index) => index !== result.index);
|
|
150
|
+
if (result.future) {
|
|
151
|
+
return { future: result.future };
|
|
152
|
+
}
|
|
153
|
+
return undefined;
|
|
154
|
+
}));
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export { FutureGroup };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Result returned when 1 step of a future completes and the result is passed on to the next step in the future
|
|
5
|
+
* (thenApply)
|
|
6
|
+
*/
|
|
7
|
+
class FutureResult {
|
|
8
|
+
/**
|
|
9
|
+
*
|
|
10
|
+
* @param value The returned value from teh previous step in the future
|
|
11
|
+
* @param signal the future signal
|
|
12
|
+
*/
|
|
13
|
+
constructor(value, signal) {
|
|
14
|
+
this.value = value;
|
|
15
|
+
this.signal = signal;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
exports.FutureResult = FutureResult;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Future } from "./future";
|
|
2
|
+
import { InferStreamResult } from "../typing/types";
|
|
3
|
+
import { Stream } from "../stream";
|
|
4
|
+
export type InferredFutureResult<T> = T extends FutureResult<infer U> ? InferredFutureResult<U> : T extends Promise<infer U> ? InferredFutureResult<U> : T extends Future<infer U> ? InferredFutureResult<U> : T extends Stream<any> ? InferStreamResult<T> : T;
|
|
5
|
+
/**
|
|
6
|
+
* Result returned when 1 step of a future completes and the result is passed on to the next step in the future
|
|
7
|
+
* (thenApply)
|
|
8
|
+
*/
|
|
9
|
+
export declare class FutureResult<T> {
|
|
10
|
+
readonly value: T;
|
|
11
|
+
readonly signal: AbortSignal;
|
|
12
|
+
/**
|
|
13
|
+
*
|
|
14
|
+
* @param value The returned value from teh previous step in the future
|
|
15
|
+
* @param signal the future signal
|
|
16
|
+
*/
|
|
17
|
+
constructor(value: T, signal: AbortSignal);
|
|
18
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Result returned when 1 step of a future completes and the result is passed on to the next step in the future
|
|
3
|
+
* (thenApply)
|
|
4
|
+
*/
|
|
5
|
+
class FutureResult {
|
|
6
|
+
/**
|
|
7
|
+
*
|
|
8
|
+
* @param value The returned value from teh previous step in the future
|
|
9
|
+
* @param signal the future signal
|
|
10
|
+
*/
|
|
11
|
+
constructor(value, signal) {
|
|
12
|
+
this.value = value;
|
|
13
|
+
this.signal = signal;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { FutureResult };
|
package/future.cjs
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var future = require('./future/future.cjs');
|
|
4
|
+
var result = require('./future/result.cjs');
|
|
5
|
+
var group = require('./future/group.cjs');
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
exports.Future = future.Future;
|
|
10
|
+
exports.FutureResult = result.FutureResult;
|
|
11
|
+
exports.FutureGroup = group.FutureGroup;
|
package/future.mjs
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Future, FutureResult } from "../future";
|
|
2
|
+
/**
|
|
3
|
+
* Context managers allow you to allocate and release resources irrespective of
|
|
4
|
+
* an executor failing to complete successfully
|
|
5
|
+
*/
|
|
6
|
+
export interface ContextManager<T> {
|
|
7
|
+
with: (functor: (value: FutureResult<T>) => any) => Future<any>;
|
|
8
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./context";
|
package/managers.cjs
ADDED
package/managers.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|