react-rx 4.1.2-canary.7 → 4.1.2-canary.8

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/index.cjs CHANGED
@@ -29,7 +29,7 @@ function useObservable(observable, initialValue, debug) {
29
29
  subscribe: (onStoreChange) => {
30
30
  debug && console.log("subscribe", observable);
31
31
  const subscription = instance.observable.subscribe(() => {
32
- console.log("onStoreChange", observable), onStoreChange();
32
+ debug && console.log("onStoreChange", observable), onStoreChange();
33
33
  });
34
34
  return () => {
35
35
  debug && console.log("unsubscribe", observable), subscription.unsubscribe();
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../src/useObservable.ts","../src/useObservableEvent.ts"],"sourcesContent":["import {useMemo, useSyncExternalStore} from 'react'\nimport {\n asapScheduler,\n catchError,\n finalize,\n type Observable,\n type ObservedValueOf,\n of,\n share,\n timer,\n} from 'rxjs'\nimport {map, tap} from 'rxjs/operators'\n\nfunction getValue<T>(value: T): T extends () => infer U ? U : T {\n return typeof value === 'function' ? value() : value\n}\n\ninterface CacheRecord<T> {\n observable: Observable<void>\n snapshot: T\n error?: unknown\n}\n\nconst cache = new WeakMap<Observable<any>, CacheRecord<any>>()\n\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n initialValue: ObservedValueOf<ObservableType> | (() => ObservedValueOf<ObservableType>),\n debug?: boolean,\n): ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n): undefined | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue: InitialValue | (() => InitialValue),\n debug?: boolean,\n): InitialValue | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue?: InitialValue | (() => InitialValue),\n debug?: boolean,\n): InitialValue | ObservedValueOf<ObservableType> {\n const store = useMemo(() => {\n if (!cache.has(observable)) {\n const entry: Partial<CacheRecord<ObservedValueOf<ObservableType>>> = {}\n entry.observable = observable.pipe(\n map((value) => ({snapshot: value, error: undefined})),\n catchError((error) => of({snapshot: undefined, error})),\n tap(({snapshot, error}) => {\n if (debug) {\n console.log('tap', snapshot, error)\n }\n entry.snapshot = snapshot\n entry.error = error\n }),\n // Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state\n // and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.\n map((value) => void value),\n // Ensure that the cache entry is deleted when the observable completes or errors.\n finalize(() => cache.delete(observable)),\n share({resetOnRefCountZero: () => timer(0, asapScheduler)}),\n )\n\n // Eagerly subscribe to sync set `entry.currentValue` to what the observable returns, and keep the observable alive until the component unmounts.\n const subscription = entry.observable.subscribe()\n subscription.unsubscribe()\n\n cache.set(observable, entry as CacheRecord<ObservedValueOf<ObservableType>>)\n }\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const instance = cache.get(observable)!\n\n return {\n subscribe: (onStoreChange: () => void) => {\n if (debug) {\n console.log('subscribe', observable)\n }\n const subscription = instance.observable.subscribe(() => {\n console.log('onStoreChange', observable)\n onStoreChange()\n })\n return () => {\n if (debug) {\n console.log('unsubscribe', observable)\n }\n subscription.unsubscribe()\n }\n },\n getSnapshot: () => {\n if (debug) {\n console.log('getSnapshot', instance.snapshot, instance.error)\n }\n if (instance.error) {\n throw instance.error\n }\n return instance.snapshot\n },\n }\n }, [debug, observable])\n\n return useSyncExternalStore<ObservedValueOf<ObservableType>>(\n store.subscribe,\n () => store.getSnapshot() ?? (getValue(initialValue) as ObservedValueOf<ObservableType>),\n typeof initialValue === 'undefined'\n ? undefined\n : () => getValue(initialValue) as ObservedValueOf<ObservableType>,\n )\n}\n","import {observableCallback} from 'observable-callback'\nimport {useEffect, useState} from 'react'\nimport {type Observable} from 'rxjs'\nimport {useEffectEvent} from 'use-effect-event'\n\n/** @public */\nexport function useObservableEvent<T, U>(\n handleEvent: (arg: Observable<T>) => Observable<U>,\n): (arg: T) => void {\n const [[calls$, call]] = useState(() => observableCallback<T>())\n\n const onEvent = useEffectEvent((observable: Observable<T>) => handleEvent(observable))\n\n useEffect(() => {\n const subscription = calls$.pipe((observable) => onEvent(observable)).subscribe()\n return () => subscription.unsubscribe()\n }, [calls$, onEvent])\n\n return call\n}\n"],"names":["useMemo","map","catchError","of","tap","finalize","share","timer","asapScheduler","useSyncExternalStore","useState","observableCallback","useEffectEvent","useEffect"],"mappings":";;;AAaA,SAAS,SAAY,OAA2C;AAC9D,SAAO,OAAO,SAAU,aAAa,MAAU,IAAA;AACjD;AAQA,MAAM,4BAAY,QAA2C;AAmB7C,SAAA,cACd,YACA,cACA,OACgD;AAC1C,QAAA,QAAQA,MAAAA,QAAQ,MAAM;AAC1B,QAAI,CAAC,MAAM,IAAI,UAAU,GAAG;AAC1B,YAAM,QAA+D,CAAC;AACtE,YAAM,aAAa,WAAW;AAAA,QAC5BC,cAAI,CAAC,WAAW,EAAC,UAAU,OAAO,OAAO,SAAW;AAAA,QACpDC,gBAAW,CAAC,UAAUC,QAAG,EAAC,UAAU,QAAW,MAAK,CAAC,CAAC;AAAA,QACtDC,UAAAA,IAAI,CAAC,EAAC,UAAU,YAAW;AACrB,mBACF,QAAQ,IAAI,OAAO,UAAU,KAAK,GAEpC,MAAM,WAAW,UACjB,MAAM,QAAQ;AAAA,QAAA,CACf;AAAA;AAAA;AAAA,QAGDH,UAAA,IAAI,CAAC,UAAO;AAAA,QAAA,CAAa;AAAA;AAAA,QAEzBI,KAAAA,SAAS,MAAM,MAAM,OAAO,UAAU,CAAC;AAAA,QACvCC,KAAA,MAAM,EAAC,qBAAqB,MAAMC,WAAM,GAAGC,KAAa,aAAA,EAAE,CAAA;AAAA,MAAA,GAIvC,MAAM,WAAW,YACzB,YAEb,GAAA,MAAM,IAAI,YAAY,KAAqD;AAAA,IAAA;AAGvE,UAAA,WAAW,MAAM,IAAI,UAAU;AAE9B,WAAA;AAAA,MACL,WAAW,CAAC,kBAA8B;AACpC,iBACF,QAAQ,IAAI,aAAa,UAAU;AAErC,cAAM,eAAe,SAAS,WAAW,UAAU,MAAM;AACvD,kBAAQ,IAAI,iBAAiB,UAAU,GACvC,cAAc;AAAA,QAAA,CACf;AACD,eAAO,MAAM;AACP,mBACF,QAAQ,IAAI,eAAe,UAAU,GAEvC,aAAa,YAAY;AAAA,QAC3B;AAAA,MACF;AAAA,MACA,aAAa,MAAM;AACb,YAAA,SACF,QAAQ,IAAI,eAAe,SAAS,UAAU,SAAS,KAAK,GAE1D,SAAS;AACX,gBAAM,SAAS;AAEjB,eAAO,SAAS;AAAA,MAAA;AAAA,IAEpB;AAAA,EAAA,GACC,CAAC,OAAO,UAAU,CAAC;AAEf,SAAAC,MAAA;AAAA,IACL,MAAM;AAAA,IACN,MAAM,MAAM,iBAAkB,SAAS,YAAY;AAAA,IACnD,OAAO,eAAiB,MACpB,SACA,MAAM,SAAS,YAAY;AAAA,EACjC;AACF;AC1GO,SAAS,mBACd,aACkB;AAClB,QAAM,CAAC,CAAC,QAAQ,IAAI,CAAC,IAAIC,MAAS,SAAA,MAAMC,mBAAAA,mBAAuB,CAAA,GAEzD,UAAUC,eAAAA,eAAe,CAAC,eAA8B,YAAY,UAAU,CAAC;AAErF,SAAAC,MAAA,UAAU,MAAM;AACR,UAAA,eAAe,OAAO,KAAK,CAAC,eAAe,QAAQ,UAAU,CAAC,EAAE,UAAU;AACzE,WAAA,MAAM,aAAa,YAAY;AAAA,EACrC,GAAA,CAAC,QAAQ,OAAO,CAAC,GAEb;AACT;;;"}
1
+ {"version":3,"file":"index.cjs","sources":["../src/useObservable.ts","../src/useObservableEvent.ts"],"sourcesContent":["import {useMemo, useSyncExternalStore} from 'react'\nimport {\n asapScheduler,\n catchError,\n finalize,\n type Observable,\n type ObservedValueOf,\n of,\n share,\n timer,\n} from 'rxjs'\nimport {map, tap} from 'rxjs/operators'\n\nfunction getValue<T>(value: T): T extends () => infer U ? U : T {\n return typeof value === 'function' ? value() : value\n}\n\ninterface CacheRecord<T> {\n observable: Observable<void>\n snapshot: T\n error?: unknown\n}\n\nconst cache = new WeakMap<Observable<any>, CacheRecord<any>>()\n\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n initialValue: ObservedValueOf<ObservableType> | (() => ObservedValueOf<ObservableType>),\n debug?: boolean,\n): ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n): undefined | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue: InitialValue | (() => InitialValue),\n debug?: boolean,\n): InitialValue | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue?: InitialValue | (() => InitialValue),\n debug?: boolean,\n): InitialValue | ObservedValueOf<ObservableType> {\n const store = useMemo(() => {\n if (!cache.has(observable)) {\n const entry: Partial<CacheRecord<ObservedValueOf<ObservableType>>> = {}\n entry.observable = observable.pipe(\n map((value) => ({snapshot: value, error: undefined})),\n catchError((error) => of({snapshot: undefined, error})),\n tap(({snapshot, error}) => {\n if (debug) {\n console.log('tap', snapshot, error)\n }\n entry.snapshot = snapshot\n entry.error = error\n }),\n // Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state\n // and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.\n map((value) => void value),\n // Ensure that the cache entry is deleted when the observable completes or errors.\n finalize(() => cache.delete(observable)),\n share({resetOnRefCountZero: () => timer(0, asapScheduler)}),\n )\n\n // Eagerly subscribe to sync set `entry.currentValue` to what the observable returns, and keep the observable alive until the component unmounts.\n const subscription = entry.observable.subscribe()\n subscription.unsubscribe()\n\n cache.set(observable, entry as CacheRecord<ObservedValueOf<ObservableType>>)\n }\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const instance = cache.get(observable)!\n\n return {\n subscribe: (onStoreChange: () => void) => {\n if (debug) {\n console.log('subscribe', observable)\n }\n const subscription = instance.observable.subscribe(() => {\n if (debug) {\n console.log('onStoreChange', observable)\n }\n onStoreChange()\n })\n return () => {\n if (debug) {\n console.log('unsubscribe', observable)\n }\n subscription.unsubscribe()\n }\n },\n getSnapshot: () => {\n if (debug) {\n console.log('getSnapshot', instance.snapshot, instance.error)\n }\n if (instance.error) {\n throw instance.error\n }\n return instance.snapshot\n },\n }\n }, [debug, observable])\n\n return useSyncExternalStore<ObservedValueOf<ObservableType>>(\n store.subscribe,\n () => store.getSnapshot() ?? (getValue(initialValue) as ObservedValueOf<ObservableType>),\n typeof initialValue === 'undefined'\n ? undefined\n : () => getValue(initialValue) as ObservedValueOf<ObservableType>,\n )\n}\n","import {observableCallback} from 'observable-callback'\nimport {useEffect, useState} from 'react'\nimport {type Observable} from 'rxjs'\nimport {useEffectEvent} from 'use-effect-event'\n\n/** @public */\nexport function useObservableEvent<T, U>(\n handleEvent: (arg: Observable<T>) => Observable<U>,\n): (arg: T) => void {\n const [[calls$, call]] = useState(() => observableCallback<T>())\n\n const onEvent = useEffectEvent((observable: Observable<T>) => handleEvent(observable))\n\n useEffect(() => {\n const subscription = calls$.pipe((observable) => onEvent(observable)).subscribe()\n return () => subscription.unsubscribe()\n }, [calls$, onEvent])\n\n return call\n}\n"],"names":["useMemo","map","catchError","of","tap","finalize","share","timer","asapScheduler","useSyncExternalStore","useState","observableCallback","useEffectEvent","useEffect"],"mappings":";;;AAaA,SAAS,SAAY,OAA2C;AAC9D,SAAO,OAAO,SAAU,aAAa,MAAU,IAAA;AACjD;AAQA,MAAM,4BAAY,QAA2C;AAmB7C,SAAA,cACd,YACA,cACA,OACgD;AAC1C,QAAA,QAAQA,MAAAA,QAAQ,MAAM;AAC1B,QAAI,CAAC,MAAM,IAAI,UAAU,GAAG;AAC1B,YAAM,QAA+D,CAAC;AACtE,YAAM,aAAa,WAAW;AAAA,QAC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,YAAY;AAAA,EACjC;AACF;AC5GO,SAAS,mBACd,aACkB;AAClB,QAAM,CAAC,CAAC,QAAQ,IAAI,CAAC,IAAIC,MAAS,SAAA,MAAMC,mBAAAA,mBAAuB,CAAA,GAEzD,UAAUC,eAAAA,eAAe,CAAC,eAA8B,YAAY,UAAU,CAAC;AAErF,SAAAC,MAAA,UAAU,MAAM;AACR,UAAA,eAAe,OAAO,KAAK,CAAC,eAAe,QAAQ,UAAU,CAAC,EAAE,UAAU;AACzE,WAAA,MAAM,aAAa,YAAY;AAAA,EACrC,GAAA,CAAC,QAAQ,OAAO,CAAC,GAEb;AACT;;;"}
package/dist/index.js CHANGED
@@ -31,7 +31,7 @@ function useObservable(observable, initialValue, debug) {
31
31
  subscribe: (onStoreChange) => {
32
32
  debug && console.log("subscribe", observable);
33
33
  const subscription = instance.observable.subscribe(() => {
34
- console.log("onStoreChange", observable), onStoreChange();
34
+ debug && console.log("onStoreChange", observable), onStoreChange();
35
35
  });
36
36
  return () => {
37
37
  debug && console.log("unsubscribe", observable), subscription.unsubscribe();
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/useObservable.ts","../src/useObservableEvent.ts"],"sourcesContent":["import {useMemo, useSyncExternalStore} from 'react'\nimport {\n asapScheduler,\n catchError,\n finalize,\n type Observable,\n type ObservedValueOf,\n of,\n share,\n timer,\n} from 'rxjs'\nimport {map, tap} from 'rxjs/operators'\n\nfunction getValue<T>(value: T): T extends () => infer U ? U : T {\n return typeof value === 'function' ? value() : value\n}\n\ninterface CacheRecord<T> {\n observable: Observable<void>\n snapshot: T\n error?: unknown\n}\n\nconst cache = new WeakMap<Observable<any>, CacheRecord<any>>()\n\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n initialValue: ObservedValueOf<ObservableType> | (() => ObservedValueOf<ObservableType>),\n debug?: boolean,\n): ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n): undefined | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue: InitialValue | (() => InitialValue),\n debug?: boolean,\n): InitialValue | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue?: InitialValue | (() => InitialValue),\n debug?: boolean,\n): InitialValue | ObservedValueOf<ObservableType> {\n const store = useMemo(() => {\n if (!cache.has(observable)) {\n const entry: Partial<CacheRecord<ObservedValueOf<ObservableType>>> = {}\n entry.observable = observable.pipe(\n map((value) => ({snapshot: value, error: undefined})),\n catchError((error) => of({snapshot: undefined, error})),\n tap(({snapshot, error}) => {\n if (debug) {\n console.log('tap', snapshot, error)\n }\n entry.snapshot = snapshot\n entry.error = error\n }),\n // Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state\n // and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.\n map((value) => void value),\n // Ensure that the cache entry is deleted when the observable completes or errors.\n finalize(() => cache.delete(observable)),\n share({resetOnRefCountZero: () => timer(0, asapScheduler)}),\n )\n\n // Eagerly subscribe to sync set `entry.currentValue` to what the observable returns, and keep the observable alive until the component unmounts.\n const subscription = entry.observable.subscribe()\n subscription.unsubscribe()\n\n cache.set(observable, entry as CacheRecord<ObservedValueOf<ObservableType>>)\n }\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const instance = cache.get(observable)!\n\n return {\n subscribe: (onStoreChange: () => void) => {\n if (debug) {\n console.log('subscribe', observable)\n }\n const subscription = instance.observable.subscribe(() => {\n console.log('onStoreChange', observable)\n onStoreChange()\n })\n return () => {\n if (debug) {\n console.log('unsubscribe', observable)\n }\n subscription.unsubscribe()\n }\n },\n getSnapshot: () => {\n if (debug) {\n console.log('getSnapshot', instance.snapshot, instance.error)\n }\n if (instance.error) {\n throw instance.error\n }\n return instance.snapshot\n },\n }\n }, [debug, observable])\n\n return useSyncExternalStore<ObservedValueOf<ObservableType>>(\n store.subscribe,\n () => store.getSnapshot() ?? (getValue(initialValue) as ObservedValueOf<ObservableType>),\n typeof initialValue === 'undefined'\n ? undefined\n : () => getValue(initialValue) as ObservedValueOf<ObservableType>,\n )\n}\n","import {observableCallback} from 'observable-callback'\nimport {useEffect, useState} from 'react'\nimport {type Observable} from 'rxjs'\nimport {useEffectEvent} from 'use-effect-event'\n\n/** @public */\nexport function useObservableEvent<T, U>(\n handleEvent: (arg: Observable<T>) => Observable<U>,\n): (arg: T) => void {\n const [[calls$, call]] = useState(() => observableCallback<T>())\n\n const onEvent = useEffectEvent((observable: Observable<T>) => handleEvent(observable))\n\n useEffect(() => {\n const subscription = calls$.pipe((observable) => onEvent(observable)).subscribe()\n return () => subscription.unsubscribe()\n }, [calls$, onEvent])\n\n return call\n}\n"],"names":[],"mappings":";;;;;AAaA,SAAS,SAAY,OAA2C;AAC9D,SAAO,OAAO,SAAU,aAAa,MAAU,IAAA;AACjD;AAQA,MAAM,4BAAY,QAA2C;AAmB7C,SAAA,cACd,YACA,cACA,OACgD;AAC1C,QAAA,QAAQ,QAAQ,MAAM;AAC1B,QAAI,CAAC,MAAM,IAAI,UAAU,GAAG;AAC1B,YAAM,QAA+D,CAAC;AACtE,YAAM,aAAa,WAAW;AAAA,QAC5B,IAAI,CAAC,WAAW,EAAC,UAAU,OAAO,OAAO,SAAW;AAAA,QACpD,WAAW,CAAC,UAAU,GAAG,EAAC,UAAU,QAAW,MAAK,CAAC,CAAC;AAAA,QACtD,IAAI,CAAC,EAAC,UAAU,YAAW;AACrB,mBACF,QAAQ,IAAI,OAAO,UAAU,KAAK,GAEpC,MAAM,WAAW,UACjB,MAAM,QAAQ;AAAA,QAAA,CACf;AAAA;AAAA;AAAA,QAGD,IAAI,CAAC,UAAO;AAAA,QAAA,CAAa;AAAA;AAAA,QAEzB,SAAS,MAAM,MAAM,OAAO,UAAU,CAAC;AAAA,QACvC,MAAM,EAAC,qBAAqB,MAAM,MAAM,GAAG,aAAa,EAAE,CAAA;AAAA,MAAA,GAIvC,MAAM,WAAW,YACzB,YAEb,GAAA,MAAM,IAAI,YAAY,KAAqD;AAAA,IAAA;AAGvE,UAAA,WAAW,MAAM,IAAI,UAAU;AAE9B,WAAA;AAAA,MACL,WAAW,CAAC,kBAA8B;AACpC,iBACF,QAAQ,IAAI,aAAa,UAAU;AAErC,cAAM,eAAe,SAAS,WAAW,UAAU,MAAM;AACvD,kBAAQ,IAAI,iBAAiB,UAAU,GACvC,cAAc;AAAA,QAAA,CACf;AACD,eAAO,MAAM;AACP,mBACF,QAAQ,IAAI,eAAe,UAAU,GAEvC,aAAa,YAAY;AAAA,QAC3B;AAAA,MACF;AAAA,MACA,aAAa,MAAM;AACb,YAAA,SACF,QAAQ,IAAI,eAAe,SAAS,UAAU,SAAS,KAAK,GAE1D,SAAS;AACX,gBAAM,SAAS;AAEjB,eAAO,SAAS;AAAA,MAAA;AAAA,IAEpB;AAAA,EAAA,GACC,CAAC,OAAO,UAAU,CAAC;AAEf,SAAA;AAAA,IACL,MAAM;AAAA,IACN,MAAM,MAAM,iBAAkB,SAAS,YAAY;AAAA,IACnD,OAAO,eAAiB,MACpB,SACA,MAAM,SAAS,YAAY;AAAA,EACjC;AACF;AC1GO,SAAS,mBACd,aACkB;AAClB,QAAM,CAAC,CAAC,QAAQ,IAAI,CAAC,IAAI,SAAS,MAAM,mBAAuB,CAAA,GAEzD,UAAU,eAAe,CAAC,eAA8B,YAAY,UAAU,CAAC;AAErF,SAAA,UAAU,MAAM;AACR,UAAA,eAAe,OAAO,KAAK,CAAC,eAAe,QAAQ,UAAU,CAAC,EAAE,UAAU;AACzE,WAAA,MAAM,aAAa,YAAY;AAAA,EACrC,GAAA,CAAC,QAAQ,OAAO,CAAC,GAEb;AACT;"}
1
+ {"version":3,"file":"index.js","sources":["../src/useObservable.ts","../src/useObservableEvent.ts"],"sourcesContent":["import {useMemo, useSyncExternalStore} from 'react'\nimport {\n asapScheduler,\n catchError,\n finalize,\n type Observable,\n type ObservedValueOf,\n of,\n share,\n timer,\n} from 'rxjs'\nimport {map, tap} from 'rxjs/operators'\n\nfunction getValue<T>(value: T): T extends () => infer U ? U : T {\n return typeof value === 'function' ? value() : value\n}\n\ninterface CacheRecord<T> {\n observable: Observable<void>\n snapshot: T\n error?: unknown\n}\n\nconst cache = new WeakMap<Observable<any>, CacheRecord<any>>()\n\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n initialValue: ObservedValueOf<ObservableType> | (() => ObservedValueOf<ObservableType>),\n debug?: boolean,\n): ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n): undefined | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue: InitialValue | (() => InitialValue),\n debug?: boolean,\n): InitialValue | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue?: InitialValue | (() => InitialValue),\n debug?: boolean,\n): InitialValue | ObservedValueOf<ObservableType> {\n const store = useMemo(() => {\n if (!cache.has(observable)) {\n const entry: Partial<CacheRecord<ObservedValueOf<ObservableType>>> = {}\n entry.observable = observable.pipe(\n map((value) => ({snapshot: value, error: undefined})),\n catchError((error) => of({snapshot: undefined, error})),\n tap(({snapshot, error}) => {\n if (debug) {\n console.log('tap', snapshot, error)\n }\n entry.snapshot = snapshot\n entry.error = error\n }),\n // Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state\n // and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.\n map((value) => void value),\n // Ensure that the cache entry is deleted when the observable completes or errors.\n finalize(() => cache.delete(observable)),\n share({resetOnRefCountZero: () => timer(0, asapScheduler)}),\n )\n\n // Eagerly subscribe to sync set `entry.currentValue` to what the observable returns, and keep the observable alive until the component unmounts.\n const subscription = entry.observable.subscribe()\n subscription.unsubscribe()\n\n cache.set(observable, entry as CacheRecord<ObservedValueOf<ObservableType>>)\n }\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const instance = cache.get(observable)!\n\n return {\n subscribe: (onStoreChange: () => void) => {\n if (debug) {\n console.log('subscribe', observable)\n }\n const subscription = instance.observable.subscribe(() => {\n if (debug) {\n console.log('onStoreChange', observable)\n }\n onStoreChange()\n })\n return () => {\n if (debug) {\n console.log('unsubscribe', observable)\n }\n subscription.unsubscribe()\n }\n },\n getSnapshot: () => {\n if (debug) {\n console.log('getSnapshot', instance.snapshot, instance.error)\n }\n if (instance.error) {\n throw instance.error\n }\n return instance.snapshot\n },\n }\n }, [debug, observable])\n\n return useSyncExternalStore<ObservedValueOf<ObservableType>>(\n store.subscribe,\n () => store.getSnapshot() ?? (getValue(initialValue) as ObservedValueOf<ObservableType>),\n typeof initialValue === 'undefined'\n ? undefined\n : () => getValue(initialValue) as ObservedValueOf<ObservableType>,\n )\n}\n","import {observableCallback} from 'observable-callback'\nimport {useEffect, useState} from 'react'\nimport {type Observable} from 'rxjs'\nimport {useEffectEvent} from 'use-effect-event'\n\n/** @public */\nexport function useObservableEvent<T, U>(\n handleEvent: (arg: Observable<T>) => Observable<U>,\n): (arg: T) => void {\n const [[calls$, call]] = useState(() => observableCallback<T>())\n\n const onEvent = useEffectEvent((observable: Observable<T>) => handleEvent(observable))\n\n useEffect(() => {\n const subscription = calls$.pipe((observable) => onEvent(observable)).subscribe()\n return () => subscription.unsubscribe()\n }, [calls$, onEvent])\n\n return call\n}\n"],"names":[],"mappings":";;;;;AAaA,SAAS,SAAY,OAA2C;AAC9D,SAAO,OAAO,SAAU,aAAa,MAAU,IAAA;AACjD;AAQA,MAAM,4BAAY,QAA2C;AAmB7C,SAAA,cACd,YACA,cACA,OACgD;AAC1C,QAAA,QAAQ,QAAQ,MAAM;AAC1B,QAAI,CAAC,MAAM,IAAI,UAAU,GAAG;AAC1B,YAAM,QAA+D,C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EACjC;AACF;AC5GO,SAAS,mBACd,aACkB;AAClB,QAAM,CAAC,CAAC,QAAQ,IAAI,CAAC,IAAI,SAAS,MAAM,mBAAuB,CAAA,GAEzD,UAAU,eAAe,CAAC,eAA8B,YAAY,UAAU,CAAC;AAErF,SAAA,UAAU,MAAM;AACR,UAAA,eAAe,OAAO,KAAK,CAAC,eAAe,QAAQ,UAAU,CAAC,EAAE,UAAU;AACzE,WAAA,MAAM,aAAa,YAAY;AAAA,EACrC,GAAA,CAAC,QAAQ,OAAO,CAAC,GAEb;AACT;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-rx",
3
- "version": "4.1.2-canary.7",
3
+ "version": "4.1.2-canary.8",
4
4
  "description": "React + RxJS = <3",
5
5
  "keywords": [
6
6
  "action",
@@ -81,7 +81,9 @@ export function useObservable<ObservableType extends Observable<any>, InitialVal
81
81
  console.log('subscribe', observable)
82
82
  }
83
83
  const subscription = instance.observable.subscribe(() => {
84
- console.log('onStoreChange', observable)
84
+ if (debug) {
85
+ console.log('onStoreChange', observable)
86
+ }
85
87
  onStoreChange()
86
88
  })
87
89
  return () => {