evg_observable 1.10.50 → 1.11.50
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
CHANGED
|
@@ -534,6 +534,40 @@ Observable an invaluable tool for managing asynchronous events.
|
|
|
534
534
|
Built with the developer's needs in mind, EVG Observable provides a wealth of capabilities at your disposal, making
|
|
535
535
|
event handling a breeze.
|
|
536
536
|
|
|
537
|
+
Here is an advanced example of the `pipe` usage which introduces a new method called `then`. It allows transforming payload data in the pipe chain by applying a user callback function.
|
|
538
|
+
|
|
539
|
+
Here is the syntax:
|
|
540
|
+
|
|
541
|
+
```typescript
|
|
542
|
+
const targetObservable$ = new Observable("");
|
|
543
|
+
const targetListener = (num: number) => console.log(num);
|
|
544
|
+
|
|
545
|
+
targetObservable$
|
|
546
|
+
.pipe()
|
|
547
|
+
.refine(str => str.includes("2")) // check if a string contains "2"
|
|
548
|
+
.then<number>(str => str.length) // transform the string to its length
|
|
549
|
+
.refine(num => num > 4) // filter out the lengths that is greater than 4
|
|
550
|
+
.then<number>(num => num * 2) // multiply the length by 2
|
|
551
|
+
.setOnce() // make sure this action only happens once
|
|
552
|
+
.subscribe(targetListener); // subscribe the listener to the observable
|
|
553
|
+
|
|
554
|
+
targetObservable$.stream([
|
|
555
|
+
"1",
|
|
556
|
+
"12",
|
|
557
|
+
"123",
|
|
558
|
+
"123",
|
|
559
|
+
"1234",
|
|
560
|
+
"12345",
|
|
561
|
+
"12345",
|
|
562
|
+
"12345",
|
|
563
|
+
"12345",
|
|
564
|
+
"12345",
|
|
565
|
+
"12345",
|
|
566
|
+
"12345",
|
|
567
|
+
]);
|
|
568
|
+
```
|
|
569
|
+
In this example, the observable is first refined with a condition to check for a string that includes "2". This string, if it passes the condition, is then transformed into its length via a then invocation. Further, this length is filtered down to lengths that are greater than 4. The lengths that pass this condition are thus doubled and the resulting observable is set to be a once-off observable to which a listener is subscribed. `
|
|
570
|
+
|
|
537
571
|
## Methods
|
|
538
572
|
|
|
539
573
|
### Observable
|
|
@@ -556,20 +590,21 @@ event handling a breeze.
|
|
|
556
590
|
|
|
557
591
|
### Observable`.pipe()`
|
|
558
592
|
|
|
559
|
-
| method | will return
|
|
560
|
-
|
|
561
|
-
| `.setOnce()` | pipe object
|
|
562
|
-
| `.unsubscribeByNegative(*condition)` | pipe object
|
|
563
|
-
| `.unsubscribeByPositive(*condition)` | pipe object
|
|
564
|
-
| `.emitByNegative(*condition)` | pipe object
|
|
565
|
-
| `.emitByPositive(*condition)` | pipe object
|
|
566
|
-
| `.refine(*condition)` | pipe object
|
|
567
|
-
| `.pushRefiners(*conditions)` | pipe object
|
|
568
|
-
| `.emitMatch(*condition)` | pipe object
|
|
569
|
-
| `.switch()` | SwitchCase object
|
|
570
|
-
| `.case(*condition)` | PipeCase object
|
|
571
|
-
| `.pushCases(*conditions)` | PipeCase object
|
|
572
|
-
| `.
|
|
593
|
+
| method | will return | description |
|
|
594
|
+
|:-------------------------------------|:---------------------------------------|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
595
|
+
| `.setOnce()` | pipe object | observable will send a value to the subscriber only once, and the subscriber will unsubscribe. |
|
|
596
|
+
| `.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
|
+
| `.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 |
|
|
598
|
+
| `.emitByNegative(*condition)` | pipe object | observable will send a value to the listener only if condition returns "false", there is no automatic unsubscription |
|
|
599
|
+
| `.emitByPositive(*condition)` | pipe object | observable will send a value to the listener only if condition returns "true", there is no automatic unsubscription |
|
|
600
|
+
| `.refine(*condition)` | pipe object | observable will send a value to the listener only if condition returns "true", there is no automatic unsubscription |
|
|
601
|
+
| `.pushRefiners(*conditions)` | pipe object | This method allows you to add a group of conditions for filtering data in the pipeline chain. |
|
|
602
|
+
| `.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 |
|
|
603
|
+
| `.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. |
|
|
604
|
+
| `.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
|
+
| `.pushCases(*conditions)` | PipeCase object | This method allows you to add a group of conditions for filtering cases data in the pipeline chain. |
|
|
606
|
+
| `.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. |
|
|
607
|
+
| `.subscribe(listener)` | subscriber | subscribe listener to observable |
|
|
573
608
|
|
|
574
609
|
_*condition_ - this is a function that should return a value that will affect the behavior of the subscriber
|
|
575
610
|
|
package/package.json
CHANGED
|
@@ -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
|
@@ -8,7 +8,8 @@ export declare abstract class Pipe<T> implements ISubscribe<T> {
|
|
|
8
8
|
unsubscribeByPositive(condition: ICallback<T>): ISetup<T>;
|
|
9
9
|
emitByNegative(condition: ICallback<T>): ISetup<T>;
|
|
10
10
|
emitByPositive(condition: ICallback<T>): ISetup<T>;
|
|
11
|
-
refine(condition: ICallback<
|
|
11
|
+
refine(condition: ICallback<T>): ISetup<T>;
|
|
12
|
+
then<K>(condition: ICallback<T>): ISetup<K>;
|
|
12
13
|
pushRefiners(conditions: ICallback<any>[]): ISetup<T>;
|
|
13
14
|
emitMatch(condition: ICallback<T>): ISetup<T>;
|
|
14
15
|
switch(): SwitchCase<T>;
|
package/src/outLib/Pipe.js
CHANGED
|
@@ -49,6 +49,14 @@ class Pipe {
|
|
|
49
49
|
refine(condition) {
|
|
50
50
|
return this.emitByPositive(condition);
|
|
51
51
|
}
|
|
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
|
+
}
|
|
52
60
|
pushRefiners(conditions) {
|
|
53
61
|
if (!Array.isArray(conditions))
|
|
54
62
|
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;
|
|
@@ -82,15 +82,21 @@ export type IOrderedEmitByNegative<T> = {
|
|
|
82
82
|
emitByNegative(condition: ICallback<any>): IOrderedSetup<T>;
|
|
83
83
|
};
|
|
84
84
|
export type IEmitByPositive<T> = {
|
|
85
|
-
emitByPositive(condition: ICallback<
|
|
86
|
-
refine(condition: ICallback<
|
|
87
|
-
pushRefiners(conditions: ICallback<
|
|
85
|
+
emitByPositive(condition: ICallback<T>): ISetup<T>;
|
|
86
|
+
refine(condition: ICallback<T>): ISetup<T>;
|
|
87
|
+
pushRefiners(conditions: ICallback<T>[]): ISetup<T>;
|
|
88
|
+
};
|
|
89
|
+
export type ITransform<T> = {
|
|
90
|
+
then<K>(condition: ICallback<T>): ISetup<K>;
|
|
88
91
|
};
|
|
89
92
|
export type IOrderedEmitByPositive<T> = {
|
|
90
93
|
emitByPositive(condition: ICallback<any>): IOrderedSetup<T>;
|
|
91
94
|
refine(condition: ICallback<any>): ISetup<T>;
|
|
92
95
|
pushRefiners(conditions: ICallback<any>[]): ISetup<T>;
|
|
93
96
|
};
|
|
97
|
+
export type IOrderedTransform<T> = {
|
|
98
|
+
then<K>(condition: ICallback<T>): ISetup<K>;
|
|
99
|
+
};
|
|
94
100
|
export type IEmitMatchCondition<T> = {
|
|
95
101
|
emitMatch(condition: ICallback<any>): ISetup<T>;
|
|
96
102
|
};
|