evg_observable 1.7.45 → 1.9.45

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.7.45",
3
+ "version": "1.9.45",
4
4
  "description": "Alternative fast and light library version - observable.",
5
5
  "directories": {
6
6
  "test": "test"
@@ -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)}class t{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}emitMatch(s){const e=this.pipeData;return this.chainHandlers.push((()=>{s(e.payload)==e.payload&&(e.isAvailable=!0)})),this}switch(){return new r(this)}}class r{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}}class n extends t{constructor(s,e){super(),this.isMarkedForUnsubscribe=!1,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){var i;return this.listener="next"in(i=s)?s=>i.next(s):i,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,function(s,e){const i=e.listener;if(!i)return e.unsubscribe();if(!e.observable)return e.unsubscribe();if(!e.isPaused){if(!e.isPipe)return i(s);for(let s=0;s<e.chainHandlers.length;s++){if(e.pipeData.isNeedUnsubscribe=!1,e.pipeData.isAvailable=!1,e.chainHandlers[s](),e.pipeData.isNeedUnsubscribe)return e.unsubscribe();if(!e.pipeData.isAvailable)return;if(e.pipeData.isBreakChain)break}i(e.pipeData.payload)}}(s,this)}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}}class h{constructor(s){this.value=s,this.listeners=[],this._isEnable=!0,this._isDestroyed=!1,this.isNextProcess=!1,this.listenersForUnsubscribe=[]}disable(){this._isEnable=!1}enable(){this._isEnable=!0}get isEnable(){return this._isEnable}next(s){if(!this._isDestroyed&&this._isEnable){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){if(!this._isDestroyed){if(this.isNextProcess&&s){const e=s;return!e.isMarkedForUnsubscribe&&this.listenersForUnsubscribe.push(s),void(e.isMarkedForUnsubscribe=!0)}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 n(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 n(this,!0);return this.listeners.push(s),s}get isDestroyed(){return this._isDestroyed}}class a extends n{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 u=window;u.Observable=h,u.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}},u.OrderedObservable=class extends h{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 a(this,!1);return this.addObserver(i,s,e),i}pipe(){if(this._isDestroyed)return;const s=new a(this,!0);return this.listeners.push(s),s}unSubscribe(s){if(!this._isDestroyed){if(this.isNextProcess&&s){const e=s;return!e.isMarkedForUnsubscribe&&this.listenersForUnsubscribe.push(s),void(e.isMarkedForUnsubscribe=!0)}this.listeners&&function(s,e){const i=s.indexOf(e);-1!==i&&s.splice(i,1)}(this.listeners,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}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}}class h extends r{constructor(s,e){super(),this.isMarkedForUnsubscribe=!1,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}}get isEmpty(){return 0===this.chainHandlers.length}filter(s){const e=this.pipeData;return this.chainHandlers.push((()=>{s(e.payload)&&(e.isAvailable=!0)})),this}switch(){return new l(this)}processChain(s){const e=this.chainHandlers,i=this.pipeData,t={isOK:!1,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}}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){if(!this._isDestroyed){if(this.isNextProcess&&s){const e=s;return!e.isMarkedForUnsubscribe&&this.listenersForUnsubscribe.push(s),void(e.isMarkedForUnsubscribe=!0)}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){if(!this._isDestroyed){if(this.isNextProcess&&s){const e=s;return!e.isMarkedForUnsubscribe&&this.listenersForUnsubscribe.push(s),void(e.isMarkedForUnsubscribe=!0)}this.listeners&&function(s,e){const i=s.indexOf(e);-1!==i&&s.splice(i,1)}(this.listeners,s)}}}})();
@@ -0,0 +1,17 @@
1
+ import { ICallback, IChainCallback, IErrorCallback, IFilter, IFilterCase, IFilterPayload, IFilterResponse, IFilterSetup, IFilterSwitch } from "./Types";
2
+ export declare class Filter<T> implements IFilter<T>, IFilterSwitch<T> {
3
+ chainHandlers: IChainCallback[];
4
+ pipeData: IFilterPayload;
5
+ private errorHandler;
6
+ get isEmpty(): boolean;
7
+ filter(condition: ICallback<any>): IFilterSetup<T>;
8
+ switch(): FilterSwitchCase<T>;
9
+ processChain(value: T): IFilterResponse;
10
+ addErrorHandler(errorHandler: IErrorCallback): void;
11
+ }
12
+ export declare class FilterSwitchCase<T> implements IFilterCase<T> {
13
+ private pipe;
14
+ private caseCounter;
15
+ constructor(pipe: Filter<T>);
16
+ case(condition: ICallback<any>): IFilterCase<T>;
17
+ }
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FilterSwitchCase = exports.Filter = void 0;
4
+ class Filter {
5
+ chainHandlers = [];
6
+ pipeData = { isBreakChain: false, isAvailable: false, payload: null };
7
+ errorHandler;
8
+ get isEmpty() {
9
+ return this.chainHandlers.length === 0;
10
+ }
11
+ filter(condition) {
12
+ const data = this.pipeData;
13
+ this.chainHandlers.push(() => {
14
+ if (condition(data.payload))
15
+ data.isAvailable = true;
16
+ });
17
+ return this;
18
+ }
19
+ switch() {
20
+ return new FilterSwitchCase(this);
21
+ }
22
+ processChain(value) {
23
+ const chain = this.chainHandlers;
24
+ const data = this.pipeData;
25
+ const response = { isOK: false, payload: undefined };
26
+ data.payload = value;
27
+ data.isBreakChain = false;
28
+ try {
29
+ for (let i = 0; i < chain.length; i++) {
30
+ data.isAvailable = false;
31
+ chain[i]();
32
+ if (!data.isAvailable)
33
+ return response;
34
+ if (data.isBreakChain)
35
+ break;
36
+ }
37
+ }
38
+ catch (err) {
39
+ if (this.errorHandler) {
40
+ this.errorHandler(err, "Filter.processChain ERROR:");
41
+ }
42
+ else {
43
+ console.log("Filter.processChain ERROR:", err);
44
+ }
45
+ return response;
46
+ }
47
+ response.isOK = true;
48
+ response.payload = data.payload;
49
+ return response;
50
+ }
51
+ addErrorHandler(errorHandler) {
52
+ this.errorHandler = errorHandler;
53
+ }
54
+ }
55
+ exports.Filter = Filter;
56
+ class FilterSwitchCase {
57
+ pipe;
58
+ caseCounter;
59
+ constructor(pipe) {
60
+ this.pipe = pipe;
61
+ this.caseCounter = pipe.chainHandlers.length ? pipe.chainHandlers.length : 0;
62
+ }
63
+ case(condition) {
64
+ this.caseCounter++;
65
+ const id = this.caseCounter;
66
+ const data = this.pipe.pipeData;
67
+ const chain = this.pipe.chainHandlers;
68
+ chain.push(() => {
69
+ data.isAvailable = true;
70
+ if (condition(data.payload))
71
+ data.isBreakChain = true;
72
+ if (id === chain.length && !data.isBreakChain)
73
+ data.isAvailable = false;
74
+ });
75
+ return this;
76
+ }
77
+ }
78
+ exports.FilterSwitchCase = FilterSwitchCase;
@@ -1,6 +1,6 @@
1
- import { IListener, ISetObservableValue, ISubscribeObject } from "./Types";
1
+ import { IListener, ISubscribeGroup, ISubscribeObject } from "./Types";
2
2
  export declare const sortAscending: (a: ISubscribeObject<any>, b: ISubscribeObject<any>) => 0 | 1 | -1;
