@nbottarini/observable 0.5.0 → 0.8.0
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 +33 -6
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/types/index.d.ts +68 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -18,11 +18,13 @@ $ yarn add @nbottarini/observable
|
|
|
18
18
|
|
|
19
19
|
## Usage
|
|
20
20
|
|
|
21
|
+
### Observables
|
|
22
|
+
|
|
21
23
|
**View1.ts:**
|
|
22
24
|
```typescript
|
|
23
25
|
export class View1 {
|
|
24
|
-
public readonly buttonClicked =
|
|
25
|
-
public readonly textChanged =
|
|
26
|
+
public readonly buttonClicked = observable<ClickEvent>()
|
|
27
|
+
public readonly textChanged = observable<TextChangedEvent>()
|
|
26
28
|
|
|
27
29
|
// Do something internally to handle UI events
|
|
28
30
|
|
|
@@ -48,11 +50,36 @@ export class View2 {
|
|
|
48
50
|
}
|
|
49
51
|
```
|
|
50
52
|
|
|
51
|
-
|
|
53
|
+
### Composite observables
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
const buttonClicked = observable<ClickEvent>()
|
|
57
|
+
const textChanged = observable<TextChangedEvent>()
|
|
58
|
+
const allEvents = compositeObservable(buttonClicked, textChanged)
|
|
59
|
+
|
|
60
|
+
allEvents.subscribe({}, (event) => {
|
|
61
|
+
// Notifies click and text changed events
|
|
62
|
+
})
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Observable values:
|
|
52
66
|
|
|
53
67
|
```typescript
|
|
54
|
-
const nameProperty =
|
|
55
|
-
nameProperty
|
|
68
|
+
const nameProperty$ = observableValue('John')
|
|
69
|
+
nameProperty$.value // 'John'
|
|
70
|
+
|
|
71
|
+
nameProperty$.changed.subscribe(this, this.onNameChanged)
|
|
72
|
+
nameProperty$.value = 'new name' // Notifies changes to subscribers
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Observable computed values:
|
|
76
|
+
```typescript
|
|
77
|
+
const property1 = observableValue(1)
|
|
78
|
+
const property2$ = observableValue(2)
|
|
79
|
+
const computedProperty$ = observableComputed((value1, value2) => value1 + value2, $property1, $property2)
|
|
80
|
+
computedProperty$.value // returns 3
|
|
81
|
+
|
|
82
|
+
computedProperty$.changed.subscribe(this, this.onComputedChanged)
|
|
83
|
+
property1$.value = 3 // Notifies new computed value 5 to computedProperty$ subscribers
|
|
56
84
|
|
|
57
|
-
nameProperty.value = 'new name'
|
|
58
85
|
```
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";class
|
|
1
|
+
"use strict";class DefaultObservable{observers=new Set;handlers=new Map;subscribe(observer,handler){this.observers.add(observer),this.handlers.set(observer,handler.bind(observer))}hasObserver(observer){return this.observers.has(observer)}unsubscribe(observer){this.observers.delete(observer),this.handlers.delete(observer)}unsubscribeAll(){this.observers.clear(),this.handlers.clear()}async notify(value){let promises=[];for(const handler of this.handlers.values())promises.push(handler(value));await Promise.all(promises)}}function observable(){return new DefaultObservable}class CompositeObservable{sources;observers=new Set;handlers=new Map;onSourceEventRef=this.onSourceEvent.bind(this);constructor(sources){this.sources=sources}hasObserver(observer){return this.observers.has(observer)}async notify(value){throw new Error("CompositeObservable cannot be notified directly")}subscribe(observer,handler){const isFirstObserver=0===this.observers.size;this.observers.add(observer),this.handlers.set(observer,handler.bind(observer)),isFirstObserver&&this.attachToSources()}unsubscribe(observer){const hadObservers=this.observers.size>0;this.observers.delete(observer),this.handlers.delete(observer),hadObservers&&0===this.observers.size&&this.detachFromSources()}unsubscribeAll(){const hadObservers=this.observers.size>0;this.observers.clear(),this.handlers.clear(),hadObservers&&this.detachFromSources()}attachToSources(){for(const source of this.sources)source.subscribe(this,this.onSourceEventRef)}detachFromSources(){for(const source of this.sources)source.unsubscribe(this)}async onSourceEvent(value){let promises=[];for(const handler of this.handlers.values())promises.push(handler(value));await Promise.all(promises)}}function compositeObservable(...sources){return new CompositeObservable(sources)}class ObservableMutableValue{_value;changed=observable();constructor(initialValue){this._value=initialValue}subscribe(observer,handler){this.changed.subscribe(observer,handler)}hasObserver(observer){return this.changed.hasObserver(observer)}unsubscribe(observer){this.changed.unsubscribe(observer)}unsubscribeAll(){this.changed.unsubscribeAll()}async notify(value){return this.changed.notify(value)}get value(){return this._value}set value(newValue){this._value!==newValue&&(this._value=newValue,this.changed.notify(this._value))}}class ObservableComputedValue{computeFunc;internalChanged=observable();changed={subscribe:(o,h)=>this.subscribe(o,h),hasObserver:o=>this.hasObserver(o),unsubscribe:o=>this.unsubscribe(o),unsubscribeAll:()=>this.unsubscribeAll(),notify:v=>this.notify(v)};depsChanged;deps;onDepsChangedRef=this.onDepsChanged.bind(this);observerCount=0;hasValue=!1;cachedValue;constructor(computeFunc,...deps){this.computeFunc=computeFunc,this.deps=deps;const sources=deps.map((it=>it.changed));this.depsChanged=compositeObservable(...sources)}subscribe(observer,handler){const observerAlreadySubscribed=this.hasObserver(observer),isFirstObserver=0===this.observerCount;this.internalChanged.subscribe(observer,handler),observerAlreadySubscribed||this.observerCount++,isFirstObserver&&this.depsChanged.subscribe(this,this.onDepsChangedRef)}hasObserver(observer){return this.internalChanged.hasObserver(observer)}unsubscribe(observer){this.internalChanged.hasObserver(observer)&&(this.internalChanged.unsubscribe(observer),this.observerCount--,0===this.observerCount&&this.depsChanged.unsubscribe(this))}unsubscribeAll(){const hadObservers=this.observerCount>0;this.internalChanged.unsubscribeAll(),this.observerCount=0,hadObservers&&this.depsChanged.unsubscribe(this)}async notify(value){throw new Error("ComputedProperty cannot be notified directly")}get value(){return 0===this.observerCount?this.compute():(this.hasValue||(this.cachedValue=this.compute(),this.hasValue=!0),this.cachedValue)}compute(){const values=this.deps.map((d=>d.value));return this.computeFunc(...values)}onDepsChanged(){const newValue=this.compute();this.hasValue&&Object.is(this.cachedValue,newValue)||(this.cachedValue=newValue,this.hasValue=!0,this.internalChanged.notify(this.cachedValue))}}exports.CompositeObservable=CompositeObservable,exports.DefaultObservable=DefaultObservable,exports.ObservableComputedValue=ObservableComputedValue,exports.ObservableMutableValue=ObservableMutableValue,exports.compositeObservable=compositeObservable,exports.observable=observable,exports.observableComputed=function(computeFunc,...deps){return new ObservableComputedValue(computeFunc,...deps)},exports.observableValue=function(initialValue){return new ObservableMutableValue(initialValue)};
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../src/Observable.ts","../../src/ObservableProperty.ts"],"sourcesContent":["export type ObserverFunc<T> = (subject: T) => any\n\nexport class Observable<T = void> {\n private observers: Set<object> = new Set()\n private handlers: Map<object, ObserverFunc<T>> = new Map()\n\n subscribe(observer: object, handler: ObserverFunc<T>) {\n this.observers.add(observer)\n this.handlers.set(observer, handler.bind(observer))\n }\n\n hasObserver(observer: object) {\n return this.observers.has(observer)\n }\n\n unsubscribe(observer: object) {\n this.observers.delete(observer)\n this.handlers.delete(observer)\n }\n\n unsubscribeAll() {\n this.observers.clear()\n this.handlers.clear()\n }\n\n async notify(value?: T) {\n let promises = []\n for (const handler of this.handlers.values()) {\n promises.push(handler(value))\n }\n await Promise.all(promises)\n }\n}\n","import { Observable } from './Observable'\n\nexport class ObservableProperty<T = any> {\n private _value: T\n readonly changed: Observable<T> = new Observable()\n\n constructor(initialValue: T) {\n this._value = initialValue\n }\n\n get value(): T {\n return this._value\n }\n\n set value(newValue: T) {\n if (this._value === newValue) return\n this._value = newValue\n this.changed.notify(this._value)\n }\n}\n"],"names":["Observable","observers","Set","handlers","Map","subscribe","observer","handler","this","add","set","bind","hasObserver","has","unsubscribe","delete","unsubscribeAll","clear","notify","value","promises","values","push","Promise","all","_value","changed","constructor","initialValue","newValue"],"mappings":"mBAEaA,WACDC,UAAyB,IAAIC,IAC7BC,SAAyC,IAAIC,IAErD,SAAAC,CAAUC,SAAkBC,SACxBC,KAAKP,UAAUQ,IAAIH,UACnBE,KAAKL,SAASO,IAAIJ,SAAUC,QAAQI,KAAKL,UAC5C,CAED,WAAAM,CAAYN,UACR,OAAOE,KAAKP,UAAUY,IAAIP,SAC7B,CAED,WAAAQ,CAAYR,UACRE,KAAKP,UAAUc,OAAOT,UACtBE,KAAKL,SAASY,OAAOT,SACxB,CAED,cAAAU,GACIR,KAAKP,UAAUgB,QACfT,KAAKL,SAASc,OACjB,CAED,YAAMC,CAAOC,OACT,IAAIC,SAAW,GACf,IAAK,MAAMb,WAAWC,KAAKL,SAASkB,SAChCD,SAASE,KAAKf,QAAQY,cAEpBI,QAAQC,IAAIJ,SACrB,iEC5BOK,OACCC,QAAyB,IAAI1B,WAEtC,WAAA2B,CAAYC,cACRpB,KAAKiB,OAASG,YACjB,CAED,SAAIT,GACA,OAAOX,KAAKiB,MACf,CAED,SAAIN,CAAMU,UACFrB,KAAKiB,SAAWI,WACpBrB,KAAKiB,OAASI,SACdrB,KAAKkB,QAAQR,OAAOV,KAAKiB,QAC5B"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/DefaultObservable.ts","../../src/CompositeObservable.ts","../../src/ObservableMutableValue.ts","../../src/ObservableComputedValue.ts"],"sourcesContent":["import { Observable, ObserverFunc } from './Observable'\n\nexport class DefaultObservable<T = void> implements Observable<T> {\n private observers: Set<object> = new Set()\n private handlers: Map<object, ObserverFunc<T>> = new Map()\n\n subscribe(observer: object, handler: ObserverFunc<T>) {\n this.observers.add(observer)\n this.handlers.set(observer, handler.bind(observer))\n }\n\n hasObserver(observer: object) {\n return this.observers.has(observer)\n }\n\n unsubscribe(observer: object) {\n this.observers.delete(observer)\n this.handlers.delete(observer)\n }\n\n unsubscribeAll() {\n this.observers.clear()\n this.handlers.clear()\n }\n\n async notify(value?: T) {\n let promises = []\n for (const handler of this.handlers.values()) {\n promises.push(handler(value))\n }\n await Promise.all(promises)\n }\n}\n\nexport function observable<T = void>(): Observable<T> {\n return new DefaultObservable<T>()\n}\n","import { Observable, ObserverFunc } from './Observable'\n\ntype EventOf<O> = O extends Observable<infer T> ? T : never\n\nexport class CompositeObservable<Sources extends readonly Observable<any>[]> implements Observable<EventOf<Sources[number]>> {\n private observers = new Set<object>()\n private handlers: Map<object, ObserverFunc<EventOf<Sources[number]>>> = new Map()\n private onSourceEventRef = this.onSourceEvent.bind(this)\n\n constructor(private readonly sources: Sources) {\n }\n\n hasObserver(observer: object): boolean {\n return this.observers.has(observer)\n }\n\n async notify(value?: EventOf<Sources[number]>) {\n throw new Error('CompositeObservable cannot be notified directly')\n }\n\n subscribe(observer: object, handler: ObserverFunc<EventOf<Sources[number]>>): void {\n const isFirstObserver = this.observers.size === 0\n this.observers.add(observer)\n this.handlers.set(observer, handler.bind(observer))\n\n if (isFirstObserver) this.attachToSources()\n }\n\n unsubscribe(observer: object): void {\n const hadObservers = this.observers.size > 0\n this.observers.delete(observer)\n this.handlers.delete(observer)\n\n if (hadObservers && this.observers.size === 0) this.detachFromSources()\n }\n\n unsubscribeAll(): void {\n const hadObservers = this.observers.size > 0\n this.observers.clear()\n this.handlers.clear()\n if (hadObservers ) this.detachFromSources()\n }\n\n private attachToSources() {\n for (const source of this.sources) {\n source.subscribe(this, this.onSourceEventRef)\n }\n }\n\n private detachFromSources() {\n for (const source of this.sources) {\n source.unsubscribe(this)\n }\n }\n\n private async onSourceEvent(value?: EventOf<Sources[number]>) {\n let promises = []\n for (const handler of this.handlers.values()) {\n promises.push(handler(value))\n }\n await Promise.all(promises)\n }\n}\n\nexport function compositeObservable<Sources extends readonly Observable<any>[]>(...sources: Sources) {\n return new CompositeObservable(sources)\n}\n","import { observable } from './DefaultObservable'\nimport { ObserverFunc } from './Observable'\nimport { ObservableValue } from './ObservableValue'\n\nexport class ObservableMutableValue<T> implements ObservableValue<T> {\n private _value: T\n readonly changed = observable<T>()\n\n constructor(initialValue: T) {\n this._value = initialValue\n }\n\n subscribe(observer: object, handler: ObserverFunc<T>) {\n this.changed.subscribe(observer, handler)\n }\n hasObserver(observer: object): boolean {\n return this.changed.hasObserver(observer)\n }\n unsubscribe(observer: object) {\n this.changed.unsubscribe(observer)\n }\n unsubscribeAll() {\n this.changed.unsubscribeAll()\n }\n\n async notify(value?: T): Promise<void> {\n return this.changed.notify(value)\n }\n\n get value(): T {\n return this._value\n }\n\n set value(newValue: T) {\n if (this._value === newValue) return\n this._value = newValue\n this.changed.notify(this._value)\n }\n}\n\nexport function observableValue<T>(initialValue: T): ObservableMutableValue<T> {\n return new ObservableMutableValue<T>(initialValue)\n}\n","import { ObservableValue } from './ObservableValue'\nimport { Observable, ObserverFunc } from './Observable'\nimport { compositeObservable } from './CompositeObservable'\nimport { observable } from './DefaultObservable'\n\ntype ValueOf<P> = P extends ObservableValue<infer T> ? T : never\n\nexport class ObservableComputedValue<Deps extends readonly ObservableValue<any>[], T> implements ObservableValue<T> {\n private readonly internalChanged = observable<T>()\n readonly changed: Observable<T> = {\n subscribe: (o, h) => this.subscribe(o, h),\n hasObserver: (o) => this.hasObserver(o),\n unsubscribe: (o) => this.unsubscribe(o),\n unsubscribeAll: () => this.unsubscribeAll(),\n notify: (v) => this.notify(v),\n }\n private readonly depsChanged: Observable<any>\n private readonly deps: Deps\n private readonly onDepsChangedRef = this.onDepsChanged.bind(this)\n private observerCount = 0\n private hasValue = false\n private cachedValue!: T\n\n constructor(private readonly computeFunc: (...values: { [K in keyof Deps]: ValueOf<Deps[K]> }) => T, ...deps: Deps) {\n this.deps = deps\n const sources = deps.map(it => it.changed)\n this.depsChanged = compositeObservable(...sources)\n }\n\n subscribe(observer: object, handler: ObserverFunc<any>) {\n const observerAlreadySubscribed = this.hasObserver(observer)\n const isFirstObserver = this.observerCount === 0\n this.internalChanged.subscribe(observer, handler)\n if (!observerAlreadySubscribed) {\n this.observerCount++\n }\n if (isFirstObserver) this.depsChanged.subscribe(this, this.onDepsChangedRef)\n }\n\n hasObserver(observer: object): boolean {\n return this.internalChanged.hasObserver(observer)\n }\n\n unsubscribe(observer: object) {\n if (!this.internalChanged.hasObserver(observer)) return\n\n this.internalChanged.unsubscribe(observer)\n this.observerCount--\n\n if (this.observerCount === 0) this.depsChanged.unsubscribe(this)\n }\n\n unsubscribeAll() {\n const hadObservers = this.observerCount > 0\n this.internalChanged.unsubscribeAll()\n this.observerCount = 0\n if (hadObservers) this.depsChanged.unsubscribe(this)\n }\n\n async notify(value?: any): Promise<void> {\n throw new Error('ComputedProperty cannot be notified directly')\n }\n\n get value(): T {\n // When there are no observers we must recompute on each call because we aren't watching dep changes\n if (this.observerCount === 0) return this.compute()\n\n if (!this.hasValue) {\n this.cachedValue = this.compute()\n this.hasValue = true\n }\n return this.cachedValue\n }\n\n private compute(): T {\n const values = this.deps.map(d => d.value) as { [K in keyof Deps]: ValueOf<Deps[K]> }\n return this.computeFunc(...values)\n }\n\n private onDepsChanged() {\n const newValue = this.compute()\n\n if (!this.hasValue || !Object.is(this.cachedValue, newValue)) {\n this.cachedValue = newValue\n this.hasValue = true\n this.internalChanged.notify(this.cachedValue)\n }\n }\n}\n\nexport function observableComputed<\n Deps extends readonly ObservableValue<any>[],\n T,\n>(computeFunc: (...values: { [K in keyof Deps]: ValueOf<Deps[K]> }) => T, ...deps: Deps) {\n return new ObservableComputedValue(computeFunc, ...deps)\n}\n"],"names":["DefaultObservable","observers","Set","handlers","Map","subscribe","observer","handler","this","add","set","bind","hasObserver","has","unsubscribe","delete","unsubscribeAll","clear","notify","value","promises","values","push","Promise","all","observable","CompositeObservable","sources","onSourceEventRef","onSourceEvent","constructor","Error","isFirstObserver","size","attachToSources","hadObservers","detachFromSources","source","compositeObservable","ObservableMutableValue","_value","changed","initialValue","newValue","ObservableComputedValue","computeFunc","internalChanged","o","h","v","depsChanged","deps","onDepsChangedRef","onDepsChanged","observerCount","hasValue","cachedValue","map","it","observerAlreadySubscribed","compute","d","Object","is"],"mappings":"mBAEaA,kBACDC,UAAyB,IAAIC,IAC7BC,SAAy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C,KAAKoD,UAEjBpD,KAAK+C,UAAaO,OAAOC,GAAGvD,KAAKgD,YAAab,YAC/CnC,KAAKgD,YAAcb,SACnBnC,KAAK+C,UAAW,EAChB/C,KAAKsC,gBAAgB5B,OAAOV,KAAKgD,aAExC,8TAMHX,eAA2EM,MACzE,OAAO,IAAIP,wBAAwBC,eAAgBM,KACvD,0BDvDM,SAA6BT,cAC/B,OAAO,IAAIH,uBAA0BG,aACzC"}
|
package/dist/esm/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
class
|
|
1
|
+
class DefaultObservable{observers=new Set;handlers=new Map;subscribe(observer,handler){this.observers.add(observer),this.handlers.set(observer,handler.bind(observer))}hasObserver(observer){return this.observers.has(observer)}unsubscribe(observer){this.observers.delete(observer),this.handlers.delete(observer)}unsubscribeAll(){this.observers.clear(),this.handlers.clear()}async notify(value){let promises=[];for(const handler of this.handlers.values())promises.push(handler(value));await Promise.all(promises)}}function observable(){return new DefaultObservable}class CompositeObservable{sources;observers=new Set;handlers=new Map;onSourceEventRef=this.onSourceEvent.bind(this);constructor(sources){this.sources=sources}hasObserver(observer){return this.observers.has(observer)}async notify(value){throw new Error("CompositeObservable cannot be notified directly")}subscribe(observer,handler){const isFirstObserver=0===this.observers.size;this.observers.add(observer),this.handlers.set(observer,handler.bind(observer)),isFirstObserver&&this.attachToSources()}unsubscribe(observer){const hadObservers=this.observers.size>0;this.observers.delete(observer),this.handlers.delete(observer),hadObservers&&0===this.observers.size&&this.detachFromSources()}unsubscribeAll(){const hadObservers=this.observers.size>0;this.observers.clear(),this.handlers.clear(),hadObservers&&this.detachFromSources()}attachToSources(){for(const source of this.sources)source.subscribe(this,this.onSourceEventRef)}detachFromSources(){for(const source of this.sources)source.unsubscribe(this)}async onSourceEvent(value){let promises=[];for(const handler of this.handlers.values())promises.push(handler(value));await Promise.all(promises)}}function compositeObservable(...sources){return new CompositeObservable(sources)}class ObservableMutableValue{_value;changed=observable();constructor(initialValue){this._value=initialValue}subscribe(observer,handler){this.changed.subscribe(observer,handler)}hasObserver(observer){return this.changed.hasObserver(observer)}unsubscribe(observer){this.changed.unsubscribe(observer)}unsubscribeAll(){this.changed.unsubscribeAll()}async notify(value){return this.changed.notify(value)}get value(){return this._value}set value(newValue){this._value!==newValue&&(this._value=newValue,this.changed.notify(this._value))}}function observableValue(initialValue){return new ObservableMutableValue(initialValue)}class ObservableComputedValue{computeFunc;internalChanged=observable();changed={subscribe:(o,h)=>this.subscribe(o,h),hasObserver:o=>this.hasObserver(o),unsubscribe:o=>this.unsubscribe(o),unsubscribeAll:()=>this.unsubscribeAll(),notify:v=>this.notify(v)};depsChanged;deps;onDepsChangedRef=this.onDepsChanged.bind(this);observerCount=0;hasValue=!1;cachedValue;constructor(computeFunc,...deps){this.computeFunc=computeFunc,this.deps=deps;const sources=deps.map((it=>it.changed));this.depsChanged=compositeObservable(...sources)}subscribe(observer,handler){const observerAlreadySubscribed=this.hasObserver(observer),isFirstObserver=0===this.observerCount;this.internalChanged.subscribe(observer,handler),observerAlreadySubscribed||this.observerCount++,isFirstObserver&&this.depsChanged.subscribe(this,this.onDepsChangedRef)}hasObserver(observer){return this.internalChanged.hasObserver(observer)}unsubscribe(observer){this.internalChanged.hasObserver(observer)&&(this.internalChanged.unsubscribe(observer),this.observerCount--,0===this.observerCount&&this.depsChanged.unsubscribe(this))}unsubscribeAll(){const hadObservers=this.observerCount>0;this.internalChanged.unsubscribeAll(),this.observerCount=0,hadObservers&&this.depsChanged.unsubscribe(this)}async notify(value){throw new Error("ComputedProperty cannot be notified directly")}get value(){return 0===this.observerCount?this.compute():(this.hasValue||(this.cachedValue=this.compute(),this.hasValue=!0),this.cachedValue)}compute(){const values=this.deps.map((d=>d.value));return this.computeFunc(...values)}onDepsChanged(){const newValue=this.compute();this.hasValue&&Object.is(this.cachedValue,newValue)||(this.cachedValue=newValue,this.hasValue=!0,this.internalChanged.notify(this.cachedValue))}}function observableComputed(computeFunc,...deps){return new ObservableComputedValue(computeFunc,...deps)}export{CompositeObservable,DefaultObservable,ObservableComputedValue,ObservableMutableValue,compositeObservable,observable,observableComputed,observableValue};
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../src/Observable.ts","../../src/ObservableProperty.ts"],"sourcesContent":["export type ObserverFunc<T> = (subject: T) => any\n\nexport class Observable<T = void> {\n private observers: Set<object> = new Set()\n private handlers: Map<object, ObserverFunc<T>> = new Map()\n\n subscribe(observer: object, handler: ObserverFunc<T>) {\n this.observers.add(observer)\n this.handlers.set(observer, handler.bind(observer))\n }\n\n hasObserver(observer: object) {\n return this.observers.has(observer)\n }\n\n unsubscribe(observer: object) {\n this.observers.delete(observer)\n this.handlers.delete(observer)\n }\n\n unsubscribeAll() {\n this.observers.clear()\n this.handlers.clear()\n }\n\n async notify(value?: T) {\n let promises = []\n for (const handler of this.handlers.values()) {\n promises.push(handler(value))\n }\n await Promise.all(promises)\n }\n}\n","import { Observable } from './Observable'\n\nexport class ObservableProperty<T = any> {\n private _value: T\n readonly changed: Observable<T> = new Observable()\n\n constructor(initialValue: T) {\n this._value = initialValue\n }\n\n get value(): T {\n return this._value\n }\n\n set value(newValue: T) {\n if (this._value === newValue) return\n this._value = newValue\n this.changed.notify(this._value)\n }\n}\n"],"names":["Observable","observers","Set","handlers","Map","subscribe","observer","handler","this","add","set","bind","hasObserver","has","unsubscribe","delete","unsubscribeAll","clear","notify","value","promises","values","push","Promise","all","ObservableProperty","_value","changed","constructor","initialValue","newValue"],"mappings":"MAEaA,WACDC,UAAyB,IAAIC,IAC7BC,SAAyC,IAAIC,IAErD,SAAAC,CAAUC,SAAkBC,SACxBC,KAAKP,UAAUQ,IAAIH,UACnBE,KAAKL,SAASO,IAAIJ,SAAUC,QAAQI,KAAKL,UAC5C,CAED,WAAAM,CAAYN,UACR,OAAOE,KAAKP,UAAUY,IAAIP,SAC7B,CAED,WAAAQ,CAAYR,UACRE,KAAKP,UAAUc,OAAOT,UACtBE,KAAKL,SAASY,OAAOT,SACxB,CAED,cAAAU,GACIR,KAAKP,UAAUgB,QACfT,KAAKL,SAASc,OACjB,CAED,YAAMC,CAAOC,OACT,IAAIC,SAAW,GACf,IAAK,MAAMb,WAAWC,KAAKL,SAASkB,SAChCD,SAASE,KAAKf,QAAQY,cAEpBI,QAAQC,IAAIJ,SACrB,QC7BQK,mBACDC,OACCC,QAAyB,IAAI3B,WAEtC,WAAA4B,CAAYC,cACRrB,KAAKkB,OAASG,YACjB,CAED,SAAIV,GACA,OAAOX,KAAKkB,MACf,CAED,SAAIP,CAAMW,UACFtB,KAAKkB,SAAWI,WACpBtB,KAAKkB,OAASI,SACdtB,KAAKmB,QAAQT,OAAOV,KAAKkB,QAC5B"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/DefaultObservable.ts","../../src/CompositeObservable.ts","../../src/ObservableMutableValue.ts","../../src/ObservableComputedValue.ts"],"sourcesContent":["import { Observable, ObserverFunc } from './Observable'\n\nexport class DefaultObservable<T = void> implements Observable<T> {\n private observers: Set<object> = new Set()\n private handlers: Map<object, ObserverFunc<T>> = new Map()\n\n subscribe(observer: object, handler: ObserverFunc<T>) {\n this.observers.add(observer)\n this.handlers.set(observer, handler.bind(observer))\n }\n\n hasObserver(observer: object) {\n return this.observers.has(observer)\n }\n\n unsubscribe(observer: object) {\n this.observers.delete(observer)\n this.handlers.delete(observer)\n }\n\n unsubscribeAll() {\n this.observers.clear()\n this.handlers.clear()\n }\n\n async notify(value?: T) {\n let promises = []\n for (const handler of this.handlers.values()) {\n promises.push(handler(value))\n }\n await Promise.all(promises)\n }\n}\n\nexport function observable<T = void>(): Observable<T> {\n return new DefaultObservable<T>()\n}\n","import { Observable, ObserverFunc } from './Observable'\n\ntype EventOf<O> = O extends Observable<infer T> ? T : never\n\nexport class CompositeObservable<Sources extends readonly Observable<any>[]> implements Observable<EventOf<Sources[number]>> {\n private observers = new Set<object>()\n private handlers: Map<object, ObserverFunc<EventOf<Sources[number]>>> = new Map()\n private onSourceEventRef = this.onSourceEvent.bind(this)\n\n constructor(private readonly sources: Sources) {\n }\n\n hasObserver(observer: object): boolean {\n return this.observers.has(observer)\n }\n\n async notify(value?: EventOf<Sources[number]>) {\n throw new Error('CompositeObservable cannot be notified directly')\n }\n\n subscribe(observer: object, handler: ObserverFunc<EventOf<Sources[number]>>): void {\n const isFirstObserver = this.observers.size === 0\n this.observers.add(observer)\n this.handlers.set(observer, handler.bind(observer))\n\n if (isFirstObserver) this.attachToSources()\n }\n\n unsubscribe(observer: object): void {\n const hadObservers = this.observers.size > 0\n this.observers.delete(observer)\n this.handlers.delete(observer)\n\n if (hadObservers && this.observers.size === 0) this.detachFromSources()\n }\n\n unsubscribeAll(): void {\n const hadObservers = this.observers.size > 0\n this.observers.clear()\n this.handlers.clear()\n if (hadObservers ) this.detachFromSources()\n }\n\n private attachToSources() {\n for (const source of this.sources) {\n source.subscribe(this, this.onSourceEventRef)\n }\n }\n\n private detachFromSources() {\n for (const source of this.sources) {\n source.unsubscribe(this)\n }\n }\n\n private async onSourceEvent(value?: EventOf<Sources[number]>) {\n let promises = []\n for (const handler of this.handlers.values()) {\n promises.push(handler(value))\n }\n await Promise.all(promises)\n }\n}\n\nexport function compositeObservable<Sources extends readonly Observable<any>[]>(...sources: Sources) {\n return new CompositeObservable(sources)\n}\n","import { observable } from './DefaultObservable'\nimport { ObserverFunc } from './Observable'\nimport { ObservableValue } from './ObservableValue'\n\nexport class ObservableMutableValue<T> implements ObservableValue<T> {\n private _value: T\n readonly changed = observable<T>()\n\n constructor(initialValue: T) {\n this._value = initialValue\n }\n\n subscribe(observer: object, handler: ObserverFunc<T>) {\n this.changed.subscribe(observer, handler)\n }\n hasObserver(observer: object): boolean {\n return this.changed.hasObserver(observer)\n }\n unsubscribe(observer: object) {\n this.changed.unsubscribe(observer)\n }\n unsubscribeAll() {\n this.changed.unsubscribeAll()\n }\n\n async notify(value?: T): Promise<void> {\n return this.changed.notify(value)\n }\n\n get value(): T {\n return this._value\n }\n\n set value(newValue: T) {\n if (this._value === newValue) return\n this._value = newValue\n this.changed.notify(this._value)\n }\n}\n\nexport function observableValue<T>(initialValue: T): ObservableMutableValue<T> {\n return new ObservableMutableValue<T>(initialValue)\n}\n","import { ObservableValue } from './ObservableValue'\nimport { Observable, ObserverFunc } from './Observable'\nimport { compositeObservable } from './CompositeObservable'\nimport { observable } from './DefaultObservable'\n\ntype ValueOf<P> = P extends ObservableValue<infer T> ? T : never\n\nexport class ObservableComputedValue<Deps extends readonly ObservableValue<any>[], T> implements ObservableValue<T> {\n private readonly internalChanged = observable<T>()\n readonly changed: Observable<T> = {\n subscribe: (o, h) => this.subscribe(o, h),\n hasObserver: (o) => this.hasObserver(o),\n unsubscribe: (o) => this.unsubscribe(o),\n unsubscribeAll: () => this.unsubscribeAll(),\n notify: (v) => this.notify(v),\n }\n private readonly depsChanged: Observable<any>\n private readonly deps: Deps\n private readonly onDepsChangedRef = this.onDepsChanged.bind(this)\n private observerCount = 0\n private hasValue = false\n private cachedValue!: T\n\n constructor(private readonly computeFunc: (...values: { [K in keyof Deps]: ValueOf<Deps[K]> }) => T, ...deps: Deps) {\n this.deps = deps\n const sources = deps.map(it => it.changed)\n this.depsChanged = compositeObservable(...sources)\n }\n\n subscribe(observer: object, handler: ObserverFunc<any>) {\n const observerAlreadySubscribed = this.hasObserver(observer)\n const isFirstObserver = this.observerCount === 0\n this.internalChanged.subscribe(observer, handler)\n if (!observerAlreadySubscribed) {\n this.observerCount++\n }\n if (isFirstObserver) this.depsChanged.subscribe(this, this.onDepsChangedRef)\n }\n\n hasObserver(observer: object): boolean {\n return this.internalChanged.hasObserver(observer)\n }\n\n unsubscribe(observer: object) {\n if (!this.internalChanged.hasObserver(observer)) return\n\n this.internalChanged.unsubscribe(observer)\n this.observerCount--\n\n if (this.observerCount === 0) this.depsChanged.unsubscribe(this)\n }\n\n unsubscribeAll() {\n const hadObservers = this.observerCount > 0\n this.internalChanged.unsubscribeAll()\n this.observerCount = 0\n if (hadObservers) this.depsChanged.unsubscribe(this)\n }\n\n async notify(value?: any): Promise<void> {\n throw new Error('ComputedProperty cannot be notified directly')\n }\n\n get value(): T {\n // When there are no observers we must recompute on each call because we aren't watching dep changes\n if (this.observerCount === 0) return this.compute()\n\n if (!this.hasValue) {\n this.cachedValue = this.compute()\n this.hasValue = true\n }\n return this.cachedValue\n }\n\n private compute(): T {\n const values = this.deps.map(d => d.value) as { [K in keyof Deps]: ValueOf<Deps[K]> }\n return this.computeFunc(...values)\n }\n\n private onDepsChanged() {\n const newValue = this.compute()\n\n if (!this.hasValue || !Object.is(this.cachedValue, newValue)) {\n this.cachedValue = newValue\n this.hasValue = true\n this.internalChanged.notify(this.cachedValue)\n }\n }\n}\n\nexport function observableComputed<\n Deps extends readonly ObservableValue<any>[],\n T,\n>(computeFunc: (...values: { [K in keyof Deps]: ValueOf<Deps[K]> }) => T, ...deps: Deps) {\n return new ObservableComputedValue(computeFunc, ...deps)\n}\n"],"names":["DefaultObservable","observers","Set","handlers","Map","subscribe","observer","handler","this","add","set","bind","hasObserver","has","unsubscribe","delete","unsubscribeAll","clear","notify","value","promises","values","push","Promise","all","observable","CompositeObservable","sources","onSourceEventRef","onSourceEvent","constructor","Error","isFirstObserver","size","attachToSources","hadObservers","detachFromSources","source","compositeObservable","ObservableMutableValue","_value","changed","initialValue","newValue","observableValue","ObservableComputedValue","computeFunc","internalChanged","o","h","v","depsChanged","deps","onDepsChangedRef","onDepsChanged","observerCount","hasValue","cachedValue","map","it","observerAlreadySubscribed","compute","d","Object","is","observableComputed"],"mappings":"MAEaA,kBACDC,UAAyB,IAAIC,IAC7BC,SAAyC,IAAIC,IAErD,SAAAC,CAAUC,SAAkBC,SACxBC,KAAKP,UAAUQ,IAAIH,UACnBE,KAAKL,SAASO,IAAIJ,SAAUC,QAAQI,KAAKL,UAC5C,CAED,WAAAM,CAAYN,UACR,OAAOE,KAAKP,UAAUY,IAAI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D,KAAKuC,gBAAgB7B,OAAOV,KAAKiD,aAExC,WAGWQ,mBAGdnB,eAA2EM,MACzE,OAAO,IAAIP,wBAAwBC,eAAgBM,KACvD"}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
type ObserverFunc<T> = (subject: T) => any;
|
|
2
|
-
|
|
2
|
+
interface Observable<T> {
|
|
3
|
+
subscribe(observer: object, handler: ObserverFunc<T>): void;
|
|
4
|
+
hasObserver(observer: object): boolean;
|
|
5
|
+
unsubscribe(observer: object): void;
|
|
6
|
+
unsubscribeAll(): void;
|
|
7
|
+
notify(value?: T): Promise<void>;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
interface ObservableValue<T = any> extends Observable<T> {
|
|
11
|
+
readonly changed: Observable<T>;
|
|
12
|
+
readonly value: T;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
declare class DefaultObservable<T = void> implements Observable<T> {
|
|
3
16
|
private observers;
|
|
4
17
|
private handlers;
|
|
5
18
|
subscribe(observer: object, handler: ObserverFunc<T>): void;
|
|
@@ -8,13 +21,65 @@ declare class Observable<T = void> {
|
|
|
8
21
|
unsubscribeAll(): void;
|
|
9
22
|
notify(value?: T): Promise<void>;
|
|
10
23
|
}
|
|
24
|
+
declare function observable<T = void>(): Observable<T>;
|
|
11
25
|
|
|
12
|
-
|
|
26
|
+
type EventOf<O> = O extends Observable<infer T> ? T : never;
|
|
27
|
+
declare class CompositeObservable<Sources extends readonly Observable<any>[]> implements Observable<EventOf<Sources[number]>> {
|
|
28
|
+
private readonly sources;
|
|
29
|
+
private observers;
|
|
30
|
+
private handlers;
|
|
31
|
+
private onSourceEventRef;
|
|
32
|
+
constructor(sources: Sources);
|
|
33
|
+
hasObserver(observer: object): boolean;
|
|
34
|
+
notify(value?: EventOf<Sources[number]>): Promise<void>;
|
|
35
|
+
subscribe(observer: object, handler: ObserverFunc<EventOf<Sources[number]>>): void;
|
|
36
|
+
unsubscribe(observer: object): void;
|
|
37
|
+
unsubscribeAll(): void;
|
|
38
|
+
private attachToSources;
|
|
39
|
+
private detachFromSources;
|
|
40
|
+
private onSourceEvent;
|
|
41
|
+
}
|
|
42
|
+
declare function compositeObservable<Sources extends readonly Observable<any>[]>(...sources: Sources): CompositeObservable<Sources>;
|
|
43
|
+
|
|
44
|
+
declare class ObservableMutableValue<T> implements ObservableValue<T> {
|
|
13
45
|
private _value;
|
|
14
46
|
readonly changed: Observable<T>;
|
|
15
47
|
constructor(initialValue: T);
|
|
48
|
+
subscribe(observer: object, handler: ObserverFunc<T>): void;
|
|
49
|
+
hasObserver(observer: object): boolean;
|
|
50
|
+
unsubscribe(observer: object): void;
|
|
51
|
+
unsubscribeAll(): void;
|
|
52
|
+
notify(value?: T): Promise<void>;
|
|
16
53
|
get value(): T;
|
|
17
54
|
set value(newValue: T);
|
|
18
55
|
}
|
|
56
|
+
declare function observableValue<T>(initialValue: T): ObservableMutableValue<T>;
|
|
57
|
+
|
|
58
|
+
type ValueOf<P> = P extends ObservableValue<infer T> ? T : never;
|
|
59
|
+
declare class ObservableComputedValue<Deps extends readonly ObservableValue<any>[], T> implements ObservableValue<T> {
|
|
60
|
+
private readonly computeFunc;
|
|
61
|
+
private readonly internalChanged;
|
|
62
|
+
readonly changed: Observable<T>;
|
|
63
|
+
private readonly depsChanged;
|
|
64
|
+
private readonly deps;
|
|
65
|
+
private readonly onDepsChangedRef;
|
|
66
|
+
private observerCount;
|
|
67
|
+
private hasValue;
|
|
68
|
+
private cachedValue;
|
|
69
|
+
constructor(computeFunc: (...values: {
|
|
70
|
+
[K in keyof Deps]: ValueOf<Deps[K]>;
|
|
71
|
+
}) => T, ...deps: Deps);
|
|
72
|
+
subscribe(observer: object, handler: ObserverFunc<any>): void;
|
|
73
|
+
hasObserver(observer: object): boolean;
|
|
74
|
+
unsubscribe(observer: object): void;
|
|
75
|
+
unsubscribeAll(): void;
|
|
76
|
+
notify(value?: any): Promise<void>;
|
|
77
|
+
get value(): T;
|
|
78
|
+
private compute;
|
|
79
|
+
private onDepsChanged;
|
|
80
|
+
}
|
|
81
|
+
declare function observableComputed<Deps extends readonly ObservableValue<any>[], T>(computeFunc: (...values: {
|
|
82
|
+
[K in keyof Deps]: ValueOf<Deps[K]>;
|
|
83
|
+
}) => T, ...deps: Deps): ObservableComputedValue<[...Deps], T>;
|
|
19
84
|
|
|
20
|
-
export { Observable,
|
|
85
|
+
export { CompositeObservable, DefaultObservable, Observable, ObservableComputedValue, ObservableMutableValue, ObservableValue, ObserverFunc, compositeObservable, observable, observableComputed, observableValue };
|
package/package.json
CHANGED