@livequery/react 2.0.93 → 2.0.94

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.js CHANGED
@@ -1,95 +1,4 @@
1
- // src/createContextFromHook.tsx
2
- import { createContext, useContext } from "react";
3
- import { jsxDEV } from "react/jsx-dev-runtime";
4
- var createContextFromHook = (fn) => {
5
- const ctx = createContext(undefined);
6
- const useState = () => {
7
- return useContext(ctx);
8
- };
9
- const Provider = ({ children, ...props }) => {
10
- const value = fn(props);
11
- return /* @__PURE__ */ jsxDEV(ctx.Provider, {
12
- value,
13
- children
14
- }, undefined, false, undefined, this);
15
- };
16
- return [useState, Provider];
17
- };
18
- // src/useCollection.ts
19
- import { LivequeryCollection } from "@livequery/core";
20
- import { useMemo, useEffect } from "react";
21
- var livequeryCoreContext = createContextFromHook((props) => props.core);
22
- var useLivequeryCore = livequeryCoreContext[0];
23
- var LivequeryCoreProvider = livequeryCoreContext[1];
24
- var useCollection = (ref, options = {}) => {
25
- const core = useLivequeryCore();
26
- const collection = useMemo(() => new LivequeryCollection(options), []);
27
- useEffect(() => {
28
- if (!ref || !core)
29
- return;
30
- const linker = collection.initialize(core, ref);
31
- return () => {
32
- linker?.unsubscribe();
33
- };
34
- }, [collection, core, ref]);
35
- return collection;
36
- };
37
- // src/useDocument.ts
38
- import { LivequeryCollection as LivequeryCollection2 } from "@livequery/core";
39
- import { useMemo as useMemo2, useEffect as useEffect3 } from "react";
1
+ import{createContext as Y,useContext as B}from"react";import{jsx as H}from"react/jsx-runtime";var U=(D)=>{let X=Y(void 0);return[()=>{return B(X)},({children:Z,...$})=>{let A=D($);return H(X.Provider,{value:A,children:Z})}]};import{LivequeryCollection as F}from"@livequery/core";import{useMemo as M,useEffect as R}from"react";var V=U((D)=>D.core),J=V[0],q=V[1],m=(D,X={})=>{let N=J(),Q=M(()=>new F(X),[]);return R(()=>{if(!D||!N)return;let Z=Q.initialize(N,D);return()=>{Z?.unsubscribe()}},[Q,N,D]),Q};import{LivequeryCollection as K}from"@livequery/core";import{useMemo as S,useEffect as T}from"react";import{useEffect as _,useState as z}from"react";import{tap as E,EMPTY as O}from"rxjs";var P=(D,X)=>{let[N,Q]=z(typeof D?.getValue==="function"?D.getValue():X);return _(()=>{try{let $=((typeof D==="function"?D():D)||O).pipe(E((A)=>{Q(A)})).subscribe();return()=>{$.unsubscribe()}}catch(Z){}},typeof D==="function"?[]:[D]),N};var c=(D,X={})=>{let N=J(),Q=S(()=>new K(X),[]);T(()=>{if(!D||!N)return;let A=Q.initialize(N,D);return()=>{A?.unsubscribe()}},[Q,N,D]);let Z=P(Q.items),$=P(Q.loading);return[Z[0],$]};var I=Symbol.for("#livequery/react/global"),a=(D,X)=>{let N=globalThis;return N[I]=N[I]||{},N[I][D]=N[I][D]||X(),N[I][D]};export{P as useObservable,J as useLivequeryCore,a as useGlobalValue,c as useDocument,m as useCollection,U as createContextFromHook,q as LivequeryCoreProvider};
40
2
 
