evg_observable 1.11.50 → 1.12.51
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 +87 -67
- package/package.json +1 -1
- package/repo/evg_observable.js +1 -1
- package/src/outLib/Pipe.d.ts +4 -1
- package/src/outLib/Pipe.js +27 -8
- package/src/outLib/Types.d.ts +18 -8
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
|
|
@@ -172,44 +172,7 @@ observable$.next('Next2 typed data');
|
|
|
172
172
|
// subscriber1 is automatically unsubscribed after first usage
|
|
173
173
|
```
|
|
174
174
|
|
|
175
|
-
### pipe().
|
|
176
|
-
|
|
177
|
-
Observable will send a value to the subscriber as long as the condition is positive, on the first negative result, the
|
|
178
|
-
subscriber will unsubscribe.
|
|
179
|
-
|
|
180
|
-
```ts
|
|
181
|
-
import {Observable} from "evg_observable/src/outLib/Observable";
|
|
182
|
-
|
|
183
|
-
const observable$ = new Observable('Some typed data (not only string)');
|
|
184
|
-
const listener1 = (value: string) => console.log('listener1:', value);
|
|
185
|
-
const listener2 = (value: string) => console.log('listener2:', value);
|
|
186
|
-
let isPositive = true;
|
|
187
|
-
|
|
188
|
-
const subscriber1 = observable$
|
|
189
|
-
.pipe()
|
|
190
|
-
.unsubscribeByNegative(() => isPositive)
|
|
191
|
-
.subscribe(listener1);
|
|
192
|
-
const subscriber2 = observable$.subscribe(listener2);
|
|
193
|
-
|
|
194
|
-
console.log(observable$.getValue());
|
|
195
|
-
// Print to console - Some typed data (not only string)
|
|
196
|
-
|
|
197
|
-
observable$.next('Next1 typed data');
|
|
198
|
-
// Print to console - listener1: Next1 typed data
|
|
199
|
-
// Print to console - listener2: Next1 typed data
|
|
200
|
-
|
|
201
|
-
observable$.next('Next2 typed data');
|
|
202
|
-
// Print to console - listener1: Next2 typed data
|
|
203
|
-
// Print to console - listener2: Next2 typed data
|
|
204
|
-
|
|
205
|
-
isPositive = false;
|
|
206
|
-
observable$.next('Next3 typed data');
|
|
207
|
-
// Print to console - listener2: Next3 typed data
|
|
208
|
-
|
|
209
|
-
// subscriber1 is automatically unsubscribed when negative condition
|
|
210
|
-
```
|
|
211
|
-
|
|
212
|
-
### pipe().unsubscribeByPositive(condition)
|
|
175
|
+
### pipe().unsubscribeBy(condition)
|
|
213
176
|
|
|
214
177
|
Observable will send a value to the subscriber as long as the condition is negative, on the first positive result, the
|
|
215
178
|
subscriber will unsubscribe.
|
|
@@ -217,40 +180,39 @@ subscriber will unsubscribe.
|
|
|
217
180
|
```ts
|
|
218
181
|
import {Observable} from "evg_observable/src/outLib/Observable";
|
|
219
182
|
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
183
|
+
type ISomeData = {
|
|
184
|
+
message: string;
|
|
185
|
+
isNeedUnsubscribe: boolean;
|
|
186
|
+
}
|
|
224
187
|
|
|
225
|
-
const
|
|
188
|
+
const observable$ = new Observable<ISomeData>({message: "some message", isNeedUnsubscribe: false});
|
|
189
|
+
const listener1 = (value: ISomeData) => console.log('listener1:', value);
|
|
190
|
+
const listener2 = (value: ISomeData) => console.log('listener2:', value);
|
|
191
|
+
|
|
192
|
+
observable$
|
|
226
193
|
.pipe()
|
|
227
|
-
.
|
|
194
|
+
.unsubscribeBy((data: ISomeData) => data.isNeedUnsubscribe)
|
|
228
195
|
.subscribe(listener1);
|
|
229
|
-
const subscriber2 = observable$.subscribe(listener2);
|
|
230
196
|
|
|
231
|
-
|
|
232
|
-
|
|
197
|
+
observable$
|
|
198
|
+
.subscribe(listener2);
|
|
233
199
|
|
|
234
|
-
observable$.
|
|
235
|
-
// Print to console - listener1: Next1 typed data
|
|
236
|
-
// Print to console - listener2: Next1 typed data
|
|
237
|
-
|
|
238
|
-
observable$.next('Next2 typed data');
|
|
239
|
-
// Print to console - listener1: Next2 typed data
|
|
240
|
-
// Print to console - listener2: Next2 typed data
|
|
241
|
-
|
|
242
|
-
isPositive = true;
|
|
243
|
-
observable$.next('Next3 typed data');
|
|
244
|
-
// Print to console - listener2: Next3 typed data
|
|
200
|
+
console.log(observable$.getValue());
|
|
245
201
|
|
|
246
|
-
|
|
202
|
+
observable$.next({message: "some message1", isNeedUnsubscribe: false});
|
|
203
|
+
observable$.next({message: "some message2", isNeedUnsubscribe: false});
|
|
204
|
+
observable$.next({message: "some message3", isNeedUnsubscribe: true});
|
|
205
|
+
|
|
206
|
+
// logs:
|
|
207
|
+
// { message: 'some message', isNeedUnsubscribe: false }
|
|
208
|
+
// listener1: { message: 'some message1', isNeedUnsubscribe: false }
|
|
209
|
+
// listener2: { message: 'some message1', isNeedUnsubscribe: false }
|
|
210
|
+
// listener1: { message: 'some message2', isNeedUnsubscribe: false }
|
|
211
|
+
// listener2: { message: 'some message2', isNeedUnsubscribe: false }
|
|
212
|
+
// listener2: { message: 'some message3', isNeedUnsubscribe: true }
|
|
247
213
|
```
|
|
248
214
|
|
|
249
|
-
### pipe().
|
|
250
|
-
|
|
251
|
-
Observable will send a value to the listener only if condition returns "false". There is no automatic unsubscription.
|
|
252
|
-
|
|
253
|
-
### pipe().emitByPositive(condition)
|
|
215
|
+
### pipe().refine(condition)
|
|
254
216
|
|
|
255
217
|
Observable will send a value to the listener only if condition returns "true". There is no automatic unsubscription.
|
|
256
218
|
|
|
@@ -294,6 +256,56 @@ observable$.next(TARGET_DATA);
|
|
|
294
256
|
// Print to console - listener2: TARGET_DATA
|
|
295
257
|
```
|
|
296
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().serialize()
|
|
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
|
+
|
|
297
309
|
## Ordered observable
|
|
298
310
|
|
|
299
311
|
Ordered observable - differs from Observable in that it allows you to emit messages in a given order. In general, they
|
|
@@ -534,7 +546,8 @@ Observable an invaluable tool for managing asynchronous events.
|
|
|
534
546
|
Built with the developer's needs in mind, EVG Observable provides a wealth of capabilities at your disposal, making
|
|
535
547
|
event handling a breeze.
|
|
536
548
|
|
|
537
|
-
Here is an advanced example of the `pipe` usage which introduces a new method called `then`. It allows transforming
|
|
549
|
+
Here is an advanced example of the `pipe` usage which introduces a new method called `then`. It allows transforming
|
|
550
|
+
payload data in the pipe chain by applying a user callback function.
|
|
538
551
|
|
|
539
552
|
Here is the syntax:
|
|
540
553
|
|
|
@@ -566,7 +579,11 @@ targetObservable$.stream([
|
|
|
566
579
|
"12345",
|
|
567
580
|
]);
|
|
568
581
|
```
|
|
569
|
-
|
|
582
|
+
|
|
583
|
+
In this example, the observable is first refined with a condition to check for a string that includes "2". This string,
|
|
584
|
+
if it passes the condition, is then transformed into its length via a then invocation. Further, this length is filtered
|
|
585
|
+
down to lengths that are greater than 4. The lengths that pass this condition are thus doubled and the resulting
|
|
586
|
+
observable is set to be a once-off observable to which a listener is subscribed. `
|
|
570
587
|
|
|
571
588
|
## Methods
|
|
572
589
|
|
|
@@ -595,6 +612,7 @@ In this example, the observable is first refined with a condition to check for a
|
|
|
595
612
|
| `.setOnce()` | pipe object | observable will send a value to the subscriber only once, and the subscriber will unsubscribe. |
|
|
596
613
|
| `.unsubscribeByNegative(*condition)` | pipe object | observable will send a value to the subscriber as long as the condition is positive, on the first negative result, the subscriber will unsubscribe |
|
|
597
614
|
| `.unsubscribeByPositive(*condition)` | pipe object | observable will send a value to the subscriber as long as the condition is negative, on the first positive result, the subscriber will unsubscribe |
|
|
615
|
+
| `.unsubscribeBy(*condition)` | pipe object | observable will send a value to the subscriber as long as the condition is negative, on the first positive result, the subscriber will unsubscribe |
|
|
598
616
|
| `.emitByNegative(*condition)` | pipe object | observable will send a value to the listener only if condition returns "false", there is no automatic unsubscription |
|
|
599
617
|
| `.emitByPositive(*condition)` | pipe object | observable will send a value to the listener only if condition returns "true", there is no automatic unsubscription |
|
|
600
618
|
| `.refine(*condition)` | pipe object | observable will send a value to the listener only if condition returns "true", there is no automatic unsubscription |
|
|
@@ -604,6 +622,8 @@ In this example, the observable is first refined with a condition to check for a
|
|
|
604
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". |
|
|
605
623
|
| `.pushCases(*conditions)` | PipeCase object | This method allows you to add a group of conditions for filtering cases data in the pipeline chain. |
|
|
606
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. |
|
|
607
627
|
| `.subscribe(listener)` | subscriber | subscribe listener to observable |
|
|
608
628
|
|
|
609
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)}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)}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)}
|
|
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
|
@@ -6,13 +6,16 @@ export declare abstract class Pipe<T> implements ISubscribe<T> {
|
|
|
6
6
|
setOnce(): ISubscribe<T>;
|
|
7
7
|
unsubscribeByNegative(condition: ICallback<T>): ISetup<T>;
|
|
8
8
|
unsubscribeByPositive(condition: ICallback<T>): ISetup<T>;
|
|
9
|
+
unsubscribeBy(condition: ICallback<T>): ISetup<T>;
|
|
9
10
|
emitByNegative(condition: ICallback<T>): ISetup<T>;
|
|
10
11
|
emitByPositive(condition: ICallback<T>): ISetup<T>;
|
|
11
12
|
refine(condition: ICallback<T>): ISetup<T>;
|
|
12
|
-
then<K>(condition: ICallback<T>): ISetup<K>;
|
|
13
13
|
pushRefiners(conditions: ICallback<any>[]): ISetup<T>;
|
|
14
14
|
emitMatch(condition: ICallback<T>): ISetup<T>;
|
|
15
15
|
switch(): SwitchCase<T>;
|
|
16
|
+
then<K>(condition: ICallback<T>): ISetup<K>;
|
|
17
|
+
serialize(): ISetup<string>;
|
|
18
|
+
deserialize<K>(): ISetup<K>;
|
|
16
19
|
processChain(listener: IListener<T>): void;
|
|
17
20
|
}
|
|
18
21
|
export declare class SwitchCase<T> implements ISubscribe<T>, IPipeCase<T> {
|
package/src/outLib/Pipe.js
CHANGED
|
@@ -30,6 +30,9 @@ class Pipe {
|
|
|
30
30
|
});
|
|
31
31
|
return this;
|
|
32
32
|
}
|
|
33
|
+
unsubscribeBy(condition) {
|
|
34
|
+
return this.unsubscribeByPositive(condition);
|
|
35
|
+
}
|
|
33
36
|
emitByNegative(condition) {
|
|
34
37
|
const data = this.pipeData;
|
|
35
38
|
this.chainHandlers.push(() => {
|
|
@@ -49,14 +52,6 @@ class Pipe {
|
|
|
49
52
|
refine(condition) {
|
|
50
53
|
return this.emitByPositive(condition);
|
|
51
54
|
}
|
|
52
|
-
then(condition) {
|
|
53
|
-
const data = this.pipeData;
|
|
54
|
-
this.chainHandlers.push(() => {
|
|
55
|
-
data.payload = condition(data.payload);
|
|
56
|
-
data.isAvailable = true;
|
|
57
|
-
});
|
|
58
|
-
return this;
|
|
59
|
-
}
|
|
60
55
|
pushRefiners(conditions) {
|
|
61
56
|
if (!Array.isArray(conditions))
|
|
62
57
|
return this;
|
|
@@ -75,6 +70,30 @@ class Pipe {
|
|
|
75
70
|
switch() {
|
|
76
71
|
return new SwitchCase(this);
|
|
77
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
|
+
}
|
|
78
97
|
processChain(listener) {
|
|
79
98
|
const chain = this.chainHandlers;
|
|
80
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;
|
|
@@ -64,22 +64,24 @@ export type ISend<T> = {
|
|
|
64
64
|
send(value: T): void;
|
|
65
65
|
};
|
|
66
66
|
export type IUnsubscribeByNegative<T> = {
|
|
67
|
-
unsubscribeByNegative(condition: ICallback<
|
|
67
|
+
unsubscribeByNegative(condition: ICallback<T>): ISetup<T>;
|
|
68
68
|
};
|
|
69
69
|
export type IOrderedUnsubscribeByNegative<T> = {
|
|
70
|
-
unsubscribeByNegative(condition: ICallback<
|
|
70
|
+
unsubscribeByNegative(condition: ICallback<T>): IOrderedSetup<T>;
|
|
71
71
|
};
|
|
72
72
|
export type IUnsubscribeByPositive<T> = {
|
|
73
|
-
unsubscribeByPositive(condition: ICallback<
|
|
73
|
+
unsubscribeByPositive(condition: ICallback<T>): ISetup<T>;
|
|
74
|
+
unsubscribeBy(condition: ICallback<T>): ISetup<T>;
|
|
74
75
|
};
|
|
75
76
|
export type IOrderedUnsubscribeByPositive<T> = {
|
|
76
|
-
unsubscribeByPositive(condition: ICallback<
|
|
77
|
+
unsubscribeByPositive(condition: ICallback<T>): IOrderedSetup<T>;
|
|
78
|
+
unsubscribeBy(condition: ICallback<T>): ISetup<T>;
|
|
77
79
|
};
|
|
78
80
|
export type IEmitByNegative<T> = {
|
|
79
|
-
emitByNegative(condition: ICallback<
|
|
81
|
+
emitByNegative(condition: ICallback<T>): ISetup<T>;
|
|
80
82
|
};
|
|
81
83
|
export type IOrderedEmitByNegative<T> = {
|
|
82
|
-
emitByNegative(condition: ICallback<
|
|
84
|
+
emitByNegative(condition: ICallback<T>): IOrderedSetup<T>;
|
|
83
85
|
};
|
|
84
86
|
export type IEmitByPositive<T> = {
|
|
85
87
|
emitByPositive(condition: ICallback<T>): ISetup<T>;
|
|
@@ -89,6 +91,10 @@ export type IEmitByPositive<T> = {
|
|
|
89
91
|
export type ITransform<T> = {
|
|
90
92
|
then<K>(condition: ICallback<T>): ISetup<K>;
|
|
91
93
|
};
|
|
94
|
+
export type ISerialisation = {
|
|
95
|
+
serialize(): ISetup<string>;
|
|
96
|
+
deserialize<K>(): ISetup<K>;
|
|
97
|
+
};
|
|
92
98
|
export type IOrderedEmitByPositive<T> = {
|
|
93
99
|
emitByPositive(condition: ICallback<any>): IOrderedSetup<T>;
|
|
94
100
|
refine(condition: ICallback<any>): ISetup<T>;
|
|
@@ -97,6 +103,10 @@ export type IOrderedEmitByPositive<T> = {
|
|
|
97
103
|
export type IOrderedTransform<T> = {
|
|
98
104
|
then<K>(condition: ICallback<T>): ISetup<K>;
|
|
99
105
|
};
|
|
106
|
+
export type IOrderedSerialisation = {
|
|
107
|
+
serialize(): ISetup<string>;
|
|
108
|
+
deserialize<K>(): ISetup<K>;
|
|
109
|
+
};
|
|
100
110
|
export type IEmitMatchCondition<T> = {
|
|
101
111
|
emitMatch(condition: ICallback<any>): ISetup<T>;
|
|
102
112
|
};
|