evg_observable 1.2.33 → 1.3.35

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
@@ -1,3 +1,4 @@
1
+ [![Socket Badge](https://socket.dev/api/badge/npm/package/evg_observable)](https://socket.dev/npm/package/evg_observable)
1
2
  <h1 align=center style="color: saddlebrown">
2
3
  EVG Observable
3
4
  </h1>
@@ -25,9 +26,14 @@ EVG Observable - is a small library for serving asynchronous events.
25
26
  import {Observable} from "evg_observable/src/outLib/Observable";
26
27
 
27
28
  const observable$ = new Observable('Some typed data (not only string)');
29
+
28
30
  const listener1 = (value: string) => console.log('listener1:', value);
31
+ const errorHandler1 = (errorData: any, errorMessage: any) => {
32
+ console.log(`listener1 catch ERROR: data ${errorData}`, errorMessage);
33
+ };
34
+ const subscriber1 = observable$.subscribe(listener1, errorHandler1);
35
+
29
36
  const listener2 = (value: string) => console.log('listener2:', value);
30
- const subscriber1 = observable$.subscribe(listener1);
31
37
  const subscriber2 = observable$.subscribe(listener2);
32
38
 
33
39
  console.log(observable$.getValue());
@@ -295,20 +301,21 @@ observable$.next('SOME DATA');
295
301
 
296
302
  ### Observable
297
303
 
298
- | method | will return | description |
299
- | :--- | :--- | :--- |
300
- | `.subscribe(listener)` | subscriber | subscribe listener to observable |
304
+ | method | will return | description |
305
+ |:---------------------------| :--- | :--- |
306
+ | `.subscribe(listener)` | subscriber | subscribe listener to observable |
301
307
  | `.unSubscribe(subscriber)` | void | unsubscribe listener from observable |
302
- | `.unsubscribeAll()` | void | unsubscribe all listeners from the current observable |
303
- | `.next(value)` | void | emit data to listeners |
304
- | `.getValue()` | value | will return the last value sent, or the value that was set during initialization |
305
- | `.size()` | number | will return the current number of subscribers |
306
- | `.disable()`| void | disable emission |
307
- | `.enable()`| void | enable emission |
308
- | `.isEnable` | boolean | read-only field that shows the state of the observer |
309
- | `.destroy()` | void | unsubscribe all listeners from the current observable and destroy it |
310
- | `.isDestroyed` | boolean | read-only field that shows the kill state of the observer |
311
- | `.pipe()` | pipe condition object | returns an object with which you can customize the subscriber's behavior |
308
+ | `.unsubscribeAll()` | void | unsubscribe all listeners from the current observable |
309
+ | `.next(value)` | void | emit data to listeners |
310
+ | `.stream(value[])` | void | pass data to listeners in parts of the array |
311
+ | `.getValue()` | value | will return the last value sent, or the value that was set during initialization |
312
+ | `.size()` | number | will return the current number of subscribers |
313
+ | `.disable()` | void | disable emission |
314
+ | `.enable()` | void | enable emission |
315
+ | `.isEnable` | boolean | read-only field that shows the state of the observer |
316
+ | `.destroy()` | void | unsubscribe all listeners from the current observable and destroy it |
317
+ | `.isDestroyed` | boolean | read-only field that shows the kill state of the observer |
318
+ | `.pipe()` | pipe condition object | returns an object with which you can customize the subscriber's behavior |
312
319
 
313
320
  ### Observable`.pipe()`
314
321
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "evg_observable",
3
- "version": "1.2.33",
4
- "description": "Light observable",
3
+ "version": "1.3.35",
4
+ "description": "Alternative fast and light library version - observable.",
5
5
  "directories": {
6
6
  "test": "test"
7
7
  },
@@ -21,7 +21,7 @@
21
21
  },
22
22
  "homepage": "https://github.com/BarushevEA/light-observable-ts#readme",
23
23
  "devDependencies": {
24
- "typescript": "^4.9.4",
24
+ "typescript": "^4.9.5",
25
25
  "@testdeck/mocha": "^0.3.3",
26
26
  "@types/chai": "^4.3.4",
27
27
  "chai": "^4.3.7",
@@ -1,4 +1,4 @@
1
- import { ICallback, IErrorCallback, IListener, IMarkedForUnsubscribe, IObserver, ISetup, ISubscribe, ISubscribeObject, ISubscriptionLike } from "./Types";
1
+ import { ICallback, IErrorCallback, IListener, IMarkedForUnsubscribe, IObserver, ISetup, IStream, ISubscribe, ISubscribeObject, ISubscriptionLike } from "./Types";
2
2
  export declare class SubscribeObject<T> implements ISubscribeObject<T>, IMarkedForUnsubscribe {
3
3
  isMarkedForUnsubscribe: boolean;
4
4
  protected observable: IObserver<T> | undefined;
@@ -29,7 +29,7 @@ export declare class SubscribeObject<T> implements ISubscribeObject<T>, IMarkedF
29
29
  get order(): number;
30
30
  set order(value: number);
31
31
  }
32
- export declare class Observable<T> implements IObserver<T> {
32
+ export declare class Observable<T> implements IObserver<T>, IStream<T> {
33
33
  private value;
34
34
  protected listeners: ISubscribeObject<T>[];
35
35
  private _isEnable;
@@ -41,6 +41,7 @@ export declare class Observable<T> implements IObserver<T> {
41
41
  enable(): void;
42
42
  get isEnable(): boolean;
43
43
  next(value: T): void;
44
+ stream(values: T[]): void;
44
45
  private handleListenersForUnsubscribe;
45
46
  unSubscribe(listener: ISubscriptionLike<T>): void;
46
47
  destroy(): void;
@@ -22,11 +22,14 @@ class SubscribeObject {
22
22
  }
23
23
  static callbackSend(value, subsObj) {
24
24
  const listener = subsObj.listener;
25
- if (!listener || !subsObj.observable) {
25
+ if (!listener) {
26
26
  subsObj.unsubscribe();
27
27
  return;
28
28
  }
29
29
  switch (true) {
30
+ case !subsObj.observable:
31
+ subsObj.unsubscribe();
32
+ return;
30
33
  case subsObj.isListenPaused:
31
34
  return;
32
35
  case !subsObj.isPipe:
@@ -156,8 +159,8 @@ class Observable {
156
159
  return;
157
160
  if (!this._isEnable)
158
161
  return;
159
- this.value = value;
160
162
  this.isNextProcess = true;
163
+ this.value = value;
161
164
  const length = this.listeners.length;
162
165
  for (let i = 0; i < length; i++) {
163
166
  this.listeners[i].send(value);
@@ -165,6 +168,15 @@ class Observable {
165
168
  this.isNextProcess = false;
166
169
  this.listenersForUnsubscribe.length && this.handleListenersForUnsubscribe();
167
170
  }
171
+ stream(values) {
172
+ if (this._isDestroyed)
173
+ return;
174
+ if (!this._isEnable)
175
+ return;
176
+ for (let i = 0; i < values.length; i++) {
177
+ this.next(values[i]);
178
+ }
179
+ }
168
180
  handleListenersForUnsubscribe() {
169
181
  const length = this.listenersForUnsubscribe.length;
170
182
  for (let i = 0; i < length; i++) {
@@ -46,6 +46,9 @@ export type IObserver<T> = ISetObservableValue & ISubscriber<T> & IDestroy & ISu
46
46
  disable(): void;
47
47
  enable(): void;
48
48
  };
49
+ export type IStream<T> = {
50
+ stream(value: T[]): void;
51
+ };
49
52
  export type IPause = {
50
53
  pause(): void;
51
54
  resume(): void;