evg_observable 2.14.61 → 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,101 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.sortAscending = sortAscending;
|
|
4
|
+
exports.sortDescending = sortDescending;
|
|
5
|
+
exports.deleteFromArray = deleteFromArray;
|
|
6
|
+
exports.quickDeleteFromArray = quickDeleteFromArray;
|
|
7
|
+
exports.getListener = getListener;
|
|
8
|
+
/**
|
|
9
|
+
* Compares two ISubscribeObject items based on their `order` property
|
|
10
|
+
* and sorts them in ascending order.
|
|
11
|
+
*
|
|
12
|
+
* @param {ISubscribeObject<any>} a - The first object to compare.
|
|
13
|
+
* @param {ISubscribeObject<any>} b - The second object to compare.
|
|
14
|
+
* @return {number} - Returns 1 if the `order` property of `a` is greater than `b`,
|
|
15
|
+
* -1 if the `order` property of `a` is less than `b`,
|
|
16
|
+
* or 0 if they are equal.
|
|
17
|
+
*/
|
|
18
|
+
function sortAscending(a, b) {
|
|
19
|
+
if (a.order > b.order)
|
|
20
|
+
return 1;
|
|
21
|
+
if (a.order < b.order)
|
|
22
|
+
return -1;
|
|
23
|
+
return 0;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Compares two objects based on their `order` property for descending sorting.
|
|
27
|
+
*
|
|
28
|
+
* @param {ISubscribeObject<any>} a - The first object to compare, expected to have an `order` property.
|
|
29
|
+
* @param {ISubscribeObject<any>} b - The second object to compare, expected to have an `order` property.
|
|
30
|
+
* @return {number} - Returns -1 if `a.order` is greater than `b.order`, 1 if `a.order` is less than `b.order`, and 0 if they are equal.
|
|
31
|
+
*/
|
|
32
|
+
function sortDescending(a, b) {
|
|
33
|
+
if (a.order > b.order)
|
|
34
|
+
return -1;
|
|
35
|
+
if (a.order < b.order)
|
|
36
|
+
return 1;
|
|
37
|
+
return 0;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Removes the specified component from the provided array if it exists.
|
|
41
|
+
*
|
|
42
|
+
* @param {T[]} arr - The array from which the component will be removed.
|
|
43
|
+
* @param {T} component - The component to be removed from the array.
|
|
44
|
+
* @return {boolean} Returns true if the component was successfully removed, false otherwise.
|
|
45
|
+
*/
|
|
46
|
+
function deleteFromArray(arr, component) {
|
|
47
|
+
const index = arr.indexOf(component);
|
|
48
|
+
if (index === -1)
|
|
49
|
+
return false;
|
|
50
|
+
arr.splice(index, 1);
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Removes a specified element from an array by replacing it with the last element
|
|
55
|
+
* and reducing the array's length. This method avoids maintaining order but performs
|
|
56
|
+
* the deletion operation efficiently.
|
|
57
|
+
*
|
|
58
|
+
* @param {T[]} arr - The array from which the element will be removed.
|
|
59
|
+
* @param {T} component - The element to be removed from the array.
|
|
60
|
+
* @return {boolean} - Returns true if the element was found and removed;
|
|
61
|
+
* otherwise, returns false.
|
|
62
|
+
*/
|
|
63
|
+
function quickDeleteFromArray(arr, component) {
|
|
64
|
+
const index = arr.indexOf(component);
|
|
65
|
+
if (index === -1)
|
|
66
|
+
return false;
|
|
67
|
+
arr[index] = arr[arr.length - 1];
|
|
68
|
+
arr.length--;
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Returns a listener function based on the provided listener group.
|
|
73
|
+
*
|
|
74
|
+
* @param {ISubscribeGroup<T>} listenerGroup - The group of listener(s). It can be a single listener
|
|
75
|
+
* or an array of listeners that are invoked when the returned listener is called.
|
|
76
|
+
* @return {IListener<T>} A single listener function that wraps the provided listener or group of listeners
|
|
77
|
+
* and invokes them with the provided data.
|
|
78
|
+
*/
|
|
79
|
+
function getListener(listenerGroup) {
|
|
80
|
+
if (Array.isArray(listenerGroup)) {
|
|
81
|
+
const group = [];
|
|
82
|
+
for (let i = 0; i < listenerGroup.length; i++)
|
|
83
|
+
group.push(wrapListener(listenerGroup[i]));
|
|
84
|
+
return (data) => {
|
|
85
|
+
for (let i = 0; i < group.length; i++)
|
|
86
|
+
group[i](data);
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
return wrapListener(listenerGroup);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Wraps the provided listener, ensuring it conforms to the IListener<T> interface.
|
|
93
|
+
*
|
|
94
|
+
* @param listener The listener to be wrapped. Can be either an IListener<T> or an ISetObservableValue.
|
|
95
|
+
* @return Returns a listener that adheres to the IListener<T> interface.
|
|
96
|
+
*/
|
|
97
|
+
function wrapListener(listener) {
|
|
98
|
+
if ("next" in listener)
|
|
99
|
+
return (value) => listener.next(value);
|
|
100
|
+
return listener;
|
|
101
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { IAddFilter, IErrorCallback, IFilterSetup, IObserver, ISetup, IStream, ISubscribeGroup, ISubscribeObject, ISubscriptionLike } from "./Types";
|
|
2
|
+
import { SubscribeObject } from "./SubscribeObject";
|
|
3
|
+
import { FilterCollection } from "./FilterCollection";
|
|
4
|
+
/**
|
|
5
|
+
* Observable is a generic class that represents an entity capable of emitting values
|
|
6
|
+
* over time to subscribers. It provides mechanisms for managing subscribers and controlling
|
|
7
|
+
* the flow of emitted data through filters and utility functions.
|
|
8
|
+
*
|
|
9
|
+
* @template T The type of the data managed by the Observable.
|
|
10
|
+
*
|
|
11
|
+
* Implements:
|
|
12
|
+
* - IObserver<T>: For observing value changes.
|
|
13
|
+
* - IStream<T>: For emitting streams of values.
|
|
14
|
+
* - IAddFilter<T>: For adding and managing filters in the value processing.
|
|
15
|
+
*
|
|
16
|
+
* The Observable class provides the following key features:
|
|
17
|
+
*
|
|
18
|
+
* - Allows subscribers to listen for emitted values.
|
|
19
|
+
* - Provides methods for enabling/disabling value emissions.
|
|
20
|
+
* - Supports applying filters to process or validate values before emission.
|
|
21
|
+
* - Allows streaming multiple values sequentially.
|
|
22
|
+
* - Supports safe termination of resources through unsubscription and destruction.
|
|
23
|
+
* - Handles asynchronous cleanup when active operations are in progress.
|
|
24
|
+
*/
|
|
25
|
+
export declare class Observable<T> implements IObserver<T>, IStream<T>, IAddFilter<T> {
|
|
26
|
+
protected subs: ISubscribeObject<T>[];
|
|
27
|
+
protected enabled: boolean;
|
|
28
|
+
protected killed: boolean;
|
|
29
|
+
protected process: boolean;
|
|
30
|
+
protected trash: ISubscriptionLike[];
|
|
31
|
+
protected filters: FilterCollection<T>;
|
|
32
|
+
protected _value: T;
|
|
33
|
+
constructor(value: T);
|
|
34
|
+
/**
|
|
35
|
+
* Adds an error handler filter to the current filter setup.
|
|
36
|
+
*
|
|
37
|
+
* @param {IErrorCallback} [errorHandler] - An optional error handler callback to handle errors in the filter.
|
|
38
|
+
* @return {IFilterSetup<T>} The updated filter setup object with the specified error handler added.
|
|
39
|
+
*/
|
|
40
|
+
addFilter(errorHandler?: IErrorCallback): IFilterSetup<T>;
|
|
41
|
+
/**
|
|
42
|
+
* Disables the current instance or functionality associated with this method.
|
|
43
|
+
* Updates the internal state to reflect that it is no longer active or enabled.
|
|
44
|
+
*
|
|
45
|
+
* @return {void} This method does not return a value.
|
|
46
|
+
*/
|
|
47
|
+
disable(): void;
|
|
48
|
+
/**
|
|
49
|
+
* Enables the current feature or functionality by setting its state to active.
|
|
50
|
+
* Updates the internal state to indicate that it is enabled.
|
|
51
|
+
*
|
|
52
|
+
* @return {void} No return value.
|
|
53
|
+
*/
|
|
54
|
+
enable(): void;
|
|
55
|
+
/**
|
|
56
|
+
* Indicates whether the current object or feature is enabled.
|
|
57
|
+
*
|
|
58
|
+
* @return {boolean} Returns true if enabled, otherwise false.
|
|
59
|
+
*/
|
|
60
|
+
get isEnable(): boolean;
|
|
61
|
+
/**
|
|
62
|
+
* Processes the given value and sends it to all subscribers, respecting the enabled status, filters, and processing state.
|
|
63
|
+
*
|
|
64
|
+
* @param {T} value - The value to be processed and passed to subscribers.
|
|
65
|
+
* @return {void} This method does not return a value.
|
|
66
|
+
*/
|
|
67
|
+
next(value: T): void;
|
|
68
|
+
/**
|
|
69
|
+
* Processes an array of values and triggers the next method for each value if the stream
|
|
70
|
+
* is not killed or disabled.
|
|
71
|
+
*
|
|
72
|
+
* @param {T[]} values - An array of values to be processed by the stream.
|
|
73
|
+
* @return {void} This method does not return a value.
|
|
74
|
+
*/
|
|
75
|
+
stream(values: T[]): void;
|
|
76
|
+
/**
|
|
77
|
+
* Clears all items in the `trash` array by unsubscribing each item and resetting the array length to zero.
|
|
78
|
+
*
|
|
79
|
+
* @return {void} No return value.
|
|
80
|
+
*/
|
|
81
|
+
private clearTrash;
|
|
82
|
+
/**
|
|
83
|
+
* Unsubscribes the provided listener by removing it from the subscribers' list
|
|
84
|
+
* or marking it for cleanup if the process is currently active.
|
|
85
|
+
*
|
|
86
|
+
* @param {ISubscriptionLike} listener - The subscription listener to be unsubscribed.
|
|
87
|
+
* @return {void} No return value.
|
|
88
|
+
*/
|
|
89
|
+
unSubscribe(listener: ISubscriptionLike): void;
|
|
90
|
+
/**
|
|
91
|
+
* Cleans up resources and terminates any ongoing processes or subscriptions associated with the instance.
|
|
92
|
+
*
|
|
93
|
+
* Sets the internal state to indicate it has been destroyed. If a process is associated, waits until the process is no longer active
|
|
94
|
+
* before clearing internal data and subscriptions.
|
|
95
|
+
*
|
|
96
|
+
* @return {void} Does not return any value.
|
|
97
|
+
*/
|
|
98
|
+
destroy(): void;
|
|
99
|
+
/**
|
|
100
|
+
* Unsubscribes from all active subscriptions by clearing the subscriptions list.
|
|
101
|
+
* Prevents further operations if the instance has already been marked as killed.
|
|
102
|
+
* Safe to call during next() - uses deferred cleanup mechanism.
|
|
103
|
+
*
|
|
104
|
+
* @return {void} Does not return a value.
|
|
105
|
+
*/
|
|
106
|
+
unsubscribeAll(): void;
|
|
107
|
+
/**
|
|
108
|
+
* Retrieves the current value if the instance is active.
|
|
109
|
+
* If the instance is marked as killed, undefined is returned.
|
|
110
|
+
*
|
|
111
|
+
* @return {T | undefined} The current value or undefined if the instance is killed.
|
|
112
|
+
*/
|
|
113
|
+
getValue(): T | undefined;
|
|
114
|
+
/**
|
|
115
|
+
* Calculates and returns the size based on the current state.
|
|
116
|
+
*
|
|
117
|
+
* @return {number} The size, which is the number of subscriptions if not killed; otherwise, 0.
|
|
118
|
+
*/
|
|
119
|
+
size(): number;
|
|
120
|
+
/**
|
|
121
|
+
* Subscribes an observer to this instance, monitoring its changes and handling errors if any.
|
|
122
|
+
*
|
|
123
|
+
* @param observer The observer object that implements the ISubscribeGroup interface which will be subscribed.
|
|
124
|
+
* @param errorHandler Optional callback function to handle errors encountered during the subscription process.
|
|
125
|
+
* @return An object implementing the ISubscriptionLike interface that represents the subscription,
|
|
126
|
+
* or undefined if the subscription is not successful.
|
|
127
|
+
*/
|
|
128
|
+
subscribe(observer: ISubscribeGroup<T>, errorHandler?: IErrorCallback): ISubscriptionLike | undefined;
|
|
129
|
+
/**
|
|
130
|
+
* Adds an observer to the subscribe object and associates it with a potential error handler.
|
|
131
|
+
* The subscribe object is added to the list of subscriptions for tracking purposes.
|
|
132
|
+
*
|
|
133
|
+
* @param {SubscribeObject<T>} subscribeObject - The object that manages the subscription to observers.
|
|
134
|
+
* @param {ISubscribeGroup<T>} observer - The observer to be subscribed to the subscribe object.
|
|
135
|
+
* @param {IErrorCallback} [errorHandler] - Optional error handler callback to handle errors during subscription.
|
|
136
|
+
* @return {void} This method does not return a value.
|
|
137
|
+
*/
|
|
138
|
+
protected addObserver(subscribeObject: SubscribeObject<T>, observer: ISubscribeGroup<T>, errorHandler?: IErrorCallback): void;
|
|
139
|
+
/**
|
|
140
|
+
* Determines if the provided object is a valid listener.
|
|
141
|
+
*
|
|
142
|
+
* @param listener An instance of ISubscribeGroup of type T to validate as a listener.
|
|
143
|
+
* @return Returns true if the provided listener is valid and the object is not marked as killed; otherwise, returns false.
|
|
144
|
+
*/
|
|
145
|
+
protected isListener(listener: ISubscribeGroup<T>): boolean;
|
|
146
|
+
/**
|
|
147
|
+
* Handles the creation and management of a SubscribeObject if the current instance is active.
|
|
148
|
+
* It returns an `ISetup<T>` object to allow additional configurations or operations.
|
|
149
|
+
* If the instance is in a "killed" state, it will not proceed and returns undefined.
|
|
150
|
+
*
|
|
151
|
+
* @return {ISetup<T> | undefined} The created SubscribeObject wrapped in an ISetup<T> interface if the instance is active, otherwise undefined.
|
|
152
|
+
*/
|
|
153
|
+
pipe(): ISetup<T> | undefined;
|
|
154
|
+
/**
|
|
155
|
+
* Determines if the object has been destroyed.
|
|
156
|
+
*
|
|
157
|
+
* @return {boolean} True if the object is destroyed, false otherwise.
|
|
158
|
+
*/
|
|
159
|
+
get isDestroyed(): boolean;
|
|
160
|
+
}
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Observable = void 0;
|
|
4
|
+
const FunctionLibs_1 = require("./FunctionLibs");
|
|
5
|
+
const SubscribeObject_1 = require("./SubscribeObject");
|
|
6
|
+
const FilterCollection_1 = require("./FilterCollection");
|
|
7
|
+
/**
|
|
8
|
+
* Observable is a generic class that represents an entity capable of emitting values
|
|
9
|
+
* over time to subscribers. It provides mechanisms for managing subscribers and controlling
|
|
10
|
+
* the flow of emitted data through filters and utility functions.
|
|
11
|
+
*
|
|
12
|
+
* @template T The type of the data managed by the Observable.
|
|
13
|
+
*
|
|
14
|
+
* Implements:
|
|
15
|
+
* - IObserver<T>: For observing value changes.
|
|
16
|
+
* - IStream<T>: For emitting streams of values.
|
|
17
|
+
* - IAddFilter<T>: For adding and managing filters in the value processing.
|
|
18
|
+
*
|
|
19
|
+
* The Observable class provides the following key features:
|
|
20
|
+
*
|
|
21
|
+
* - Allows subscribers to listen for emitted values.
|
|
22
|
+
* - Provides methods for enabling/disabling value emissions.
|
|
23
|
+
* - Supports applying filters to process or validate values before emission.
|
|
24
|
+
* - Allows streaming multiple values sequentially.
|
|
25
|
+
* - Supports safe termination of resources through unsubscription and destruction.
|
|
26
|
+
* - Handles asynchronous cleanup when active operations are in progress.
|
|
27
|
+
*/
|
|
28
|
+
class Observable {
|
|
29
|
+
subs = [];
|
|
30
|
+
enabled = true;
|
|
31
|
+
killed = false;
|
|
32
|
+
process = false;
|
|
33
|
+
trash = [];
|
|
34
|
+
filters = new FilterCollection_1.FilterCollection();
|
|
35
|
+
_value;
|
|
36
|
+
constructor(value) {
|
|
37
|
+
this._value = value;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Adds an error handler filter to the current filter setup.
|
|
41
|
+
*
|
|
42
|
+
* @param {IErrorCallback} [errorHandler] - An optional error handler callback to handle errors in the filter.
|
|
43
|
+
* @return {IFilterSetup<T>} The updated filter setup object with the specified error handler added.
|
|
44
|
+
*/
|
|
45
|
+
addFilter(errorHandler) {
|
|
46
|
+
if (errorHandler)
|
|
47
|
+
this.filters.addErrorHandler(errorHandler);
|
|
48
|
+
return this.filters;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Disables the current instance or functionality associated with this method.
|
|
52
|
+
* Updates the internal state to reflect that it is no longer active or enabled.
|
|
53
|
+
*
|
|
54
|
+
* @return {void} This method does not return a value.
|
|
55
|
+
*/
|
|
56
|
+
disable() {
|
|
57
|
+
this.enabled = false;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Enables the current feature or functionality by setting its state to active.
|
|
61
|
+
* Updates the internal state to indicate that it is enabled.
|
|
62
|
+
*
|
|
63
|
+
* @return {void} No return value.
|
|
64
|
+
*/
|
|
65
|
+
enable() {
|
|
66
|
+
this.enabled = true;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Indicates whether the current object or feature is enabled.
|
|
70
|
+
*
|
|
71
|
+
* @return {boolean} Returns true if enabled, otherwise false.
|
|
72
|
+
*/
|
|
73
|
+
get isEnable() {
|
|
74
|
+
return this.enabled;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Processes the given value and sends it to all subscribers, respecting the enabled status, filters, and processing state.
|
|
78
|
+
*
|
|
79
|
+
* @param {T} value - The value to be processed and passed to subscribers.
|
|
80
|
+
* @return {void} This method does not return a value.
|
|
81
|
+
*/
|
|
82
|
+
next(value) {
|
|
83
|
+
if (this.killed)
|
|
84
|
+
return;
|
|
85
|
+
if (!this.enabled)
|
|
86
|
+
return;
|
|
87
|
+
if (!this.subs.length)
|
|
88
|
+
return;
|
|
89
|
+
if (!this.filters.isEmpty && !this.filters.processChain(value).isOK)
|
|
90
|
+
return;
|
|
91
|
+
this.process = true;
|
|
92
|
+
this._value = value;
|
|
93
|
+
const subs = this.subs;
|
|
94
|
+
const len = subs.length;
|
|
95
|
+
for (let i = 0; i < len; i++)
|
|
96
|
+
subs[i].send(value);
|
|
97
|
+
this.process = false;
|
|
98
|
+
this.trash.length && this.clearTrash();
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Processes an array of values and triggers the next method for each value if the stream
|
|
102
|
+
* is not killed or disabled.
|
|
103
|
+
*
|
|
104
|
+
* @param {T[]} values - An array of values to be processed by the stream.
|
|
105
|
+
* @return {void} This method does not return a value.
|
|
106
|
+
*/
|
|
107
|
+
stream(values) {
|
|
108
|
+
if (this.killed)
|
|
109
|
+
return;
|
|
110
|
+
if (!this.enabled)
|
|
111
|
+
return;
|
|
112
|
+
for (let i = 0; i < values.length; i++)
|
|
113
|
+
this.next(values[i]);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Clears all items in the `trash` array by unsubscribing each item and resetting the array length to zero.
|
|
117
|
+
*
|
|
118
|
+
* @return {void} No return value.
|
|
119
|
+
*/
|
|
120
|
+
clearTrash() {
|
|
121
|
+
const length = this.trash.length;
|
|
122
|
+
for (let i = 0; i < length; i++)
|
|
123
|
+
this.unSubscribe(this.trash[i]);
|
|
124
|
+
this.trash.length = 0;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Unsubscribes the provided listener by removing it from the subscribers' list
|
|
128
|
+
* or marking it for cleanup if the process is currently active.
|
|
129
|
+
*
|
|
130
|
+
* @param {ISubscriptionLike} listener - The subscription listener to be unsubscribed.
|
|
131
|
+
* @return {void} No return value.
|
|
132
|
+
*/
|
|
133
|
+
unSubscribe(listener) {
|
|
134
|
+
if (this.killed)
|
|
135
|
+
return;
|
|
136
|
+
if (this.process && listener) {
|
|
137
|
+
this.trash.push(listener);
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
this.subs && !(0, FunctionLibs_1.quickDeleteFromArray)(this.subs, listener);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Cleans up resources and terminates any ongoing processes or subscriptions associated with the instance.
|
|
144
|
+
*
|
|
145
|
+
* Sets the internal state to indicate it has been destroyed. If a process is associated, waits until the process is no longer active
|
|
146
|
+
* before clearing internal data and subscriptions.
|
|
147
|
+
*
|
|
148
|
+
* @return {void} Does not return any value.
|
|
149
|
+
*/
|
|
150
|
+
destroy() {
|
|
151
|
+
if (this.killed)
|
|
152
|
+
return;
|
|
153
|
+
this.killed = true;
|
|
154
|
+
if (!this.process) {
|
|
155
|
+
this._value = null;
|
|
156
|
+
this.subs.length = 0;
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
Promise.resolve().then(() => {
|
|
160
|
+
this._value = null;
|
|
161
|
+
this.subs.length = 0;
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Unsubscribes from all active subscriptions by clearing the subscriptions list.
|
|
166
|
+
* Prevents further operations if the instance has already been marked as killed.
|
|
167
|
+
* Safe to call during next() - uses deferred cleanup mechanism.
|
|
168
|
+
*
|
|
169
|
+
* @return {void} Does not return a value.
|
|
170
|
+
*/
|
|
171
|
+
unsubscribeAll() {
|
|
172
|
+
if (this.killed)
|
|
173
|
+
return;
|
|
174
|
+
if (this.process) {
|
|
175
|
+
// Defer removal until next() completes
|
|
176
|
+
const subs = this.subs;
|
|
177
|
+
for (let i = 0; i < subs.length; i++)
|
|
178
|
+
this.trash.push(subs[i]);
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
this.subs.length = 0;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Retrieves the current value if the instance is active.
|
|
185
|
+
* If the instance is marked as killed, undefined is returned.
|
|
186
|
+
*
|
|
187
|
+
* @return {T | undefined} The current value or undefined if the instance is killed.
|
|
188
|
+
*/
|
|
189
|
+
getValue() {
|
|
190
|
+
if (this.killed)
|
|
191
|
+
return undefined;
|
|
192
|
+
return this._value;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Calculates and returns the size based on the current state.
|
|
196
|
+
*
|
|
197
|
+
* @return {number} The size, which is the number of subscriptions if not killed; otherwise, 0.
|
|
198
|
+
*/
|
|
199
|
+
size() {
|
|
200
|
+
if (this.killed)
|
|
201
|
+
return 0;
|
|
202
|
+
return this.subs.length;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Subscribes an observer to this instance, monitoring its changes and handling errors if any.
|
|
206
|
+
*
|
|
207
|
+
* @param observer The observer object that implements the ISubscribeGroup interface which will be subscribed.
|
|
208
|
+
* @param errorHandler Optional callback function to handle errors encountered during the subscription process.
|
|
209
|
+
* @return An object implementing the ISubscriptionLike interface that represents the subscription,
|
|
210
|
+
* or undefined if the subscription is not successful.
|
|
211
|
+
*/
|
|
212
|
+
subscribe(observer, errorHandler) {
|
|
213
|
+
if (this.killed)
|
|
214
|
+
return undefined;
|
|
215
|
+
if (!this.isListener(observer))
|
|
216
|
+
return undefined;
|
|
217
|
+
const subscribeObject = new SubscribeObject_1.SubscribeObject(this, false);
|
|
218
|
+
this.addObserver(subscribeObject, observer, errorHandler);
|
|
219
|
+
return subscribeObject;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Adds an observer to the subscribe object and associates it with a potential error handler.
|
|
223
|
+
* The subscribe object is added to the list of subscriptions for tracking purposes.
|
|
224
|
+
*
|
|
225
|
+
* @param {SubscribeObject<T>} subscribeObject - The object that manages the subscription to observers.
|
|
226
|
+
* @param {ISubscribeGroup<T>} observer - The observer to be subscribed to the subscribe object.
|
|
227
|
+
* @param {IErrorCallback} [errorHandler] - Optional error handler callback to handle errors during subscription.
|
|
228
|
+
* @return {void} This method does not return a value.
|
|
229
|
+
*/
|
|
230
|
+
addObserver(subscribeObject, observer, errorHandler) {
|
|
231
|
+
subscribeObject.subscribe(observer, errorHandler);
|
|
232
|
+
this.subs.push(subscribeObject);
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Determines if the provided object is a valid listener.
|
|
236
|
+
*
|
|
237
|
+
* @param listener An instance of ISubscribeGroup of type T to validate as a listener.
|
|
238
|
+
* @return Returns true if the provided listener is valid and the object is not marked as killed; otherwise, returns false.
|
|
239
|
+
*/
|
|
240
|
+
isListener(listener) {
|
|
241
|
+
if (this.killed)
|
|
242
|
+
return false;
|
|
243
|
+
return !!listener;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Handles the creation and management of a SubscribeObject if the current instance is active.
|
|
247
|
+
* It returns an `ISetup<T>` object to allow additional configurations or operations.
|
|
248
|
+
* If the instance is in a "killed" state, it will not proceed and returns undefined.
|
|
249
|
+
*
|
|
250
|
+
* @return {ISetup<T> | undefined} The created SubscribeObject wrapped in an ISetup<T> interface if the instance is active, otherwise undefined.
|
|
251
|
+
*/
|
|
252
|
+
pipe() {
|
|
253
|
+
if (this.killed)
|
|
254
|
+
return undefined;
|
|
255
|
+
const subscribeObject = new SubscribeObject_1.SubscribeObject(this, true);
|
|
256
|
+
this.subs.push(subscribeObject);
|
|
257
|
+
return subscribeObject;
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Determines if the object has been destroyed.
|
|
261
|
+
*
|
|
262
|
+
* @return {boolean} True if the object is destroyed, false otherwise.
|
|
263
|
+
*/
|
|
264
|
+
get isDestroyed() {
|
|
265
|
+
return this.killed;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
exports.Observable = Observable;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { Observable } from "./Observable";
|
|
2
|
+
import { IErrorCallback, IOrdered, IOrderedSetup, IOrderedSubscriptionLike, ISubscribeGroup, ISubscriptionLike } from "./Types";
|
|
3
|
+
/**
|
|
4
|
+
* Represents an observable data structure that maintains elements in a specific order and provides
|
|
5
|
+
* methods to manipulate and subscribe to its elements.
|
|
6
|
+
*
|
|
7
|
+
* The `OrderedObservable` class supports sorting elements in ascending or descending order and allows
|
|
8
|
+
* for subscription to changes in its state. It extends the capabilities of a basic `Observable` by
|
|
9
|
+
* incorporating sorting and ordered processing mechanisms.
|
|
10
|
+
*
|
|
11
|
+
* @template T The type of data stored and handled by the observable.
|
|
12
|
+
* @extends Observable<T>
|
|
13
|
+
* @implements IOrdered<T>
|
|
14
|
+
*/
|
|
15
|
+
export declare class OrderedObservable<T> extends Observable<T> implements IOrdered<T> {
|
|
16
|
+
/**
|
|
17
|
+
* Represents the direction in which sorting should occur.
|
|
18
|
+
* The value is expected to indicate whether the sorting
|
|
19
|
+
* should be performed in ascending or descending order.
|
|
20
|
+
*
|
|
21
|
+
* Possible values:
|
|
22
|
+
* - `sortAscending`: Sorting in ascending order.
|
|
23
|
+
* - `sortDescending`: Sorting in descending order.
|
|
24
|
+
*/
|
|
25
|
+
private sortDirection;
|
|
26
|
+
/**
|
|
27
|
+
* Sets the sorting order to ascending and applies the sorting.
|
|
28
|
+
*
|
|
29
|
+
* @return {boolean} Returns true if the sorting operation is successful, otherwise false.
|
|
30
|
+
*/
|
|
31
|
+
setAscendingSort(): boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Sets the sorting order to descending and updates the sort configuration.
|
|
34
|
+
*
|
|
35
|
+
* @return {boolean} Returns `true` if the sorting operation is configured and applied successfully, otherwise returns `false`.
|
|
36
|
+
*/
|
|
37
|
+
setDescendingSort(): boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Sorts the `subs` array based on the specified sort direction if the object is not in a "killed" state.
|
|
40
|
+
*
|
|
41
|
+
* @return {boolean} Returns true if the sorting was performed, false if the object is in a "killed" state.
|
|
42
|
+
*/
|
|
43
|
+
sortByOrder(): boolean;
|
|
44
|
+
/**
|
|
45
|
+
* Subscribes a listener to this instance with an optional error handler.
|
|
46
|
+
*
|
|
47
|
+
* @param {ISubscribeGroup<T>} listener - The listener object that should be notified of changes.
|
|
48
|
+
* @param {IErrorCallback} [errorHandler] - An optional handler to process errors during execution.
|
|
49
|
+
* @return {IOrderedSubscriptionLike | undefined} Returns an instance of IOrderedSubscriptionLike if the listener is valid; otherwise, returns undefined.
|
|
50
|
+
*/
|
|
51
|
+
subscribe(listener: ISubscribeGroup<T>, errorHandler?: IErrorCallback): IOrderedSubscriptionLike | undefined;
|
|
52
|
+
/**
|
|
53
|
+
* Creates and returns an instance of `OrderedSubscribeObject` tied to the current object,
|
|
54
|
+
* facilitating ordered subscription setup. If the instance is marked as killed, it returns undefined.
|
|
55
|
+
*
|
|
56
|
+
* @return {IOrderedSetup<T> | undefined} An instance of `OrderedSubscribeObject` for subscription setup,
|
|
57
|
+
* or undefined if the operation is not allowed.
|
|
58
|
+
*/
|
|
59
|
+
pipe(): IOrderedSetup<T> | undefined;
|
|
60
|
+
/**
|
|
61
|
+
* Removes a previously subscribed listener from the subscription list,
|
|
62
|
+
* preventing it from receiving further updates. If the system is in a
|
|
63
|
+
* "killed" state or specific conditions in the process flow are met,
|
|
64
|
+
* the listener may instead be added to a trash list.
|
|
65
|
+
*
|
|
66
|
+
* @param {ISubscriptionLike} listener - The subscription listener to be unsubscribed or handled accordingly.
|
|
67
|
+
* @return {void} Does not return any value.
|
|
68
|
+
*/
|
|
69
|
+
unSubscribe(listener: ISubscriptionLike): void;
|
|
70
|
+
}
|