evg_observable 1.4.37 → 1.5.38
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 +46 -1
- package/package.json +1 -1
- package/repo/evg_observable.js +1 -0
package/README.md
CHANGED
|
@@ -18,6 +18,11 @@ EVG Observable - is a small library for serving asynchronous events.
|
|
|
18
18
|
|
|
19
19
|
$ npm install evg_observable
|
|
20
20
|
|
|
21
|
+
### Browser
|
|
22
|
+
```html
|
|
23
|
+
<script src="https://unpkg.com/evg_observable/repo/evg_observable.js"></script>
|
|
24
|
+
```
|
|
25
|
+
|
|
21
26
|
# Usage
|
|
22
27
|
|
|
23
28
|
## Observable simple usage
|
|
@@ -53,6 +58,46 @@ observable$.destroy(); // all subscribers have automatically unsubscribed
|
|
|
53
58
|
// also if observable$ needs to be used further, but subscribers are not needed, you can use the observable$.unsubscribeAll() method
|
|
54
59
|
```
|
|
55
60
|
|
|
61
|
+
### Browser simple usage
|
|
62
|
+
|
|
63
|
+
```html
|
|
64
|
+
<!DOCTYPE html>
|
|
65
|
+
<html lang="en">
|
|
66
|
+
<head>
|
|
67
|
+
<meta charset="UTF-8">
|
|
68
|
+
<title>Title</title>
|
|
69
|
+
<script src="https://unpkg.com/evg_observable/repo/evg_observable.js"></script>
|
|
70
|
+
</head>
|
|
71
|
+
<body>
|
|
72
|
+
<script>
|
|
73
|
+
const observable$ = new Observable('Some typed data (not only string)');
|
|
74
|
+
|
|
75
|
+
const listener1 = (value) => console.log('listener1:', value);
|
|
76
|
+
const subscriber1 = observable$.subscribe(listener1);
|
|
77
|
+
|
|
78
|
+
const listener2 = (value) => console.log('listener2:', value);
|
|
79
|
+
const subscriber2 = observable$.subscribe(listener2);
|
|
80
|
+
|
|
81
|
+
console.log(observable$.getValue());
|
|
82
|
+
// Print to console - Some typed data (not only string)
|
|
83
|
+
|
|
84
|
+
observable$.next('Next typed data');
|
|
85
|
+
// Print to console - listener1: Next typed data
|
|
86
|
+
// Print to console - listener2: Next typed data
|
|
87
|
+
|
|
88
|
+
// if subscribers are not needed
|
|
89
|
+
subscriber1.unsubscribe();
|
|
90
|
+
subscriber2.unsubscribe();
|
|
91
|
+
|
|
92
|
+
// if a observable$ is not needed
|
|
93
|
+
observable$.destroy(); // all subscribers have automatically unsubscribed
|
|
94
|
+
|
|
95
|
+
// also if observable$ needs to be used further, but subscribers are not needed, you can use the observable$.unsubscribeAll() method
|
|
96
|
+
</script>
|
|
97
|
+
</body>
|
|
98
|
+
</html>
|
|
99
|
+
```
|
|
100
|
+
|
|
56
101
|
## Observable pipe usage
|
|
57
102
|
|
|
58
103
|
### pipe().setOnce()
|
|
@@ -360,4 +405,4 @@ Has the same methods as subscriber. But there is an "order" field.
|
|
|
360
405
|
|
|
361
406
|
## License
|
|
362
407
|
|
|
363
|
-
MIT
|
|
408
|
+
MIT
|
package/package.json
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(()=>{"use strict";var e={909:(e,s,i)=>{s.P=void 0;const t=i(594);s.P=class{constructor(){this.list=[],this._isDestroyed=!1}collect(...e){this._isDestroyed||this.list.push(...e)}unsubscribe(e){this._isDestroyed||(null==e||e.unsubscribe(),(0,t.quickDeleteFromArray)(this.list,e))}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}}},594:(e,s)=>{Object.defineProperty(s,"__esModule",{value:!0}),s.positiveCallback=s.negativeCallback=s.quickDeleteFromArray=s.deleteFromArray=void 0,s.deleteFromArray=function(e,s){const i=e.indexOf(s);return-1!==i&&(e.splice(i,1),!0)},s.quickDeleteFromArray=function(e,s){const i=e.indexOf(s);return-1!==i&&(e[i]=e[e.length-1],e.length=e.length-1,!0)},s.negativeCallback=()=>!1,s.positiveCallback=()=>!0},637:(e,s,i)=>{Object.defineProperty(s,"__esModule",{value:!0}),s.Observable=s.SubscribeObject=void 0;const t=i(594);class r{constructor(e,s){this.isMarkedForUnsubscribe=!1,this.errorHandler=(e,s)=>{console.log(`(Unit of SubscribeObject).send(${e}) ERROR:`,s)},this._order=0,this.isListenPaused=!1,this.once={isOnce:!1,isFinished:!1},this.unsubscribeByNegativeCondition=null,this.unsubscribeByPositiveCondition=null,this.emitByNegativeCondition=null,this.emitByPositiveCondition=null,this.emitMatchCondition=null,this.isPipe=!1,this.observable=e,this.isPipe=!!s}static callbackSend(e,s){const i=s.listener;if(i)switch(!0){case!s.observable:return void s.unsubscribe();case s.isListenPaused:return;case!s.isPipe:return void i(e);case s.once.isOnce:s.once.isFinished=!0,i(e),s.unsubscribe();break;case!!s.unsubscribeByNegativeCondition:if(!s.unsubscribeByNegativeCondition(e))return s.unsubscribeByNegativeCondition=null,void s.unsubscribe();i(e);break;case!!s.unsubscribeByPositiveCondition:if(s.unsubscribeByPositiveCondition(e))return s.unsubscribeByPositiveCondition=null,void s.unsubscribe();i(e);break;case!!s.emitByNegativeCondition:!s.emitByNegativeCondition(e)&&i(e);break;case!!s.emitByPositiveCondition:s.emitByPositiveCondition(e)&&i(e);break;case!!s.emitMatchCondition:s.emitMatchCondition(e)===e&&i(e)}else s.unsubscribe()}subscribe(e,s){return this.listener=e,s&&(this.errorHandler=s),this}unsubscribe(){this.observable&&(this.observable.unSubscribe(this),this.observable=0,this.listener=0)}send(e){try{r.callbackSend(e,this)}catch(s){this.errorHandler(e,s)}}setOnce(){return this.once.isOnce=!0,this}unsubscribeByNegative(e){return this.unsubscribeByNegativeCondition=e||t.negativeCallback,this}unsubscribeByPositive(e){return this.unsubscribeByPositiveCondition=e||t.positiveCallback,this}emitByNegative(e){return this.emitByNegativeCondition=e||t.positiveCallback,this}emitByPositive(e){return this.emitByPositiveCondition=e||t.negativeCallback,this}emitMatch(e){const s=typeof e;return"function"!==s&&(e=()=>`ERROR CONDITION TYPE ${s}, CONTROL STATE ${this.observable&&!this.observable.getValue()}`),this.emitMatchCondition=e,this}resume(){this.isListenPaused=!1}pause(){this.isListenPaused=!0}get order(){return this._order}set order(e){this._order=e}}s.SubscribeObject=r,s.Observable=class{constructor(e){this.value=e,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(e){if(this._isDestroyed)return;if(!this._isEnable)return;this.isNextProcess=!0,this.value=e;const s=this.listeners.length;for(let i=0;i<s;i++)this.listeners[i].send(e);this.isNextProcess=!1,this.listenersForUnsubscribe.length&&this.handleListenersForUnsubscribe()}stream(e){if(!this._isDestroyed&&this._isEnable)for(let s=0;s<e.length;s++)this.next(e[s])}handleListenersForUnsubscribe(){const e=this.listenersForUnsubscribe.length;for(let s=0;s<e;s++){const e=this.listenersForUnsubscribe[s];this.unSubscribe(e)}this.listenersForUnsubscribe.length=0}unSubscribe(e){if(!this._isDestroyed){if(this.isNextProcess&&e){const s=e;return!s.isMarkedForUnsubscribe&&this.listenersForUnsubscribe.push(e),void(s.isMarkedForUnsubscribe=!0)}this.listeners&&(0,t.quickDeleteFromArray)(this.listeners,e)}}destroy(){this.value=0,this.unsubscribeAll(),this.listeners=0,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(e,s){if(this._isDestroyed)return;if(!e)return;const i=new r(this,!1);return i.subscribe(e,s),this.listeners.push(i),i}pipe(){if(this._isDestroyed)return;const e=new r(this,!0);return this.listeners.push(e),e}get isDestroyed(){return this._isDestroyed}}},546:(e,s,i)=>{s.Nc=void 0;const t=i(637),r=i(594);class n extends t.SubscribeObject{constructor(e,s){super(e,s)}get order(){return this._order}set order(e){!this.observable||this.observable&&this.observable.isDestroyed?this._order=void 0:(this._order=e,this.observable.sortByOrder())}subscribe(e,s){return this.listener=e,s&&(this.errorHandler=s),this}setOnce(){return super.setOnce()}unsubscribeByNegative(e){return super.unsubscribeByNegative(e)}unsubscribeByPositive(e){return super.unsubscribeByPositive(e)}emitByNegative(e){return super.emitByNegative(e)}emitByPositive(e){return super.emitByPositive(e)}emitMatch(e){return super.emitMatch(e)}}class o extends t.Observable{sortByOrder(){return!this._isDestroyed&&(this.listeners.sort(((e,s)=>e.order>s.order?1:e.order<s.order?-1:0)),!0)}subscribe(e,s){if(this._isDestroyed)return;if(!e)return;const i=new n(this,!1);return i.subscribe(e,s),this.listeners.push(i),i}pipe(){if(this._isDestroyed)return;const e=new n(this,!0);return this.listeners.push(e),e}unSubscribe(e){if(!this._isDestroyed){if(this.isNextProcess&&e){const s=e;return!s.isMarkedForUnsubscribe&&this.listenersForUnsubscribe.push(e),void(s.isMarkedForUnsubscribe=!0)}this.listeners&&(0,r.deleteFromArray)(this.listeners,e)}}}s.Nc=o}},s={};function i(t){var r=s[t];if(void 0!==r)return r.exports;var n=s[t]={exports:{}};return e[t](n,n.exports,i),n.exports}(()=>{var e=i(637),s=i(909),t=i(546);const r=window;r.Observable=e.Observable,r.Collector=s.P,r.OrderedObservable=t.Nc})()})();
|