@nbottarini/observable 0.7.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 +6 -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 +8 -8
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -62,21 +62,21 @@ allEvents.subscribe({}, (event) => {
|
|
|
62
62
|
})
|
|
63
63
|
```
|
|
64
64
|
|
|
65
|
-
### Observable
|
|
65
|
+
### Observable values:
|
|
66
66
|
|
|
67
67
|
```typescript
|
|
68
|
-
const nameProperty$ =
|
|
68
|
+
const nameProperty$ = observableValue('John')
|
|
69
69
|
nameProperty$.value // 'John'
|
|
70
70
|
|
|
71
71
|
nameProperty$.changed.subscribe(this, this.onNameChanged)
|
|
72
72
|
nameProperty$.value = 'new name' // Notifies changes to subscribers
|
|
73
73
|
```
|
|
74
74
|
|
|
75
|
-
###
|
|
75
|
+
### Observable computed values:
|
|
76
76
|
```typescript
|
|
77
|
-
const property1 =
|
|
78
|
-
const property2$ =
|
|
79
|
-
const computedProperty$ =
|
|
77
|
+
const property1 = observableValue(1)
|
|
78
|
+
const property2$ = observableValue(2)
|
|
79
|
+
const computedProperty$ = observableComputed((value1, value2) => value1 + value2, $property1, $property2)
|
|
80
80
|
computedProperty$.value // returns 3
|
|
81
81
|
|
|
82
82
|
computedProperty$.changed.subscribe(this, this.onComputedChanged)
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
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
|
|
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/DefaultObservable.ts","../../src/CompositeObservable.ts","../../src/MutableProperty.ts","../../src/ComputedProperty.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 { ObservableProperty } from './ObservableProperty'\n\nexport class MutableProperty<T> implements ObservableProperty<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 property<T>(initialValue: T): MutableProperty<T> {\n return new MutableProperty<T>(initialValue)\n}\n","import { ObservableProperty } from './ObservableProperty'\nimport { Observable, ObserverFunc } from './Observable'\nimport { compositeObservable } from './CompositeObservable'\nimport { observable } from './DefaultObservable'\n\ntype ValueOf<P> = P extends ObservableProperty<infer T> ? T : never\n\nexport class ComputedProperty<Deps extends readonly ObservableProperty<any>[], T> implements ObservableProperty<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 computed<\n Deps extends readonly ObservableProperty<any>[],\n T,\n>(computeFunc: (...values: { [K in keyof Deps]: ValueOf<Deps[K]> }) => T, ...deps: Deps) {\n return new ComputedProperty(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","MutableProperty","_value","changed","initialValue","newValue","ComputedProperty","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,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,C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AAO,IAAIP,iBAAiBC,eAAgBM,KAChD,iDDvDM,SAAsBT,cACxB,OAAO,IAAIH,gBAAmBG,aAClC"}
|
|
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,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,WAGWK,aACZ,OAAO,IAAIzB,iBACf,OChCa0B,oBAKoBC,QAJrB1B,UAAY,IAAIC,IAChBC,SAAgE,IAAIC,IACpEwB,iBAAmBpB,KAAKqB,cAAclB,KAAKH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,gBAAgBlC,YAAYN,YAEtCE,KAAKsC,gBAAgBhC,YAAYR,UACjCE,KAAK8C,gBAEsB,IAAvB9C,KAAK8C,eAAqB9C,KAAK0C,YAAYpC,YAAYN,MAC9D,CAED,cAAAQ,GACI,MAAMmB,aAAe3B,KAAK8C,cAAgB,EAC1C9C,KAAKsC,gBAAgB9B,iBACrBR,KAAK8C,cAAgB,EACjBnB,cAAc3B,KAAK0C,YAAYpC,YAAYN,KAClD,CAED,YAAMU,CAAOC,OACT,MAAM,IAAIY,MAAM,+CACnB,CAED,SAAIZ,GAEA,OAA2B,IAAvBX,KAAK8C,cAA4B9C,KAAKoD,WAErCpD,KAAK+C,WACN/C,KAAKgD,YAAchD,KAAKoD,UACxBpD,KAAK+C,UAAW,GAEb/C,KAAKgD,YACf,CAEO,OAAAI,GACJ,MAAMvC,OAASb,KAAK2C,KAAKM,KAAII,GAAKA,EAAE1C,QACpC,OAAOX,KAAKqC,eAAexB,OAC9B,CAEO,aAAAgC,GACJ,MAAMV,SAAWnC,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 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
|
|
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/DefaultObservable.ts","../../src/CompositeObservable.ts","../../src/MutableProperty.ts","../../src/ComputedProperty.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 { ObservableProperty } from './ObservableProperty'\n\nexport class MutableProperty<T> implements ObservableProperty<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 property<T>(initialValue: T): MutableProperty<T> {\n return new MutableProperty<T>(initialValue)\n}\n","import { ObservableProperty } from './ObservableProperty'\nimport { Observable, ObserverFunc } from './Observable'\nimport { compositeObservable } from './CompositeObservable'\nimport { observable } from './DefaultObservable'\n\ntype ValueOf<P> = P extends ObservableProperty<infer T> ? T : never\n\nexport class ComputedProperty<Deps extends readonly ObservableProperty<any>[], T> implements ObservableProperty<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 computed<\n Deps extends readonly ObservableProperty<any>[],\n T,\n>(computeFunc: (...values: { [K in keyof Deps]: ValueOf<Deps[K]> }) => T, ...deps: Deps) {\n return new ComputedProperty(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","MutableProperty","_value","changed","initialValue","newValue","property","ComputedProperty","computeFunc","internalChanged","o","h","v","depsChanged","deps","onDepsChangedRef","onDepsChanged","observerCount","hasValue","cachedValue","map","it","observerAlreadySubscribed","compute","d","Object","is","computed"],"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,IAAIP,SAC7B,CAED,WAAAQ,CAAYR,UACRE,KAAKP,UAAUc,OAAOT,UACtBE,KAAKL,SAASY,OAAOT,SACxB,CAED,cAAAU,GACIR,KAAKP,UAAUgB,QACfT,KAAKL,SA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ources":["../../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,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,WAGWK,aACZ,OAAO,IAAIzB,iBACf,OChCa0B,oBAKoBC,QAJrB1B,UAAY,IAAIC,IAChBC,SAAgE,IAAIC,IACpEwB,iBAAmBpB,KAAKqB,cAAclB,KAAKH,MAEnD,WAAAsB,CAA6BH,SAAAnB,KAAOmB,QAAPA,OAC5B,CAED,WAAAf,CAAYN,UACR,OAAOE,KAAKP,UAAUY,IAAIP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,UACjCE,KAAK+C,gBAEsB,IAAvB/C,KAAK+C,eAAqB/C,KAAK2C,YAAYrC,YAAYN,MAC9D,CAED,cAAAQ,GACI,MAAMmB,aAAe3B,KAAK+C,cAAgB,EAC1C/C,KAAKuC,gBAAgB/B,iBACrBR,KAAK+C,cAAgB,EACjBpB,cAAc3B,KAAK2C,YAAYrC,YAAYN,KAClD,CAED,YAAMU,CAAOC,OACT,MAAM,IAAIY,MAAM,+CACnB,CAED,SAAIZ,GAEA,OAA2B,IAAvBX,KAAK+C,cAA4B/C,KAAKqD,WAErCrD,KAAKgD,WACNhD,KAAKiD,YAAcjD,KAAKqD,UACxBrD,KAAKgD,UAAW,GAEbhD,KAAKiD,YACf,CAEO,OAAAI,GACJ,MAAMxC,OAASb,KAAK4C,KAAKM,KAAII,GAAKA,EAAE3C,QACpC,OAAOX,KAAKsC,eAAezB,OAC9B,CAEO,aAAAiC,GACJ,MAAMX,SAAWnC,KAAKqD,UAEjBrD,KAAKgD,UAAaO,OAAOC,GAAGxD,KAAKiD,YAAad,YAC/CnC,KAAKiD,YAAcd,SACnBnC,KAAKgD,UAAW,EAChBhD,KAAKuC,gBAAgB7B,OAAOV,KAAKiD,aAExC,WAGWQ,mBAGdnB,eAA2EM,MACzE,OAAO,IAAIP,wBAAwBC,eAAgBM,KACvD"}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ interface Observable<T> {
|
|
|
7
7
|
notify(value?: T): Promise<void>;
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
interface
|
|
10
|
+
interface ObservableValue<T = any> extends Observable<T> {
|
|
11
11
|
readonly changed: Observable<T>;
|
|
12
12
|
readonly value: T;
|
|
13
13
|
}
|
|
@@ -41,7 +41,7 @@ declare class CompositeObservable<Sources extends readonly Observable<any>[]> im
|
|
|
41
41
|
}
|
|
42
42
|
declare function compositeObservable<Sources extends readonly Observable<any>[]>(...sources: Sources): CompositeObservable<Sources>;
|
|
43
43
|
|
|
44
|
-
declare class
|
|
44
|
+
declare class ObservableMutableValue<T> implements ObservableValue<T> {
|
|
45
45
|
private _value;
|
|
46
46
|
readonly changed: Observable<T>;
|
|
47
47
|
constructor(initialValue: T);
|
|
@@ -53,10 +53,10 @@ declare class MutableProperty<T> implements ObservableProperty<T> {
|
|
|
53
53
|
get value(): T;
|
|
54
54
|
set value(newValue: T);
|
|
55
55
|
}
|
|
56
|
-
declare function
|
|
56
|
+
declare function observableValue<T>(initialValue: T): ObservableMutableValue<T>;
|
|
57
57
|
|
|
58
|
-
type ValueOf<P> = P extends
|
|
59
|
-
declare class
|
|
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
60
|
private readonly computeFunc;
|
|
61
61
|
private readonly internalChanged;
|
|
62
62
|
readonly changed: Observable<T>;
|
|
@@ -78,8 +78,8 @@ declare class ComputedProperty<Deps extends readonly ObservableProperty<any>[],
|
|
|
78
78
|
private compute;
|
|
79
79
|
private onDepsChanged;
|
|
80
80
|
}
|
|
81
|
-
declare function
|
|
81
|
+
declare function observableComputed<Deps extends readonly ObservableValue<any>[], T>(computeFunc: (...values: {
|
|
82
82
|
[K in keyof Deps]: ValueOf<Deps[K]>;
|
|
83
|
-
}) => T, ...deps: Deps):
|
|
83
|
+
}) => T, ...deps: Deps): ObservableComputedValue<[...Deps], T>;
|
|
84
84
|
|
|
85
|
-
export { CompositeObservable,
|
|
85
|
+
export { CompositeObservable, DefaultObservable, Observable, ObservableComputedValue, ObservableMutableValue, ObservableValue, ObserverFunc, compositeObservable, observable, observableComputed, observableValue };
|
package/package.json
CHANGED