@dxos/echo-react 0.8.4-main.d05539e30a → 0.8.4-main.d9fc60f731
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/lib/neutral/index.mjs +17 -21
- package/dist/lib/neutral/index.mjs.map +4 -4
- package/dist/lib/neutral/meta.json +1 -1
- package/dist/types/src/index.d.ts +1 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/useType.d.ts +10 -0
- package/dist/types/src/useType.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +5 -5
- package/src/index.ts +1 -1
- package/src/useType.ts +49 -0
- package/dist/types/src/useSchema.d.ts +0 -6
- package/dist/types/src/useSchema.d.ts.map +0 -1
- package/src/useSchema.ts +0 -42
|
@@ -31,41 +31,37 @@ var useQuery = (resource, queryOrFilter) => {
|
|
|
31
31
|
return objects;
|
|
32
32
|
};
|
|
33
33
|
|
|
34
|
-
// src/
|
|
34
|
+
// src/useType.ts
|
|
35
35
|
import { useMemo as useMemo2, useSyncExternalStore as useSyncExternalStore2 } from "react";
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
import { Filter as Filter2, Query as Query2, Scope, Type } from "@dxos/echo";
|
|
37
|
+
var useType = (db, typename) => {
|
|
38
|
+
const { subscribe, getType } = useMemo2(() => {
|
|
38
39
|
if (!typename || !db) {
|
|
39
40
|
return {
|
|
40
41
|
subscribe: () => () => {
|
|
41
42
|
},
|
|
42
|
-
|
|
43
|
+
getType: () => void 0
|
|
43
44
|
};
|
|
44
45
|
}
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
"database",
|
|
49
|
-
"runtime"
|
|
50
|
-
]
|
|
51
|
-
});
|
|
52
|
-
const initialResult = query.runSync()[0];
|
|
53
|
-
let currentSchema = initialResult;
|
|
46
|
+
const queryResult = db.query(Query2.select(Filter2.type(Type.Type)).from(Scope.space(), Scope.registry()));
|
|
47
|
+
let subscribed = false;
|
|
48
|
+
const find = () => subscribed ? queryResult.results.find((type) => Type.getTypename(type) === typename) : void 0;
|
|
54
49
|
return {
|
|
55
50
|
subscribe: (onStoreChange) => {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
51
|
+
subscribed = true;
|
|
52
|
+
const unsubscribe = queryResult.subscribe(onStoreChange);
|
|
53
|
+
return () => {
|
|
54
|
+
unsubscribe();
|
|
55
|
+
subscribed = false;
|
|
56
|
+
};
|
|
61
57
|
},
|
|
62
|
-
|
|
58
|
+
getType: find
|
|
63
59
|
};
|
|
64
60
|
}, [
|
|
65
61
|
typename,
|
|
66
62
|
db
|
|
67
63
|
]);
|
|
68
|
-
return useSyncExternalStore2(subscribe,
|
|
64
|
+
return useSyncExternalStore2(subscribe, getType);
|
|
69
65
|
};
|
|
70
66
|
|
|
71
67
|
// src/useObject.ts
|
|
@@ -149,6 +145,6 @@ export {
|
|
|
149
145
|
useObject,
|
|
150
146
|
useObjects,
|
|
151
147
|
useQuery,
|
|
152
|
-
|
|
148
|
+
useType
|
|
153
149
|
};
|
|
154
150
|
//# sourceMappingURL=index.mjs.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../../src/useQuery.ts", "../../../src/
|
|
4
|
-
"sourcesContent": ["//\n// Copyright 2022 DXOS.org\n//\n\nimport { useMemo, useSyncExternalStore } from 'react';\n\nimport { type Database, type Entity, Filter, Query } from '@dxos/echo';\n\nconst EMPTY_ARRAY: never[] = [];\n\nconst noop = () => {};\n\ninterface UseQueryFn {\n <Q extends Query.Any, O extends Entity.Entity<Query.Type<Q>> = Entity.Entity<Query.Type<Q>>>(\n resource: Database.Queryable | undefined,\n query: Q,\n ): O[];\n\n <F extends Filter.Any, O extends Entity.Entity<Filter.Type<F>> = Entity.Entity<Filter.Type<F>>>(\n resource: Database.Queryable | undefined,\n filter: F,\n ): O[];\n}\n\n/**\n * Create subscription.\n *\n * @param queryOrFilter - The query or filter to apply. Query is memoized based on the AST. No need to call useMemo.\n */\nexport const useQuery: UseQueryFn = (\n resource: Database.Queryable | undefined,\n queryOrFilter: Query.Any | Filter.Any,\n): Entity.Any[] => {\n const query = Filter.is(queryOrFilter) ? Query.select(queryOrFilter) : queryOrFilter;\n\n const { getObjects, subscribe } = useMemo(() => {\n let queryResult = undefined;\n if (resource) {\n queryResult = resource.query(query);\n }\n\n let subscribed = false;\n return {\n getObjects: () => (subscribed && queryResult ? queryResult.results : EMPTY_ARRAY),\n subscribe: (cb: () => void) => {\n subscribed = true;\n const unsubscribe = queryResult?.subscribe(cb) ?? noop;\n return () => {\n unsubscribe?.();\n subscribed = false;\n };\n },\n };\n }, [resource, JSON.stringify(query.ast)]);\n\n // https://beta.reactjs.org/reference/react/useSyncExternalStore\n // NOTE: This hook will resubscribe whenever the callback passed to the first argument changes; make sure it is stable.\n const objects = useSyncExternalStore<Entity.Any[]>(subscribe, getObjects);\n return objects;\n};\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport { useMemo, useSyncExternalStore } from 'react';\n\nimport { type Database, type Type } from '@dxos/echo';\n\n/**\n * Subscribe to and retrieve schema changes from a space's schema registry.\n */\nexport const useSchema = <T extends Type.AnyEntity = Type.AnyEntity>(\n db?: Database.Database,\n typename?: string,\n): T | undefined => {\n const { subscribe, getSchema } = useMemo(() => {\n if (!typename || !db) {\n return {\n subscribe: () => () => {},\n getSchema: () => undefined,\n };\n }\n\n const query = db.schemaRegistry.query({ typename, location: ['database', 'runtime'] });\n const initialResult = query.runSync()[0];\n let currentSchema = initialResult;\n\n return {\n subscribe: (onStoreChange: () => void) => {\n const unsubscribe = query.subscribe(() => {\n currentSchema = query.results[0];\n onStoreChange();\n });\n\n return unsubscribe;\n },\n getSchema: () => currentSchema,\n };\n }, [typename, db]);\n\n return useSyncExternalStore(subscribe, getSchema) as T;\n};\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport { useAtomValue } from '@effect-atom/atom-react';\nimport * as Atom from '@effect-atom/atom/Atom';\nimport { useCallback, useMemo } from 'react';\n\nimport { Obj, Ref } from '@dxos/echo';\nimport { AtomObj } from '@dxos/echo-atom';\n\nexport interface ObjectUpdateCallback<T> {\n (update: (obj: Obj.Mutable<T>) => void): void;\n (update: (obj: Obj.Mutable<T>) => Obj.Mutable<T>): void;\n}\n\nexport interface ObjectPropUpdateCallback<T> {\n (update: (value: Obj.Mutable<T>) => void): void;\n (update: (value: Obj.Mutable<T>) => Obj.Mutable<T>): void;\n (newValue: T): void;\n}\n\nexport const useObject: {\n /**\n * Hook to subscribe to a Ref's target object.\n * Automatically dereferences the ref and handles async loading.\n * Returns a snapshot (undefined if the ref hasn't loaded yet).\n *\n * @param ref - The Ref to dereference and subscribe to\n * @returns The current target snapshot (or undefined if not loaded) and update callback\n */\n <T extends Obj.Unknown>(ref: Ref.Ref<T>): [Obj.Snapshot<T> | undefined, ObjectUpdateCallback<T>];\n\n /**\n * Hook to subscribe to a Ref's target object that may be undefined.\n * Returns a snapshot (undefined if the ref is undefined or hasn't loaded yet).\n *\n * @param ref - The Ref to dereference and subscribe to (can be undefined)\n * @returns The current target snapshot (or undefined) and update callback\n */\n <T extends Obj.Unknown>(ref: Ref.Ref<T> | undefined): [Obj.Snapshot<T> | undefined, ObjectUpdateCallback<T>];\n\n /**\n * Hook to subscribe to an entire Echo object.\n * Returns a snapshot of the current object value and automatically re-renders when the object changes.\n *\n * @param obj - The Echo object to subscribe to (objects only, not relations)\n * @returns The current object snapshot and update callback\n */\n <T extends Obj.Unknown>(obj: T): [Obj.Snapshot<T>, ObjectUpdateCallback<T>];\n\n /**\n * Hook to subscribe to an entire Echo object that may be undefined.\n * Returns a snapshot (undefined if the object is undefined).\n *\n * @param obj - The Echo object to subscribe to (can be undefined, objects only)\n * @returns The current object snapshot (or undefined) and update callback\n */\n <T extends Obj.Unknown>(obj: T | undefined): [Obj.Snapshot<T> | undefined, ObjectUpdateCallback<T>];\n\n /**\n * Hook to subscribe to an Echo object or Ref.\n * Handles both cases - if passed a Ref, dereferences it and subscribes to the target.\n * Returns a snapshot (undefined if ref hasn't loaded).\n *\n * @param objOrRef - The Echo object or Ref to subscribe to\n * @returns The current object snapshot (or undefined) and update callback\n */\n <T extends Obj.Unknown>(objOrRef: T | Ref.Ref<T>): [Obj.Snapshot<T> | undefined, ObjectUpdateCallback<T>];\n\n /**\n * Hook to subscribe to a specific property of an Echo object.\n * Returns the current property value and automatically re-renders when the property changes.\n *\n * @param obj - The Echo object to subscribe to (objects only, not relations)\n * @param property - Property key to subscribe to\n * @returns The current property value and update callback\n */\n <T extends Obj.Unknown, K extends keyof T>(obj: T, property: K): [T[K], ObjectPropUpdateCallback<T[K]>];\n\n /**\n * Hook to subscribe to a specific property of an Echo object that may be undefined.\n * Returns undefined if the object is undefined.\n *\n * @param obj - The Echo object to subscribe to (can be undefined, objects only)\n * @param property - Property key to subscribe to\n * @returns The current property value (or undefined) and update callback\n */\n <T extends Obj.Unknown, K extends keyof T>(\n obj: T | undefined,\n property: K,\n ): [T[K] | undefined, ObjectPropUpdateCallback<T[K]>];\n\n /**\n * Hook to subscribe to a specific property of a Ref's target object.\n * Automatically dereferences the ref and handles async loading.\n * Returns undefined if the ref hasn't loaded yet.\n *\n * @param ref - The Ref to dereference and subscribe to\n * @param property - Property key to subscribe to\n * @returns The current property value (or undefined if not loaded) and update callback\n */\n <T, K extends keyof T>(ref: Ref.Ref<T>, property: K): [T[K] | undefined, ObjectPropUpdateCallback<T[K]>];\n\n /**\n * Hook to subscribe to a specific property of a Ref's target object that may be undefined.\n * Returns undefined if the ref is undefined or hasn't loaded yet.\n *\n * @param ref - The Ref to dereference and subscribe to (can be undefined)\n * @param property - Property key to subscribe to\n * @returns The current property value (or undefined) and update callback\n */\n <T, K extends keyof T>(ref: Ref.Ref<T> | undefined, property: K): [T[K] | undefined, ObjectPropUpdateCallback<T[K]>];\n} = (<T extends Obj.Unknown, K extends keyof T>(objOrRef: T | Ref.Ref<T> | undefined, property?: K): any => {\n // Get the live object for the callback (refs need to dereference).\n const isRef = Ref.isRef(objOrRef);\n const liveObj = isRef ? (objOrRef as Ref.Ref<T>)?.target : (objOrRef as T | undefined);\n\n const callback: ObjectPropUpdateCallback<unknown> = useCallback(\n (updateOrValue: unknown | ((obj: unknown) => unknown)) => {\n // Get current target for refs (may have loaded since render).\n const obj = isRef ? (objOrRef as Ref.Ref<T>)?.target : liveObj;\n if (obj === undefined) {\n return;\n }\n Obj.update(obj, (obj: any) => {\n if (typeof updateOrValue === 'function') {\n const returnValue = updateOrValue(property !== undefined ? obj[property] : obj);\n if (returnValue !== undefined) {\n if (property === undefined) {\n throw new Error('Cannot re-assign the entire object');\n }\n obj[property] = returnValue;\n }\n } else {\n if (property === undefined) {\n throw new Error('Cannot re-assign the entire object');\n }\n obj[property] = updateOrValue;\n }\n });\n },\n [objOrRef, property, isRef, liveObj],\n );\n\n if (property !== undefined) {\n // For property subscriptions on refs, we subscribe to trigger re-render on load.\n // TODO(dxos): Property subscriptions on refs may not update correctly until the ref loads.\n useObjectValue(objOrRef);\n return [useObjectProperty(liveObj, property as any), callback];\n } else {\n return [useObjectValue(objOrRef), callback];\n }\n}) as any;\n\n/**\n * Internal hook for subscribing to an Echo object or Ref.\n * AtomObj.make handles both objects and refs, returning snapshots.\n */\nconst useObjectValue = <T extends Obj.Unknown>(objOrRef: T | Ref.Ref<T> | undefined): Obj.Snapshot<T> | undefined => {\n const atom = useMemo(() => AtomObj.make(objOrRef), [objOrRef]);\n return useAtomValue(atom) as Obj.Snapshot<T> | undefined;\n};\n\n/**\n * Internal hook for subscribing to a specific property of an Echo object.\n * Uses useAtomValue directly since makeProperty returns the value directly.\n */\nconst useObjectProperty = <T extends Obj.Unknown, K extends keyof T>(\n obj: T | undefined,\n property: K,\n): T[K] | undefined => {\n const atom = useMemo(() => AtomObj.makeProperty(obj, property), [obj, property]);\n return useAtomValue(atom);\n};\n\n/**\n * Hook to subscribe to multiple Refs' target objects.\n * Automatically dereferences each ref and handles async loading.\n * Returns an array of loaded snapshots (filtering out undefined values).\n *\n * This hook is useful for aggregate computations like counts or filtering\n * across multiple refs without using .target directly.\n *\n * @param refs - Array of Refs to dereference and subscribe to\n * @returns Array of loaded target snapshots (excludes unloaded refs)\n */\nexport const useObjects = <T extends Obj.Unknown>(refs: readonly Ref.Ref<T>[]): Obj.Snapshot<T>[] => {\n const atom = useMemo(\n () =>\n Atom.make((get) => {\n const results: Obj.Snapshot<T>[] = [];\n for (const ref of refs) {\n const value = get(AtomObj.make(ref));\n if (value !== undefined) {\n results.push(value as Obj.Snapshot<T>);\n }\n }\n return results;\n }),\n [refs],\n );\n return useAtomValue(atom);\n};\n"],
|
|
5
|
-
"mappings": ";AAIA,SAASA,SAASC,4BAA4B;AAE9C,SAAqCC,QAAQC,aAAa;AAE1D,IAAMC,cAAuB,CAAA;AAE7B,IAAMC,OAAO,MAAA;AAAO;AAmBb,IAAMC,WAAuB,CAClCC,UACAC,kBAAAA;AAEA,QAAMC,QAAQP,OAAOQ,GAAGF,aAAAA,IAAiBL,MAAMQ,OAAOH,aAAAA,IAAiBA;AAEvE,QAAM,EAAEI,YAAYC,UAAS,IAAKb,QAAQ,MAAA;AACxC,QAAIc,cAAcC;AAClB,QAAIR,UAAU;AACZO,oBAAcP,SAASE,MAAMA,KAAAA;IAC/B;AAEA,QAAIO,aAAa;AACjB,WAAO;MACLJ,YAAY,MAAOI,cAAcF,cAAcA,YAAYG,UAAUb;MACrES,WAAW,CAACK,OAAAA;AACVF,qBAAa;AACb,cAAMG,cAAcL,aAAaD,UAAUK,EAAAA,KAAOb;AAClD,eAAO,MAAA;AACLc,wBAAAA;AACAH,uBAAa;QACf;MACF;IACF;EACF,GAAG;IAACT;IAAUa,KAAKC,UAAUZ,MAAMa,GAAG;GAAE;AAIxC,QAAMC,UAAUtB,qBAAmCY,WAAWD,UAAAA;AAC9D,SAAOW;AACT;;;ACvDA,SAASC,WAAAA,UAASC,wBAAAA,6BAA4B;
|
|
6
|
-
"names": ["useMemo", "useSyncExternalStore", "Filter", "Query", "EMPTY_ARRAY", "noop", "useQuery", "resource", "queryOrFilter", "query", "is", "select", "getObjects", "subscribe", "queryResult", "undefined", "subscribed", "results", "cb", "unsubscribe", "JSON", "stringify", "ast", "objects", "useMemo", "useSyncExternalStore", "
|
|
3
|
+
"sources": ["../../../src/useQuery.ts", "../../../src/useType.ts", "../../../src/useObject.ts"],
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2022 DXOS.org\n//\n\nimport { useMemo, useSyncExternalStore } from 'react';\n\nimport { type Database, type Entity, Filter, Query } from '@dxos/echo';\n\nconst EMPTY_ARRAY: never[] = [];\n\nconst noop = () => {};\n\ninterface UseQueryFn {\n <Q extends Query.Any, O extends Entity.Entity<Query.Type<Q>> = Entity.Entity<Query.Type<Q>>>(\n resource: Database.Queryable | undefined,\n query: Q,\n ): O[];\n\n <F extends Filter.Any, O extends Entity.Entity<Filter.Type<F>> = Entity.Entity<Filter.Type<F>>>(\n resource: Database.Queryable | undefined,\n filter: F,\n ): O[];\n}\n\n/**\n * Create subscription.\n *\n * @param queryOrFilter - The query or filter to apply. Query is memoized based on the AST. No need to call useMemo.\n */\nexport const useQuery: UseQueryFn = (\n resource: Database.Queryable | undefined,\n queryOrFilter: Query.Any | Filter.Any,\n): Entity.Any[] => {\n const query = Filter.is(queryOrFilter) ? Query.select(queryOrFilter) : queryOrFilter;\n\n const { getObjects, subscribe } = useMemo(() => {\n let queryResult = undefined;\n if (resource) {\n queryResult = resource.query(query);\n }\n\n let subscribed = false;\n return {\n getObjects: () => (subscribed && queryResult ? queryResult.results : EMPTY_ARRAY),\n subscribe: (cb: () => void) => {\n subscribed = true;\n const unsubscribe = queryResult?.subscribe(cb) ?? noop;\n return () => {\n unsubscribe?.();\n subscribed = false;\n };\n },\n };\n }, [resource, JSON.stringify(query.ast)]);\n\n // https://beta.reactjs.org/reference/react/useSyncExternalStore\n // NOTE: This hook will resubscribe whenever the callback passed to the first argument changes; make sure it is stable.\n const objects = useSyncExternalStore<Entity.Any[]>(subscribe, getObjects);\n return objects;\n};\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport { useMemo, useSyncExternalStore } from 'react';\n\nimport { type Database, Filter, Query, Scope, Type } from '@dxos/echo';\n\n/**\n * Subscribe to and retrieve a type by typename from a space.\n *\n * Fans across the owning space db (persisted custom types) and the shared\n * registry (static/runtime plugin types). Persisted types live only in the db,\n * so a registry-only lookup misses them.\n */\nexport const useType = <T extends Type.AnyEntity = Type.AnyEntity>(\n db?: Database.Database,\n typename?: string,\n): T | undefined => {\n const { subscribe, getType } = useMemo(() => {\n if (!typename || !db) {\n return {\n subscribe: () => () => {},\n getType: (): T | undefined => undefined,\n };\n }\n\n const queryResult = db.query(Query.select(Filter.type(Type.Type)).from(Scope.space(), Scope.registry()));\n let subscribed = false;\n const find = (): T | undefined =>\n subscribed\n ? (queryResult.results.find((type) => Type.getTypename(type) === typename) as T | undefined)\n : undefined;\n\n return {\n subscribe: (onStoreChange: () => void) => {\n subscribed = true;\n const unsubscribe = queryResult.subscribe(onStoreChange);\n return () => {\n unsubscribe();\n subscribed = false;\n };\n },\n getType: find,\n };\n }, [typename, db]);\n\n return useSyncExternalStore(subscribe, getType);\n};\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport { useAtomValue } from '@effect-atom/atom-react';\nimport * as Atom from '@effect-atom/atom/Atom';\nimport { useCallback, useMemo } from 'react';\n\nimport { Obj, Ref } from '@dxos/echo';\nimport { AtomObj } from '@dxos/echo-atom';\n\nexport interface ObjectUpdateCallback<T> {\n (update: (obj: Obj.Mutable<T>) => void): void;\n (update: (obj: Obj.Mutable<T>) => Obj.Mutable<T>): void;\n}\n\nexport interface ObjectPropUpdateCallback<T> {\n (update: (value: Obj.Mutable<T>) => void): void;\n (update: (value: Obj.Mutable<T>) => Obj.Mutable<T>): void;\n (newValue: T): void;\n}\n\nexport const useObject: {\n /**\n * Hook to subscribe to a Ref's target object.\n * Automatically dereferences the ref and handles async loading.\n * Returns a snapshot (undefined if the ref hasn't loaded yet).\n *\n * @param ref - The Ref to dereference and subscribe to\n * @returns The current target snapshot (or undefined if not loaded) and update callback\n */\n <T extends Obj.Unknown>(ref: Ref.Ref<T>): [Obj.Snapshot<T> | undefined, ObjectUpdateCallback<T>];\n\n /**\n * Hook to subscribe to a Ref's target object that may be undefined.\n * Returns a snapshot (undefined if the ref is undefined or hasn't loaded yet).\n *\n * @param ref - The Ref to dereference and subscribe to (can be undefined)\n * @returns The current target snapshot (or undefined) and update callback\n */\n <T extends Obj.Unknown>(ref: Ref.Ref<T> | undefined): [Obj.Snapshot<T> | undefined, ObjectUpdateCallback<T>];\n\n /**\n * Hook to subscribe to an entire Echo object.\n * Returns a snapshot of the current object value and automatically re-renders when the object changes.\n *\n * @param obj - The Echo object to subscribe to (objects only, not relations)\n * @returns The current object snapshot and update callback\n */\n <T extends Obj.Unknown>(obj: T): [Obj.Snapshot<T>, ObjectUpdateCallback<T>];\n\n /**\n * Hook to subscribe to an entire Echo object that may be undefined.\n * Returns a snapshot (undefined if the object is undefined).\n *\n * @param obj - The Echo object to subscribe to (can be undefined, objects only)\n * @returns The current object snapshot (or undefined) and update callback\n */\n <T extends Obj.Unknown>(obj: T | undefined): [Obj.Snapshot<T> | undefined, ObjectUpdateCallback<T>];\n\n /**\n * Hook to subscribe to an Echo object or Ref.\n * Handles both cases - if passed a Ref, dereferences it and subscribes to the target.\n * Returns a snapshot (undefined if ref hasn't loaded).\n *\n * @param objOrRef - The Echo object or Ref to subscribe to\n * @returns The current object snapshot (or undefined) and update callback\n */\n <T extends Obj.Unknown>(objOrRef: T | Ref.Ref<T>): [Obj.Snapshot<T> | undefined, ObjectUpdateCallback<T>];\n\n /**\n * Hook to subscribe to a specific property of an Echo object.\n * Returns the current property value and automatically re-renders when the property changes.\n *\n * @param obj - The Echo object to subscribe to (objects only, not relations)\n * @param property - Property key to subscribe to\n * @returns The current property value and update callback\n */\n <T extends Obj.Unknown, K extends keyof T>(obj: T, property: K): [T[K], ObjectPropUpdateCallback<T[K]>];\n\n /**\n * Hook to subscribe to a specific property of an Echo object that may be undefined.\n * Returns undefined if the object is undefined.\n *\n * @param obj - The Echo object to subscribe to (can be undefined, objects only)\n * @param property - Property key to subscribe to\n * @returns The current property value (or undefined) and update callback\n */\n <T extends Obj.Unknown, K extends keyof T>(\n obj: T | undefined,\n property: K,\n ): [T[K] | undefined, ObjectPropUpdateCallback<T[K]>];\n\n /**\n * Hook to subscribe to a specific property of a Ref's target object.\n * Automatically dereferences the ref and handles async loading.\n * Returns undefined if the ref hasn't loaded yet.\n *\n * @param ref - The Ref to dereference and subscribe to\n * @param property - Property key to subscribe to\n * @returns The current property value (or undefined if not loaded) and update callback\n */\n <T, K extends keyof T>(ref: Ref.Ref<T>, property: K): [T[K] | undefined, ObjectPropUpdateCallback<T[K]>];\n\n /**\n * Hook to subscribe to a specific property of a Ref's target object that may be undefined.\n * Returns undefined if the ref is undefined or hasn't loaded yet.\n *\n * @param ref - The Ref to dereference and subscribe to (can be undefined)\n * @param property - Property key to subscribe to\n * @returns The current property value (or undefined) and update callback\n */\n <T, K extends keyof T>(ref: Ref.Ref<T> | undefined, property: K): [T[K] | undefined, ObjectPropUpdateCallback<T[K]>];\n} = (<T extends Obj.Unknown, K extends keyof T>(objOrRef: T | Ref.Ref<T> | undefined, property?: K): any => {\n // Get the live object for the callback (refs need to dereference).\n const isRef = Ref.isRef(objOrRef);\n const liveObj = isRef ? (objOrRef as Ref.Ref<T>)?.target : (objOrRef as T | undefined);\n\n const callback: ObjectPropUpdateCallback<unknown> = useCallback(\n (updateOrValue: unknown | ((obj: unknown) => unknown)) => {\n // Get current target for refs (may have loaded since render).\n const obj = isRef ? (objOrRef as Ref.Ref<T>)?.target : liveObj;\n if (obj === undefined) {\n return;\n }\n Obj.update(obj, (obj: any) => {\n if (typeof updateOrValue === 'function') {\n const returnValue = updateOrValue(property !== undefined ? obj[property] : obj);\n if (returnValue !== undefined) {\n if (property === undefined) {\n throw new Error('Cannot re-assign the entire object');\n }\n obj[property] = returnValue;\n }\n } else {\n if (property === undefined) {\n throw new Error('Cannot re-assign the entire object');\n }\n obj[property] = updateOrValue;\n }\n });\n },\n [objOrRef, property, isRef, liveObj],\n );\n\n if (property !== undefined) {\n // For property subscriptions on refs, we subscribe to trigger re-render on load.\n // TODO(dxos): Property subscriptions on refs may not update correctly until the ref loads.\n useObjectValue(objOrRef);\n return [useObjectProperty(liveObj, property as any), callback];\n } else {\n return [useObjectValue(objOrRef), callback];\n }\n}) as any;\n\n/**\n * Internal hook for subscribing to an Echo object or Ref.\n * AtomObj.make handles both objects and refs, returning snapshots.\n */\nconst useObjectValue = <T extends Obj.Unknown>(objOrRef: T | Ref.Ref<T> | undefined): Obj.Snapshot<T> | undefined => {\n const atom = useMemo(() => AtomObj.make(objOrRef), [objOrRef]);\n return useAtomValue(atom) as Obj.Snapshot<T> | undefined;\n};\n\n/**\n * Internal hook for subscribing to a specific property of an Echo object.\n * Uses useAtomValue directly since makeProperty returns the value directly.\n */\nconst useObjectProperty = <T extends Obj.Unknown, K extends keyof T>(\n obj: T | undefined,\n property: K,\n): T[K] | undefined => {\n const atom = useMemo(() => AtomObj.makeProperty(obj, property), [obj, property]);\n return useAtomValue(atom);\n};\n\n/**\n * Hook to subscribe to multiple Refs' target objects.\n * Automatically dereferences each ref and handles async loading.\n * Returns an array of loaded snapshots (filtering out undefined values).\n *\n * This hook is useful for aggregate computations like counts or filtering\n * across multiple refs without using .target directly.\n *\n * @param refs - Array of Refs to dereference and subscribe to\n * @returns Array of loaded target snapshots (excludes unloaded refs)\n */\nexport const useObjects = <T extends Obj.Unknown>(refs: readonly Ref.Ref<T>[]): Obj.Snapshot<T>[] => {\n const atom = useMemo(\n () =>\n Atom.make((get) => {\n const results: Obj.Snapshot<T>[] = [];\n for (const ref of refs) {\n const value = get(AtomObj.make(ref));\n if (value !== undefined) {\n results.push(value as Obj.Snapshot<T>);\n }\n }\n return results;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F,aAAaO,QAAW;AAG1BI,mBAAeZ,QAAAA;AACf,WAAO;MAACa,kBAAkBV,SAASF,QAAAA;MAAkBI;;EACvD,OAAO;AACL,WAAO;MAACO,eAAeZ,QAAAA;MAAWK;;EACpC;AACF;AAMA,IAAMO,iBAAiB,CAAwBZ,aAAAA;AAC7C,QAAMc,OAAOnB,SAAQ,MAAMG,QAAQiB,KAAKf,QAAAA,GAAW;IAACA;GAAS;AAC7D,SAAOR,aAAasB,IAAAA;AACtB;AAMA,IAAMD,oBAAoB,CACxBN,KACAN,aAAAA;AAEA,QAAMa,OAAOnB,SAAQ,MAAMG,QAAQkB,aAAaT,KAAKN,QAAAA,GAAW;IAACM;IAAKN;GAAS;AAC/E,SAAOT,aAAasB,IAAAA;AACtB;AAaO,IAAMG,aAAa,CAAwBC,SAAAA;AAChD,QAAMJ,OAAOnB,SACX,MACOoB,UAAK,CAACI,QAAAA;AACT,UAAMC,UAA6B,CAAA;AACnC,eAAWC,OAAOH,MAAM;AACtB,YAAMI,QAAQH,IAAIrB,QAAQiB,KAAKM,GAAAA,CAAAA;AAC/B,UAAIC,UAAUd,QAAW;AACvBY,gBAAQG,KAAKD,KAAAA;MACf;IACF;AACA,WAAOF;EACT,CAAA,GACF;IAACF;GAAK;AAER,SAAO1B,aAAasB,IAAAA;AACtB;",
|
|
6
|
+
"names": ["useMemo", "useSyncExternalStore", "Filter", "Query", "EMPTY_ARRAY", "noop", "useQuery", "resource", "queryOrFilter", "query", "is", "select", "getObjects", "subscribe", "queryResult", "undefined", "subscribed", "results", "cb", "unsubscribe", "JSON", "stringify", "ast", "objects", "useMemo", "useSyncExternalStore", "Filter", "Query", "Scope", "Type", "useType", "db", "typename", "subscribe", "getType", "undefined", "queryResult", "query", "select", "type", "from", "space", "registry", "subscribed", "find", "results", "getTypename", "onStoreChange", "unsubscribe", "useAtomValue", "Atom", "useCallback", "useMemo", "Obj", "Ref", "AtomObj", "useObject", "objOrRef", "property", "isRef", "liveObj", "target", "callback", "updateOrValue", "obj", "undefined", "update", "returnValue", "Error", "useObjectValue", "useObjectProperty", "atom", "make", "makeProperty", "useObjects", "refs", "get", "results", "ref", "value", "push"]
|
|
7
7
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"inputs":{"src/useQuery.ts":{"bytes":5393,"imports":[{"path":"react","kind":"import-statement","external":true},{"path":"@dxos/echo","kind":"import-statement","external":true}],"format":"esm"},"src/
|
|
1
|
+
{"inputs":{"src/useQuery.ts":{"bytes":5393,"imports":[{"path":"react","kind":"import-statement","external":true},{"path":"@dxos/echo","kind":"import-statement","external":true}],"format":"esm"},"src/useType.ts":{"bytes":4888,"imports":[{"path":"react","kind":"import-statement","external":true},{"path":"@dxos/echo","kind":"import-statement","external":true}],"format":"esm"},"src/useObject.ts":{"bytes":17406,"imports":[{"path":"@effect-atom/atom-react","kind":"import-statement","external":true},{"path":"@effect-atom/atom/Atom","kind":"import-statement","external":true},{"path":"react","kind":"import-statement","external":true},{"path":"@dxos/echo","kind":"import-statement","external":true},{"path":"@dxos/echo-atom","kind":"import-statement","external":true}],"format":"esm"},"src/index.ts":{"bytes":552,"imports":[{"path":"src/useQuery.ts","kind":"import-statement","original":"./useQuery"},{"path":"src/useType.ts","kind":"import-statement","original":"./useType"},{"path":"src/useObject.ts","kind":"import-statement","original":"./useObject"}],"format":"esm"}},"outputs":{"dist/lib/neutral/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":15694},"dist/lib/neutral/index.mjs":{"imports":[{"path":"react","kind":"import-statement","external":true},{"path":"@dxos/echo","kind":"import-statement","external":true},{"path":"react","kind":"import-statement","external":true},{"path":"@dxos/echo","kind":"import-statement","external":true},{"path":"@effect-atom/atom-react","kind":"import-statement","external":true},{"path":"@effect-atom/atom/Atom","kind":"import-statement","external":true},{"path":"react","kind":"import-statement","external":true},{"path":"@dxos/echo","kind":"import-statement","external":true},{"path":"@dxos/echo-atom","kind":"import-statement","external":true}],"exports":["useObject","useObjects","useQuery","useType"],"entryPoint":"src/index.ts","inputs":{"src/useQuery.ts":{"bytesInOutput":921},"src/index.ts":{"bytesInOutput":0},"src/useType.ts":{"bytesInOutput":1002},"src/useObject.ts":{"bytesInOutput":2013}},"bytes":4091}}}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAIA,cAAc,YAAY,CAAC;AAC3B,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAIA,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type Database, Type } from '@dxos/echo';
|
|
2
|
+
/**
|
|
3
|
+
* Subscribe to and retrieve a type by typename from a space.
|
|
4
|
+
*
|
|
5
|
+
* Fans across the owning space db (persisted custom types) and the shared
|
|
6
|
+
* registry (static/runtime plugin types). Persisted types live only in the db,
|
|
7
|
+
* so a registry-only lookup misses them.
|
|
8
|
+
*/
|
|
9
|
+
export declare const useType: <T extends Type.AnyEntity = Type.AnyEntity>(db?: Database.Database, typename?: string) => T | undefined;
|
|
10
|
+
//# sourceMappingURL=useType.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useType.d.ts","sourceRoot":"","sources":["../../../src/useType.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,KAAK,QAAQ,EAAwB,IAAI,EAAE,MAAM,YAAY,CAAC;AAEvE;;;;;;GAMG;AACH,eAAO,MAAM,OAAO,GAAI,CAAC,SAAS,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,SAAS,OAC1D,QAAQ,CAAC,QAAQ,aACX,MAAM,KAChB,CAAC,GAAG,SA8BN,CAAC"}
|