41
- // src/useObservable.ts
42
- import { useEffect as useEffect2, useState } from "react";
43
- import { tap, EMPTY } from "rxjs";
44
- var useObservable = (o, default_value) => {
45
- const [v, s] = useState(typeof o?.["getValue"] === "function" ? o.getValue() : default_value);
46
- useEffect2(() => {
47
- try {
48
- const target = (typeof o === "function" ? o() : o) || EMPTY;
49
- const subscription = target.pipe(tap((value) => {
50
- s(value);
51
- })).subscribe();
52
- return () => {
53
- subscription.unsubscribe();
54
- };
55
- } catch (e) {}
56
- }, typeof o === "function" ? [] : [o]);
57
- return v;
58
- };
59
-
60
- // src/useDocument.ts
61
- var useDocument = (ref, options = {}) => {
62
- const core = useLivequeryCore();
63
- const collection = useMemo2(() => new LivequeryCollection2(options), []);
64
- useEffect3(() => {
65
- if (!ref || !core)
66
- return;
67
- const linker = collection.initialize(core, ref);
68
- return () => {
69
- linker?.unsubscribe();
70
- };
71
- }, [collection, core, ref]);
72
- const items = useObservable(collection.items);
73
- const loading = useObservable(collection.loading);
74
- return [items[0], loading];
75
- };
76
- // src/useGlobalValue.ts
77
- var WindowGlobalKey = Symbol.for("#livequery/react/global");
78
- var useGlobalValue = (key, resolver) => {
79
- const G = globalThis;
80
- G[WindowGlobalKey] = G[WindowGlobalKey] || {};
81
- G[WindowGlobalKey][key] = G[WindowGlobalKey][key] || resolver();
82
- return G[WindowGlobalKey][key];
83
- };
84
- export {
85
- useObservable,
86
- useLivequeryCore,
87
- useGlobalValue,
88
- useDocument,
89
- useCollection,
90
- createContextFromHook,
91
- LivequeryCoreProvider
92
- };
93
-
94
- //# debugId=05EC7BC94672323A64756E2164756E21
3
+ //# debugId=691A0C6DD6BB1CED64756E2164756E21
95
4
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -4,11 +4,11 @@
4
4
  "sourcesContent": [
5
5
  "import { createContext, useContext, type PropsWithChildren } from \"react\"\n\n/**\n * Creates a React context pair from a hook-like factory.\n *\n * The returned tuple contains:\n * - a consumer hook that reads the computed value from context\n * - a provider component that calls `fn(props)` and places the result into context\n *\n * This pattern is useful when some shared state should feel like a normal hook at call\n * sites, while still being configured once at the edge of a subtree.\n *\n * @param fn Factory that receives provider props and returns the context value.\n * @returns A tuple of `[useValue, Provider]`.\n */\nexport const createContextFromHook = <T, R>(fn: ((props: T) => R) | (() => R)) => {\n const ctx = createContext<R | undefined>(undefined)\n\n /**\n * Reads the current context value produced by the paired provider.\n */\n const useState = () => {\n return useContext(ctx) as R\n }\n\n /**\n * Computes the context value from provider props and exposes it to descendants.\n */\n const Provider = ({ children, ...props }: PropsWithChildren<T>) => {\n const value = fn(props as T)\n return (\n <ctx.Provider value={value}>\n {children}\n </ctx.Provider>\n )\n }\n return [useState, Provider,] as [() => R, React.FC<PropsWithChildren<T>>]\n}",
6
6
  "import { LivequeryCollection, LivequeryCore, type Doc, type LivequeryCollectionOptions } from \"@livequery/core\"\nimport { useMemo, useEffect } from \"react\"\nimport { createContextFromHook } from \"./createContextFromHook\"\n\nconst livequeryCoreContext = createContextFromHook(\n (props: { core: LivequeryCore }) => props.core\n)\n\n/**\n * Returns the `LivequeryCore` instance from the nearest provider.\n */\nexport const useLivequeryCore = livequeryCoreContext[0]\n\n/**\n * Provides a `LivequeryCore` instance to all descendants that use this package's hooks.\n */\nexport const LivequeryCoreProvider = livequeryCoreContext[1]\n\n/**\n * Creates and initializes a `LivequeryCollection` for the supplied collection path.\n *\n * The collection instance is stable for the lifetime of the component. Initialization is\n * skipped when `ref` is falsy.\n *\n * @param ref Collection path understood by `@livequery/core`.\n * @param options Collection options forwarded to `LivequeryCollection`.\n * @returns A stable `LivequeryCollection` instance.\n */\nexport const useCollection = <T extends Doc>(ref: string | undefined | '' | null | false, options: Partial<LivequeryCollectionOptions<T>> = {}) => {\n const core = useLivequeryCore()\n const collection = useMemo(() => new LivequeryCollection<T>(options), [])\n useEffect(() => {\n if (!ref || !core) return\n const linker = collection.initialize(core, ref)\n return () => {\n linker?.unsubscribe()\n }\n }, [collection, core, ref])\n\n return collection\n}",
7
- "import { LivequeryCollection, type Doc } from \"@livequery/core\"\nimport { useMemo, useEffect } from \"react\"\nimport { useLivequeryCore } from \"./useCollection\"\nimport { useObservable } from \"./useObservable\"\n\n/**\n * Subscribes to a single document path and returns the first emitted item with loading state.\n *\n * Internally this hook creates a `LivequeryCollection`, initializes it with the document ref,\n * then reads the first item from the collection stream.\n *\n * @param ref Document path understood by `@livequery/core`.\n * @param options Collection options used when creating the backing collection.\n * @returns A tuple of `[document, loading]`.\n */\nexport const useDocument = <T extends Doc>(ref: string | undefined | '' | null | false, options: { lazy?: boolean } = {}) => {\n const core = useLivequeryCore()\n\n const collection = useMemo(() => new LivequeryCollection<T>(options), [])\n useEffect(() => {\n if (!ref || !core) return\n const linker = collection.initialize(core, ref)\n return () => {\n linker?.unsubscribe()\n }\n }, [collection, core, ref])\n\n const items = useObservable(collection.items)\n const loading = useObservable(collection.loading)\n return [items[0], loading] as const\n // const doc: LivequeryDocument<T> | null = items[0] || of(null)\n // return Object.assign(doc, {\n // loading,\n // del: () => id && collection.delete(id)\n // })\n\n}",
7
+ "import { LivequeryCollection, type Doc } from \"@livequery/core\"\nimport { useMemo, useEffect } from \"react\"\nimport { useLivequeryCore } from \"./useCollection.js\"\nimport { useObservable } from \"./useObservable.js\"\n\n/**\n * Subscribes to a single document path and returns the first emitted item with loading state.\n *\n * Internally this hook creates a `LivequeryCollection`, initializes it with the document ref,\n * then reads the first item from the collection stream.\n *\n * @param ref Document path understood by `@livequery/core`.\n * @param options Collection options used when creating the backing collection.\n * @returns A tuple of `[document, loading]`.\n */\nexport const useDocument = <T extends Doc>(ref: string | undefined | '' | null | false, options: { lazy?: boolean } = {}) => {\n const core = useLivequeryCore()\n\n const collection = useMemo(() => new LivequeryCollection<T>(options), [])\n useEffect(() => {\n if (!ref || !core) return\n const linker = collection.initialize(core, ref)\n return () => {\n linker?.unsubscribe()\n }\n }, [collection, core, ref])\n\n const items = useObservable(collection.items)\n const loading = useObservable(collection.loading)\n return [items[0], loading] as const\n // const doc: LivequeryDocument<T> | null = items[0] || of(null)\n // return Object.assign(doc, {\n // loading,\n // del: () => id && collection.delete(id)\n // })\n\n}",
8
8
  "import { useEffect, useState } from \"react\";\nimport { Observable, BehaviorSubject, tap, EMPTY } from \"rxjs\";\n\n/**\n * Accepts either a value or a lazy factory returning that value.\n */\nexport type MaybeFunction<T> = T | (() => T)\n\n/**\n * Subscribes to an RxJS observable and mirrors its latest value into React state.\n *\n * When a `BehaviorSubject` is provided, the hook uses `getValue()` for the initial render.\n * When a factory is provided, the observable is resolved lazily inside the effect.\n *\n * @param o Observable source or factory returning the source.\n * @param default_value Fallback value used before the first emission when the source is not a `BehaviorSubject`.\n * @returns The latest value emitted by the source.\n */\nexport const useObservable = <T extends any>(o: MaybeFunction<BehaviorSubject<T> | Observable<T>>, default_value?: T) => {\n const [v, s] = useState<T>(typeof (o as any)?.['getValue'] === 'function' ? (o as any).getValue() : (default_value as T))\n useEffect(() => {\n try {\n const target = (typeof o === 'function' ? o() : o) || EMPTY\n const subscription = target.pipe(\n tap(value => {\n s(value)\n })\n ).subscribe()\n return () => {\n subscription.unsubscribe()\n }\n } catch (e) { }\n }, typeof o === 'function' ? [] : [o])\n return v\n}",
9
9
  "import { useMemo } from \"react\"\n\nconst WindowGlobalKey = Symbol.for('#livequery/react/global')\n\n/**\n * Resolves a process-wide singleton value stored on `globalThis`.\n *\n * The resolver runs only the first time a given key is requested in the current runtime.\n * Later calls with the same key return the cached value.\n *\n * @param key Unique key within the package global registry.\n * @param resolver Lazy factory used when the value does not exist yet.\n * @returns The cached or newly created value.\n */\nexport const useGlobalValue = <T>(key: string, resolver: () => T) => {\n const G = globalThis as any\n G[WindowGlobalKey] = G[WindowGlobalKey] || {}\n G[WindowGlobalKey]![key] = G[WindowGlobalKey]![key] || resolver()\n return G[WindowGlobalKey]![key] as T\n}"
10
10
  ],
