evg_observable 1.0.10 → 1.0.11

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
@@ -47,5 +47,99 @@ observable$.destroy(); // all subscribers have automatically unsubscribed
47
47
  ```
48
48
 
49
49
  ## Observable pipe usage
50
+ ### pipe().setOnce()
50
51
 
51
- ###
52
+ Observable will send a value to the subscriber only once, and the subscriber will unsubscribe.
53
+
54
+ ```ts
55
+ import {Observable} from "evg_observable/src/outLib/Observables/Observable";
56
+
57
+ const observable$ = new Observable('Some typed data (not only string)');
58
+ const listener1 = (value: string) => console.log('listener1:',value);
59
+ const listener2 = (value: string) => console.log('listener2:',value);
60
+
61
+ const subscriber1 = observable$
62
+ .pipe()
63
+ .setOnce()
64
+ .subscribe(listener1);
65
+ const subscriber2 = observable$.subscribe(listener2);
66
+
67
+ console.log(observable$.getValue());
68
+ // Print to console - Some typed data (not only string)
69
+
70
+ observable$.next('Next1 typed data')
71
+ // Print to console - listener1: Next1 typed data
72
+ // Print to console - listener2: Next1 typed data
73
+
74
+ observable$.next('Next2 typed data')
75
+ // Print to console - listener2: Next2 typed data
76
+
77
+ // subscriber1 is automatically unsubscribed after first usage
78
+ ```
79
+ ### pipe().unsubscribeByNegative(condition)
80
+
81
+ Observable will send a value to the subscriber as long as the condition is positive, on the first negative result, the subscriber will unsubscribe.
82
+ ```ts
83
+ import {Observable} from "evg_observable/src/outLib/Observables/Observable";
84
+
85
+ const observable$ = new Observable('Some typed data (not only string)');
86
+ const listener1 = (value: string) => console.log('listener1:', value);
87
+ const listener2 = (value: string) => console.log('listener2:', value);
88
+ let isPositive = true;
89
+
90
+ const subscriber1 = observable$
91
+ .pipe()
92
+ .unsubscribeByNegative(() => isPositive)
93
+ .subscribe(listener1);
94
+ const subscriber2 = observable$.subscribe(listener2);
95
+
96
+ console.log(observable$.getValue());
97
+ // Print to console - Some typed data (not only string)
98
+
99
+ observable$.next('Next1 typed data')
100
+ // Print to console - listener1: Next1 typed data
101
+ // Print to console - listener2: Next1 typed data
102
+
103
+ observable$.next('Next2 typed data')
104
+ // Print to console - listener1: Next2 typed data
105
+ // Print to console - listener2: Next2 typed data
106
+
107
+ isPositive = false;
108
+ observable$.next('Next3 typed data')
109
+ // Print to console - listener2: Next3 typed data
110
+
111
+ // subscriber1 is automatically unsubscribed when negative condition
112
+ ```
113
+ ### pipe().unsubscribeByPositive(condition)
114
+ Observable will send a value to the subscriber as long as the condition is negative, on the first positive result, the subscriber will unsubscribe.
115
+ ```ts
116
+ import {Observable} from "evg_observable/src/outLib/Observables/Observable";
117
+
118
+ const observable$ = new Observable('Some typed data (not only string)');
119
+ const listener1 = (value: string) => console.log('listener1:', value);
120
+ const listener2 = (value: string) => console.log('listener2:', value);
121
+ let isPositive = false;
122
+
123
+ const subscriber1 = observable$
124
+ .pipe()
125
+ .unsubscribeByPositive(() => isPositive)
126
+ .subscribe(listener1);
127
+ const subscriber2 = observable$.subscribe(listener2);
128
+
129
+ console.log(observable$.getValue());
130
+ // Print to console - Some typed data (not only string)
131
+
132
+ observable$.next('Next1 typed data')
133
+ // Print to console - listener1: Next1 typed data
134
+ // Print to console - listener2: Next1 typed data
135
+
136
+ observable$.next('Next2 typed data')
137
+ // Print to console - listener1: Next2 typed data
138
+ // Print to console - listener2: Next2 typed data
139
+
140
+ isPositive = true;
141
+ observable$.next('Next3 typed data')
142
+ // Print to console - listener2: Next3 typed data
143
+
144
+ // subscriber1 is automatically unsubscribed when positive condition
145
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "evg_observable",
3
- "version": "1.0.10",
3
+ "version": "1.0.11",
4
4
  "description": "Light observable",
