@nbottarini/observable 0.9.0 → 0.9.1
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/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 +3 -0
- package/package.json +1 -1
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 DerivedObservable{source;predicate;mapper;observers=new Set;handlers=new Map;onSourceEventRef=this.onSourceEvent.bind(this);constructor(source,predicate=(()=>!0),mapper){this.source=source,this.predicate=predicate,this.mapper=mapper}hasObserver(observer){return this.observers.has(observer)}async notify(_value){throw new Error("DerivedObservable 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.attachToSource()}unsubscribe(observer){const hadObservers=this.observers.size>0;this.observers.delete(observer),this.handlers.delete(observer),hadObservers&&0===this.observers.size&&this.detachFromSource()}unsubscribeAll(){const hadObservers=this.observers.size>0;this.observers.clear(),this.handlers.clear(),hadObservers&&this.detachFromSource()}attachToSource(){this.source.subscribe(this,this.onSourceEventRef)}detachFromSource(){this.source.unsubscribe(this)}async onSourceEvent(value){if(!this.predicate(value))return;const mapped=this.mapper?this.mapper(value):value;let promises=[];for(const handler of this.handlers.values())promises.push(handler(mapped));await Promise.all(promises)}}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 DerivedObservable{source;predicate;mapper;observers=new Set;handlers=new Map;onSourceEventRef=this.onSourceEvent.bind(this);constructor(source,predicate=(()=>!0),mapper){this.source=source,this.predicate=predicate,this.mapper=mapper}hasObserver(observer){return this.observers.has(observer)}async notify(_value){throw new Error("DerivedObservable 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.attachToSource()}unsubscribe(observer){const hadObservers=this.observers.size>0;this.observers.delete(observer),this.handlers.delete(observer),hadObservers&&0===this.observers.size&&this.detachFromSource()}unsubscribeAll(){const hadObservers=this.observers.size>0;this.observers.clear(),this.handlers.clear(),hadObservers&&this.detachFromSource()}attachToSource(){this.source.subscribe(this,this.onSourceEventRef)}detachFromSource(){this.source.unsubscribe(this)}async onSourceEvent(value){if(!this.predicate(value))return;const mapped=this.mapper?this.mapper(value):value;let promises=[];for(const handler of this.handlers.values())promises.push(handler(mapped));await Promise.all(promises)}}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))}map(mapper){return observableComputed(mapper,this)}}function observableComputed(computeFunc,...deps){return new ObservableComputedValue(computeFunc,...deps)}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))}map(mapper){return observableComputed(mapper,this)}}exports.CompositeObservable=CompositeObservable,exports.DefaultObservable=DefaultObservable,exports.DerivedObservable=DerivedObservable,exports.ObservableComputedValue=ObservableComputedValue,exports.ObservableMutableValue=ObservableMutableValue,exports.compositeObservable=compositeObservable,exports.derivedObservable=function(source,predicate,mapper){return new DerivedObservable(source,predicate,mapper)},exports.observable=observable,exports.observableComputed=observableComputed,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/DerivedObservable.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, ObserverFunc } from './Observable'\n\ntype Mapper<S, T> = (value?: S) => T\ntype Predicate<S> = (value?: S) => boolean\n\nexport class DerivedObservable<S, T = S> implements Observable<T> {\n private observers = new Set<object>()\n private handlers: Map<object, ObserverFunc<T>> = new Map()\n private onSourceEventRef = this.onSourceEvent.bind(this)\n\n constructor(\n private readonly source: Observable<S>,\n private readonly predicate: Predicate<S> = () => true,\n private readonly mapper?: Mapper<S, T>,\n ) {}\n\n hasObserver(observer: object): boolean {\n return this.observers.has(observer)\n }\n\n async notify(_value?: T) {\n throw new Error('DerivedObservable cannot be notified directly')\n }\n\n subscribe(observer: object, handler: ObserverFunc<T>): void {\n const isFirstObserver = this.observers.size === 0\n\n this.observers.add(observer)\n this.handlers.set(observer, handler.bind(observer))\n\n if (isFirstObserver) this.attachToSource()\n }\n\n unsubscribe(observer: object): void {\n const hadObservers = this.observers.size > 0\n\n this.observers.delete(observer)\n this.handlers.delete(observer)\n\n if (hadObservers && this.observers.size === 0) this.detachFromSource()\n }\n\n unsubscribeAll(): void {\n const hadObservers = this.observers.size > 0\n this.observers.clear()\n this.handlers.clear()\n if (hadObservers) this.detachFromSource()\n }\n\n private attachToSource() {\n this.source.subscribe(this, this.onSourceEventRef)\n }\n\n private detachFromSource() {\n this.source.unsubscribe(this)\n }\n\n private async onSourceEvent(value?: S) {\n if (!this.predicate(value)) return\n\n const mapped = this.mapper ? this.mapper(value) : (value as unknown as T)\n\n let promises = []\n for (const handler of this.handlers.values()) {\n promises.push(handler(mapped))\n }\n await Promise.all(promises)\n }\n}\n\nexport function derivedObservable<S, T = S>(\n source: Observable<S>,\n predicate: Predicate<S>,\n mapper?: Mapper<S, T>,\n) {\n return new DerivedObservable<S, T>(source, predicate, mapper)\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","DerivedObservable","predicate","mapper","_value","attachToSource","detachFromSource","mapped","ObservableMutableValue","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,MAEnD,WAAAsB,CAA6BH,SAAAnB,KAAOmB,QAAPA,OAC5B,CAED,WAAAf,CAAYN,UACR,OAAOE,KAAKP,UAAUY,IAAIP,SAC7B,CAED,YAAMY,CAAOC,OACT,MAAM,IAAIY,MAAM,kDACnB,CAED,SAAA1B,CAAUC,SAAkBC,SACxB,MAAMyB,gBAA0C,IAAxBxB,KAAKP,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KAAKoD,cAAgB,EACjBzB,cAAc3B,KAAKgD,YAAY1C,YAAYN,KAClD,CAED,YAAMU,CAAOC,OACT,MAAM,IAAIY,MAAM,+CACnB,CAED,SAAIZ,GAEA,OAA2B,IAAvBX,KAAKoD,cAA4BpD,KAAK0D,WAErC1D,KAAKqD,WACNrD,KAAKsD,YAActD,KAAK0D,UACxB1D,KAAKqD,UAAW,GAEbrD,KAAKsD,YACf,CAEO,OAAAI,GACJ,MAAM7C,OAASb,KAAKiD,KAAKM,KAAII,GAAKA,EAAEhD,QACpC,OAAOX,KAAK2C,eAAe9B,OAC9B,CAEO,aAAAsC,GACJ,MAAMV,SAAWzC,KAAK0D,UAEjB1D,KAAKqD,UAAaO,OAAOC,GAAG7D,KAAKsD,YAAab,YAC/CzC,KAAKsD,YAAcb,SACnBzC,KAAKqD,UAAW,EAChBrD,KAAK4C,gBAAgBlC,OAAOV,KAAKsD,aAExC,2UFhBDzB,OACAG,UACAC,QAEA,OAAO,IAAIF,kBAAwBF,OAAQG,UAAWC,OAC1D,oEEiBEU,eAA2EM,MACzE,OAAO,IAAIP,wBAAwBC,eAAgBM,KACvD,0BDvDM,SAA6BT,cAC/B,OAAO,IAAIF,uBAA0BE,aACzC"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/DefaultObservable.ts","../../src/CompositeObservable.ts","../../src/DerivedObservable.ts","../../src/ObservableComputedValue.ts","../../src/ObservableMutableValue.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, ObserverFunc } from './Observable'\n\ntype Mapper<S, T> = (value?: S) => T\ntype Predicate<S> = (value?: S) => boolean\n\nexport class DerivedObservable<S, T = S> implements Observable<T> {\n private observers = new Set<object>()\n private handlers: Map<object, ObserverFunc<T>> = new Map()\n private onSourceEventRef = this.onSourceEvent.bind(this)\n\n constructor(\n private readonly source: Observable<S>,\n private readonly predicate: Predicate<S> = () => true,\n private readonly mapper?: Mapper<S, T>,\n ) {}\n\n hasObserver(observer: object): boolean {\n return this.observers.has(observer)\n }\n\n async notify(_value?: T) {\n throw new Error('DerivedObservable cannot be notified directly')\n }\n\n subscribe(observer: object, handler: ObserverFunc<T>): void {\n const isFirstObserver = this.observers.size === 0\n\n this.observers.add(observer)\n this.handlers.set(observer, handler.bind(observer))\n\n if (isFirstObserver) this.attachToSource()\n }\n\n unsubscribe(observer: object): void {\n const hadObservers = this.observers.size > 0\n\n this.observers.delete(observer)\n this.handlers.delete(observer)\n\n if (hadObservers && this.observers.size === 0) this.detachFromSource()\n }\n\n unsubscribeAll(): void {\n const hadObservers = this.observers.size > 0\n this.observers.clear()\n this.handlers.clear()\n if (hadObservers) this.detachFromSource()\n }\n\n private attachToSource() {\n this.source.subscribe(this, this.onSourceEventRef)\n }\n\n private detachFromSource() {\n this.source.unsubscribe(this)\n }\n\n private async onSourceEvent(value?: S) {\n if (!this.predicate(value)) return\n\n const mapped = this.mapper ? this.mapper(value) : (value as unknown as T)\n\n let promises = []\n for (const handler of this.handlers.values()) {\n promises.push(handler(mapped))\n }\n await Promise.all(promises)\n }\n}\n\nexport function derivedObservable<S, T = S>(\n source: Observable<S>,\n predicate: Predicate<S>,\n mapper?: Mapper<S, T>,\n) {\n return new DerivedObservable<S, T>(source, predicate, mapper)\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 map<S>(mapper: (v:T) => S): ObservableValue<S> {\n return observableComputed(mapper, this)\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","import { observable } from './DefaultObservable'\nimport { ObserverFunc } from './Observable'\nimport { ObservableValue } from './ObservableValue'\nimport { observableComputed } from './ObservableComputedValue'\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 map<S>(mapper: (v:T) => S): ObservableValue<S> {\n return observableComputed(mapper, this)\n }\n}\n\nexport function observableValue<T>(initialValue: T): ObservableMutableValue<T> {\n return new ObservableMutableValue<T>(initialValue)\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","DerivedObservable","predicate","mapper","_value","attachToSource","detachFromSource","mapped","ObservableComputedValue","computeFunc","internalChanged","changed","o","h","v","depsChanged","deps","onDepsChangedRef","onDepsChanged","observerCount","hasValue","cachedValue","map","it","observerAlreadySubscribed","compute","d","newValue","Object","is","observableComputed","ObservableMutableValue","initialValue"],"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,MAEnD,WAAAsB,CAA6BH,SAAAnB,KAAOmB,QAAPA,OAC5B,CAED,WAAAf,CAAYN,UACR,OAAOE,KAAKP,UAAUY,IAAIP,SAC7B,CAED,YAAMY,CAAOC,OACT,MAAM,IAAIY,MAAM,kDACnB,CAED,SAAA1B,CAAUC,SAAkBC,SACxB,MAAMyB,gBAA0C,IAAxBxB,KAAKP,UAAUgC,KACvCzB,KAAKP,UAAUQ,IAAIH,UACnBE,KAAKL,SAASO,IAAIJ,SAAUC,QAAQI,KAAKL,WAErC0B,iBAAiBxB,KAAK0B,iBAC7B,CAED,WAAApB,CAAYR,UACR,MAAM6B,aAAgB3B,KAAKP,UAAUgC,KAAO,EAC5CzB,KAAKP,UAAUc,OAAOT,UACtBE,KAAKL,SAASY,OAAOT,UAEjB6B,cAA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jE,CAAUC,SAAkBC,SACxBC,KAAKyC,QAAQ5C,UAAUC,SAAUC,QACpC,CACD,WAAAK,CAAYN,UACR,OAAOE,KAAKyC,QAAQrC,YAAYN,SACnC,CACD,WAAAQ,CAAYR,UACRE,KAAKyC,QAAQnC,YAAYR,SAC5B,CACD,cAAAU,GACIR,KAAKyC,QAAQjC,gBAChB,CAED,YAAME,CAAOC,OACT,OAAOX,KAAKyC,QAAQ/B,OAAOC,MAC9B,CAED,SAAIA,GACA,OAAOX,KAAKkC,MACf,CAED,SAAIvB,CAAM8C,UACFzD,KAAKkC,SAAWuB,WACpBzD,KAAKkC,OAASuB,SACdzD,KAAKyC,QAAQ/B,OAAOV,KAAKkC,QAC5B,CAED,GAAAkB,CAAOnB,QACH,OAAO2B,mBAAmB3B,OAAQjC,KACrC,2UF6BD6B,OACAG,UACAC,QAEA,OAAO,IAAIF,kBAAwBF,OAAQG,UAAWC,OAC1D,sGE/BM,SAA6B6B,cAC/B,OAAO,IAAID,uBAA0BC,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 DerivedObservable{source;predicate;mapper;observers=new Set;handlers=new Map;onSourceEventRef=this.onSourceEvent.bind(this);constructor(source,predicate=(()=>!0),mapper){this.source=source,this.predicate=predicate,this.mapper=mapper}hasObserver(observer){return this.observers.has(observer)}async notify(_value){throw new Error("DerivedObservable 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.attachToSource()}unsubscribe(observer){const hadObservers=this.observers.size>0;this.observers.delete(observer),this.handlers.delete(observer),hadObservers&&0===this.observers.size&&this.detachFromSource()}unsubscribeAll(){const hadObservers=this.observers.size>0;this.observers.clear(),this.handlers.clear(),hadObservers&&this.detachFromSource()}attachToSource(){this.source.subscribe(this,this.onSourceEventRef)}detachFromSource(){this.source.unsubscribe(this)}async onSourceEvent(value){if(!this.predicate(value))return;const mapped=this.mapper?this.mapper(value):value;let promises=[];for(const handler of this.handlers.values())promises.push(handler(mapped));await Promise.all(promises)}}function derivedObservable(source,predicate,mapper){return new DerivedObservable(source,predicate,mapper)}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 DerivedObservable{source;predicate;mapper;observers=new Set;handlers=new Map;onSourceEventRef=this.onSourceEvent.bind(this);constructor(source,predicate=(()=>!0),mapper){this.source=source,this.predicate=predicate,this.mapper=mapper}hasObserver(observer){return this.observers.has(observer)}async notify(_value){throw new Error("DerivedObservable 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.attachToSource()}unsubscribe(observer){const hadObservers=this.observers.size>0;this.observers.delete(observer),this.handlers.delete(observer),hadObservers&&0===this.observers.size&&this.detachFromSource()}unsubscribeAll(){const hadObservers=this.observers.size>0;this.observers.clear(),this.handlers.clear(),hadObservers&&this.detachFromSource()}attachToSource(){this.source.subscribe(this,this.onSourceEventRef)}detachFromSource(){this.source.unsubscribe(this)}async onSourceEvent(value){if(!this.predicate(value))return;const mapped=this.mapper?this.mapper(value):value;let promises=[];for(const handler of this.handlers.values())promises.push(handler(mapped));await Promise.all(promises)}}function derivedObservable(source,predicate,mapper){return new DerivedObservable(source,predicate,mapper)}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))}map(mapper){return observableComputed(mapper,this)}}function observableComputed(computeFunc,...deps){return new ObservableComputedValue(computeFunc,...deps)}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))}map(mapper){return observableComputed(mapper,this)}}function observableValue(initialValue){return new ObservableMutableValue(initialValue)}export{CompositeObservable,DefaultObservable,DerivedObservable,ObservableComputedValue,ObservableMutableValue,compositeObservable,derivedObservable,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/DerivedObservable.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, ObserverFunc } from './Observable'\n\ntype Mapper<S, T> = (value?: S) => T\ntype Predicate<S> = (value?: S) => boolean\n\nexport class DerivedObservable<S, T = S> implements Observable<T> {\n private observers = new Set<object>()\n private handlers: Map<object, ObserverFunc<T>> = new Map()\n private onSourceEventRef = this.onSourceEvent.bind(this)\n\n constructor(\n private readonly source: Observable<S>,\n private readonly predicate: Predicate<S> = () => true,\n private readonly mapper?: Mapper<S, T>,\n ) {}\n\n hasObserver(observer: object): boolean {\n return this.observers.has(observer)\n }\n\n async notify(_value?: T) {\n throw new Error('DerivedObservable cannot be notified directly')\n }\n\n subscribe(observer: object, handler: ObserverFunc<T>): void {\n const isFirstObserver = this.observers.size === 0\n\n this.observers.add(observer)\n this.handlers.set(observer, handler.bind(observer))\n\n if (isFirstObserver) this.attachToSource()\n }\n\n unsubscribe(observer: object): void {\n const hadObservers = this.observers.size > 0\n\n this.observers.delete(observer)\n this.handlers.delete(observer)\n\n if (hadObservers && this.observers.size === 0) this.detachFromSource()\n }\n\n unsubscribeAll(): void {\n const hadObservers = this.observers.size > 0\n this.observers.clear()\n this.handlers.clear()\n if (hadObservers) this.detachFromSource()\n }\n\n private attachToSource() {\n this.source.subscribe(this, this.onSourceEventRef)\n }\n\n private detachFromSource() {\n this.source.unsubscribe(this)\n }\n\n private async onSourceEvent(value?: S) {\n if (!this.predicate(value)) return\n\n const mapped = this.mapper ? this.mapper(value) : (value as unknown as T)\n\n let promises = []\n for (const handler of this.handlers.values()) {\n promises.push(handler(mapped))\n }\n await Promise.all(promises)\n }\n}\n\nexport function derivedObservable<S, T = S>(\n source: Observable<S>,\n predicate: Predicate<S>,\n mapper?: Mapper<S, T>,\n) {\n return new DerivedObservable<S, T>(source, predicate, mapper)\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","DerivedObservable","predicate","mapper","_value","attachToSource","detachFromSource","mapped","derivedObservable","ObservableMutableValue","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,IAA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AyC,IAAvBxB,KAAKsD,cAC7BtD,KAAK8C,gBAAgBjD,UAAUC,SAAUC,SACpC4D,2BACD3D,KAAKsD,gBAEL9B,iBAAiBxB,KAAKkD,YAAYrD,UAAUG,KAAMA,KAAKoD,iBAC9D,CAED,WAAAhD,CAAYN,UACR,OAAOE,KAAK8C,gBAAgB1C,YAAYN,SAC3C,CAED,WAAAQ,CAAYR,UACHE,KAAK8C,gBAAgB1C,YAAYN,YAEtCE,KAAK8C,gBAAgBxC,YAAYR,UACjCE,KAAKsD,gBAEsB,IAAvBtD,KAAKsD,eAAqBtD,KAAKkD,YAAY5C,YAAYN,MAC9D,CAED,cAAAQ,GACI,MAAMmB,aAAe3B,KAAKsD,cAAgB,EAC1CtD,KAAK8C,gBAAgBtC,iBACrBR,KAAKsD,cAAgB,EACjB3B,cAAc3B,KAAKkD,YAAY5C,YAAYN,KAClD,CAED,YAAMU,CAAOC,OACT,MAAM,IAAIY,MAAM,+CACnB,CAED,SAAIZ,GAEA,OAA2B,IAAvBX,KAAKsD,cAA4BtD,KAAK4D,WAErC5D,KAAKuD,WACNvD,KAAKwD,YAAcxD,KAAK4D,UACxB5D,KAAKuD,UAAW,GAEbvD,KAAKwD,YACf,CAEO,OAAAI,GACJ,MAAM/C,OAASb,KAAKmD,KAAKM,KAAII,GAAKA,EAAElD,QACpC,OAAOX,KAAK6C,eAAehC,OAC9B,CAEO,aAAAwC,GACJ,MAAMX,SAAW1C,KAAK4D,UAEjB5D,KAAKuD,UAAaO,OAAOC,GAAG/D,KAAKwD,YAAad,YAC/C1C,KAAKwD,YAAcd,SACnB1C,KAAKuD,UAAW,EAChBvD,KAAK8C,gBAAgBpC,OAAOV,KAAKwD,aAExC,WAGWQ,mBAGdnB,eAA2EM,MACzE,OAAO,IAAIP,wBAAwBC,eAAgBM,KACvD"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/DefaultObservable.ts","../../src/CompositeObservable.ts","../../src/DerivedObservable.ts","../../src/ObservableComputedValue.ts","../../src/ObservableMutableValue.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, ObserverFunc } from './Observable'\n\ntype Mapper<S, T> = (value?: S) => T\ntype Predicate<S> = (value?: S) => boolean\n\nexport class DerivedObservable<S, T = S> implements Observable<T> {\n private observers = new Set<object>()\n private handlers: Map<object, ObserverFunc<T>> = new Map()\n private onSourceEventRef = this.onSourceEvent.bind(this)\n\n constructor(\n private readonly source: Observable<S>,\n private readonly predicate: Predicate<S> = () => true,\n private readonly mapper?: Mapper<S, T>,\n ) {}\n\n hasObserver(observer: object): boolean {\n return this.observers.has(observer)\n }\n\n async notify(_value?: T) {\n throw new Error('DerivedObservable cannot be notified directly')\n }\n\n subscribe(observer: object, handler: ObserverFunc<T>): void {\n const isFirstObserver = this.observers.size === 0\n\n this.observers.add(observer)\n this.handlers.set(observer, handler.bind(observer))\n\n if (isFirstObserver) this.attachToSource()\n }\n\n unsubscribe(observer: object): void {\n const hadObservers = this.observers.size > 0\n\n this.observers.delete(observer)\n this.handlers.delete(observer)\n\n if (hadObservers && this.observers.size === 0) this.detachFromSource()\n }\n\n unsubscribeAll(): void {\n const hadObservers = this.observers.size > 0\n this.observers.clear()\n this.handlers.clear()\n if (hadObservers) this.detachFromSource()\n }\n\n private attachToSource() {\n this.source.subscribe(this, this.onSourceEventRef)\n }\n\n private detachFromSource() {\n this.source.unsubscribe(this)\n }\n\n private async onSourceEvent(value?: S) {\n if (!this.predicate(value)) return\n\n const mapped = this.mapper ? this.mapper(value) : (value as unknown as T)\n\n let promises = []\n for (const handler of this.handlers.values()) {\n promises.push(handler(mapped))\n }\n await Promise.all(promises)\n }\n}\n\nexport function derivedObservable<S, T = S>(\n source: Observable<S>,\n predicate: Predicate<S>,\n mapper?: Mapper<S, T>,\n) {\n return new DerivedObservable<S, T>(source, predicate, mapper)\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 map<S>(mapper: (v:T) => S): ObservableValue<S> {\n return observableComputed(mapper, this)\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","import { observable } from './DefaultObservable'\nimport { ObserverFunc } from './Observable'\nimport { ObservableValue } from './ObservableValue'\nimport { observableComputed } from './ObservableComputedValue'\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 map<S>(mapper: (v:T) => S): ObservableValue<S> {\n return observableComputed(mapper, this)\n }\n}\n\nexport function observableValue<T>(initialValue: T): ObservableMutableValue<T> {\n return new ObservableMutableValue<T>(initialValue)\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","DerivedObservable","predicate","mapper","_value","attachToSource","detachFromSource","mapped","derivedObservable","ObservableComputedValue","computeFunc","internalChanged","changed","o","h","v","depsChanged","deps","onDepsChangedRef","onDepsChanged","observerCount","hasValue","cachedValue","map","it","observerAlreadySubscribed","compute","d","newValue","Object","is","observableComputed","ObservableMutableValue","initialValue","observableValue"],"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,SAC7B,C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QACpC,OAAOX,KAAKwC,eAAe3B,OAC9B,CAEO,aAAAoC,GACJ,MAAMS,SAAW1D,KAAKwD,UAEjBxD,KAAKmD,UAAaQ,OAAOC,GAAG5D,KAAKoD,YAAaM,YAC/C1D,KAAKoD,YAAcM,SACnB1D,KAAKmD,UAAW,EAChBnD,KAAKyC,gBAAgB/B,OAAOV,KAAKoD,aAExC,CAED,GAAAC,CAAOpB,QACH,OAAO4B,mBAAmB5B,OAAQjC,KACrC,WAGW6D,mBAGdrB,eAA2EO,MACzE,OAAO,IAAIR,wBAAwBC,eAAgBO,KACvD,OC9Fae,uBACD5B,OACCQ,QAAUzB,aAEnB,WAAAK,CAAYyC,cACR/D,KAAKkC,OAAS6B,YACjB,CAED,SAAAlE,CAAUC,SAAkBC,SACxBC,KAAK0C,QAAQ7C,UAAUC,SAAUC,QACpC,CACD,WAAAK,CAAYN,UACR,OAAOE,KAAK0C,QAAQtC,YAAYN,SACnC,CACD,WAAAQ,CAAYR,UACRE,KAAK0C,QAAQpC,YAAYR,SAC5B,CACD,cAAAU,GACIR,KAAK0C,QAAQlC,gBAChB,CAED,YAAME,CAAOC,OACT,OAAOX,KAAK0C,QAAQhC,OAAOC,MAC9B,CAED,SAAIA,GACA,OAAOX,KAAKkC,MACf,CAED,SAAIvB,CAAM+C,UACF1D,KAAKkC,SAAWwB,WACpB1D,KAAKkC,OAASwB,SACd1D,KAAK0C,QAAQhC,OAAOV,KAAKkC,QAC5B,CAED,GAAAmB,CAAOpB,QACH,OAAO4B,mBAAmB5B,OAAQjC,KACrC,EAGC,SAAUgE,gBAAmBD,cAC/B,OAAO,IAAID,uBAA0BC,aACzC"}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ interface Observable<T> {
|
|
|
10
10
|
interface ObservableValue<T = any> extends Observable<T> {
|
|
11
11
|
readonly changed: Observable<T>;
|
|
12
12
|
readonly value: T;
|
|
13
|
+
map<S>(mapper: (v: T) => S): ObservableValue<S>;
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
declare class DefaultObservable<T = void> implements Observable<T> {
|
|
@@ -73,6 +74,7 @@ declare class ObservableMutableValue<T> implements ObservableValue<T> {
|
|
|
73
74
|
notify(value?: T): Promise<void>;
|
|
74
75
|
get value(): T;
|
|
75
76
|
set value(newValue: T);
|
|
77
|
+
map<S>(mapper: (v: T) => S): ObservableValue<S>;
|
|
76
78
|
}
|
|
77
79
|
declare function observableValue<T>(initialValue: T): ObservableMutableValue<T>;
|
|
78
80
|
|
|
@@ -98,6 +100,7 @@ declare class ObservableComputedValue<Deps extends readonly ObservableValue<any>
|
|
|
98
100
|
get value(): T;
|
|
99
101
|
private compute;
|
|
100
102
|
private onDepsChanged;
|
|
103
|
+
map<S>(mapper: (v: T) => S): ObservableValue<S>;
|
|
101
104
|
}
|
|
102
105
|
declare function observableComputed<Deps extends readonly ObservableValue<any>[], T>(computeFunc: (...values: {
|
|
103
106
|
[K in keyof Deps]: ValueOf<Deps[K]>;
|
package/package.json
CHANGED