3
3
  export declare const sortDescending: (a: ISubscribeObject<any>, b: ISubscribeObject<any>) => 0 | 1 | -1;
4
4
  export declare function deleteFromArray<T>(arr: T[], component: T): boolean;
5
5
  export declare function quickDeleteFromArray<T>(arr: T[], component: T): boolean;
6
- export declare function getListener<T>(listener: IListener<T> | ISetObservableValue): any;
6
+ export declare function getListener<T>(listener: ISubscribeGroup<T>): IListener<T>;
@@ -35,8 +35,21 @@ function quickDeleteFromArray(arr, component) {
35
35
  }
36
36
  exports.quickDeleteFromArray = quickDeleteFromArray;
37
37
  function getListener(listener) {
38
+ if (Array.isArray(listener)) {
39
+ const group = [];
40
+ for (let i = 0; i < listener.length; i++) {
41
+ group.push(wrapListener(listener[i]));
42
+ }
43
+ return (data) => {
44
+ for (let i = 0; i < group.length; i++)
45
+ group[i](data);
46
+ };
47
+ }
48
+ return wrapListener(listener);
49
+ }
50
+ exports.getListener = getListener;
51
+ function wrapListener(listener) {
38
52
  if ("next" in listener)
39
53
  return (value) => listener.next(value);
40
54
  return listener;
41
55
  }
