@@ -4,7 +4,7 @@ import type { AnyDocumentId, DocHandle, Repo } from '@automerge/automerge-repo';
44import { useRepo } from '@automerge/automerge-repo-react-hooks' ;
55import { Entity , Utils } from '@graphprotocol/hypergraph' ;
66import * as Schema from 'effect/Schema' ;
7- import { type ReactNode , createContext , useContext , useRef , useSyncExternalStore } from 'react' ;
7+ import { type ReactNode , createContext , useContext , useRef , useState , useSyncExternalStore } from 'react' ;
88
99export type HypergraphContext = {
1010 space : string ;
@@ -31,11 +31,13 @@ export function HypergraphSpaceProvider({ space, children }: { space: string; ch
3131 let current = ref . current ;
3232 if ( current === undefined || space !== current . space || repo !== current . repo ) {
3333 const id = Utils . idToAutomergeId ( space ) as AnyDocumentId ;
34+ const handle = repo . find < Entity . DocumentContent > ( id ) ;
35+
3436 current = ref . current = {
3537 space,
3638 repo,
3739 id,
38- handle : repo . find ( id ) ,
40+ handle,
3941 } ;
4042 }
4143
@@ -59,37 +61,11 @@ export function useDeleteEntity() {
5961
6062export function useQueryEntities < const S extends Entity . AnyNoContext > ( type : S ) {
6163 const hypergraph = useHypergraph ( ) ;
62- const equal = isEqual ( type ) ;
63-
64- // store as a map of type to array of entities of the type
65- const prevEntitiesRef = useRef < Readonly < Array < Entity . Entity < S > > > > ( [ ] ) ;
66-
67- const subscribe = ( callback : ( ) => void ) => {
68- const handleChange = ( ) => {
69- callback ( ) ;
70- } ;
71-
72- const handleDelete = ( ) => {
73- callback ( ) ;
74- } ;
75-
76- hypergraph . handle . on ( 'change' , handleChange ) ;
77- hypergraph . handle . on ( 'delete' , handleDelete ) ;
78-
79- return ( ) => {
80- hypergraph . handle . off ( 'change' , handleChange ) ;
81- hypergraph . handle . off ( 'delete' , handleDelete ) ;
82- } ;
83- } ;
84-
85- return useSyncExternalStore < Readonly < Array < Entity . Entity < S > > > > ( subscribe , ( ) => {
86- const filtered = Entity . findMany ( hypergraph . handle , type ) ;
87- if ( ! equal ( filtered , prevEntitiesRef . current ) ) {
88- prevEntitiesRef . current = filtered ;
89- }
90-
91- return prevEntitiesRef . current ;
64+ const [ subscription ] = useState ( ( ) => {
65+ return Entity . subscribeToFindMany ( hypergraph . handle , type ) ;
9266 } ) ;
67+
68+ return useSyncExternalStore ( subscription . subscribe , subscription . getEntities , ( ) => [ ] ) ;
9369}
9470
9571export function useQueryEntity < const S extends Entity . AnyNoContext > ( type : S , id : string ) {
@@ -135,22 +111,3 @@ export function useQueryEntity<const S extends Entity.AnyNoContext>(type: S, id:
135111 return prevEntityRef . current ;
136112 } ) ;
137113}
138-
139- /** @internal */
140- const isEqual = < A , E > ( type : Schema . Schema < A , E , never > ) => {
141- const equals = Schema . equivalence ( type ) ;
142-
143- return ( a : ReadonlyArray < A > , b : ReadonlyArray < A > ) => {
144- if ( a . length !== b . length ) {
145- return false ;
146- }
147-
148- for ( let i = 0 ; i < a . length ; i ++ ) {
149- if ( ! equals ( a [ i ] , b [ i ] ) ) {
150- return false ;
151- }
152- }
153-
154- return true ;
155- } ;
156- } ;
0 commit comments