5
5
  "main": "index.js",
6
6
  "directories": {
@@ -1,9 +0,0 @@
1
- export function deleteFromArray<T>(arr: T[], component: T): boolean {
2
- const index = arr.indexOf(component);
3
- if (index === -1) return false;
4
- const length = arr.length - 1;
5
- for (let i = index; i < length;) arr[i++] = arr[i];
6
- arr.length--;
7
- return true;
8
- }
9
-
@@ -1,44 +0,0 @@
1
- import {ICollector, ISubscriptionLike} from "./Types";
2
- import {deleteFromArray} from "../FunctionLibs";
3
-
4
- export class Collector implements ICollector {
5
- private list: ISubscriptionLike<any>[] = [];
6
- private _isDestroyed = false;
7
-
8
- collect(...subscriptionLikeList: ISubscriptionLike<any>[]): void {
9
- if(this._isDestroyed) return null;
10
- for (let i = 0; i < subscriptionLikeList.length; i++) {
11
- this.list.push(subscriptionLikeList[i]);
12
- }
13
- }
14
-
15
- unsubscribe(subscriptionLike: ISubscriptionLike<any>): void {
16
- if(this._isDestroyed) return null;
17
- subscriptionLike && subscriptionLike.unsubscribe();
18
- deleteFromArray(this.list, subscriptionLike);
19
- }
20
-
21
- unsubscribeAll(): void {
22
- if(this._isDestroyed) return null;
23
- const length = this.list.length;
24
- for (let i = 0; i < length; i++) {
25
- this.unsubscribe(this.list.pop());
26
- }
27
- }
28
-
29
- size(): number {
30
- if(this._isDestroyed) return 0;
31
- return this.list.length;
32
- }
33
-
34
- destroy(): void {
35
- this.unsubscribeAll();
36
- this.list.length = 0;
37
- this.list = <any>0;
38
- this._isDestroyed = true;
39
- }
40
-
41
- get isDestroyed(): boolean {
42
- return this._isDestroyed;
43
- }
44
- }
@@ -1,222 +0,0 @@
1
- import {
2
- ICallback,
3
- IListener,
4
- IObserver,
5
- IOnceMarker,
6
- ISetup,
7
- ISubscribe,
8
- ISubscribeObject,
9
- ISubscriptionLike
10
- } from "./Types";
11
- import {deleteFromArray} from "../FunctionLibs";
12
-
13
- export class SubscribeObject<T> implements ISubscribeObject<T> {
14
- protected observable: IObserver<T>;
15
- protected listener: IListener<T>;
16
- private isListenPaused = false;
17
- private once: IOnceMarker = {isOnce: false, isFinished: false};
18
- private unsubscribeByNegativeCondition: ICallback<any> = null;
19
- private unsubscribeByPositiveCondition: ICallback<any> = null;
20
- private emitByNegativeCondition: ICallback<any> = null;
21
- private emitByPositiveCondition: ICallback<any> = null;
22
- private emitMatchCondition: ICallback<any> = null;
23
- protected _order = 0;
24
-
25
- constructor(observable?: IObserver<T>, listener?: IListener<T>) {
26
- this.observable = observable;
27
- this.listener = listener;
28
- }
29
-
30
- subscribe(listener: IListener<T>): ISubscriptionLike<T> {
31
- this.listener = listener;
32
- return this;
33
- }
34
-
35
- public unsubscribe(): void {
36
- if (!!this.observable) {
37
- this.observable.unSubscribe(this);
38
- this.observable = <any>0;
39
- this.listener = <any>0;
40
- }
41
- }
42
-
43
- send(value: T): void {
44
- switch (true) {
45
- case !this.observable:
46
- case !this.listener:
47
- this.unsubscribe();
48
- return;
49
- case this.isListenPaused:
50
- return;
51
- case this.once.isOnce:
52
- this.once.isFinished = true;
53
- this.listener(value);
54
- this.unsubscribe();
55
- break;
56
- case !!this.unsubscribeByNegativeCondition:
57
- if (!this.unsubscribeByNegativeCondition()) {
58
- this.unsubscribeByNegativeCondition = null;
59
- this.unsubscribe();
60
- return;
61
- }
62
- this.listener(value);
63
- break;
64
- case !!this.unsubscribeByPositiveCondition:
65
- if (this.unsubscribeByPositiveCondition()) {
66
- this.unsubscribeByPositiveCondition = null;
67
- this.unsubscribe();
68
- return;
69
- }
70
- this.listener(value);
71
- break;
72
- case !!this.emitByNegativeCondition:
73
- !this.emitByNegativeCondition() && this.listener(value);
74
- break;
75
- case !!this.emitByPositiveCondition:
76
- this.emitByPositiveCondition() && this.listener(value);
77
- break;
78
- case !!this.emitMatchCondition:
79
- (this.emitMatchCondition() === value) && this.listener(value);
80
- break;
81
- default:
82
- this.listener(value);
83
- }
84
- }
85
-
86
- setOnce(): ISubscribe<T> {
87
- this.once.isOnce = true;
88
- return this;
89
- }
90
-
91
- unsubscribeByNegative(condition: ICallback<any>): ISubscribe<T> {
92
- this.unsubscribeByNegativeCondition = condition;
93
- return this
94
- }
95
-
96
- unsubscribeByPositive(condition: ICallback<any>): ISubscribe<T> {
97
- this.unsubscribeByPositiveCondition = condition;
98
- return this;
99
- }
100
-
101
- emitByNegative(condition: ICallback<any>): ISubscribe<T> {
102
- this.emitByNegativeCondition = condition;
103
- return this;
104
- }
105
-
106
- emitByPositive(condition: ICallback<any>): ISubscribe<T> {
107
- this.emitByPositiveCondition = condition;
108
- return this;
109
- }
110
-
111
- emitMatch(condition: ICallback<any>): ISubscribe<T> {
112
- this.emitMatchCondition = condition;
113
- return this;
114
- }
115
-
116
- resume(): void {
117
- this.isListenPaused = false;
118
- }
119
-
120
- pause(): void {
121
- this.isListenPaused = true;
122
- }
123
-
124
- get order(): number {
125
- return this._order;
126
- }
127
-
128
- set order(value: number) {
129
- this._order = value;
130
- }
131
- }
132
-
133
- export class Observable<T> implements IObserver<T> {
134
- protected listeners: ISubscribeObject<T>[] = [];
135
- private _isEnable: boolean = true;
136
- protected _isDestroyed = false;
137
- private isNextProcess = false;
138
- private listenersForUnsubscribe: ISubscriptionLike<T>[] = [];
139
-
140
- constructor(private value: T) {
141
- }
142
-
143
- disable(): void {
144
- this._isEnable = false;
145
- }
146
-
147
- enable(): void {
148
- this._isEnable = true;
149
- }
150
-
151
- get isEnable(): boolean {
152
- return this._isEnable;
153
- }
154
-
155
- public next(value: T): void {
156
- if (this._isDestroyed) return;
157
- if (!this._isEnable) return;
158
- this.value = value;
159
- this.isNextProcess = true;
160
- for (let i = 0; i < this.listeners.length; i++) this.listeners[i].send(value);
161
- this.isNextProcess = false;
162
- this.handleListenersForUnsubscribe();
163
- }
164
-
165
- private handleListenersForUnsubscribe(): void {
166
- for (const listener of this.listenersForUnsubscribe) {
167
- this.unSubscribe(listener);
168
- }
169
- this.listenersForUnsubscribe.length = 0;
170
- }
171
-
172
- public unSubscribe(listener: ISubscriptionLike<T>): void {
173
- if (this._isDestroyed) return;
174
- if (this.isNextProcess) {
175
- this.listenersForUnsubscribe.push(listener);
176
- return;
177
- }
178
- this.listeners &&
179
- !deleteFromArray(this.listeners, listener);
180
- }
181
-
182
- public destroy(): void {
183
- this.value = <any>0;
184
- this.unsubscribeAll();
185
- this.listeners = <any>0;
186
- this._isDestroyed = true;
187
- }
188
-
189
- public unsubscribeAll(): void {
190
- if (this._isDestroyed) return;
191
- const length = this.listeners.length;
192
- for (let i = 0; i < length; i++) (this.listeners.pop()).unsubscribe();
193
- }
194
-
195
- public getValue(): T {
196
- if (this._isDestroyed) return undefined;
197
- return this.value;
198
- }
199
-
200
- public size(): number {
201
- if (this._isDestroyed) return 0;
202
- return this.listeners.length;
203
- }
204
-
205
- public subscribe(listener: IListener<T>): ISubscriptionLike<T> {
206
- if (this._isDestroyed) return undefined;
207
- const subscribeObject = new SubscribeObject(this, listener);
208
- this.listeners.push(subscribeObject);
209
- return subscribeObject;
210
- }
211
-
212
- pipe(): ISetup<T> {
213
- if (this._isDestroyed) return undefined;
214
- const subscribeObject = new SubscribeObject(this);
215
- this.listeners.push(subscribeObject);
216
- return subscribeObject;
217
- }
218
-
219
- get isDestroyed(): boolean {
220
- return this._isDestroyed;
221
- }
222
- }
@@ -1,86 +0,0 @@
1
- import {Observable, SubscribeObject} from "./Observable";
2
- import {
3
- ICallback,
4
- IListener,
5
- IObserver,
6
- IOrdered,
7
- IOrderedObservable,
8
- IOrderedSetup,
9
- IOrderedSubscribe,
10
- IOrderedSubscriptionLike
11
- } from "./Types";
12
-
13
- export class OrderedSubscribeObject<T> extends SubscribeObject<T> {
14
- constructor(observable: OrderedObservable<T> | IOrdered<T>, listener?: IListener<T>) {
15
- super(<IObserver<T>>observable, listener);
16
- }
17
-
18
- get order(): number {
19
- return this._order;
20
- }
21
-
22
- set order(value: number) {
23
- if (!this.observable ||
24
- (this.observable && this.observable.isDestroyed)) {
25
- this._order = undefined;
26
- return
27
- }
28
- this._order = value;
29
- (<IOrderedObservable><any>this.observable).sortByOrder();
30
- }
31
-
32
- subscribe(listener: IListener<T>): IOrderedSubscriptionLike<T> {
33
- this.listener = listener;
34
- return this;
35
- }
36
-
37
- setOnce(): IOrderedSubscribe<T> {
38
- return <any>super.setOnce();
39
- }
40
-
41
- unsubscribeByNegative(condition: ICallback<any>): IOrderedSubscribe<T> {
42
- return <any>super.unsubscribeByNegative(condition);
43
- }
44
-
45
- unsubscribeByPositive(condition: ICallback<any>): IOrderedSubscribe<T> {
46
- return <any>super.unsubscribeByPositive(condition);
47
- }
48
-
49
- emitByNegative(condition: ICallback<any>): IOrderedSubscribe<T> {
50
- return <any>super.emitByNegative(condition);
51
- }
52
-
53
- emitByPositive(condition: ICallback<any>): IOrderedSubscribe<T> {
54
- return <any>super.emitByPositive(condition);
55
- }
56
-
57
- emitMatch(condition: ICallback<any>): IOrderedSubscribe<T> {
58
- return <any>super.emitMatch(condition);
59
- }
60
- }
61
-
62
- export class OrderedObservable<T>
63
- extends Observable<T> implements IOrdered<T> {
64
- sortByOrder(): void {
65
- if (this._isDestroyed) return undefined;
66
- this.listeners.sort((a, b) => {
67
- if (a.order > b.order) return 1;
68
- if (a.order < b.order) return -1;
69
- return 0;
70
- });
71
- }
72
-
73
- subscribe(listener: IListener<T>): IOrderedSubscriptionLike<T> {
74
- if (this._isDestroyed) return undefined;
75
- const subscribeObject = new OrderedSubscribeObject(this, listener);
76
- this.listeners.push(subscribeObject);
77
- return subscribeObject;
78
- }
79
-
80
- pipe(): IOrderedSetup<T> {
81
- if (this._isDestroyed) return undefined;
82
- const subscribeObject = new OrderedSubscribeObject(this);
83
- this.listeners.push(subscribeObject);
84
- return subscribeObject;
85
- }
86
- }
@@ -1,159 +0,0 @@
1
- export type ICallback<T> = (value?: T) => any;
2
-
3
- export type ISubscribe<T> = {
4
- subscribe(listener: IListener<T>): ISubscriptionLike<T>;
5
- };
6
-
7
- export type IListener<T> = ICallback<T>;
8
-
9
- export type IDestroy = {
10
- destroy(): void;
11
- isDestroyed: boolean;
12
- };
13
-
14
- export type IOnceMarker = {
15
- isOnce: boolean;
16
- isFinished: boolean;
17
- };
18
-
19
- export type IOrder = {
20
- order: number;
21
- };
22
-
23
- export type IOnce<T> = {
24
- setOnce(): ISubscribe<T>;
25
- };
26
-
27
- export type IOrderedOnce<T> = {
28
- setOnce(): IOrderedSubscribe<T>;
29
- };
30
-
31
- export type ISetObservableValue = {
32
- next(value: any): void;
33
- };
34
-
35
- export type ISubscriptionLike<T> = {
36
- unsubscribe(): void;
37
- };
38
-
39
- export type ISetup<T> =
40
- IUnsubscribeByNegative<T> &
41
- IUnsubscribeByPositive<T> &
42
- IEmitByNegative<T> &
43
- IEmitByPositive<T> &
44
- IEmitMatchCondition<T> &
45
- IOnce<T> &
46
- ISubscribe<T>;
47
-
48
- export type IOrderedSetup<T> =
49
- IOrderedUnsubscribeByNegative<T> &
50
- IOrderedUnsubscribeByPositive<T> &
51
- IOrderedEmitByNegative<T> &
52
- IOrderedEmitByPositive<T> &
53
- IOrderedEmitMatchCondition<T> &
54
- IOrderedOnce<T> &
55
- IOrderedSubscribe<T>;
56
-
57
- export type ISubscribeObject<T> =
58
- ISubscriptionLike<T> &
59
- IPause &
60
- IOrder &
61
- ISend<T> &
62
- ISetup<T>;
63
-
64
- export type ISubscribeCounter = {
65
- size(): number;
66
- };
67
-
68
- export type ISubscriber<T> =
69
- {
70
- getValue(): T,
71
- isEnable: boolean
72
- } &
73
- ISubscribe<T>;
74
-
75
- export type IObserver<T> =
76
- ISetObservableValue &
77
- ISubscriber<T> &
78
- IDestroy &
79
- ISubscribeCounter &
80
- IObservablePipe<T> &
81
- {
82
- unSubscribe(ISubscriptionLike): void,
83
- unsubscribeAll(): void,
84
- disable(): void,
85
- enable(): void,
86
- };
87
-
88
- export type IPause = {
89
- pause(): void;
90
- resume(): void;
91
- };
92
-
93
- export type IObservablePipe<T> = {
94
- pipe(): ISetup<T>
95
- };
96
-
97
- export type ISend<T> = {
98
- send(value: T): void;
99
- };
100
-
101
- export type IUnsubscribeByNegative<T> = {
102
- unsubscribeByNegative(condition: ICallback<any>): ISubscribe<T>;
103
- };
104
-
105
- export type IOrderedUnsubscribeByNegative<T> = {
106
- unsubscribeByNegative(condition: ICallback<any>): IOrderedSubscribe<T>;
107
- };
108
-
109
- export type IUnsubscribeByPositive<T> = {
110
- unsubscribeByPositive(condition: ICallback<any>): ISubscribe<T>;
111
- };
112
-
113
- export type IOrderedUnsubscribeByPositive<T> = {
114
- unsubscribeByPositive(condition: ICallback<any>): IOrderedSubscribe<T>;
115
- };
116
-
117
- export type IEmitByNegative<T> = {
118
- emitByNegative(condition: ICallback<any>): ISubscribe<T>;
119
- };
120
-
121
- export type IOrderedEmitByNegative<T> = {
122
- emitByNegative(condition: ICallback<any>): IOrderedSubscribe<T>;
123
- };
124
-
125
- export type IEmitByPositive<T> = {
126
- emitByPositive(condition: ICallback<any>): ISubscribe<T>;
127
- };
128
-
129
- export type IOrderedEmitByPositive<T> = {
130
- emitByPositive(condition: ICallback<any>): IOrderedSubscribe<T>;
131
- };
132
-
133
- export type IEmitMatchCondition<T> = {
134
- emitMatch(condition: ICallback<any>): ISubscribe<T>;
135
- };
136
-
137
- export type IOrderedEmitMatchCondition<T> = {
138
- emitMatch(condition: ICallback<any>): IOrderedSubscribe<T>;
139
- };
140
-
141
- export type ICollector =
142
- IDestroy &
143
- ISubscribeCounter &
144
- {
145
- collect(...subscriptionLikeList: ISubscriptionLike<any>[]): void;
146
- unsubscribe(subscriptionLike: ISubscriptionLike<any>): void;
147
- unsubscribeAll(): void;
148
- };
149
-
150
- export type IOrderedObservable = {
151
- sortByOrder(): void;
152
- };
153
-
154
- export type IOrdered<T> = IObserver<T> & IOrderedObservable;
155
-
156
- export type IOrderedSubscriptionLike<T> = ISubscriptionLike<T> & IOrder;
157
- export type IOrderedSubscribe<T> = {
158
- subscribe(listener: IListener<T>): IOrderedSubscriptionLike<T>;
159
- };