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/stream/call.cjs
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _tslib = require('../_virtual/_tslib.cjs');
|
|
4
|
+
var types = require('../networking/types.cjs');
|
|
5
|
+
var CallExecutionError = require('../errors/CallExecutionError.cjs');
|
|
6
|
+
var MissingArgumentsError = require('../errors/MissingArgumentsError.cjs');
|
|
7
|
+
var model = require('../data/model/model.cjs');
|
|
8
|
+
require('ramda');
|
|
9
|
+
var types$1 = require('../data/model/types.cjs');
|
|
10
|
+
require('../data/model/credentials.cjs');
|
|
11
|
+
var stream = require('../stream.cjs');
|
|
12
|
+
var stream_state = require('./state.cjs');
|
|
13
|
+
var future = require('../future/future.cjs');
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Abstract Callable class with generic processing functionalities
|
|
17
|
+
*/
|
|
18
|
+
class Callable extends stream.Stream {
|
|
19
|
+
constructor(executor, ModelClass, responseType, arrayResponse = false) {
|
|
20
|
+
super(((v) => {
|
|
21
|
+
const result = executor(v);
|
|
22
|
+
if (result instanceof Promise)
|
|
23
|
+
return result.then((v) => new stream_state.ExecutorState(true, v));
|
|
24
|
+
return new stream_state.ExecutorState(true, result);
|
|
25
|
+
}));
|
|
26
|
+
if (ModelClass != null && responseType === undefined) {
|
|
27
|
+
throw new MissingArgumentsError.MissingArgumentsError(["responseType"]);
|
|
28
|
+
}
|
|
29
|
+
if (responseType != null && ModelClass === undefined && responseType !== types.ResponseType.BINARY) {
|
|
30
|
+
throw new MissingArgumentsError.MissingArgumentsError(["responseType"]);
|
|
31
|
+
}
|
|
32
|
+
this.ModelClass = ModelClass;
|
|
33
|
+
this.responseType = responseType;
|
|
34
|
+
this.arrayResponseSupport = arrayResponse;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Converts json object to a Model instance/Array of Models
|
|
38
|
+
* @param json json object
|
|
39
|
+
* @param resp optional Response object
|
|
40
|
+
* @protected
|
|
41
|
+
*/
|
|
42
|
+
__process_json__(json, resp) {
|
|
43
|
+
var _a, _b;
|
|
44
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
45
|
+
if (((_a = this.ModelClass) === null || _a === void 0 ? void 0 : _a.name) === String.name && !this.arrayResponseSupport) {
|
|
46
|
+
throw new CallExecutionError.CallExecutionError("Invalid decorator combination. Cannot use String model with JSON formatting", resp);
|
|
47
|
+
}
|
|
48
|
+
if (Array.isArray(json) && this.arrayResponseSupport && ((_b = this.ModelClass) === null || _b === void 0 ? void 0 : _b.name) === String.name) {
|
|
49
|
+
return json.map((v) => (typeof v !== "string" ? String(v) : v));
|
|
50
|
+
}
|
|
51
|
+
const invoker = this.ModelClass.prototype instanceof model.Model ? this.ModelClass.fromJSON : this.ModelClass;
|
|
52
|
+
if ((json instanceof Array || Array.isArray(json)) && this.arrayResponseSupport) {
|
|
53
|
+
return json.map((v) => invoker(v));
|
|
54
|
+
}
|
|
55
|
+
else if (!(json instanceof Array || Array.isArray(json)) && !this.arrayResponseSupport) {
|
|
56
|
+
return invoker(json);
|
|
57
|
+
}
|
|
58
|
+
throw new CallExecutionError.CallExecutionError("Invalid data format received", resp);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Processes the response body to appropriate return type for a Callable
|
|
63
|
+
* @param resp response object
|
|
64
|
+
* @param responseType ResponseType
|
|
65
|
+
* @protected
|
|
66
|
+
*/
|
|
67
|
+
__process_response__(resp, responseType) {
|
|
68
|
+
var _a;
|
|
69
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
70
|
+
switch (responseType) {
|
|
71
|
+
case types.ResponseType.JSON: {
|
|
72
|
+
const result = yield resp.json();
|
|
73
|
+
return yield this.__process_json__(result, resp);
|
|
74
|
+
}
|
|
75
|
+
case types.ResponseType.STRING: {
|
|
76
|
+
const result = yield resp.text();
|
|
77
|
+
return String(result);
|
|
78
|
+
}
|
|
79
|
+
case types.ResponseType.XML: {
|
|
80
|
+
if (((_a = this.ModelClass) === null || _a === void 0 ? void 0 : _a.name) === String.name) {
|
|
81
|
+
throw new CallExecutionError.CallExecutionError("Invalid decorator combination. Cannot use String model with JSON formatting", resp);
|
|
82
|
+
}
|
|
83
|
+
const invoker = this.ModelClass.prototype instanceof model.Model ? this.ModelClass.fromString : this.ModelClass;
|
|
84
|
+
return invoker(yield resp.text(), types$1.DataFormat.XML);
|
|
85
|
+
}
|
|
86
|
+
case types.ResponseType.BINARY: {
|
|
87
|
+
return (yield resp.blob());
|
|
88
|
+
}
|
|
89
|
+
default:
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* A Callable structure for executing single network request
|
|
97
|
+
*/
|
|
98
|
+
class Call extends Callable {
|
|
99
|
+
constructor(executor, callback, ModelClass, throttle, responseType, arrayResponse = false) {
|
|
100
|
+
super(executor, ModelClass, responseType, arrayResponse);
|
|
101
|
+
this.callback = callback;
|
|
102
|
+
this.throttle = throttle;
|
|
103
|
+
this.executingCallback = false;
|
|
104
|
+
}
|
|
105
|
+
clone() {
|
|
106
|
+
const newStream = new Call(this.executor, this.callback, this.ModelClass, this.throttle, this.responseType, this.arrayResponseSupport);
|
|
107
|
+
newStream.executor = this.executor;
|
|
108
|
+
newStream.actions = [...this.actions];
|
|
109
|
+
newStream.executed = this.executed;
|
|
110
|
+
newStream.done = this.done;
|
|
111
|
+
newStream.backlog = [...this.backlog];
|
|
112
|
+
return newStream;
|
|
113
|
+
}
|
|
114
|
+
execute(rawResponse = false) {
|
|
115
|
+
const _super = Object.create(null, {
|
|
116
|
+
execute: { get: () => super.execute }
|
|
117
|
+
});
|
|
118
|
+
return future.Future.of((resolve, reject, signal) => _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
119
|
+
var _a, _b;
|
|
120
|
+
if (this.throttle && this.executingCallback)
|
|
121
|
+
this.cancel();
|
|
122
|
+
this.executingCallback = true;
|
|
123
|
+
if ((_a = this.throttle) === null || _a === void 0 ? void 0 : _a.waitPeriodInMs) {
|
|
124
|
+
yield future.Future.sleep({ milliseconds: this.throttle.waitPeriodInMs }, signal);
|
|
125
|
+
}
|
|
126
|
+
const resp = (yield _super.execute.call(this).registerSignal(signal));
|
|
127
|
+
this.executingCallback = false;
|
|
128
|
+
if (!resp)
|
|
129
|
+
return resp;
|
|
130
|
+
if (!(resp instanceof Response))
|
|
131
|
+
return resolve(resp);
|
|
132
|
+
if (!resp.ok) {
|
|
133
|
+
return reject(new CallExecutionError.CallExecutionError(`Received response status code of ${resp.status}`, resp));
|
|
134
|
+
}
|
|
135
|
+
const response = rawResponse
|
|
136
|
+
? (yield resp.blob())
|
|
137
|
+
: yield this.__process_response__(resp, this.responseType);
|
|
138
|
+
let callbackResponse = (_b = this.callback) === null || _b === void 0 ? void 0 : _b.bind({ context: response })();
|
|
139
|
+
if (callbackResponse instanceof Promise) {
|
|
140
|
+
callbackResponse = yield callbackResponse;
|
|
141
|
+
}
|
|
142
|
+
else if (callbackResponse instanceof Callable) {
|
|
143
|
+
callbackResponse = yield callbackResponse.execute().registerSignal(signal);
|
|
144
|
+
}
|
|
145
|
+
if (callbackResponse)
|
|
146
|
+
return resolve(callbackResponse);
|
|
147
|
+
resolve(response);
|
|
148
|
+
})).schedule();
|
|
149
|
+
}
|
|
150
|
+
__execute__() {
|
|
151
|
+
const preprocessor = (v) => (v instanceof Response ? this.__process_response__(v, this.responseType) : v);
|
|
152
|
+
return super.__execute__(preprocessor);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
exports.Call = Call;
|
|
157
|
+
exports.Callable = Callable;
|
package/stream/call.d.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { NONE, ResponseType, ThrottleByProps } from "../networking/types";
|
|
2
|
+
import { Model } from "../data/model";
|
|
3
|
+
import { Executor, Stream } from "./index";
|
|
4
|
+
import { Future } from "../future";
|
|
5
|
+
/**
|
|
6
|
+
* Abstract Callable class with generic processing functionalities
|
|
7
|
+
*/
|
|
8
|
+
export declare class Callable<T extends Model | Model[] | String | String[] | string | string[] | Blob | Blob[] | NONE> extends Stream<T> {
|
|
9
|
+
/**
|
|
10
|
+
* Type of response body expected from the callback's Response Object
|
|
11
|
+
* @protected
|
|
12
|
+
*/
|
|
13
|
+
protected readonly responseType?: ResponseType;
|
|
14
|
+
/**
|
|
15
|
+
* Model used to deserialize response object retrieved from the network request
|
|
16
|
+
* @protected
|
|
17
|
+
*/
|
|
18
|
+
protected readonly ModelClass?: typeof Model | typeof String;
|
|
19
|
+
/**
|
|
20
|
+
* Used to check if the response is expected to be an array of ModelClass
|
|
21
|
+
* @protected
|
|
22
|
+
*/
|
|
23
|
+
protected readonly arrayResponseSupport: boolean;
|
|
24
|
+
protected constructor(executor: Executor, ModelClass?: typeof Model | typeof String, responseType?: ResponseType, arrayResponse?: boolean);
|
|
25
|
+
/**
|
|
26
|
+
* Converts json object to a Model instance/Array of Models
|
|
27
|
+
* @param json json object
|
|
28
|
+
* @param resp optional Response object
|
|
29
|
+
* @protected
|
|
30
|
+
*/
|
|
31
|
+
protected __process_json__(json: Object | Object[], resp?: Response): Promise<T>;
|
|
32
|
+
/**
|
|
33
|
+
* Processes the response body to appropriate return type for a Callable
|
|
34
|
+
* @param resp response object
|
|
35
|
+
* @param responseType ResponseType
|
|
36
|
+
* @protected
|
|
37
|
+
*/
|
|
38
|
+
protected __process_response__(resp: Response, responseType?: ResponseType): Promise<T>;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* A Callable structure for executing single network request
|
|
42
|
+
*/
|
|
43
|
+
export declare class Call<T extends Model | Model[] | String | string | string[] | Blob | NONE> extends Callable<T> {
|
|
44
|
+
/**
|
|
45
|
+
* Callback function to do post-processing once network request has successfully completed
|
|
46
|
+
* @private
|
|
47
|
+
*/
|
|
48
|
+
protected readonly callback?: Function;
|
|
49
|
+
/**
|
|
50
|
+
* Throttle configuration for the call
|
|
51
|
+
* @private
|
|
52
|
+
*/
|
|
53
|
+
protected readonly throttle?: ThrottleByProps;
|
|
54
|
+
/**
|
|
55
|
+
* Checks if the callable is currently executing
|
|
56
|
+
* @private
|
|
57
|
+
*/
|
|
58
|
+
private executingCallback;
|
|
59
|
+
constructor(executor: Executor, callback?: Function, ModelClass?: typeof Model | typeof String, throttle?: ThrottleByProps, responseType?: ResponseType, arrayResponse?: boolean);
|
|
60
|
+
clone(): Call<T>;
|
|
61
|
+
execute(rawResponse?: boolean): Future<T>;
|
|
62
|
+
protected __execute__(): Future<{
|
|
63
|
+
state: import("./index").State;
|
|
64
|
+
value?: T;
|
|
65
|
+
}>;
|
|
66
|
+
}
|
package/stream/call.mjs
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { __awaiter } from '../_virtual/_tslib.mjs';
|
|
2
|
+
import { ResponseType } from '../networking/types.mjs';
|
|
3
|
+
import { CallExecutionError } from '../errors/CallExecutionError.mjs';
|
|
4
|
+
import { MissingArgumentsError } from '../errors/MissingArgumentsError.mjs';
|
|
5
|
+
import { Model } from '../data/model/model.mjs';
|
|
6
|
+
import 'ramda';
|
|
7
|
+
import { DataFormat } from '../data/model/types.mjs';
|
|
8
|
+
import '../data/model/credentials.mjs';
|
|
9
|
+
import { Stream } from '../stream.mjs';
|
|
10
|
+
import { ExecutorState } from './state.mjs';
|
|
11
|
+
import { Future } from '../future/future.mjs';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Abstract Callable class with generic processing functionalities
|
|
15
|
+
*/
|
|
16
|
+
class Callable extends Stream {
|
|
17
|
+
constructor(executor, ModelClass, responseType, arrayResponse = false) {
|
|
18
|
+
super(((v) => {
|
|
19
|
+
const result = executor(v);
|
|
20
|
+
if (result instanceof Promise)
|
|
21
|
+
return result.then((v) => new ExecutorState(true, v));
|
|
22
|
+
return new ExecutorState(true, result);
|
|
23
|
+
}));
|
|
24
|
+
if (ModelClass != null && responseType === undefined) {
|
|
25
|
+
throw new MissingArgumentsError(["responseType"]);
|
|
26
|
+
}
|
|
27
|
+
if (responseType != null && ModelClass === undefined && responseType !== ResponseType.BINARY) {
|
|
28
|
+
throw new MissingArgumentsError(["responseType"]);
|
|
29
|
+
}
|
|
30
|
+
this.ModelClass = ModelClass;
|
|
31
|
+
this.responseType = responseType;
|
|
32
|
+
this.arrayResponseSupport = arrayResponse;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Converts json object to a Model instance/Array of Models
|
|
36
|
+
* @param json json object
|
|
37
|
+
* @param resp optional Response object
|
|
38
|
+
* @protected
|
|
39
|
+
*/
|
|
40
|
+
__process_json__(json, resp) {
|
|
41
|
+
var _a, _b;
|
|
42
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
43
|
+
if (((_a = this.ModelClass) === null || _a === void 0 ? void 0 : _a.name) === String.name && !this.arrayResponseSupport) {
|
|
44
|
+
throw new CallExecutionError("Invalid decorator combination. Cannot use String model with JSON formatting", resp);
|
|
45
|
+
}
|
|
46
|
+
if (Array.isArray(json) && this.arrayResponseSupport && ((_b = this.ModelClass) === null || _b === void 0 ? void 0 : _b.name) === String.name) {
|
|
47
|
+
return json.map((v) => (typeof v !== "string" ? String(v) : v));
|
|
48
|
+
}
|
|
49
|
+
const invoker = this.ModelClass.prototype instanceof Model ? this.ModelClass.fromJSON : this.ModelClass;
|
|
50
|
+
if ((json instanceof Array || Array.isArray(json)) && this.arrayResponseSupport) {
|
|
51
|
+
return json.map((v) => invoker(v));
|
|
52
|
+
}
|
|
53
|
+
else if (!(json instanceof Array || Array.isArray(json)) && !this.arrayResponseSupport) {
|
|
54
|
+
return invoker(json);
|
|
55
|
+
}
|
|
56
|
+
throw new CallExecutionError("Invalid data format received", resp);
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Processes the response body to appropriate return type for a Callable
|
|
61
|
+
* @param resp response object
|
|
62
|
+
* @param responseType ResponseType
|
|
63
|
+
* @protected
|
|
64
|
+
*/
|
|
65
|
+
__process_response__(resp, responseType) {
|
|
66
|
+
var _a;
|
|
67
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
68
|
+
switch (responseType) {
|
|
69
|
+
case ResponseType.JSON: {
|
|
70
|
+
const result = yield resp.json();
|
|
71
|
+
return yield this.__process_json__(result, resp);
|
|
72
|
+
}
|
|
73
|
+
case ResponseType.STRING: {
|
|
74
|
+
const result = yield resp.text();
|
|
75
|
+
return String(result);
|
|
76
|
+
}
|
|
77
|
+
case ResponseType.XML: {
|
|
78
|
+
if (((_a = this.ModelClass) === null || _a === void 0 ? void 0 : _a.name) === String.name) {
|
|
79
|
+
throw new CallExecutionError("Invalid decorator combination. Cannot use String model with JSON formatting", resp);
|
|
80
|
+
}
|
|
81
|
+
const invoker = this.ModelClass.prototype instanceof Model ? this.ModelClass.fromString : this.ModelClass;
|
|
82
|
+
return invoker(yield resp.text(), DataFormat.XML);
|
|
83
|
+
}
|
|
84
|
+
case ResponseType.BINARY: {
|
|
85
|
+
return (yield resp.blob());
|
|
86
|
+
}
|
|
87
|
+
default:
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* A Callable structure for executing single network request
|
|
95
|
+
*/
|
|
96
|
+
class Call extends Callable {
|
|
97
|
+
constructor(executor, callback, ModelClass, throttle, responseType, arrayResponse = false) {
|
|
98
|
+
super(executor, ModelClass, responseType, arrayResponse);
|
|
99
|
+
this.callback = callback;
|
|
100
|
+
this.throttle = throttle;
|
|
101
|
+
this.executingCallback = false;
|
|
102
|
+
}
|
|
103
|
+
clone() {
|
|
104
|
+
const newStream = new Call(this.executor, this.callback, this.ModelClass, this.throttle, this.responseType, this.arrayResponseSupport);
|
|
105
|
+
newStream.executor = this.executor;
|
|
106
|
+
newStream.actions = [...this.actions];
|
|
107
|
+
newStream.executed = this.executed;
|
|
108
|
+
newStream.done = this.done;
|
|
109
|
+
newStream.backlog = [...this.backlog];
|
|
110
|
+
return newStream;
|
|
111
|
+
}
|
|
112
|
+
execute(rawResponse = false) {
|
|
113
|
+
const _super = Object.create(null, {
|
|
114
|
+
execute: { get: () => super.execute }
|
|
115
|
+
});
|
|
116
|
+
return Future.of((resolve, reject, signal) => __awaiter(this, void 0, void 0, function* () {
|
|
117
|
+
var _a, _b;
|
|
118
|
+
if (this.throttle && this.executingCallback)
|
|
119
|
+
this.cancel();
|
|
120
|
+
this.executingCallback = true;
|
|
121
|
+
if ((_a = this.throttle) === null || _a === void 0 ? void 0 : _a.waitPeriodInMs) {
|
|
122
|
+
yield Future.sleep({ milliseconds: this.throttle.waitPeriodInMs }, signal);
|
|
123
|
+
}
|
|
124
|
+
const resp = (yield _super.execute.call(this).registerSignal(signal));
|
|
125
|
+
this.executingCallback = false;
|
|
126
|
+
if (!resp)
|
|
127
|
+
return resp;
|
|
128
|
+
if (!(resp instanceof Response))
|
|
129
|
+
return resolve(resp);
|
|
130
|
+
if (!resp.ok) {
|
|
131
|
+
return reject(new CallExecutionError(`Received response status code of ${resp.status}`, resp));
|
|
132
|
+
}
|
|
133
|
+
const response = rawResponse
|
|
134
|
+
? (yield resp.blob())
|
|
135
|
+
: yield this.__process_response__(resp, this.responseType);
|
|
136
|
+
let callbackResponse = (_b = this.callback) === null || _b === void 0 ? void 0 : _b.bind({ context: response })();
|
|
137
|
+
if (callbackResponse instanceof Promise) {
|
|
138
|
+
callbackResponse = yield callbackResponse;
|
|
139
|
+
}
|
|
140
|
+
else if (callbackResponse instanceof Callable) {
|
|
141
|
+
callbackResponse = yield callbackResponse.execute().registerSignal(signal);
|
|
142
|
+
}
|
|
143
|
+
if (callbackResponse)
|
|
144
|
+
return resolve(callbackResponse);
|
|
145
|
+
resolve(response);
|
|
146
|
+
})).schedule();
|
|
147
|
+
}
|
|
148
|
+
__execute__() {
|
|
149
|
+
const preprocessor = (v) => (v instanceof Response ? this.__process_response__(v, this.responseType) : v);
|
|
150
|
+
return super.__execute__(preprocessor);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export { Call, Callable };
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _tslib = require('../_virtual/_tslib.cjs');
|
|
4
|
+
var future = require('../future/future.cjs');
|
|
5
|
+
require('ramda');
|
|
6
|
+
require('../stream.cjs');
|
|
7
|
+
var Stack = require('../data-structures/array/Stack.cjs');
|
|
8
|
+
require('../data-structures/object/WatchableObject.cjs');
|
|
9
|
+
require('object-hash');
|
|
10
|
+
require('uuid');
|
|
11
|
+
var AbortError = require('../errors/AbortError.cjs');
|
|
12
|
+
var SimpleQueue = require('../data-structures/object/SimpleQueue.cjs');
|
|
13
|
+
|
|
14
|
+
class Collectors {
|
|
15
|
+
static read(functor) {
|
|
16
|
+
return (stream) => stream.forEach(functor);
|
|
17
|
+
}
|
|
18
|
+
static asList() {
|
|
19
|
+
return (stream) => {
|
|
20
|
+
return future.Future.of((resolve, reject, signal) => _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
21
|
+
var _a, e_1, _b, _c;
|
|
22
|
+
const collection = [];
|
|
23
|
+
try {
|
|
24
|
+
for (var _d = true, stream_1 = _tslib.__asyncValues(stream), stream_1_1; stream_1_1 = yield stream_1.next(), _a = stream_1_1.done, !_a;) {
|
|
25
|
+
_c = stream_1_1.value;
|
|
26
|
+
_d = false;
|
|
27
|
+
try {
|
|
28
|
+
const value = _c;
|
|
29
|
+
collection.push(value);
|
|
30
|
+
if (signal.aborted) {
|
|
31
|
+
reject(new AbortError.AbortError());
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
finally {
|
|
36
|
+
_d = true;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
41
|
+
finally {
|
|
42
|
+
try {
|
|
43
|
+
if (!_d && !_a && (_b = stream_1.return)) yield _b.call(stream_1);
|
|
44
|
+
}
|
|
45
|
+
finally { if (e_1) throw e_1.error; }
|
|
46
|
+
}
|
|
47
|
+
resolve(collection);
|
|
48
|
+
}));
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
static asSet() {
|
|
52
|
+
return (stream) => {
|
|
53
|
+
return future.Future.of((resolve, reject, signal) => _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
54
|
+
var _a, e_2, _b, _c;
|
|
55
|
+
const collection = new Set();
|
|
56
|
+
try {
|
|
57
|
+
for (var _d = true, stream_2 = _tslib.__asyncValues(stream), stream_2_1; stream_2_1 = yield stream_2.next(), _a = stream_2_1.done, !_a;) {
|
|
58
|
+
_c = stream_2_1.value;
|
|
59
|
+
_d = false;
|
|
60
|
+
try {
|
|
61
|
+
const value = _c;
|
|
62
|
+
collection.add(value);
|
|
63
|
+
if (signal.aborted) {
|
|
64
|
+
reject(new AbortError.AbortError());
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
finally {
|
|
69
|
+
_d = true;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
74
|
+
finally {
|
|
75
|
+
try {
|
|
76
|
+
if (!_d && !_a && (_b = stream_2.return)) yield _b.call(stream_2);
|
|
77
|
+
}
|
|
78
|
+
finally { if (e_2) throw e_2.error; }
|
|
79
|
+
}
|
|
80
|
+
resolve(collection);
|
|
81
|
+
}));
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
static joining(delimiter = "") {
|
|
85
|
+
return (stream) => {
|
|
86
|
+
return future.Future.of((resolve, reject, signal) => _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
87
|
+
let data = "";
|
|
88
|
+
let value;
|
|
89
|
+
const iterator = stream[Symbol.asyncIterator]();
|
|
90
|
+
value = yield iterator.next();
|
|
91
|
+
if (!value.done) {
|
|
92
|
+
do {
|
|
93
|
+
const nextValue = yield iterator.next();
|
|
94
|
+
if (!nextValue.done) {
|
|
95
|
+
data += value.value + delimiter;
|
|
96
|
+
value = nextValue;
|
|
97
|
+
}
|
|
98
|
+
else
|
|
99
|
+
break;
|
|
100
|
+
} while (!signal.aborted);
|
|
101
|
+
}
|
|
102
|
+
if (signal.aborted) {
|
|
103
|
+
reject(new AbortError.AbortError());
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
if (value.value !== undefined && value.value !== null) {
|
|
107
|
+
data += value.value;
|
|
108
|
+
}
|
|
109
|
+
resolve(data);
|
|
110
|
+
}));
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
static asStack(maxSize = undefined) {
|
|
114
|
+
return (stream) => {
|
|
115
|
+
return future.Future.of((resolve, reject, signal) => _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
116
|
+
var _a, e_3, _b, _c;
|
|
117
|
+
const collection = new Stack.Stack(maxSize);
|
|
118
|
+
try {
|
|
119
|
+
for (var _d = true, stream_3 = _tslib.__asyncValues(stream), stream_3_1; stream_3_1 = yield stream_3.next(), _a = stream_3_1.done, !_a;) {
|
|
120
|
+
_c = stream_3_1.value;
|
|
121
|
+
_d = false;
|
|
122
|
+
try {
|
|
123
|
+
const value = _c;
|
|
124
|
+
collection.push(value);
|
|
125
|
+
if (collection.full) {
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
else if (signal.aborted) {
|
|
129
|
+
reject(new AbortError.AbortError());
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
finally {
|
|
134
|
+
_d = true;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
139
|
+
finally {
|
|
140
|
+
try {
|
|
141
|
+
if (!_d && !_a && (_b = stream_3.return)) yield _b.call(stream_3);
|
|
142
|
+
}
|
|
143
|
+
finally { if (e_3) throw e_3.error; }
|
|
144
|
+
}
|
|
145
|
+
resolve(collection);
|
|
146
|
+
}));
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
static asQueue() {
|
|
150
|
+
return (stream) => {
|
|
151
|
+
return future.Future.of((resolve, reject, signal) => _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
152
|
+
var _a, e_4, _b, _c;
|
|
153
|
+
const collection = new SimpleQueue.SimpleQueue();
|
|
154
|
+
try {
|
|
155
|
+
for (var _d = true, stream_4 = _tslib.__asyncValues(stream), stream_4_1; stream_4_1 = yield stream_4.next(), _a = stream_4_1.done, !_a;) {
|
|
156
|
+
_c = stream_4_1.value;
|
|
157
|
+
_d = false;
|
|
158
|
+
try {
|
|
159
|
+
const value = _c;
|
|
160
|
+
collection.enqueue(value);
|
|
161
|
+
if (signal.aborted) {
|
|
162
|
+
reject(new AbortError.AbortError());
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
finally {
|
|
167
|
+
_d = true;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
172
|
+
finally {
|
|
173
|
+
try {
|
|
174
|
+
if (!_d && !_a && (_b = stream_4.return)) yield _b.call(stream_4);
|
|
175
|
+
}
|
|
176
|
+
finally { if (e_4) throw e_4.error; }
|
|
177
|
+
}
|
|
178
|
+
resolve(collection);
|
|
179
|
+
}));
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
static counting() {
|
|
183
|
+
return (stream) => {
|
|
184
|
+
return future.Future.of((resolve, reject, signal) => _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
185
|
+
var _a, e_5, _b, _c;
|
|
186
|
+
let total = 0;
|
|
187
|
+
try {
|
|
188
|
+
for (var _d = true, stream_5 = _tslib.__asyncValues(stream), stream_5_1; stream_5_1 = yield stream_5.next(), _a = stream_5_1.done, !_a;) {
|
|
189
|
+
_c = stream_5_1.value;
|
|
190
|
+
_d = false;
|
|
191
|
+
try {
|
|
192
|
+
const _ = _c;
|
|
193
|
+
total++;
|
|
194
|
+
if (signal.aborted) {
|
|
195
|
+
reject(new AbortError.AbortError());
|
|
196
|
+
break;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
finally {
|
|
200
|
+
_d = true;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
|
205
|
+
finally {
|
|
206
|
+
try {
|
|
207
|
+
if (!_d && !_a && (_b = stream_5.return)) yield _b.call(stream_5);
|
|
208
|
+
}
|
|
209
|
+
finally { if (e_5) throw e_5.error; }
|
|
210
|
+
}
|
|
211
|
+
resolve(total);
|
|
212
|
+
}));
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
exports.Collectors = Collectors;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Stream } from "./index";
|
|
2
|
+
import { Future } from "../future";
|
|
3
|
+
import { Stack } from "../data-structures/array";
|
|
4
|
+
import { SimpleQueue } from "../data-structures/object";
|
|
5
|
+
export type Collector<T, K> = (stream: Stream<K>) => Future<T>;
|
|
6
|
+
export declare class Collectors {
|
|
7
|
+
static read<K>(functor: (value: K) => void | Future<void>): (stream: Stream<K>) => Future<void>;
|
|
8
|
+
static asList(): <K>(stream: Stream<K>) => Future<K[]>;
|
|
9
|
+
static asSet(): <K>(stream: Stream<K>) => Future<Set<K>>;
|
|
10
|
+
static joining(delimiter?: string): <K>(stream: Stream<K>) => Future<string>;
|
|
11
|
+
static asStack(maxSize?: number | undefined): <K>(stream: Stream<K>) => Future<Stack<K>>;
|
|
12
|
+
static asQueue(): <K>(stream: Stream<K>) => Future<SimpleQueue<K>>;
|
|
13
|
+
static counting(): <K>(stream: Stream<K>) => Future<number>;
|
|
14
|
+
}
|