evg_observable 2.14.60 → 2.15.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/BREAKING_CHANGES.md +70 -0
- package/package.json +12 -6
- package/src/outLib/src/Libraries/Observables/AbstractSwitchCase.d.ts +37 -0
- package/src/outLib/src/Libraries/Observables/AbstractSwitchCase.js +61 -0
- package/src/outLib/src/Libraries/Observables/Collector.d.ts +56 -0
- package/src/outLib/src/Libraries/Observables/Collector.js +86 -0
- package/src/outLib/src/Libraries/Observables/FilterCollection.d.ts +70 -0
- package/src/outLib/src/Libraries/Observables/FilterCollection.js +122 -0
- package/src/outLib/src/Libraries/Observables/FunctionLibs.d.ts +48 -0
- package/src/outLib/src/Libraries/Observables/FunctionLibs.js +101 -0
- package/src/outLib/src/Libraries/Observables/Observable.d.ts +160 -0
- package/src/outLib/src/Libraries/Observables/Observable.js +268 -0
- package/src/outLib/src/Libraries/Observables/OrderedObservable.d.ts +70 -0
- package/src/outLib/src/Libraries/Observables/OrderedObservable.js +106 -0
- package/src/outLib/src/Libraries/Observables/OrderedSubscribeObject.d.ts +53 -0
- package/src/outLib/src/Libraries/Observables/OrderedSubscribeObject.js +72 -0
- package/src/outLib/src/Libraries/Observables/Pipe.d.ts +108 -0
- package/src/outLib/src/Libraries/Observables/Pipe.js +161 -0
- package/src/outLib/src/Libraries/Observables/SubscribeObject.d.ts +83 -0
- package/src/outLib/src/Libraries/Observables/SubscribeObject.js +139 -0
- package/src/outLib/src/Libraries/Observables/Types.d.ts +727 -0
- package/src/outLib/{index.d.ts → src/Libraries/Observables/index.d.ts} +1 -0
- package/repo/evg_observable.js +0 -1
- package/src/outLib/AbstractSwitchCase.d.ts +0 -8
- package/src/outLib/AbstractSwitchCase.js +0 -32
- package/src/outLib/Collector.d.ts +0 -11
- package/src/outLib/Collector.js +0 -39
- package/src/outLib/FilterCollection.d.ts +0 -17
- package/src/outLib/FilterCollection.js +0 -68
- package/src/outLib/FunctionLibs.d.ts +0 -6
- package/src/outLib/FunctionLibs.js +0 -54
- package/src/outLib/Observable.d.ts +0 -29
- package/src/outLib/Observable.js +0 -130
- package/src/outLib/OrderedObservable.d.ts +0 -11
- package/src/outLib/OrderedObservable.js +0 -47
- package/src/outLib/OrderedSubscribeObject.d.ts +0 -10
- package/src/outLib/OrderedSubscribeObject.js +0 -29
- package/src/outLib/Pipe.d.ts +0 -20
- package/src/outLib/Pipe.js +0 -79
- package/src/outLib/SubscribeObject.d.ts +0 -19
- package/src/outLib/SubscribeObject.js +0 -68
- package/src/outLib/Types.d.ts +0 -165
- /package/src/outLib/{Types.js → src/Libraries/Observables/Types.js} +0 -0
- /package/src/outLib/{index.js → src/Libraries/Observables/index.js} +0 -0
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OrderedObservable = void 0;
|
|
4
|
+
const Observable_1 = require("./Observable");
|
|
5
|
+
const FunctionLibs_1 = require("./FunctionLibs");
|
|
6
|
+
const OrderedSubscribeObject_1 = require("./OrderedSubscribeObject");
|
|
7
|
+
/**
|
|
8
|
+
* Represents an observable data structure that maintains elements in a specific order and provides
|
|
9
|
+
* methods to manipulate and subscribe to its elements.
|
|
10
|
+
*
|
|
11
|
+
* The `OrderedObservable` class supports sorting elements in ascending or descending order and allows
|
|
12
|
+
* for subscription to changes in its state. It extends the capabilities of a basic `Observable` by
|
|
13
|
+
* incorporating sorting and ordered processing mechanisms.
|
|
14
|
+
*
|
|
15
|
+
* @template T The type of data stored and handled by the observable.
|
|
16
|
+
* @extends Observable<T>
|
|
17
|
+
* @implements IOrdered<T>
|
|
18
|
+
*/
|
|
19
|
+
class OrderedObservable extends Observable_1.Observable {
|
|
20
|
+
/**
|
|
21
|
+
* Represents the direction in which sorting should occur.
|
|
22
|
+
* The value is expected to indicate whether the sorting
|
|
23
|
+
* should be performed in ascending or descending order.
|
|
24
|
+
*
|
|
25
|
+
* Possible values:
|
|
26
|
+
* - `sortAscending`: Sorting in ascending order.
|
|
27
|
+
* - `sortDescending`: Sorting in descending order.
|
|
28
|
+
*/
|
|
29
|
+
sortDirection = FunctionLibs_1.sortAscending;
|
|
30
|
+
/**
|
|
31
|
+
* Sets the sorting order to ascending and applies the sorting.
|
|
32
|
+
*
|
|
33
|
+
* @return {boolean} Returns true if the sorting operation is successful, otherwise false.
|
|
34
|
+
*/
|
|
35
|
+
setAscendingSort() {
|
|
36
|
+
this.sortDirection = FunctionLibs_1.sortAscending;
|
|
37
|
+
return this.sortByOrder();
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Sets the sorting order to descending and updates the sort configuration.
|
|
41
|
+
*
|
|
42
|
+
* @return {boolean} Returns `true` if the sorting operation is configured and applied successfully, otherwise returns `false`.
|
|
43
|
+
*/
|
|
44
|
+
setDescendingSort() {
|
|
45
|
+
this.sortDirection = FunctionLibs_1.sortDescending;
|
|
46
|
+
return this.sortByOrder();
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Sorts the `subs` array based on the specified sort direction if the object is not in a "killed" state.
|
|
50
|
+
*
|
|
51
|
+
* @return {boolean} Returns true if the sorting was performed, false if the object is in a "killed" state.
|
|
52
|
+
*/
|
|
53
|
+
sortByOrder() {
|
|
54
|
+
if (this.killed)
|
|
55
|
+
return false;
|
|
56
|
+
this.subs.sort(this.sortDirection);
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Subscribes a listener to this instance with an optional error handler.
|
|
61
|
+
*
|
|
62
|
+
* @param {ISubscribeGroup<T>} listener - The listener object that should be notified of changes.
|
|
63
|
+
* @param {IErrorCallback} [errorHandler] - An optional handler to process errors during execution.
|
|
64
|
+
* @return {IOrderedSubscriptionLike | undefined} Returns an instance of IOrderedSubscriptionLike if the listener is valid; otherwise, returns undefined.
|
|
65
|
+
*/
|
|
66
|
+
subscribe(listener, errorHandler) {
|
|
67
|
+
if (!this.isListener(listener))
|
|
68
|
+
return undefined;
|
|
69
|
+
const subscribeObject = new OrderedSubscribeObject_1.OrderedSubscribeObject(this, false);
|
|
70
|
+
this.addObserver(subscribeObject, listener, errorHandler);
|
|
71
|
+
return subscribeObject;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Creates and returns an instance of `OrderedSubscribeObject` tied to the current object,
|
|
75
|
+
* facilitating ordered subscription setup. If the instance is marked as killed, it returns undefined.
|
|
76
|
+
*
|
|
77
|
+
* @return {IOrderedSetup<T> | undefined} An instance of `OrderedSubscribeObject` for subscription setup,
|
|
78
|
+
* or undefined if the operation is not allowed.
|
|
79
|
+
*/
|
|
80
|
+
pipe() {
|
|
81
|
+
if (this.killed)
|
|
82
|
+
return undefined;
|
|
83
|
+
const subscribeObject = new OrderedSubscribeObject_1.OrderedSubscribeObject(this, true);
|
|
84
|
+
this.subs.push(subscribeObject);
|
|
85
|
+
return subscribeObject;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Removes a previously subscribed listener from the subscription list,
|
|
89
|
+
* preventing it from receiving further updates. If the system is in a
|
|
90
|
+
* "killed" state or specific conditions in the process flow are met,
|
|
91
|
+
* the listener may instead be added to a trash list.
|
|
92
|
+
*
|
|
93
|
+
* @param {ISubscriptionLike} listener - The subscription listener to be unsubscribed or handled accordingly.
|
|
94
|
+
* @return {void} Does not return any value.
|
|
95
|
+
*/
|
|
96
|
+
unSubscribe(listener) {
|
|
97
|
+
if (this.killed)
|
|
98
|
+
return;
|
|
99
|
+
if (this.process && listener) {
|
|
100
|
+
this.trash.push(listener);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
this.subs && !(0, FunctionLibs_1.quickDeleteFromArray)(this.subs, listener);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
exports.OrderedObservable = OrderedObservable;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { SubscribeObject } from "./SubscribeObject";
|
|
2
|
+
import { IErrorCallback, IListener, IOrdered, IOrderedSetup, IOrderedSubscribe, IOrderedSubscriptionLike, ISetObservableValue } from "./Types";
|
|
3
|
+
import { OrderedObservable } from "./OrderedObservable";
|
|
4
|
+
/**
|
|
5
|
+
* Represents an ordered subscription object, extending the functionality
|
|
6
|
+
* of the SubscribeObject to include ordering and the ability to manage
|
|
7
|
+
* subscriptions based on a given order.
|
|
8
|
+
*
|
|
9
|
+
* This class is designed to work with observables that support ordering
|
|
10
|
+
* functionality. It implements the `IOrderedSetup` interface to manage
|
|
11
|
+
* ordering behavior and sorting operations.
|
|
12
|
+
*
|
|
13
|
+
* @template T The type of the data managed by the subscription.
|
|
14
|
+
*/
|
|
15
|
+
export declare class OrderedSubscribeObject<T> extends SubscribeObject<T> implements IOrderedSetup<T> {
|
|
16
|
+
/**
|
|
17
|
+
* Creates an instance of the constructor with the specified observable and pipe flag.
|
|
18
|
+
*
|
|
19
|
+
* @param {OrderedObservable<T> | IOrdered<T>} observable - The ordered observable or IOrdered instance to attach to this instance.
|
|
20
|
+
* @param {boolean} [isPipe] - Optional flag indicating if the stream is part of a piping sequence.
|
|
21
|
+
* @return {void}
|
|
22
|
+
*/
|
|
23
|
+
constructor(observable: OrderedObservable<T> | IOrdered<T>, isPipe?: boolean);
|
|
24
|
+
/**
|
|
25
|
+
* Retrieves the order value.
|
|
26
|
+
*
|
|
27
|
+
* @return {number} The current order value.
|
|
28
|
+
*/
|
|
29
|
+
get order(): number;
|
|
30
|
+
/**
|
|
31
|
+
* Sets the order value for this object. If the observer is not defined or is destroyed,
|
|
32
|
+
* the order value is reset to undefined. Otherwise, the order value is updated, and
|
|
33
|
+
* the observer is instructed to sort items by the updated order.
|
|
34
|
+
*
|
|
35
|
+
* @param {number} value - The new order value to be set.
|
|
36
|
+
*/
|
|
37
|
+
set order(value: number);
|
|
38
|
+
/**
|
|
39
|
+
* Subscribes an observer to the observable, allowing it to receive updates.
|
|
40
|
+
*
|
|
41
|
+
* @param {IListener<T> | ISetObservableValue} observer - The observer that will receive updates. It can be either a listener function or a setter for the observable value.
|
|
42
|
+
* @param {IErrorCallback} [errorHandler] - An optional error callback that will be invoked if an error occurs during the subscription process.
|
|
43
|
+
* @return {IOrderedSubscriptionLike} The current subscription instance for chaining purposes.
|
|
44
|
+
*/
|
|
45
|
+
subscribe(observer: IListener<T> | ISetObservableValue, errorHandler?: IErrorCallback): IOrderedSubscriptionLike;
|
|
46
|
+
/**
|
|
47
|
+
* Sets the subscription to be invoked only once. After the subscription
|
|
48
|
+
* is called for the first time, it will be automatically removed.
|
|
49
|
+
*
|
|
50
|
+
* @return {IOrderedSubscribe<T>} The subscription instance configured to execute only once.
|
|
51
|
+
*/
|
|
52
|
+
setOnce(): IOrderedSubscribe<T>;
|
|
53
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OrderedSubscribeObject = void 0;
|
|
4
|
+
const SubscribeObject_1 = require("./SubscribeObject");
|
|
5
|
+
/**
|
|
6
|
+
* Represents an ordered subscription object, extending the functionality
|
|
7
|
+
* of the SubscribeObject to include ordering and the ability to manage
|
|
8
|
+
* subscriptions based on a given order.
|
|
9
|
+
*
|
|
10
|
+
* This class is designed to work with observables that support ordering
|
|
11
|
+
* functionality. It implements the `IOrderedSetup` interface to manage
|
|
12
|
+
* ordering behavior and sorting operations.
|
|
13
|
+
*
|
|
14
|
+
* @template T The type of the data managed by the subscription.
|
|
15
|
+
*/
|
|
16
|
+
class OrderedSubscribeObject extends SubscribeObject_1.SubscribeObject {
|
|
17
|
+
/**
|
|
18
|
+
* Creates an instance of the constructor with the specified observable and pipe flag.
|
|
19
|
+
*
|
|
20
|
+
* @param {OrderedObservable<T> | IOrdered<T>} observable - The ordered observable or IOrdered instance to attach to this instance.
|
|
21
|
+
* @param {boolean} [isPipe] - Optional flag indicating if the stream is part of a piping sequence.
|
|
22
|
+
* @return {void}
|
|
23
|
+
*/
|
|
24
|
+
constructor(observable, isPipe) {
|
|
25
|
+
super(observable, isPipe);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Retrieves the order value.
|
|
29
|
+
*
|
|
30
|
+
* @return {number} The current order value.
|
|
31
|
+
*/
|
|
32
|
+
get order() {
|
|
33
|
+
return this._order;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Sets the order value for this object. If the observer is not defined or is destroyed,
|
|
37
|
+
* the order value is reset to undefined. Otherwise, the order value is updated, and
|
|
38
|
+
* the observer is instructed to sort items by the updated order.
|
|
39
|
+
*
|
|
40
|
+
* @param {number} value - The new order value to be set.
|
|
41
|
+
*/
|
|
42
|
+
set order(value) {
|
|
43
|
+
if (!this.observer ||
|
|
44
|
+
(this.observer && this.observer.isDestroyed)) {
|
|
45
|
+
this._order = undefined;
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
this._order = value;
|
|
49
|
+
this.observer.sortByOrder();
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Subscribes an observer to the observable, allowing it to receive updates.
|
|
53
|
+
*
|
|
54
|
+
* @param {IListener<T> | ISetObservableValue} observer - The observer that will receive updates. It can be either a listener function or a setter for the observable value.
|
|
55
|
+
* @param {IErrorCallback} [errorHandler] - An optional error callback that will be invoked if an error occurs during the subscription process.
|
|
56
|
+
* @return {IOrderedSubscriptionLike} The current subscription instance for chaining purposes.
|
|
57
|
+
*/
|
|
58
|
+
subscribe(observer, errorHandler) {
|
|
59
|
+
super.subscribe(observer, errorHandler);
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Sets the subscription to be invoked only once. After the subscription
|
|
64
|
+
* is called for the first time, it will be automatically removed.
|
|
65
|
+
*
|
|
66
|
+
* @return {IOrderedSubscribe<T>} The subscription instance configured to execute only once.
|
|
67
|
+
*/
|
|
68
|
+
setOnce() {
|
|
69
|
+
return super.setOnce();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
exports.OrderedSubscribeObject = OrderedSubscribeObject;
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { ICallback, IChainCallback, IErrorCallback, IListener, IPipeCase, IPipePayload, ISetObservableValue, ISetup, ISubscribe, ISubscriptionLike } from "./Types";
|
|
2
|
+
import { SwitchCase } from "./AbstractSwitchCase";
|
|
3
|
+
/**
|
|
4
|
+
* An abstract class that provides a flexible pipeline mechanism to process and transform streamed data.
|
|
5
|
+
* The `Pipe` class allows chaining of operations, conditional transformations, and controlled subscription handling.
|
|
6
|
+
*
|
|
7
|
+
* @template T The type of data handled by this pipeline.
|
|
8
|
+
*/
|
|
9
|
+
export declare abstract class Pipe<T> implements ISubscribe<T> {
|
|
10
|
+
chain: IChainCallback[];
|
|
11
|
+
flow: IPipePayload;
|
|
12
|
+
/**
|
|
13
|
+
* Subscribes a listener to observe changes or updates. Can optionally handle errors during the subscription process.
|
|
14
|
+
*
|
|
15
|
+
* @param {IListener<T> | ISetObservableValue} listener - The listener or handler that will receive notifications of updates.
|
|
16
|
+
* @param {IErrorCallback} [errorHandler] - An optional callback to handle errors that occur during subscription.
|
|
17
|
+
* @return {ISubscriptionLike | undefined} An object representing the subscription, or undefined if the subscription could not be created.
|
|
18
|
+
*/
|
|
19
|
+
abstract subscribe(listener: IListener<T> | ISetObservableValue, errorHandler?: IErrorCallback): ISubscriptionLike | undefined;
|
|
20
|
+
private push;
|
|
21
|
+
/**
|
|
22
|
+
* Subscribes to an event and ensures the listener is called only once.
|
|
23
|
+
* After the listener is invoked, it unsubscribes automatically.
|
|
24
|
+
*
|
|
25
|
+
* @return {ISubscribe<T>} The subscription instance allowing further chaining or management.
|
|
26
|
+
*/
|
|
27
|
+
setOnce(): ISubscribe<T>;
|
|
28
|
+
/**
|
|
29
|
+
* Unsubscribes based on a given condition. The condition is evaluated against
|
|
30
|
+
* the payload, and if the condition returns true, the subscription is marked
|
|
31
|
+
* for unsubscription.
|
|
32
|
+
*
|
|
33
|
+
* @param {ICallback<T>} condition - A callback function that determines whether a
|
|
34
|
+
* subscription should be unsubscribed. It receives the payload as an argument
|
|
35
|
+
* and should return a boolean value.
|
|
36
|
+
* @return {ISetup<T>} The current setup instance for chaining purposes.
|
|
37
|
+
*/
|
|
38
|
+
unsubscribeBy(condition: ICallback<T>): ISetup<T>;
|
|
39
|
+
/**
|
|
40
|
+
* Applies a refinement condition to the workflow pipeline.
|
|
41
|
+
*
|
|
42
|
+
* @param {ICallback<T>} condition - A callback function that evaluates a condition
|
|
43
|
+
* on the payload data and returns a boolean.
|
|
44
|
+
* @return {ISetup<T>} Returns the updated setup with the refined condition applied.
|
|
45
|
+
*/
|
|
46
|
+
refine(condition: ICallback<T>): ISetup<T>;
|
|
47
|
+
/**
|
|
48
|
+
* Adds an array of conditions to be processed by the refinement function.
|
|
49
|
+
*
|
|
50
|
+
* @param {ICallback<any>[]} conditions - An array of callback functions to be refined.
|
|
51
|
+
* @return {ISetup<T>} The current setup instance after processing the conditions.
|
|
52
|
+
*/
|
|
53
|
+
pushRefiners(conditions: ICallback<any>[]): ISetup<T>;
|
|
54
|
+
/**
|
|
55
|
+
* Creates and returns a new instance of the PipeSwitchCase class,
|
|
56
|
+
* enabling conditional logic or case-switch handling for the current context.
|
|
57
|
+
*
|
|
58
|
+
* @return {PipeSwitchCase<T>} A new instance of PipeSwitchCase associated with the current context.
|
|
59
|
+
*/
|
|
60
|
+
switch(): PipeSwitchCase<T>;
|
|
61
|
+
/**
|
|
62
|
+
* Registers a condition callback to be executed within the pipeline and modifies the payload of the current data.
|
|
63
|
+
* The condition is applied to the current payload and sets a flag indicating its availability.
|
|
64
|
+
*
|
|
65
|
+
* @param {ICallback<T>} condition - The callback function to execute on the current payload. It processes the payload and returns a new value.
|
|
66
|
+
* @return {ISetup<K>} An instance of the setup interface, allowing further chaining of operations.
|
|
67
|
+
*/
|
|
68
|
+
then<K>(condition: ICallback<T>): ISetup<K>;
|
|
69
|
+
/**
|
|
70
|
+
* Serializes the payload of the given data object into a JSON string and
|
|
71
|
+
* sets the `isAvailable` property to true.
|
|
72
|
+
*
|
|
73
|
+
* @return {ISetup<string>} The modified setup instance with the serialized payload.
|
|
74
|
+
*/
|
|
75
|
+
serialize(): ISetup<string>;
|
|
76
|
+
/**
|
|
77
|
+
* Deserializes the payload of the provided data into a JavaScript object using JSON.parse
|
|
78
|
+
* and marks the data as available.
|
|
79
|
+
*
|
|
80
|
+
* @template K - The type of the setup to be returned.
|
|
81
|
+
* @return {ISetup<K>} The setup instance after deserializing the payload.
|
|
82
|
+
*/
|
|
83
|
+
deserialize<K>(): ISetup<K>;
|
|
84
|
+
/**
|
|
85
|
+
* Processes a chain of functions with the given listener and flow data.
|
|
86
|
+
*
|
|
87
|
+
* @param {IListener<T>} listener - The listener to be executed after the chain is processed.
|
|
88
|
+
* Receives the payload of the flow data.
|
|
89
|
+
* @return {void} This method does not return a value.
|
|
90
|
+
*/
|
|
91
|
+
processChain(listener: IListener<T>): void;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* The `PipeSwitchCase` class extends the functionality of the `SwitchCase`
|
|
95
|
+
* class, tailored for use with `Pipe` objects. It provides a mechanism for
|
|
96
|
+
* managing and subscribing to a pipeline of transformations or processes
|
|
97
|
+
* conditioned by specific cases.
|
|
98
|
+
*
|
|
99
|
+
* This class also implements `ISubscribe` to allow subscription to the
|
|
100
|
+
* underlying pipeline for observing or reacting to its events or values.
|
|
101
|
+
*
|
|
102
|
+
* @template T The type of the data being handled by the `PipeSwitchCase`.
|
|
103
|
+
* @extends {SwitchCase<T, Pipe<T>, IPipeCase<T>>}
|
|
104
|
+
* @implements {ISubscribe<T>}
|
|
105
|
+
*/
|
|
106
|
+
export declare class PipeSwitchCase<T> extends SwitchCase<T, Pipe<T>, IPipeCase<T>> implements ISubscribe<T> {
|
|
107
|
+
subscribe(listener: IListener<T> | ISetObservableValue, errorHandler?: IErrorCallback): ISubscriptionLike | undefined;
|
|
108
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PipeSwitchCase = exports.Pipe = void 0;
|
|
4
|
+
const AbstractSwitchCase_1 = require("./AbstractSwitchCase");
|
|
5
|
+
/**
|
|
6
|
+
* An abstract class that provides a flexible pipeline mechanism to process and transform streamed data.
|
|
7
|
+
* The `Pipe` class allows chaining of operations, conditional transformations, and controlled subscription handling.
|
|
8
|
+
*
|
|
9
|
+
* @template T The type of data handled by this pipeline.
|
|
10
|
+
*/
|
|
11
|
+
class Pipe {
|
|
12
|
+
chain = [];
|
|
13
|
+
flow = { isBreak: false, isUnsubscribe: false, isAvailable: false, payload: null };
|
|
14
|
+
push(callback) {
|
|
15
|
+
this.chain.push(callback);
|
|
16
|
+
return this;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Subscribes to an event and ensures the listener is called only once.
|
|
20
|
+
* After the listener is invoked, it unsubscribes automatically.
|
|
21
|
+
*
|
|
22
|
+
* @return {ISubscribe<T>} The subscription instance allowing further chaining or management.
|
|
23
|
+
*/
|
|
24
|
+
setOnce() {
|
|
25
|
+
return this.push((data) => {
|
|
26
|
+
this.listener(data.payload);
|
|
27
|
+
data.isUnsubscribe = true;
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Unsubscribes based on a given condition. The condition is evaluated against
|
|
32
|
+
* the payload, and if the condition returns true, the subscription is marked
|
|
33
|
+
* for unsubscription.
|
|
34
|
+
*
|
|
35
|
+
* @param {ICallback<T>} condition - A callback function that determines whether a
|
|
36
|
+
* subscription should be unsubscribed. It receives the payload as an argument
|
|
37
|
+
* and should return a boolean value.
|
|
38
|
+
* @return {ISetup<T>} The current setup instance for chaining purposes.
|
|
39
|
+
*/
|
|
40
|
+
unsubscribeBy(condition) {
|
|
41
|
+
return this.push((data) => {
|
|
42
|
+
data.isAvailable = true;
|
|
43
|
+
if (condition(data.payload))
|
|
44
|
+
data.isUnsubscribe = true;
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Applies a refinement condition to the workflow pipeline.
|
|
49
|
+
*
|
|
50
|
+
* @param {ICallback<T>} condition - A callback function that evaluates a condition
|
|
51
|
+
* on the payload data and returns a boolean.
|
|
52
|
+
* @return {ISetup<T>} Returns the updated setup with the refined condition applied.
|
|
53
|
+
*/
|
|
54
|
+
refine(condition) {
|
|
55
|
+
return this.push((data) => condition(data.payload) && (data.isAvailable = true));
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Adds an array of conditions to be processed by the refinement function.
|
|
59
|
+
*
|
|
60
|
+
* @param {ICallback<any>[]} conditions - An array of callback functions to be refined.
|
|
61
|
+
* @return {ISetup<T>} The current setup instance after processing the conditions.
|
|
62
|
+
*/
|
|
63
|
+
pushRefiners(conditions) {
|
|
64
|
+
if (!Array.isArray(conditions))
|
|
65
|
+
return this;
|
|
66
|
+
for (let i = 0; i < conditions.length; i++)
|
|
67
|
+
this.refine(conditions[i]);
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Creates and returns a new instance of the PipeSwitchCase class,
|
|
72
|
+
* enabling conditional logic or case-switch handling for the current context.
|
|
73
|
+
*
|
|
74
|
+
* @return {PipeSwitchCase<T>} A new instance of PipeSwitchCase associated with the current context.
|
|
75
|
+
*/
|
|
76
|
+
switch() {
|
|
77
|
+
return new PipeSwitchCase(this);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Registers a condition callback to be executed within the pipeline and modifies the payload of the current data.
|
|
81
|
+
* The condition is applied to the current payload and sets a flag indicating its availability.
|
|
82
|
+
*
|
|
83
|
+
* @param {ICallback<T>} condition - The callback function to execute on the current payload. It processes the payload and returns a new value.
|
|
84
|
+
* @return {ISetup<K>} An instance of the setup interface, allowing further chaining of operations.
|
|
85
|
+
*/
|
|
86
|
+
then(condition) {
|
|
87
|
+
return this.push((data) => {
|
|
88
|
+
data.payload = condition(data.payload);
|
|
89
|
+
data.isAvailable = true;
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Serializes the payload of the given data object into a JSON string and
|
|
94
|
+
* sets the `isAvailable` property to true.
|
|
95
|
+
*
|
|
96
|
+
* @return {ISetup<string>} The modified setup instance with the serialized payload.
|
|
97
|
+
*/
|
|
98
|
+
serialize() {
|
|
99
|
+
return this.push((data) => {
|
|
100
|
+
data.payload = JSON.stringify(data.payload);
|
|
101
|
+
data.isAvailable = true;
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Deserializes the payload of the provided data into a JavaScript object using JSON.parse
|
|
106
|
+
* and marks the data as available.
|
|
107
|
+
*
|
|
108
|
+
* @template K - The type of the setup to be returned.
|
|
109
|
+
* @return {ISetup<K>} The setup instance after deserializing the payload.
|
|
110
|
+
*/
|
|
111
|
+
deserialize() {
|
|
112
|
+
return this.push((data) => {
|
|
113
|
+
data.payload = JSON.parse(data.payload);
|
|
114
|
+
data.isAvailable = true;
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Processes a chain of functions with the given listener and flow data.
|
|
119
|
+
*
|
|
120
|
+
* @param {IListener<T>} listener - The listener to be executed after the chain is processed.
|
|
121
|
+
* Receives the payload of the flow data.
|
|
122
|
+
* @return {void} This method does not return a value.
|
|
123
|
+
*/
|
|
124
|
+
processChain(listener) {
|
|
125
|
+
const chain = this.chain;
|
|
126
|
+
const data = this.flow;
|
|
127
|
+
const len = chain.length;
|
|
128
|
+
for (let i = 0; i < len; i++) {
|
|
129
|
+
data.isUnsubscribe = false;
|
|
130
|
+
data.isAvailable = false;
|
|
131
|
+
chain[i](data);
|
|
132
|
+
if (data.isUnsubscribe)
|
|
133
|
+
return this.unsubscribe();
|
|
134
|
+
if (!data.isAvailable)
|
|
135
|
+
return;
|
|
136
|
+
if (data.isBreak)
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
return listener(data.payload);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
exports.Pipe = Pipe;
|
|
143
|
+
/**
|
|
144
|
+
* The `PipeSwitchCase` class extends the functionality of the `SwitchCase`
|
|
145
|
+
* class, tailored for use with `Pipe` objects. It provides a mechanism for
|
|
146
|
+
* managing and subscribing to a pipeline of transformations or processes
|
|
147
|
+
* conditioned by specific cases.
|
|
148
|
+
*
|
|
149
|
+
* This class also implements `ISubscribe` to allow subscription to the
|
|
150
|
+
* underlying pipeline for observing or reacting to its events or values.
|
|
151
|
+
*
|
|
152
|
+
* @template T The type of the data being handled by the `PipeSwitchCase`.
|
|
153
|
+
* @extends {SwitchCase<T, Pipe<T>, IPipeCase<T>>}
|
|
154
|
+
* @implements {ISubscribe<T>}
|
|
155
|
+
*/
|
|
156
|
+
class PipeSwitchCase extends AbstractSwitchCase_1.SwitchCase {
|
|
157
|
+
subscribe(listener, errorHandler) {
|
|
158
|
+
return this.pipe.subscribe(listener, errorHandler);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
exports.PipeSwitchCase = PipeSwitchCase;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { IErrorCallback, IListener, IObserver, ISubscribeGroup, ISubscribeObject, ISubscriptionLike } from "./Types";
|
|
2
|
+
import { Pipe } from "./Pipe";
|
|
3
|
+
/**
|
|
4
|
+
* A class that represents an observable object with subscription, pausing,
|
|
5
|
+
* and piping functionalities. It allows subscribing to updates, handling errors,
|
|
6
|
+
* and processing values through a chain.
|
|
7
|
+
*
|
|
8
|
+
* @template T The type of values handled by SubscribeObject.
|
|
9
|
+
* @extends Pipe<T>
|
|
10
|
+
* @implements ISubscribeObject<T>
|
|
11
|
+
*/
|
|
12
|
+
export declare class SubscribeObject<T> extends Pipe<T> implements ISubscribeObject<T> {
|
|
13
|
+
observer: IObserver<T> | undefined;
|
|
14
|
+
listener: IListener<T> | undefined;
|
|
15
|
+
/**
|
|
16
|
+
* A callback function used for handling errors in the context of the SubscribeObject.
|
|
17
|
+
* This function logs the provided error data and error message to the console for debugging purposes.
|
|
18
|
+
*
|
|
19
|
+
* @type {IErrorCallback}
|
|
20
|
+
* @param {any} errorData - The data related to the error encountered.
|
|
21
|
+
* @param {any} errorMessage - A descriptive message detailing the error.
|
|
22
|
+
*/
|
|
23
|
+
errorHandler: IErrorCallback;
|
|
24
|
+
_order: number;
|
|
25
|
+
paused: boolean;
|
|
26
|
+
piped: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Constructs an instance of the class.
|
|
29
|
+
*
|
|
30
|
+
* @param {IObserver<T>} [observable] - The observer instance to be assigned. Optional parameter.
|
|
31
|
+
* @param {boolean} [isPipe=false] - Determines whether the instance is piped. Defaults to false.
|
|
32
|
+
* @return {void}
|
|
33
|
+
*/
|
|
34
|
+
constructor(observable?: IObserver<T>, isPipe?: boolean);
|
|
35
|
+
/**
|
|
36
|
+
* Subscribes an observer to the current instance and optionally assigns an error handler.
|
|
37
|
+
*
|
|
38
|
+
* @param {ISubscribeGroup<T>} observer - The observer group to subscribe.
|
|
39
|
+
* @param {IErrorCallback} [errorHandler] - Optional callback to handle errors.
|
|
40
|
+
* @return {ISubscriptionLike} An instance representing the subscription.
|
|
41
|
+
*/
|
|
42
|
+
subscribe(observer: ISubscribeGroup<T>, errorHandler?: IErrorCallback): ISubscriptionLike;
|
|
43
|
+
/**
|
|
44
|
+
* Unsubscribes the current instance from the associated observer, clears the listener,
|
|
45
|
+
* and resets the internal chain.
|
|
46
|
+
* This method ensures that the instance is properly cleaned up and no longer receives updates.
|
|
47
|
+
*
|
|
48
|
+
* @return {void} Does not return a value.
|
|
49
|
+
*/
|
|
50
|
+
unsubscribe(): void;
|
|
51
|
+
/**
|
|
52
|
+
* Sends the specified value for processing and updates the flow state.
|
|
53
|
+
*
|
|
54
|
+
* @param {T} value - The value to be sent and processed.
|
|
55
|
+
* @return {void} Does not return a value.
|
|
56
|
+
*/
|
|
57
|
+
send(value: T): void;
|
|
58
|
+
/**
|
|
59
|
+
* Resumes the current process or operation from a paused state.
|
|
60
|
+
* Updates the internal state to indicate that it is no longer paused.
|
|
61
|
+
*
|
|
62
|
+
* @return {void} Does not return a value.
|
|
63
|
+
*/
|
|
64
|
+
resume(): void;
|
|
65
|
+
/**
|
|
66
|
+
* Pauses the current operation or process by setting the paused state to true.
|
|
67
|
+
*
|
|
68
|
+
* @return {void} No value is returned.
|
|
69
|
+
*/
|
|
70
|
+
pause(): void;
|
|
71
|
+
/**
|
|
72
|
+
* Retrieves the current order value.
|
|
73
|
+
*
|
|
74
|
+
* @return {number} The current value of the order.
|
|
75
|
+
*/
|
|
76
|
+
get order(): number;
|
|
77
|
+
/**
|
|
78
|
+
* Sets the order value.
|
|
79
|
+
*
|
|
80
|
+
* @param {number} value - The numerical value to set as the order.
|
|
81
|
+
*/
|
|
82
|
+
set order(value: number);
|
|
83
|
+
}
|