11
- "mappings": ";AAAA;AAAA;AAeO,IAAM,wBAAwB,CAAO,OAAsC;AAAA,EAC9E,MAAM,MAAM,cAA6B,SAAS;AAAA,EAKlD,MAAM,WAAW,MAAM;AAAA,IACnB,OAAO,WAAW,GAAG;AAAA;AAAA,EAMzB,MAAM,WAAW,GAAG,aAAa,YAAkC;AAAA,IAC/D,MAAM,QAAQ,GAAG,KAAU;AAAA,IAC3B,uBACI,OAEE,IAAI,UAFN;AAAA,MAAc;AAAA,MAAd;AAAA,wCAEE;AAAA;AAAA,EAGV,OAAO,CAAC,UAAU,QAAS;AAAA;;ACpC/B;AACA;AAGA,IAAM,uBAAuB,sBACzB,CAAC,UAAmC,MAAM,IAC9C;AAKO,IAAM,mBAAmB,qBAAqB;AAK9C,IAAM,wBAAwB,qBAAqB;AAYnD,IAAM,gBAAgB,CAAgB,KAA6C,UAAkD,CAAC,MAAM;AAAA,EAC/I,MAAM,OAAO,iBAAiB;AAAA,EAC9B,MAAM,aAAa,QAAQ,MAAM,IAAI,oBAAuB,OAAO,GAAG,CAAC,CAAC;AAAA,EACxE,UAAU,MAAM;AAAA,IACZ,IAAI,C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U,SAAS,EAAK,CAAmB,EAcxH,OAbA,EAAU,IAAM,CACZ,GAAI,CAEA,IAAM,IADU,OAAO,IAAM,WAAa,EAAE,EAAI,IAAM,GAC1B,KACxB,EAAI,KAAS,CACT,EAAE,CAAK,EACV,CACL,EAAE,UAAU,EACZ,MAAO,IAAM,CACT,EAAa,YAAY,GAE/B,MAAO,EAAG,IACb,OAAO,IAAM,WAAa,CAAC,EAAI,CAAC,CAAC,CAAC,EAC9B,GDlBJ,IAAM,EAAc,CAAgB,EAA6C,EAA8B,CAAC,IAAM,CACzH,IAAM,EAAO,EAAiB,EAExB,EAAa,EAAQ,IAAM,IAAI,EAAuB,CAAO,EAAG,CAAC,CAAC,EACxE,EAAU,IAAM,CACZ,GAAI,CAAC,GAAO,CAAC,EAAM,OACnB,IAAM,EAAS,EAAW,WAAW,EAAM,CAAG,EAC9C,MAAO,IAAM,CACT,GAAQ,YAAY,IAEzB,CAAC,EAAY,EAAM,CAAG,CAAC,EAE1B,IAAM,EAAQ,EAAc,EAAW,KAAK,EACtC,EAAU,EAAc,EAAW,OAAO,EAChD,MAAO,CAAC,EAAM,GAAI,CAAO,GE3B7B,IAAM,EAAkB,OAAO,IAAI,yBAAyB,EAY/C,EAAiB,CAAI,EAAa,IAAsB,CACjE,IAAM,EAAI,WAGV,OAFA,EAAE,GAAmB,EAAE,IAAoB,CAAC,EAC5C,EAAE,GAAkB,GAAO,EAAE,GAAkB,IAAQ,EAAS,EACzD,EAAE,GAAkB",
12
+ "debugId": "691A0C6DD6BB1CED64756E2164756E21",
13
13
  "names": []
14
14
  }
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "url": "https://github.com/livequery/react"
5
5
  },
6
6
  "type": "module",
7
- "version": "2.0.93",
7
+ "version": "2.0.94",
8
8
  "description": "",
9
9
  "main": "./dist/index.js",
10
10
  "types": "./dist/index.d.ts",
@@ -30,11 +30,10 @@
30
30
  },
31
31
  "scripts": {
32
32
  "clean": "rm -rf dist",
33
- "build:js": "bun build ./src/index.ts --outdir ./dist --target browser --format esm --sourcemap --packages=external",
33
+ "build:js": "bun build ./src/index.ts --outdir ./dist --target browser --format esm --sourcemap --packages=external --production",
34
34
  "build:types": "bunx tsc -p tsconfig.build.json",
35
35
  "build": "bun run clean && bun run build:js && bun run build:types",
36
- "build:watch": "bun build ./src/index.ts --outdir ./dist --target browser --format esm --watch --packages=external",
37
- "prepublishOnly": "bun run build"
36
+ "build:watch": "bun build ./src/index.ts --outdir ./dist --target browser --format esm --sourcemap --packages=external --watch"
38
37
  },
39
38
  "author": "",
40
39
  "license": "ISC"