parent
6ce5c27f8b
commit
1f9388871d
@ -0,0 +1,21 @@ |
||||
import type { LdoBase, ShapeType } from "@ldo/ldo"; |
||||
import type { QuadMatch } from "@ldo/rdf-utils"; |
||||
import type { LdoBuilder } from "@ldo/ldo"; |
||||
import { useCallback } from "react"; |
||||
import { useTrackingProxy } from "./util/useTrackingProxy"; |
||||
|
||||
export function useMatchObject<Type extends LdoBase>( |
||||
shapeType: ShapeType<Type>, |
||||
subject?: QuadMatch[0] | string, |
||||
predicate?: QuadMatch[1] | string, |
||||
graph?: QuadMatch[3] | string, |
||||
): Type[] { |
||||
const matchObject = useCallback( |
||||
(builder: LdoBuilder<Type>) => { |
||||
return builder.matchObject(subject, predicate, graph); |
||||
}, |
||||
[subject, predicate, graph], |
||||
); |
||||
|
||||
return useTrackingProxy(shapeType, matchObject); |
||||
} |
@ -0,0 +1,21 @@ |
||||
import type { LdoBase, ShapeType } from "@ldo/ldo"; |
||||
import type { QuadMatch } from "@ldo/rdf-utils"; |
||||
import type { LdoBuilder } from "@ldo/ldo"; |
||||
import { useCallback } from "react"; |
||||
import { useTrackingProxy } from "./util/useTrackingProxy"; |
||||
|
||||
export function useMatchSubject<Type extends LdoBase>( |
||||
shapeType: ShapeType<Type>, |
||||
predicate?: QuadMatch[1] | string, |
||||
object?: QuadMatch[2] | string, |
||||
graph?: QuadMatch[3] | string, |
||||
): Type[] { |
||||
const matchSubject = useCallback( |
||||
(builder: LdoBuilder<Type>) => { |
||||
return builder.matchSubject(predicate, object, graph); |
||||
}, |
||||
[predicate, object, graph], |
||||
); |
||||
|
||||
return useTrackingProxy(shapeType, matchSubject); |
||||
} |
@ -0,0 +1,55 @@ |
||||
import { |
||||
ContextUtil, |
||||
JsonldDatasetProxyBuilder, |
||||
} from "@ldo/jsonld-dataset-proxy"; |
||||
import { LdoBuilder } from "@ldo/ldo"; |
||||
import type { LdoBase, ShapeType } from "@ldo/ldo"; |
||||
import { useCallback, useEffect, useMemo, useState } from "react"; |
||||
import { TrackingProxyContext } from "./TrackingProxyContext"; |
||||
import { defaultGraph } from "@rdfjs/data-model"; |
||||
import { useLdo } from "../SolidLdoProvider"; |
||||
|
||||
export function useTrackingProxy<Type extends LdoBase, ReturnType>( |
||||
shapeType: ShapeType<Type>, |
||||
createLdo: (builder: LdoBuilder<Type>) => ReturnType, |
||||
): ReturnType { |
||||
const { dataset } = useLdo(); |
||||
|
||||
const [forceUpdateCounter, setForceUpdateCounter] = useState(0); |
||||
const forceUpdate = useCallback( |
||||
() => setForceUpdateCounter((val) => val + 1), |
||||
[], |
||||
); |
||||
|
||||
// The main linked data object
|
||||
const linkedDataObject = useMemo(() => { |
||||
// Remove all current subscriptions
|
||||
dataset.removeListenerFromAllEvents(forceUpdate); |
||||
|
||||
// Rebuild the LdoBuilder from scratch to inject TrackingProxyContext
|
||||
const contextUtil = new ContextUtil(shapeType.context); |
||||
const proxyContext = new TrackingProxyContext( |
||||
{ |
||||
dataset, |
||||
contextUtil, |
||||
writeGraphs: [defaultGraph()], |
||||
languageOrdering: ["none", "en", "other"], |
||||
}, |
||||
forceUpdate, |
||||
); |
||||
const builder = new LdoBuilder( |
||||
new JsonldDatasetProxyBuilder(proxyContext), |
||||
shapeType, |
||||
); |
||||
return createLdo(builder); |
||||
}, [shapeType, dataset, forceUpdateCounter, forceUpdate, createLdo]); |
||||
|
||||
useEffect(() => { |
||||
// Unregister force update listener upon unmount
|
||||
return () => { |
||||
dataset.removeListenerFromAllEvents(forceUpdate); |
||||
}; |
||||
}, [shapeType]); |
||||
|
||||
return linkedDataObject; |
||||
} |
Loading…
Reference in new issue