evg_observable 1.10.50 → 1.11.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 +81 -78
- package/package.json +1 -1
- package/repo/evg_observable.js +1 -1
- package/src/outLib/OrderedSubscribeObject.d.ts +1 -3
- package/src/outLib/OrderedSubscribeObject.js +0 -6
- package/src/outLib/Pipe.d.ts +3 -1
- package/src/outLib/Pipe.js +11 -0
- package/src/outLib/Types.d.ts +19 -11
package/README.md
CHANGED
|
@@ -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
|
-
|
|
231
|
-
console.log(observable$.getValue());
|
|
232
|
-
// Print to console - Some typed data (not only string)
|
|
233
|
-
|
|
234
|
-
observable$.next('Next1 typed data');
|
|
235
|
-
// Print to console - listener1: Next1 typed data
|
|
236
|
-
// Print to console - listener2: Next1 typed data
|
|
237
196
|
|
|
238
|
-
observable
|
|
239
|
-
|
|
240
|
-
// Print to console - listener2: Next2 typed data
|
|
197
|
+
observable$
|
|
198
|
+
.subscribe(listener2);
|
|
241
199
|
|
|
242
|
-
|
|
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
|
|
|
@@ -534,6 +496,45 @@ Observable an invaluable tool for managing asynchronous events.
|
|
|
534
496
|
Built with the developer's needs in mind, EVG Observable provides a wealth of capabilities at your disposal, making
|
|
535
497
|
event handling a breeze.
|
|
536
498
|
|
|
499
|
+
Here is an advanced example of the `pipe` usage which introduces a new method called `then`. It allows transforming
|
|
500
|
+
payload data in the pipe chain by applying a user callback function.
|
|
501
|
+
|
|
502
|
+
Here is the syntax:
|
|
503
|
+
|
|
504
|
+
```typescript
|
|
505
|
+
const targetObservable$ = new Observable("");
|
|
506
|
+
const targetListener = (num: number) => console.log(num);
|
|
507
|
+
|
|
508
|
+
targetObservable$
|
|
509
|
+
.pipe()
|
|
510
|
+
.refine(str => str.includes("2")) // check if a string contains "2"
|
|
511
|
+
.then<number>(str => str.length) // transform the string to its length
|
|
512
|
+
.refine(num => num > 4) // filter out the lengths that is greater than 4
|
|
513
|
+
.then<number>(num => num * 2) // multiply the length by 2
|
|
514
|
+
.setOnce() // make sure this action only happens once
|
|
515
|
+
.subscribe(targetListener); // subscribe the listener to the observable
|
|
516
|
+
|
|
517
|
+
targetObservable$.stream([
|
|
518
|
+
"1",
|
|
519
|
+
"12",
|
|
520
|
+
"123",
|
|
521
|
+
"123",
|
|
522
|
+
"1234",
|
|
523
|
+
"12345",
|
|
524
|
+
"12345",
|
|
525
|
+
"12345",
|
|
526
|
+
"12345",
|
|
527
|
+
"12345",
|
|
528
|
+
"12345",
|
|
529
|
+
"12345",
|
|
530
|
+
]);
|
|
531
|
+
```
|
|
532
|
+
|
|
533
|
+
In this example, the observable is first refined with a condition to check for a string that includes "2". This string,
|
|
534
|
+
if it passes the condition, is then transformed into its length via a then invocation. Further, this length is filtered
|
|
535
|
+
down to lengths that are greater than 4. The lengths that pass this condition are thus doubled and the resulting
|
|
536
|
+
observable is set to be a once-off observable to which a listener is subscribed. `
|
|
537
|
+
|
|
537
538
|
## Methods
|
|
538
539
|
|
|
539
540
|
### Observable
|
|
@@ -556,20 +557,22 @@ event handling a breeze.
|
|
|
556
557
|
|
|
557
558
|
### Observable`.pipe()`
|
|
558
559
|
|
|
559
|
-
| method | will return
|
|
560
|
-
|
|
561
|
-
| `.setOnce()` | pipe object
|
|
562
|
-
| `.unsubscribeByNegative(*condition)` | pipe object
|
|
563
|
-
| `.unsubscribeByPositive(*condition)` | pipe object
|
|
564
|
-
| `.
|
|
565
|
-
| `.
|
|
566
|
-
| `.
|
|
567
|
-
| `.
|
|
568
|
-
| `.
|
|
569
|
-
| `.
|
|
570
|
-
| `.
|
|
571
|
-
| `.
|
|
572
|
-
| `.
|
|
560
|
+
| method | will return | description |
|
|
561
|
+
|:-------------------------------------|:---------------------------------------|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
562
|
+
| `.setOnce()` | pipe object | observable will send a value to the subscriber only once, and the subscriber will unsubscribe. |
|
|
563
|
+
| `.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 |
|
|
564
|
+
| `.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 |
|
|
565
|
+
| `.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 |
|
|
566
|
+
| `.emitByNegative(*condition)` | pipe object | observable will send a value to the listener only if condition returns "false", there is no automatic unsubscription |
|
|
567
|
+
| `.emitByPositive(*condition)` | pipe object | observable will send a value to the listener only if condition returns "true", there is no automatic unsubscription |
|
|
568
|
+
| `.refine(*condition)` | pipe object | observable will send a value to the listener only if condition returns "true", there is no automatic unsubscription |
|
|
569
|
+
| `.pushRefiners(*conditions)` | pipe object | This method allows you to add a group of conditions for filtering data in the pipeline chain. |
|
|
570
|
+
| `.emitMatch(*condition)` | pipe object | observable will send a value to the subscriber only if the return value of the condition matches the data being sent, in this case, there is no automatic unsubscription |
|
|
571
|
+
| `.switch()` | SwitchCase object | transitions the pipe into switch-case mode. In this mode, only the first condition that returns a positive result is triggered, and all others are ignored. This allows you to handle multiple cases more conveniently. |
|
|
572
|
+
| `.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
|
+
| `.pushCases(*conditions)` | PipeCase object | This method allows you to add a group of conditions for filtering cases data in the pipeline chain. |
|
|
574
|
+
| `.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. |
|
|
575
|
+
| `.subscribe(listener)` | subscriber | subscribe listener to observable |
|
|
573
576
|
|
|
574
577
|
_*condition_ - this is a function that should return a value that will affect the behavior of the subscriber
|
|
575
578
|
|
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}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)}then(s){const e=this.pipeData;return this.chainHandlers.push((()=>{e.payload=s(e.payload),e.isAvailable=!0})),this}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)}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))}}})();
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { SubscribeObject } from "./SubscribeObject";
|
|
2
|
-
import { ICallback, IErrorCallback, IListener, IOrdered, IOrderedSetup, IOrderedSubscribe, IOrderedSubscriptionLike, ISetObservableValue
|
|
2
|
+
import { ICallback, IErrorCallback, IListener, IOrdered, IOrderedSetup, IOrderedSubscribe, IOrderedSubscriptionLike, ISetObservableValue } from "./Types";
|
|
3
3
|
import { OrderedObservable } from "./OrderedObservable";
|
|
4
4
|
export declare class OrderedSubscribeObject<T> extends SubscribeObject<T> implements IOrderedSetup<T> {
|
|
5
5
|
constructor(observable: OrderedObservable<T> | IOrdered<T>, isPipe?: boolean);
|
|
@@ -11,7 +11,5 @@ export declare class OrderedSubscribeObject<T> extends SubscribeObject<T> implem
|
|
|
11
11
|
unsubscribeByPositive(condition: ICallback<any>): IOrderedSetup<T>;
|
|
12
12
|
emitByNegative(condition: ICallback<any>): IOrderedSetup<T>;
|
|
13
13
|
emitByPositive(condition: ICallback<any>): IOrderedSetup<T>;
|
|
14
|
-
refine(condition: ICallback<any>): ISetup<T>;
|
|
15
|
-
pushRefiners(conditions: ICallback<any>[]): ISetup<T>;
|
|
16
14
|
emitMatch(condition: ICallback<any>): IOrderedSetup<T>;
|
|
17
15
|
}
|
|
@@ -37,12 +37,6 @@ class OrderedSubscribeObject extends SubscribeObject_1.SubscribeObject {
|
|
|
37
37
|
emitByPositive(condition) {
|
|
38
38
|
return super.emitByPositive(condition);
|
|
39
39
|
}
|
|
40
|
-
refine(condition) {
|
|
41
|
-
return super.emitByPositive(condition);
|
|
42
|
-
}
|
|
43
|
-
pushRefiners(conditions) {
|
|
44
|
-
return super.pushRefiners(conditions);
|
|
45
|
-
}
|
|
46
40
|
emitMatch(condition) {
|
|
47
41
|
return super.emitMatch(condition);
|
|
48
42
|
}
|
package/src/outLib/Pipe.d.ts
CHANGED
|
@@ -6,9 +6,11 @@ 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
|
-
refine(condition: ICallback<
|
|
12
|
+
refine(condition: ICallback<T>): ISetup<T>;
|
|
13
|
+
then<K>(condition: ICallback<T>): ISetup<K>;
|
|
12
14
|
pushRefiners(conditions: ICallback<any>[]): ISetup<T>;
|
|
13
15
|
emitMatch(condition: ICallback<T>): ISetup<T>;
|
|
14
16
|
switch(): SwitchCase<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,6 +52,14 @@ class Pipe {
|
|
|
49
52
|
refine(condition) {
|
|
50
53
|
return this.emitByPositive(condition);
|
|
51
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
|
+
}
|
|
52
63
|
pushRefiners(conditions) {
|
|
53
64
|
if (!Array.isArray(conditions))
|
|
54
65
|
return this;
|
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> & ISubscribe<T>;
|
|
35
|
-
export type IOrderedSetup<T> = IOrderedUnsubscribeByNegative<T> & IOrderedUnsubscribeByPositive<T> & IOrderedEmitByNegative<T> & IOrderedEmitByPositive<T> & IOrderedEmitMatchCondition<T> & IOrderedOnce<T> & IOrderedSwitch<T> & IOrderedSubscribe<T>;
|
|
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>;
|
|
36
36
|
export type ISubscribeObject<T> = ISubscriptionLike & IPause & IOrder & ISend<T> & ISetup<T>;
|
|
37
37
|
export type ISubscribeCounter = {
|
|
38
38
|
size(): number;
|
|
@@ -64,33 +64,41 @@ 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
|
-
emitByPositive(condition: ICallback<
|
|
86
|
-
refine(condition: ICallback<
|
|
87
|
-
pushRefiners(conditions: ICallback<
|
|
87
|
+
emitByPositive(condition: ICallback<T>): ISetup<T>;
|
|
88
|
+
refine(condition: ICallback<T>): ISetup<T>;
|
|
89
|
+
pushRefiners(conditions: ICallback<T>[]): ISetup<T>;
|
|
90
|
+
};
|
|
91
|
+
export type ITransform<T> = {
|
|
92
|
+
then<K>(condition: ICallback<T>): ISetup<K>;
|
|
88
93
|
};
|
|
89
94
|
export type IOrderedEmitByPositive<T> = {
|
|
90
95
|
emitByPositive(condition: ICallback<any>): IOrderedSetup<T>;
|
|
91
96
|
refine(condition: ICallback<any>): ISetup<T>;
|
|
92
97
|
pushRefiners(conditions: ICallback<any>[]): ISetup<T>;
|
|
93
98
|
};
|
|
99
|
+
export type IOrderedTransform<T> = {
|
|
100
|
+
then<K>(condition: ICallback<T>): ISetup<K>;
|
|
101
|
+
};
|
|
94
102
|
export type IEmitMatchCondition<T> = {
|
|
95
103
|
emitMatch(condition: ICallback<any>): ISetup<T>;
|
|
96
104
|
};
|