evg_observable 1.4.35 → 1.4.37

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "evg_observable",
3
- "version": "1.4.35",
3
+ "version": "1.4.37",
4
4
  "description": "Alternative fast and light library version - observable.",
5
5
  "directories": {
6
6
  "test": "test"
@@ -2,8 +2,8 @@ import { ICollector, ISubscriptionLike } from "./Types";
2
2
  export declare class Collector implements ICollector {
3
3
  protected list: ISubscriptionLike<any>[];
4
4
  private _isDestroyed;
5
- collect(...subscriptionLikeList: ISubscriptionLike<any>[]): void | null;
6
- unsubscribe(subscriptionLike: ISubscriptionLike<any> | undefined): void | null;
5
+ collect(...subscriptionLikeList: ISubscriptionLike<any>[]): void;
6
+ unsubscribe(subscriptionLike: ISubscriptionLike<any> | undefined): void;
7
7
  unsubscribeAll(): void | null;
8
8
  size(): number;
9
9
  destroy(): void;
@@ -8,24 +8,20 @@ class Collector {
8
8
  this._isDestroyed = false;
9
9
  }
10
10
  collect(...subscriptionLikeList) {
11
- if (this._isDestroyed)
12
- return null;
13
- for (let i = 0; i < subscriptionLikeList.length; i++) {
14
- const subscription = subscriptionLikeList[i];
15
- subscription && this.list.push(subscription);
11
+ if (!this._isDestroyed) {
12
+ this.list.push(...subscriptionLikeList);
16
13
  }
17
14
  }
18
15
  unsubscribe(subscriptionLike) {
19
16
  if (this._isDestroyed)
20
- return null;
21
- subscriptionLike && subscriptionLike.unsubscribe();
22
- (0, FunctionLibs_1.deleteFromArray)(this.list, subscriptionLike);
17
+ return;
18
+ subscriptionLike === null || subscriptionLike === void 0 ? void 0 : subscriptionLike.unsubscribe();
19
+ (0, FunctionLibs_1.quickDeleteFromArray)(this.list, subscriptionLike);
23
20
  }
24
21
  unsubscribeAll() {
25
22
  if (this._isDestroyed)
26
- return null;
27
- const length = this.list.length;
28
- for (let i = 0; i < length; i++) {
23
+ return;
24
+ while (this.list.length > 0) {
29
25
  this.unsubscribe(this.list.pop());
30
26
  }
31
27
  }
@@ -1 +1,4 @@
1
1
  export declare function deleteFromArray<T>(arr: T[], component: T): boolean;
2
+ export declare function quickDeleteFromArray<T>(arr: T[], component: T): boolean;
3
+ export declare const negativeCallback: () => boolean;
4
+ export declare const positiveCallback: () => boolean;
@@ -1,14 +1,24 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.deleteFromArray = void 0;
3
+ exports.positiveCallback = exports.negativeCallback = exports.quickDeleteFromArray = exports.deleteFromArray = void 0;
4
4
  function deleteFromArray(arr, component) {
5
5
  const index = arr.indexOf(component);
6
6
  if (index === -1)
7
7
  return false;
8
- const length = arr.length - 1;
9
- for (let i = index; i < length;)
10
- arr[i++] = arr[i];
11
- arr.length = length;
8
+ arr.splice(index, 1);
12
9
  return true;
13
10
  }
14
11
  exports.deleteFromArray = deleteFromArray;
12
+ function quickDeleteFromArray(arr, component) {
13
+ const index = arr.indexOf(component);
14
+ if (index === -1)
15
+ return false;
16
+ arr[index] = arr[arr.length - 1];
17
+ arr.length = arr.length - 1;
18
+ return true;
19
+ }
20
+ exports.quickDeleteFromArray = quickDeleteFromArray;
21
+ const negativeCallback = () => false;
22
+ exports.negativeCallback = negativeCallback;
23
+ const positiveCallback = () => true;
24
+ exports.positiveCallback = positiveCallback;
@@ -34,8 +34,8 @@ export declare class Observable<T> implements IObserver<T>, IStream<T> {
34
34
  protected listeners: ISubscribeObject<T>[];
35
35
  private _isEnable;
36
36
  protected _isDestroyed: boolean;
37
- private isNextProcess;
38
- private listenersForUnsubscribe;
37
+ protected isNextProcess: boolean;
38
+ protected listenersForUnsubscribe: ISubscriptionLike<T>[];
39
39
  constructor(value: T);
40
40
  disable(): void;
41
41
  enable(): void;
@@ -92,32 +92,25 @@ class SubscribeObject {
92
92
  return this;
93
93
  }
94
94
  unsubscribeByNegative(condition) {
95
- if (typeof condition !== "function")
96
- condition = () => false;
97
- this.unsubscribeByNegativeCondition = condition;
95
+ this.unsubscribeByNegativeCondition = !!condition ? condition : FunctionLibs_1.negativeCallback;
98
96
  return this;
99
97
  }
100
98
  unsubscribeByPositive(condition) {
101
- if (typeof condition !== "function")
102
- condition = () => true;
103
- this.unsubscribeByPositiveCondition = condition;
99
+ this.unsubscribeByPositiveCondition = !!condition ? condition : FunctionLibs_1.positiveCallback;
104
100
  return this;
105
101
  }
106
102
  emitByNegative(condition) {
107
- if (typeof condition !== "function")
108
- condition = () => true;
109
- this.emitByNegativeCondition = condition;
103
+ this.emitByNegativeCondition = !!condition ? condition : FunctionLibs_1.positiveCallback;
110
104
  return this;
111
105
  }
112
106
  emitByPositive(condition) {
113
- if (typeof condition !== "function")
114
- condition = () => false;
115
- this.emitByPositiveCondition = condition;
107
+ this.emitByPositiveCondition = !!condition ? condition : FunctionLibs_1.negativeCallback;
116
108
  return this;
117
109
  }
118
110
  emitMatch(condition) {
119
- if (typeof condition !== "function") {
120
- condition = () => `ERROR CONDITION TYPE ${typeof condition}, CONTROL STATE ${this.observable && !this.observable.getValue()}`;
111
+ const type = typeof condition;
112
+ if (type !== "function") {
113
+ condition = () => `ERROR CONDITION TYPE ${type}, CONTROL STATE ${this.observable && !this.observable.getValue()}`;
121
114
  }
122
115
  this.emitMatchCondition = condition;
123
116
  return this;
@@ -194,7 +187,7 @@ class Observable {
194
187
  marker.isMarkedForUnsubscribe = true;
195
188
  return;
196
189
  }
197
- this.listeners && !(0, FunctionLibs_1.deleteFromArray)(this.listeners, listener);
190
+ this.listeners && !(0, FunctionLibs_1.quickDeleteFromArray)(this.listeners, listener);
198
191
  }
199
192
  destroy() {
200
193
  this.value = 0;
@@ -1,5 +1,5 @@
1
1
  import { Observable, SubscribeObject } from "./Observable";
2
- import { ICallback, IErrorCallback, IListener, IOrdered, IOrderedSetup, IOrderedSubscribe, IOrderedSubscriptionLike } from "./Types";
2
+ import { ICallback, IErrorCallback, IListener, IOrdered, IOrderedSetup, IOrderedSubscribe, IOrderedSubscriptionLike, ISubscriptionLike } from "./Types";
3
3
  export declare class OrderedSubscribeObject<T> extends SubscribeObject<T> {
4
4
  constructor(observable: OrderedObservable<T> | IOrdered<T>, isPipe?: boolean);
5
5
  get order(): number;
@@ -16,4 +16,5 @@ export declare class OrderedObservable<T> extends Observable<T> implements IOrde
16
16
  sortByOrder(): boolean;
17
17
  subscribe(listener: IListener<T>, errorHandler?: IErrorCallback): IOrderedSubscriptionLike<T> | undefined;
18
18
  pipe(): IOrderedSetup<T> | undefined;
19
+ unSubscribe(listener: ISubscriptionLike<T>): void;
19
20
  }
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.OrderedObservable = exports.OrderedSubscribeObject = void 0;
4
4
  const Observable_1 = require("./Observable");
5
+ const FunctionLibs_1 = require("./FunctionLibs");
5
6
  class OrderedSubscribeObject extends Observable_1.SubscribeObject {
6
7
  constructor(observable, isPipe) {
7
8
  super(observable, isPipe);
@@ -73,5 +74,16 @@ class OrderedObservable extends Observable_1.Observable {
73
74
  this.listeners.push(subscribeObject);
74
75
  return subscribeObject;
75
76
  }
77
+ unSubscribe(listener) {
78
+ if (this._isDestroyed)
79
+ return;
80
+ if (this.isNextProcess && listener) {
81
+ const marker = listener;
82
+ !marker.isMarkedForUnsubscribe && this.listenersForUnsubscribe.push(listener);
83
+ marker.isMarkedForUnsubscribe = true;
84
+ return;
85
+ }
86
+ this.listeners && !(0, FunctionLibs_1.deleteFromArray)(this.listeners, listener);
87
+ }
76
88
  }
77
89
  exports.OrderedObservable = OrderedObservable;