evg_observable 1.11.51 → 1.12.52
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/README.md +53 -1
- package/package.json +1 -1
- package/repo/evg_observable.js +1 -1
- package/src/outLib/Pipe.d.ts +3 -1
- package/src/outLib/Pipe.js +24 -8
- package/src/outLib/Types.d.ts +10 -2
package/README.md
CHANGED
|
@@ -131,7 +131,7 @@ observable$.destroy(); // all subscribers have automatically unsubscribed
|
|
|
131
131
|
subscriber1.unsubscribe();
|
|
132
132
|
subscriber2.unsubscribe();
|
|
133
133
|
|
|
134
|
-
// if
|
|
134
|
+
// if an observable$ is not needed
|
|
135
135
|
observable$.destroy(); // all subscribers have automatically unsubscribed
|
|
136
136
|
|
|
137
137
|
// also if observable$ needs to be used further, but subscribers are not needed, you can use the observable$.unsubscribeAll() method
|
|
@@ -256,6 +256,56 @@ observable$.next(TARGET_DATA);
|
|
|
256
256
|
// Print to console - listener2: TARGET_DATA
|
|
257
257
|
```
|
|
258
258
|
|
|
259
|
+
### pipe().serialize()
|
|
260
|
+
|
|
261
|
+
To convert the observable's data to JSON format, you can use the serialize method. This method turns the observer's
|
|
262
|
+
input data into a JSON string before sending them to subscribers.
|
|
263
|
+
Return Value: An ISetup<string> object
|
|
264
|
+
Usage Example:
|
|
265
|
+
|
|
266
|
+
```ts
|
|
267
|
+
import {Observable} from "evg_observable/src/outLib/Observable";
|
|
268
|
+
type IPoint = { x: number, y: number };
|
|
269
|
+
|
|
270
|
+
const rawObject: IPoint = {x: 10, y: 20};
|
|
271
|
+
const listener = (data: string) => {
|
|
272
|
+
console.log('Received data:', data); // Received data: {"x":10,"y":20}
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
const observable = new Observable<IPoint>(null);
|
|
276
|
+
observable
|
|
277
|
+
.pipe()
|
|
278
|
+
.serialize()
|
|
279
|
+
.subscribe(listener);
|
|
280
|
+
observable.next(rawObject);
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
### pipe().deserialize<K>()
|
|
284
|
+
|
|
285
|
+
The deserialize method is used to convert data received from the observer from a JSON string back into a JavaScript
|
|
286
|
+
object.
|
|
287
|
+
Return Value: An ISetup<K> object, where K is the type of data resulting from the transformation.
|
|
288
|
+
Usage Example:
|
|
289
|
+
|
|
290
|
+
```ts
|
|
291
|
+
import {Observable} from "evg_observable/src/outLib/Observable";
|
|
292
|
+
type IPoint = { x: number, y: number };
|
|
293
|
+
|
|
294
|
+
const rawObject: IPoint = {x: 10, y: 20};
|
|
295
|
+
const json: string = JSON.stringify(rawObject);
|
|
296
|
+
const listener = (data: IPoint) => {
|
|
297
|
+
console.log('Received data.x:', data.x); // Received data.x: 10
|
|
298
|
+
console.log('Received data.y:', data.y); // Received data.y: 20
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
const observable = new Observable<string>("");
|
|
302
|
+
observable
|
|
303
|
+
.pipe()
|
|
304
|
+
.deserialize<IPoint>()
|
|
305
|
+
.subscribe(listener);
|
|
306
|
+
observable.next(json);
|
|
307
|
+
```
|
|
308
|
+
|
|
259
309
|
## Ordered observable
|
|
260
310
|
|
|
261
311
|
Ordered observable - differs from Observable in that it allows you to emit messages in a given order. In general, they
|
|
@@ -572,6 +622,8 @@ observable is set to be a once-off observable to which a listener is subscribed.
|
|
|
572
622
|
| `.case(*condition)` | PipeCase object | Adds a condition to the chain of cases. The entire chain operates on the principle of "OR". This is different from other pipe methods which, when chained, operate on the principle of "AND". |
|
|
573
623
|
| `.pushCases(*conditions)` | PipeCase object | This method allows you to add a group of conditions for filtering cases data in the pipeline chain. |
|
|
574
624
|
| `.then<K>(condition: ICallback<T>)` | Observable instance with new data type | This method allows transforming payload data in the pipe chain by applying user callback function. `condition` should be a function that takes the current data and returns transformed data of possibly another type. |
|
|
625
|
+
| `.serialize()` | pipe object | Converts the observers data into a JSON string. |
|
|
626
|
+
| `.deserialize<K>()` | pipe object | Converts a JSON string into an object of type K. |
|
|
575
627
|
| `.subscribe(listener)` | subscriber | subscribe listener to observable |
|
|
576
628
|
|
|
577
629
|
_*condition_ - this is a function that should return a value that will affect the behavior of the subscriber
|
package/package.json
CHANGED
package/repo/evg_observable.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{"use strict";const s=(s,e)=>s.order>e.order?1:s.order<e.order?-1:0,e=(s,e)=>s.order>e.order?-1:s.order<e.order?1:0;function i(s,e){const i=s.indexOf(e);return-1!==i&&(s[i]=s[s.length-1],s.length=s.length-1,!0)}function t(s){return"next"in s?e=>s.next(e):s}class r{constructor(){this.chainHandlers=[],this.pipeData={isBreakChain:!1,isNeedUnsubscribe:!1,isAvailable:!1,payload:null}}setOnce(){const s=this.pipeData;return this.chainHandlers.push((()=>{this.listener(s.payload),s.isNeedUnsubscribe=!0})),this}unsubscribeByNegative(s){const e=this.pipeData;return this.chainHandlers.push((()=>{e.isAvailable=!0,s(e.payload)||(e.isNeedUnsubscribe=!0)})),this}unsubscribeByPositive(s){const e=this.pipeData;return this.chainHandlers.push((()=>{e.isAvailable=!0,s(e.payload)&&(e.isNeedUnsubscribe=!0)})),this}emitByNegative(s){const e=this.pipeData;return this.chainHandlers.push((()=>{s(e.payload)||(e.isAvailable=!0)})),this}emitByPositive(s){const e=this.pipeData;return this.chainHandlers.push((()=>{s(e.payload)&&(e.isAvailable=!0)})),this}refine(s){return this.emitByPositive(s)}
|
|
1
|
+
(()=>{"use strict";const s=(s,e)=>s.order>e.order?1:s.order<e.order?-1:0,e=(s,e)=>s.order>e.order?-1:s.order<e.order?1:0;function i(s,e){const i=s.indexOf(e);return-1!==i&&(s[i]=s[s.length-1],s.length=s.length-1,!0)}function t(s){return"next"in s?e=>s.next(e):s}class r{constructor(){this.chainHandlers=[],this.pipeData={isBreakChain:!1,isNeedUnsubscribe:!1,isAvailable:!1,payload:null}}setOnce(){const s=this.pipeData;return this.chainHandlers.push((()=>{this.listener(s.payload),s.isNeedUnsubscribe=!0})),this}unsubscribeByNegative(s){const e=this.pipeData;return this.chainHandlers.push((()=>{e.isAvailable=!0,s(e.payload)||(e.isNeedUnsubscribe=!0)})),this}unsubscribeByPositive(s){const e=this.pipeData;return this.chainHandlers.push((()=>{e.isAvailable=!0,s(e.payload)&&(e.isNeedUnsubscribe=!0)})),this}unsubscribeBy(s){return this.unsubscribeByPositive(s)}emitByNegative(s){const e=this.pipeData;return this.chainHandlers.push((()=>{s(e.payload)||(e.isAvailable=!0)})),this}emitByPositive(s){const e=this.pipeData;return this.chainHandlers.push((()=>{s(e.payload)&&(e.isAvailable=!0)})),this}refine(s){return this.emitByPositive(s)}pushRefiners(s){if(!Array.isArray(s))return this;for(let e=0;e<s.length;e++)this.emitByPositive(s[e]);return this}emitMatch(s){const e=this.pipeData;return this.chainHandlers.push((()=>{s(e.payload)==e.payload&&(e.isAvailable=!0)})),this}switch(){return new n(this)}then(s){const e=this.pipeData;return this.chainHandlers.push((()=>{e.payload=s(e.payload),e.isAvailable=!0})),this}serialize(){const s=this.pipeData;return this.chainHandlers.push((()=>{s.payload=JSON.stringify(s.payload),s.isAvailable=!0})),this}deserialize(){const s=this.pipeData;return this.chainHandlers.push((()=>{s.payload=JSON.parse(s.payload),s.isAvailable=!0})),this}processChain(s){const e=this.chainHandlers,i=this.pipeData;for(let s=0;s<e.length;s++){if(i.isNeedUnsubscribe=!1,i.isAvailable=!1,e[s](),i.isNeedUnsubscribe)return this.unsubscribe();if(!i.isAvailable)return;if(i.isBreakChain)break}return s(i.payload)}}class n{constructor(s){this.pipe=s,this.caseCounter=s.chainHandlers.length?s.chainHandlers.length:0}subscribe(s,e){return this.pipe.subscribe(s,e)}case(s){this.caseCounter++;const e=this.caseCounter,i=this.pipe.pipeData,t=this.pipe.chainHandlers;return t.push((()=>{i.isAvailable=!0,s(i.payload)&&(i.isBreakChain=!0),e!==t.length||i.isBreakChain||(i.isAvailable=!1)})),this}pushCases(s){if(!Array.isArray(s))return this;for(let e=0;e<s.length;e++)this.case(s[e]);return this}}class h extends r{constructor(s,e){super(),this.errorHandler=(s,e)=>{console.log(`(Unit of SubscribeObject).send(${s}) ERROR:`,e)},this._order=0,this.isPaused=!1,this.isPipe=!1,this.observable=s,this.isPipe=!!e}subscribe(s,e){return this.listener=function(s){if(Array.isArray(s)){const e=[];for(let i=0;i<s.length;i++)e.push(t(s[i]));return s=>{for(let i=0;i<e.length;i++)e[i](s)}}return t(s)}(s),e&&(this.errorHandler=e),this}unsubscribe(){this.observable&&(this.observable.unSubscribe(this),this.observable=null,this.listener=null,this.chainHandlers.length=0)}send(s){try{this.pipeData.payload=s,this.pipeData.isBreakChain=!1,this.processValue(s)}catch(e){this.errorHandler(s,e)}}resume(){this.isPaused=!1}pause(){this.isPaused=!0}get order(){return this._order}set order(s){this._order=s}processValue(s){const e=this.listener;return e&&this.observable?this.isPaused?void 0:this.isPipe?this.processChain(e):e(s):this.unsubscribe()}}class a{constructor(){this.chainHandlers=[],this.pipeData={isBreakChain:!1,isAvailable:!1,payload:null},this.response={isOK:!1,payload:void 0}}get isEmpty(){return!this.chainHandlers.length}filter(s){const e=this.pipeData;return this.chainHandlers.push((()=>{s(e.payload)&&(e.isAvailable=!0)})),this}pushFilters(s){if(!Array.isArray(s))return this;for(let e=0;e<s.length;e++)this.filter(s[e]);return this}switch(){return new l(this)}processChain(s){const e=this.chainHandlers,i=this.pipeData,t=this.response;t.isOK=!1,t.payload=void 0,i.payload=s,i.isBreakChain=!1;try{for(let s=0;s<e.length;s++){if(i.isAvailable=!1,e[s](),!i.isAvailable)return t;if(i.isBreakChain)break}}catch(s){return this.errorHandler?this.errorHandler(s,"Filter.processChain ERROR:"):console.log("Filter.processChain ERROR:",s),t}return t.isOK=!0,t.payload=i.payload,t}addErrorHandler(s){this.errorHandler=s}}class l{constructor(s){this.pipe=s,this.caseCounter=s.chainHandlers.length?s.chainHandlers.length:0}case(s){this.caseCounter++;const e=this.caseCounter,i=this.pipe.pipeData,t=this.pipe.chainHandlers;return t.push((()=>{i.isAvailable=!0,s(i.payload)&&(i.isBreakChain=!0),e!==t.length||i.isBreakChain||(i.isAvailable=!1)})),this}pushCases(s){if(!Array.isArray(s))return this;for(let e=0;e<s.length;e++)this.case(s[e]);return this}}class o{constructor(s){this.value=s,this.listeners=[],this._isEnable=!0,this._isDestroyed=!1,this.isNextProcess=!1,this.listenersForUnsubscribe=[],this.filterCase=new a}addFilter(s){return s&&this.filterCase.addErrorHandler(s),this.filterCase}disable(){this._isEnable=!1}enable(){this._isEnable=!0}get isEnable(){return this._isEnable}next(s){if(!this._isDestroyed&&this._isEnable&&(this.filterCase.isEmpty||this.filterCase.processChain(s).isOK)){this.isNextProcess=!0,this.value=s;for(let e=0;e<this.listeners.length;e++)this.listeners[e].send(s);this.isNextProcess=!1,this.listenersForUnsubscribe.length&&this.handleListenersForUnsubscribe()}}stream(s){if(!this._isDestroyed&&this._isEnable)for(let e=0;e<s.length;e++)this.next(s[e])}handleListenersForUnsubscribe(){const s=this.listenersForUnsubscribe.length;for(let e=0;e<s;e++)this.unSubscribe(this.listenersForUnsubscribe[e]);this.listenersForUnsubscribe.length=0}unSubscribe(s){this._isDestroyed||(this.isNextProcess&&s?this.listenersForUnsubscribe.push(s):this.listeners&&i(this.listeners,s))}destroy(){this.value=null,this.unsubscribeAll(),this.listeners=null,this._isDestroyed=!0}unsubscribeAll(){this._isDestroyed||(this.listeners.length=0)}getValue(){if(!this._isDestroyed)return this.value}size(){return this._isDestroyed?0:this.listeners.length}subscribe(s,e){if(!this.isSubsValid(s))return;const i=new h(this,!1);return this.addObserver(i,s,e),i}addObserver(s,e,i){s.subscribe(e,i),this.listeners.push(s)}isSubsValid(s){return!this._isDestroyed&&!!s}pipe(){if(this._isDestroyed)return;const s=new h(this,!0);return this.listeners.push(s),s}get isDestroyed(){return this._isDestroyed}}class u extends h{constructor(s,e){super(s,e)}get order(){return this._order}set order(s){!this.observable||this.observable&&this.observable.isDestroyed?this._order=void 0:(this._order=s,this.observable.sortByOrder())}subscribe(s,e){return super.subscribe(s,e),this}setOnce(){return super.setOnce()}unsubscribeByNegative(s){return super.unsubscribeByNegative(s)}unsubscribeByPositive(s){return super.unsubscribeByPositive(s)}emitByNegative(s){return super.emitByNegative(s)}emitByPositive(s){return super.emitByPositive(s)}emitMatch(s){return super.emitMatch(s)}}const c=window;c.Observable=o,c.Collector=class{constructor(){this.list=[],this._isDestroyed=!1}collect(...s){this._isDestroyed||this.list.push(...s)}unsubscribe(s){this._isDestroyed||(s?.unsubscribe(),i(this.list,s))}unsubscribeAll(){if(!this._isDestroyed)for(;this.list.length>0;)this.unsubscribe(this.list.pop())}size(){return this._isDestroyed?0:this.list.length}destroy(){this.unsubscribeAll(),this.list.length=0,this.list=0,this._isDestroyed=!0}get isDestroyed(){return this._isDestroyed}},c.OrderedObservable=class extends o{constructor(){super(...arguments),this.sortDirection=s}setAscendingSort(){return this.sortDirection=s,this.sortByOrder()}setDescendingSort(){return this.sortDirection=e,this.sortByOrder()}sortByOrder(){return!this._isDestroyed&&(this.listeners.sort(this.sortDirection),!0)}subscribe(s,e){if(!this.isSubsValid(s))return;const i=new u(this,!1);return this.addObserver(i,s,e),i}pipe(){if(this._isDestroyed)return;const s=new u(this,!0);return this.listeners.push(s),s}unSubscribe(s){this._isDestroyed||(this.isNextProcess&&s?this.listenersForUnsubscribe.push(s):this.listeners&&function(s,e){const i=s.indexOf(e);-1!==i&&s.splice(i,1)}(this.listeners,s))}}})();
|
package/src/outLib/Pipe.d.ts
CHANGED
|
@@ -10,10 +10,12 @@ export declare abstract class Pipe<T> implements ISubscribe<T> {
|
|
|
10
10
|
emitByNegative(condition: ICallback<T>): ISetup<T>;
|
|
11
11
|
emitByPositive(condition: ICallback<T>): ISetup<T>;
|
|
12
12
|
refine(condition: ICallback<T>): ISetup<T>;
|
|
13
|
-
then<K>(condition: ICallback<T>): ISetup<K>;
|
|
14
13
|
pushRefiners(conditions: ICallback<any>[]): ISetup<T>;
|
|
15
14
|
emitMatch(condition: ICallback<T>): ISetup<T>;
|
|
16
15
|
switch(): SwitchCase<T>;
|
|
16
|
+
then<K>(condition: ICallback<T>): ISetup<K>;
|
|
17
|
+
serialize(): ISetup<string>;
|
|
18
|
+
deserialize<K>(): ISetup<K>;
|
|
17
19
|
processChain(listener: IListener<T>): void;
|
|
18
20
|
}
|
|
19
21
|
export declare class SwitchCase<T> implements ISubscribe<T>, IPipeCase<T> {
|
package/src/outLib/Pipe.js
CHANGED
|
@@ -52,14 +52,6 @@ class Pipe {
|
|
|
52
52
|
refine(condition) {
|
|
53
53
|
return this.emitByPositive(condition);
|
|
54
54
|
}
|
|
55
|
-
then(condition) {
|
|
56
|
-
const data = this.pipeData;
|
|
57
|
-
this.chainHandlers.push(() => {
|
|
58
|
-
data.payload = condition(data.payload);
|
|
59
|
-
data.isAvailable = true;
|
|
60
|
-
});
|
|
61
|
-
return this;
|
|
62
|
-
}
|
|
63
55
|
pushRefiners(conditions) {
|
|
64
56
|
if (!Array.isArray(conditions))
|
|
65
57
|
return this;
|
|
@@ -78,6 +70,30 @@ class Pipe {
|
|
|
78
70
|
switch() {
|
|
79
71
|
return new SwitchCase(this);
|
|
80
72
|
}
|
|
73
|
+
then(condition) {
|
|
74
|
+
const data = this.pipeData;
|
|
75
|
+
this.chainHandlers.push(() => {
|
|
76
|
+
data.payload = condition(data.payload);
|
|
77
|
+
data.isAvailable = true;
|
|
78
|
+
});
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
serialize() {
|
|
82
|
+
const data = this.pipeData;
|
|
83
|
+
this.chainHandlers.push(() => {
|
|
84
|
+
data.payload = JSON.stringify(data.payload);
|
|
85
|
+
data.isAvailable = true;
|
|
86
|
+
});
|
|
87
|
+
return this;
|
|
88
|
+
}
|
|
89
|
+
deserialize() {
|
|
90
|
+
const data = this.pipeData;
|
|
91
|
+
this.chainHandlers.push(() => {
|
|
92
|
+
data.payload = JSON.parse(data.payload);
|
|
93
|
+
data.isAvailable = true;
|
|
94
|
+
});
|
|
95
|
+
return this;
|
|
96
|
+
}
|
|
81
97
|
processChain(listener) {
|
|
82
98
|
const chain = this.chainHandlers;
|
|
83
99
|
const data = this.pipeData;
|
package/src/outLib/Types.d.ts
CHANGED
|
@@ -31,8 +31,8 @@ export type ISetObservableValue = {
|
|
|
31
31
|
export type ISubscriptionLike = {
|
|
32
32
|
unsubscribe(): void;
|
|
33
33
|
};
|
|
34
|
-
export type ISetup<T> = IUnsubscribeByNegative<T> & IUnsubscribeByPositive<T> & IEmitByNegative<T> & IEmitByPositive<T> & IEmitMatchCondition<T> & IOnce<T> & ISwitch<T> & ITransform<T> & ISubscribe<T>;
|
|
35
|
-
export type IOrderedSetup<T> = IOrderedUnsubscribeByNegative<T> & IOrderedUnsubscribeByPositive<T> & IOrderedEmitByNegative<T> & IOrderedEmitByPositive<T> & IOrderedEmitMatchCondition<T> & IOrderedOnce<T> & IOrderedSwitch<T> & IOrderedTransform<T> & IOrderedSubscribe<T>;
|
|
34
|
+
export type ISetup<T> = IUnsubscribeByNegative<T> & IUnsubscribeByPositive<T> & IEmitByNegative<T> & IEmitByPositive<T> & IEmitMatchCondition<T> & IOnce<T> & ISwitch<T> & ITransform<T> & ISerialisation & ISubscribe<T>;
|
|
35
|
+
export type IOrderedSetup<T> = IOrderedUnsubscribeByNegative<T> & IOrderedUnsubscribeByPositive<T> & IOrderedEmitByNegative<T> & IOrderedEmitByPositive<T> & IOrderedEmitMatchCondition<T> & IOrderedOnce<T> & IOrderedSwitch<T> & IOrderedTransform<T> & IOrderedSerialisation & IOrderedSubscribe<T>;
|
|
36
36
|
export type ISubscribeObject<T> = ISubscriptionLike & IPause & IOrder & ISend<T> & ISetup<T>;
|
|
37
37
|
export type ISubscribeCounter = {
|
|
38
38
|
size(): number;
|
|
@@ -91,6 +91,10 @@ export type IEmitByPositive<T> = {
|
|
|
91
91
|
export type ITransform<T> = {
|
|
92
92
|
then<K>(condition: ICallback<T>): ISetup<K>;
|
|
93
93
|
};
|
|
94
|
+
export type ISerialisation = {
|
|
95
|
+
serialize(): ISetup<string>;
|
|
96
|
+
deserialize<K>(): ISetup<K>;
|
|
97
|
+
};
|
|
94
98
|
export type IOrderedEmitByPositive<T> = {
|
|
95
99
|
emitByPositive(condition: ICallback<any>): IOrderedSetup<T>;
|
|
96
100
|
refine(condition: ICallback<any>): ISetup<T>;
|
|
@@ -99,6 +103,10 @@ export type IOrderedEmitByPositive<T> = {
|
|
|
99
103
|
export type IOrderedTransform<T> = {
|
|
100
104
|
then<K>(condition: ICallback<T>): ISetup<K>;
|
|
101
105
|
};
|
|
106
|
+
export type IOrderedSerialisation = {
|
|
107
|
+
serialize(): ISetup<string>;
|
|
108
|
+
deserialize<K>(): ISetup<K>;
|
|
109
|
+
};
|
|
102
110
|
export type IEmitMatchCondition<T> = {
|
|
103
111
|
emitMatch(condition: ICallback<any>): ISetup<T>;
|
|
104
112
|
};
|