42
- exports.getListener = getListener;
@@ -1,13 +1,15 @@
1
- import { IErrorCallback, IListener, IObserver, ISetObservableValue, ISetup, IStream, ISubscribeObject, ISubscriptionLike } from "./Types";
1
+ import { IAddFilter, IErrorCallback, IFilterSetup, IObserver, ISetup, IStream, ISubscribeGroup, ISubscribeObject, ISubscriptionLike } from "./Types";
2
2
  import { SubscribeObject } from "./SubscribeObject";
3
- export declare class Observable<T> implements IObserver<T>, IStream<T> {
3
+ export declare class Observable<T> implements IObserver<T>, IStream<T>, IAddFilter<T> {
4
4
  private value;
5
5
  protected listeners: ISubscribeObject<T>[];
6
6
  private _isEnable;
7
7
  protected _isDestroyed: boolean;
8
8
  protected isNextProcess: boolean;
9
9
  protected listenersForUnsubscribe: ISubscriptionLike[];
10
+ private filterCase;
10
11
  constructor(value: T);
12
+ addFilter(errorHandler?: IErrorCallback): IFilterSetup<T>;
11
13
  disable(): void;
12
14
  enable(): void;
13
15
  get isEnable(): boolean;
@@ -19,9 +21,9 @@ export declare class Observable<T> implements IObserver<T>, IStream<T> {
19
21
  unsubscribeAll(): void;
20
22
  getValue(): T | undefined;
21
23
  size(): number;
22
- subscribe(observer: IListener<T> | ISetObservableValue, errorHandler?: IErrorCallback): ISubscriptionLike | undefined;
23
- protected addObserver(subscribeObject: SubscribeObject<T>, observer: IListener<T> | ISetObservableValue, errorHandler?: IErrorCallback): void;
24
- protected isSubsValid(listener: IListener<T> | ISetObservableValue): boolean;
24
+ subscribe(observer: ISubscribeGroup<T>, errorHandler?: IErrorCallback): ISubscriptionLike | undefined;
25
+ protected addObserver(subscribeObject: SubscribeObject<T>, observer: ISubscribeGroup<T>, errorHandler?: IErrorCallback): void;
26
+ protected isSubsValid(listener: ISubscribeGroup<T>): boolean;
25
27
  pipe(): ISetup<T> | undefined;
26
28
  get isDestroyed(): boolean;
27
29
  }
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Observable = void 0;
4
4
  const FunctionLibs_1 = require("./FunctionLibs");
5
5
  const SubscribeObject_1 = require("./SubscribeObject");
6
+ const Filter_1 = require("./Filter");
6
7
  class Observable {
7
8
  value;
8
9
  listeners = [];
@@ -10,9 +11,16 @@ class Observable {
10
11
  _isDestroyed = false;
11
12
  isNextProcess = false;
12
13
  listenersForUnsubscribe = [];
14
+ filterCase = new Filter_1.Filter();
13
15
  constructor(value) {
14
16
  this.value = value;
15
17
  }
18
+ addFilter(errorHandler) {
19
+ if (errorHandler) {
20
+ this.filterCase.addErrorHandler(errorHandler);
21
+ }
22
+ return this.filterCase;
23
+ }
16
24
  disable() {
17
25
  this._isEnable = false;
18
26
  }
@@ -27,6 +35,10 @@ class Observable {
27
35
  return;
28
36
  if (!this._isEnable)
29
37
  return;
38
+ if (!this.filterCase.isEmpty) {
39
+ if (!this.filterCase.processChain(value).isOK)
40
+ return;
41
+ }
30
42
  this.isNextProcess = true;
31
43
  this.value = value;
32
44
  for (let i = 0; i < this.listeners.length; i++)
@@ -1,11 +1,11 @@
1
1
  import { Observable } from "./Observable";
2
- import { IErrorCallback, IListener, IOrdered, IOrderedSetup, IOrderedSubscriptionLike, ISetObservableValue, ISubscriptionLike } from "./Types";
2
+ import { IErrorCallback, IOrdered, IOrderedSetup, IOrderedSubscriptionLike, ISubscribeGroup, ISubscriptionLike } from "./Types";
3
3
  export declare class OrderedObservable<T> extends Observable<T> implements IOrdered<T> {
4
4
  private sortDirection;
5
5
  setAscendingSort(): boolean;
6
6
  setDescendingSort(): boolean;
7
7
  sortByOrder(): boolean;
8
- subscribe(listener: IListener<T> | ISetObservableValue, errorHandler?: IErrorCallback): IOrderedSubscriptionLike | undefined;
8
+ subscribe(listener: ISubscribeGroup<T>, errorHandler?: IErrorCallback): IOrderedSubscriptionLike | undefined;
9
9
  pipe(): IOrderedSetup<T> | undefined;
10
10
  unSubscribe(listener: ISubscriptionLike): void;
11
11
  }
@@ -10,6 +10,7 @@ export declare abstract class Pipe<T> implements ISubscribe<T> {
10
10
  emitByPositive(condition: ICallback<T>): ISetup<T>;
11
11
  emitMatch(condition: ICallback<T>): ISetup<T>;
12
12
  switch(): SwitchCase<T>;
13
+ processChain(listener: IListener<T>): void;
13
14
  }
14
15
  export declare class SwitchCase<T> implements ISubscribe<T>, IPipeCase<T> {
15
16
  private pipe;
@@ -57,6 +57,22 @@ class Pipe {
57
57
  switch() {
58
58
  return new SwitchCase(this);
59
59
  }
60
+ processChain(listener) {
61
+ const chain = this.chainHandlers;
62
+ const data = this.pipeData;
63
+ for (let i = 0; i < chain.length; i++) {
64
+ data.isNeedUnsubscribe = false;
65
+ data.isAvailable = false;
66
+ chain[i]();
67
+ if (data.isNeedUnsubscribe)
68
+ return this.unsubscribe();
69
+ if (!data.isAvailable)
70
+ return;
71
+ if (data.isBreakChain)
72
+ break;
73
+ }
74
+ return listener(data.payload);
75
+ }
60
76
  }
61
77
  exports.Pipe = Pipe;
62
78
  class SwitchCase {
@@ -1,4 +1,4 @@
1
- import { IErrorCallback, IListener, IMarkedForUnsubscribe, IObserver, ISetObservableValue, ISubscribeObject, ISubscriptionLike } from "./Types";
1
+ import { IErrorCallback, IListener, IMarkedForUnsubscribe, IObserver, ISubscribeGroup, ISubscribeObject, ISubscriptionLike } from "./Types";
2
2
  import { Pipe } from "./Pipe";
3
3
  export declare class SubscribeObject<T> extends Pipe<T> implements ISubscribeObject<T>, IMarkedForUnsubscribe {
4
4
  isMarkedForUnsubscribe: boolean;
@@ -9,11 +9,12 @@ export declare class SubscribeObject<T> extends Pipe<T> implements ISubscribeObj
9
9
  isPaused: boolean;
10
10
  isPipe: boolean;
11
11
  constructor(observable?: IObserver<T>, isPipe?: boolean);
12
- subscribe(observer: IListener<T> | ISetObservableValue, errorHandler?: IErrorCallback): ISubscriptionLike;
12
+ subscribe(observer: ISubscribeGroup<T>, errorHandler?: IErrorCallback): ISubscriptionLike;
13
13
  unsubscribe(): void;
14
14
  send(value: T): void;
15
15
  resume(): void;
16
16
  pause(): void;
17
17
  get order(): number;
18
18
  set order(value: number);
19
+ processValue<T>(value: T): void;
19
20
  }
@@ -35,7 +35,7 @@ class SubscribeObject extends Pipe_1.Pipe {
35
35
  try {
36
36
  this.pipeData.payload = value;
37
37
  this.pipeData.isBreakChain = false;
38
- processValue(value, this);
38
+ this.processValue(value);
39
39
  }
40
40
  catch (err) {
41
41
  this.errorHandler(value, err);
@@ -53,28 +53,17 @@ class SubscribeObject extends Pipe_1.Pipe {
53
53
  set order(value) {
54
54
  this._order = value;
55
55
  }
56
- }
57
- exports.SubscribeObject = SubscribeObject;
58
- function processValue(value, subsObj) {
59
- const listener = subsObj.listener;
60
- if (!listener)
61
- return subsObj.unsubscribe();
62
- if (!subsObj.observable)
63
- return subsObj.unsubscribe();
64
- if (subsObj.isPaused)
65
- return;
66
- if (!subsObj.isPipe)
67
- return listener(value);
68
- for (let i = 0; i < subsObj.chainHandlers.length; i++) {
69
- subsObj.pipeData.isNeedUnsubscribe = false;
70
- subsObj.pipeData.isAvailable = false;
71
- subsObj.chainHandlers[i]();
72
- if (subsObj.pipeData.isNeedUnsubscribe)
73
- return subsObj.unsubscribe();
74
- if (!subsObj.pipeData.isAvailable)
56
+ processValue(value) {
57
+ const listener = this.listener;
58
+ if (!listener)
59
+ return this.unsubscribe();
60
+ if (!this.observable)
61
+ return this.unsubscribe();
62
+ if (this.isPaused)
75
63
  return;
76
- if (subsObj.pipeData.isBreakChain)
77
- break;
64
+ if (!this.isPipe)
65
+ return listener(value);
66
+ return this.processChain(listener);
78
67
  }
79
- return listener(subsObj.pipeData.payload);
80
68
  }
69
+ exports.SubscribeObject = SubscribeObject;
@@ -1,11 +1,12 @@
1
1
  import { SwitchCase } from "./Pipe";
2
+ import { FilterSwitchCase } from "./Filter";
2
3
  export type ICallback<T> = (value?: T) => any;
3
4
  export type IMarkedForUnsubscribe = {
4
5
  isMarkedForUnsubscribe: boolean;
5
6
  };
6
7
  export type IErrorCallback = (errorData: any, errorMessage: any) => void;
7
8
  export type ISubscribe<T> = {
8
- subscribe(listener: IListener<T> | ISetObservableValue, errorHandler?: IErrorCallback): ISubscriptionLike | undefined;
9
+ subscribe(listener: ISubscribeGroup<T>, errorHandler?: IErrorCallback): ISubscriptionLike | undefined;
9
10
  };
10
11
  export type IListener<T> = ICallback<T>;
11
12
  export type IDestroy = {
@@ -118,3 +119,27 @@ export type IChainCallback = () => void;
118
119
  export type IPipeCase<T> = {
119
120
  case(condition: ICallback<any>): IPipeCase<T> & ISubscribe<T>;
120
121
  };
122
+ export type ICombinedSubscriber<T> = IListener<T> | ISetObservableValue;
123
+ export type ISubscribeGroup<T> = ICombinedSubscriber<T> | ICombinedSubscriber<T>[];
124
+ export type IAddFilter<T> = {
125
+ addFilter(): IFilterSetup<T>;
126
+ };
127
+ export type IFilterSetup<T> = IFilter<T> & IFilterSwitch<T>;
128
+ export type IFilter<T> = {
129
+ filter(condition: ICallback<any>): IFilterSetup<T>;
130
+ };
131
+ export type IFilterSwitch<T> = {
132
+ switch(): FilterSwitchCase<T>;
133
+ };
134
+ export type IFilterCase<T> = {
135
+ case(condition: ICallback<any>): IFilterCase<T>;
136
+ };
137
+ export type IFilterPayload = {
138
+ isBreakChain: boolean;
139
+ isAvailable: boolean;
140
+ payload: any;
141
+ };
142
+ export type IFilterResponse = {
143
+ isOK: boolean;
144
+ payload: any;
145
+ };