@ldo/solid-react 0.0.1-alpha.14
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/README.md +13 -0
- package/dist/BrowserSolidLdoProvider.d.ts +2 -0
- package/dist/BrowserSolidLdoProvider.js +58 -0
- package/dist/BrowserSolidLdoProvider.js.map +1 -0
- package/dist/LdoContext.d.ts +9 -0
- package/dist/LdoContext.js +9 -0
- package/dist/LdoContext.js.map +1 -0
- package/dist/LdoProvider.d.ts +9 -0
- package/dist/LdoProvider.js +45 -0
- package/dist/LdoProvider.js.map +1 -0
- package/dist/SolidAuthContext.d.ts +14 -0
- package/dist/SolidAuthContext.js +10 -0
- package/dist/SolidAuthContext.js.map +1 -0
- package/dist/SolidAuthProvider.d.ts +17 -0
- package/dist/SolidAuthProvider.js +56 -0
- package/dist/SolidAuthProvider.js.map +1 -0
- package/dist/SolidLdoProvider.d.ts +8 -0
- package/dist/SolidLdoProvider.js +31 -0
- package/dist/SolidLdoProvider.js.map +1 -0
- package/dist/document/DocumentStore.d.ts +11 -0
- package/dist/document/DocumentStore.js +24 -0
- package/dist/document/DocumentStore.js.map +1 -0
- package/dist/document/FetchableDocument.d.ts +28 -0
- package/dist/document/FetchableDocument.js +75 -0
- package/dist/document/FetchableDocument.js.map +1 -0
- package/dist/document/accessRules/AccessRules.d.ts +19 -0
- package/dist/document/accessRules/AccessRules.js +53 -0
- package/dist/document/accessRules/AccessRules.js.map +1 -0
- package/dist/document/accessRules/AccessRulesStore.d.ts +10 -0
- package/dist/document/accessRules/AccessRulesStore.js +12 -0
- package/dist/document/accessRules/AccessRulesStore.js.map +1 -0
- package/dist/document/errors/DocumentError.d.ts +5 -0
- package/dist/document/errors/DocumentError.js +11 -0
- package/dist/document/errors/DocumentError.js.map +1 -0
- package/dist/document/errors/DocumentFetchError.d.ts +6 -0
- package/dist/document/errors/DocumentFetchError.js +12 -0
- package/dist/document/errors/DocumentFetchError.js.map +1 -0
- package/dist/document/resource/Resource.d.ts +23 -0
- package/dist/document/resource/Resource.js +48 -0
- package/dist/document/resource/Resource.js.map +1 -0
- package/dist/document/resource/binaryResource/BinaryResource.d.ts +7 -0
- package/dist/document/resource/binaryResource/BinaryResource.js +11 -0
- package/dist/document/resource/binaryResource/BinaryResource.js.map +1 -0
- package/dist/document/resource/binaryResource/BinaryResourceStore.d.ts +10 -0
- package/dist/document/resource/binaryResource/BinaryResourceStore.js +15 -0
- package/dist/document/resource/binaryResource/BinaryResourceStore.js.map +1 -0
- package/dist/document/resource/dataResource/DataResource.d.ts +20 -0
- package/dist/document/resource/dataResource/DataResource.js +81 -0
- package/dist/document/resource/dataResource/DataResource.js.map +1 -0
- package/dist/document/resource/dataResource/DataResourceStore.d.ts +10 -0
- package/dist/document/resource/dataResource/DataResourceStore.js +15 -0
- package/dist/document/resource/dataResource/DataResourceStore.js.map +1 -0
- package/dist/document/resource/dataResource/containerResource/ContainerResource.d.ts +20 -0
- package/dist/document/resource/dataResource/containerResource/ContainerResource.js +74 -0
- package/dist/document/resource/dataResource/containerResource/ContainerResource.js.map +1 -0
- package/dist/document/resource/dataResource/containerResource/ContainerResourceStore.d.ts +13 -0
- package/dist/document/resource/dataResource/containerResource/ContainerResourceStore.js +33 -0
- package/dist/document/resource/dataResource/containerResource/ContainerResourceStore.js.map +1 -0
- package/dist/documentHooks/useAccessRules.d.ts +3 -0
- package/dist/documentHooks/useAccessRules.js +16 -0
- package/dist/documentHooks/useAccessRules.js.map +1 -0
- package/dist/documentHooks/useBinaryResource.d.ts +2 -0
- package/dist/documentHooks/useBinaryResource.js +16 -0
- package/dist/documentHooks/useBinaryResource.js.map +1 -0
- package/dist/documentHooks/useContainerResource.d.ts +2 -0
- package/dist/documentHooks/useContainerResource.js +16 -0
- package/dist/documentHooks/useContainerResource.js.map +1 -0
- package/dist/documentHooks/useDataResource.d.ts +2 -0
- package/dist/documentHooks/useDataResource.js +16 -0
- package/dist/documentHooks/useDataResource.js.map +1 -0
- package/dist/documentHooks/useDocument.d.ts +5 -0
- package/dist/documentHooks/useDocument.js +20 -0
- package/dist/documentHooks/useDocument.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -0
- package/dist/ldo/solid.context.d.ts +2 -0
- package/dist/ldo/solid.context.js +33 -0
- package/dist/ldo/solid.context.js.map +1 -0
- package/dist/ldo/solid.schema.d.ts +2 -0
- package/dist/ldo/solid.schema.js +211 -0
- package/dist/ldo/solid.schema.js.map +1 -0
- package/dist/ldo/solid.shapeTypes.d.ts +4 -0
- package/dist/ldo/solid.shapeTypes.js +16 -0
- package/dist/ldo/solid.shapeTypes.js.map +1 -0
- package/dist/ldo/solid.typings.d.ts +26 -0
- package/dist/ldo/solid.typings.js +3 -0
- package/dist/ldo/solid.typings.js.map +1 -0
- package/dist/ldoHooks/helpers/TrackingProxyContext.d.ts +10 -0
- package/dist/ldoHooks/helpers/TrackingProxyContext.js +68 -0
- package/dist/ldoHooks/helpers/TrackingProxyContext.js.map +1 -0
- package/dist/ldoHooks/helpers/UpdateManager.d.ts +12 -0
- package/dist/ldoHooks/helpers/UpdateManager.js +64 -0
- package/dist/ldoHooks/helpers/UpdateManager.js.map +1 -0
- package/dist/ldoHooks/useMatchingObjects.d.ts +0 -0
- package/dist/ldoHooks/useMatchingObjects.js +2 -0
- package/dist/ldoHooks/useMatchingObjects.js.map +1 -0
- package/dist/ldoHooks/useMatchingSubjects.d.ts +0 -0
- package/dist/ldoHooks/useMatchingSubjects.js +2 -0
- package/dist/ldoHooks/useMatchingSubjects.js.map +1 -0
- package/dist/ldoHooks/useSubject.d.ts +3 -0
- package/dist/ldoHooks/useSubject.js +38 -0
- package/dist/ldoHooks/useSubject.js.map +1 -0
- package/dist/old/LdoContext.d.ts +9 -0
- package/dist/old/LdoContext.js +9 -0
- package/dist/old/LdoContext.js.map +1 -0
- package/dist/old/LdoProvider.d.ts +9 -0
- package/dist/old/LdoProvider.js +41 -0
- package/dist/old/LdoProvider.js.map +1 -0
- package/dist/old/SolidAuthProvider.d.ts +3 -0
- package/dist/old/SolidAuthProvider.js +56 -0
- package/dist/old/SolidAuthProvider.js.map +1 -0
- package/dist/old/documentHooks/useAccessRules.d.ts +3 -0
- package/dist/old/documentHooks/useAccessRules.js +17 -0
- package/dist/old/documentHooks/useAccessRules.js.map +1 -0
- package/dist/old/documentHooks/useBinaryResource.d.ts +2 -0
- package/dist/old/documentHooks/useBinaryResource.js +11 -0
- package/dist/old/documentHooks/useBinaryResource.js.map +1 -0
- package/dist/old/documentHooks/useContainerResource.d.ts +2 -0
- package/dist/old/documentHooks/useContainerResource.js +11 -0
- package/dist/old/documentHooks/useContainerResource.js.map +1 -0
- package/dist/old/documentHooks/useDataResource.d.ts +2 -0
- package/dist/old/documentHooks/useDataResource.js +11 -0
- package/dist/old/documentHooks/useDataResource.js.map +1 -0
- package/dist/old/documentHooks/useDocument.d.ts +6 -0
- package/dist/old/documentHooks/useDocument.js +24 -0
- package/dist/old/documentHooks/useDocument.js.map +1 -0
- package/dist/old/ldoHooks/helpers/TrackingProxyContext.d.ts +10 -0
- package/dist/old/ldoHooks/helpers/TrackingProxyContext.js +68 -0
- package/dist/old/ldoHooks/helpers/TrackingProxyContext.js.map +1 -0
- package/dist/old/ldoHooks/helpers/UpdateManager.d.ts +9 -0
- package/dist/old/ldoHooks/helpers/UpdateManager.js +61 -0
- package/dist/old/ldoHooks/helpers/UpdateManager.js.map +1 -0
- package/dist/old/ldoHooks/useMatchingObjects.d.ts +0 -0
- package/dist/old/ldoHooks/useMatchingObjects.js +2 -0
- package/dist/old/ldoHooks/useMatchingObjects.js.map +1 -0
- package/dist/old/ldoHooks/useMatchingSubjects.d.ts +0 -0
- package/dist/old/ldoHooks/useMatchingSubjects.js +2 -0
- package/dist/old/ldoHooks/useMatchingSubjects.js.map +1 -0
- package/dist/old/ldoHooks/useSubject.d.ts +3 -0
- package/dist/old/ldoHooks/useSubject.js +38 -0
- package/dist/old/ldoHooks/useSubject.js.map +1 -0
- package/dist/old/useLdo.d.ts +18 -0
- package/dist/old/useLdo.js +46 -0
- package/dist/old/useLdo.js.map +1 -0
- package/dist/useLdo.d.ts +18 -0
- package/dist/useLdo.js +46 -0
- package/dist/useLdo.js.map +1 -0
- package/dist/useLdoMethods.d.ts +12 -0
- package/dist/useLdoMethods.js +34 -0
- package/dist/useLdoMethods.js.map +1 -0
- package/dist/useResource.d.ts +11 -0
- package/dist/useResource.js +48 -0
- package/dist/useResource.js.map +1 -0
- package/dist/useRootContainer.d.ts +2 -0
- package/dist/useRootContainer.js +23 -0
- package/dist/useRootContainer.js.map +1 -0
- package/dist/useSubject.d.ts +5 -0
- package/dist/useSubject.js +36 -0
- package/dist/useSubject.js.map +1 -0
- package/dist/util/TrackingProxyContext.d.ts +15 -0
- package/dist/util/TrackingProxyContext.js +74 -0
- package/dist/util/TrackingProxyContext.js.map +1 -0
- package/dist/util/changesToSparqlUpdate.d.ts +3 -0
- package/dist/util/changesToSparqlUpdate.js +26 -0
- package/dist/util/changesToSparqlUpdate.js.map +1 -0
- package/dist/util/createGlobalHook.d.ts +6 -0
- package/dist/util/createGlobalHook.js +18 -0
- package/dist/util/createGlobalHook.js.map +1 -0
- package/dist/util/createWrapperProxy.d.ts +2 -0
- package/dist/util/createWrapperProxy.js +8 -0
- package/dist/util/createWrapperProxy.js.map +1 -0
- package/dist/util/splitChangesByGraph.d.ts +6 -0
- package/dist/util/splitChangesByGraph.js +45 -0
- package/dist/util/splitChangesByGraph.js.map +1 -0
- package/dist/util/useForceReload.d.ts +1 -0
- package/dist/util/useForceReload.js +10 -0
- package/dist/util/useForceReload.js.map +1 -0
- package/package.json +51 -0
- package/src/BrowserSolidLdoProvider.tsx +91 -0
- package/src/SolidAuthContext.ts +26 -0
- package/src/SolidLdoProvider.tsx +55 -0
- package/src/index.ts +9 -0
- package/src/useLdoMethods.ts +92 -0
- package/src/useResource.ts +97 -0
- package/src/useRootContainer.ts +24 -0
- package/src/useSubject.ts +67 -0
- package/src/util/TrackingProxyContext.ts +99 -0
- package/src/util/createWrapperProxy.ts +7 -0
- package/src/util/useForceReload.ts +6 -0
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import React, { createContext, useContext } from "react";
|
|
2
|
+
import {
|
|
3
|
+
useMemo,
|
|
4
|
+
type FunctionComponent,
|
|
5
|
+
type PropsWithChildren,
|
|
6
|
+
useEffect,
|
|
7
|
+
} from "react";
|
|
8
|
+
import { useSolidAuth } from "./SolidAuthContext";
|
|
9
|
+
import type { SolidLdoDataset } from "@ldo/solid";
|
|
10
|
+
import { createSolidLdoDataset } from "@ldo/solid";
|
|
11
|
+
import type { UseLdoMethods } from "./useLdoMethods";
|
|
12
|
+
import { createUseLdoMethods } from "./useLdoMethods";
|
|
13
|
+
|
|
14
|
+
export const SolidLdoReactContext =
|
|
15
|
+
// This will be set in the provider
|
|
16
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
17
|
+
// @ts-ignore
|
|
18
|
+
createContext<UseLdoMethods>(undefined);
|
|
19
|
+
|
|
20
|
+
export function useLdo(): UseLdoMethods {
|
|
21
|
+
return useContext(SolidLdoReactContext);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface SolidLdoProviderProps extends PropsWithChildren {}
|
|
25
|
+
|
|
26
|
+
export const SolidLdoProvider: FunctionComponent<SolidLdoProviderProps> = ({
|
|
27
|
+
children,
|
|
28
|
+
}) => {
|
|
29
|
+
const { fetch } = useSolidAuth();
|
|
30
|
+
|
|
31
|
+
// Initialize storeDependencies before render
|
|
32
|
+
const solidLdoDataset: SolidLdoDataset = useMemo(() => {
|
|
33
|
+
const ldoDataset = createSolidLdoDataset({
|
|
34
|
+
fetch,
|
|
35
|
+
});
|
|
36
|
+
ldoDataset.setMaxListeners(1000);
|
|
37
|
+
return ldoDataset;
|
|
38
|
+
}, []);
|
|
39
|
+
|
|
40
|
+
// Keep context in sync with props
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
solidLdoDataset.context.fetch = fetch;
|
|
43
|
+
}, [fetch]);
|
|
44
|
+
|
|
45
|
+
const value: UseLdoMethods = useMemo(
|
|
46
|
+
() => createUseLdoMethods(solidLdoDataset),
|
|
47
|
+
[solidLdoDataset],
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
return (
|
|
51
|
+
<SolidLdoReactContext.Provider value={value}>
|
|
52
|
+
{children}
|
|
53
|
+
</SolidLdoReactContext.Provider>
|
|
54
|
+
);
|
|
55
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { LdoBase, ShapeType } from "@ldo/ldo";
|
|
2
|
+
import { transactionChanges } from "@ldo/ldo";
|
|
3
|
+
import { write } from "@ldo/ldo";
|
|
4
|
+
import { startTransaction } from "@ldo/ldo";
|
|
5
|
+
import type { DatasetChanges, SubjectNode } from "@ldo/rdf-utils";
|
|
6
|
+
import type { Resource, SolidLdoDataset } from "@ldo/solid";
|
|
7
|
+
import type { Quad } from "@rdfjs/types";
|
|
8
|
+
|
|
9
|
+
export interface UseLdoMethods {
|
|
10
|
+
dataset: SolidLdoDataset;
|
|
11
|
+
getResource: SolidLdoDataset["getResource"];
|
|
12
|
+
getSubject<Type extends LdoBase>(
|
|
13
|
+
shapeType: ShapeType<Type>,
|
|
14
|
+
subject: string | SubjectNode,
|
|
15
|
+
): Type | Error;
|
|
16
|
+
createData<Type extends LdoBase>(
|
|
17
|
+
shapeType: ShapeType<Type>,
|
|
18
|
+
subject: string | SubjectNode,
|
|
19
|
+
...resources: Resource[]
|
|
20
|
+
): Type;
|
|
21
|
+
changeData<Type extends LdoBase>(input: Type, ...resources: Resource[]): Type;
|
|
22
|
+
commitData(input: LdoBase): ReturnType<SolidLdoDataset["commitChangesToPod"]>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function createUseLdoMethods(dataset: SolidLdoDataset): UseLdoMethods {
|
|
26
|
+
return {
|
|
27
|
+
dataset: dataset,
|
|
28
|
+
/**
|
|
29
|
+
* Gets a resource
|
|
30
|
+
*/
|
|
31
|
+
getResource: dataset.getResource.bind(dataset),
|
|
32
|
+
/**
|
|
33
|
+
* Returns a Linked Data Object for a subject
|
|
34
|
+
* @param shapeType The shape type for the data
|
|
35
|
+
* @param subject Subject Node
|
|
36
|
+
* @returns A Linked Data Object
|
|
37
|
+
*/
|
|
38
|
+
getSubject<Type extends LdoBase>(
|
|
39
|
+
shapeType: ShapeType<Type>,
|
|
40
|
+
subject: string | SubjectNode,
|
|
41
|
+
): Type | Error {
|
|
42
|
+
return dataset.usingType(shapeType).fromSubject(subject);
|
|
43
|
+
},
|
|
44
|
+
/**
|
|
45
|
+
* Begins tracking changes to eventually commit for a new subject
|
|
46
|
+
* @param shapeType The shape type that defines the created data
|
|
47
|
+
* @param subject The RDF subject for a Linked Data Object
|
|
48
|
+
* @param resources Any number of resources to which this data should be written
|
|
49
|
+
* @returns A Linked Data Object to modify and commit
|
|
50
|
+
*/
|
|
51
|
+
createData<Type extends LdoBase>(
|
|
52
|
+
shapeType: ShapeType<Type>,
|
|
53
|
+
subject: string | SubjectNode,
|
|
54
|
+
...resources: Resource[]
|
|
55
|
+
): Type {
|
|
56
|
+
const linkedDataObject = dataset
|
|
57
|
+
.usingType(shapeType)
|
|
58
|
+
.write(...resources.map((r) => r.uri))
|
|
59
|
+
.fromSubject(subject);
|
|
60
|
+
startTransaction(linkedDataObject);
|
|
61
|
+
return linkedDataObject;
|
|
62
|
+
},
|
|
63
|
+
/**
|
|
64
|
+
* Begins tracking changes to eventually commit
|
|
65
|
+
* @param input A linked data object to track changes on
|
|
66
|
+
* @param resources
|
|
67
|
+
*/
|
|
68
|
+
changeData<Type extends LdoBase>(
|
|
69
|
+
input: Type,
|
|
70
|
+
...resources: Resource[]
|
|
71
|
+
): Type {
|
|
72
|
+
// Clone the input and set a graph
|
|
73
|
+
const [transactionLdo] = write(...resources.map((r) => r.uri)).usingCopy(
|
|
74
|
+
input,
|
|
75
|
+
);
|
|
76
|
+
// Start a transaction with the input
|
|
77
|
+
startTransaction(transactionLdo);
|
|
78
|
+
// Return
|
|
79
|
+
return transactionLdo;
|
|
80
|
+
},
|
|
81
|
+
/**
|
|
82
|
+
* Commits the transaction to the global dataset, syncing all subscribing
|
|
83
|
+
* components and Solid Pods
|
|
84
|
+
*/
|
|
85
|
+
commitData(
|
|
86
|
+
input: LdoBase,
|
|
87
|
+
): ReturnType<SolidLdoDataset["commitChangesToPod"]> {
|
|
88
|
+
const changes = transactionChanges(input);
|
|
89
|
+
return dataset.commitChangesToPod(changes as DatasetChanges<Quad>);
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { useMemo, useEffect, useRef, useState, useCallback } from "react";
|
|
2
|
+
import type {
|
|
3
|
+
Container,
|
|
4
|
+
ContainerUri,
|
|
5
|
+
LeafUri,
|
|
6
|
+
Resource,
|
|
7
|
+
Leaf,
|
|
8
|
+
} from "@ldo/solid";
|
|
9
|
+
import { useLdo } from "./SolidLdoProvider";
|
|
10
|
+
|
|
11
|
+
export interface UseResourceOptions {
|
|
12
|
+
suppressInitialRead?: boolean;
|
|
13
|
+
reloadOnMount?: boolean;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function useResource(
|
|
17
|
+
uri: ContainerUri,
|
|
18
|
+
options?: UseResourceOptions,
|
|
19
|
+
): Container;
|
|
20
|
+
export function useResource(uri: LeafUri, options?: UseResourceOptions): Leaf;
|
|
21
|
+
export function useResource(
|
|
22
|
+
uri: string,
|
|
23
|
+
options?: UseResourceOptions,
|
|
24
|
+
): Leaf | Container;
|
|
25
|
+
export function useResource(
|
|
26
|
+
uri?: ContainerUri,
|
|
27
|
+
options?: UseResourceOptions,
|
|
28
|
+
): Container | undefined;
|
|
29
|
+
export function useResource(
|
|
30
|
+
uri?: LeafUri,
|
|
31
|
+
options?: UseResourceOptions,
|
|
32
|
+
): Leaf | undefined;
|
|
33
|
+
export function useResource(
|
|
34
|
+
uri?: string,
|
|
35
|
+
options?: UseResourceOptions,
|
|
36
|
+
): Leaf | Container | undefined;
|
|
37
|
+
export function useResource(
|
|
38
|
+
uri?: string,
|
|
39
|
+
options?: UseResourceOptions,
|
|
40
|
+
): Leaf | Container | undefined {
|
|
41
|
+
const { getResource } = useLdo();
|
|
42
|
+
|
|
43
|
+
// Get the resource
|
|
44
|
+
const resource = useMemo(() => {
|
|
45
|
+
if (uri) {
|
|
46
|
+
const resource = getResource(uri);
|
|
47
|
+
// Run read operations if necissary
|
|
48
|
+
if (!options?.suppressInitialRead) {
|
|
49
|
+
if (options?.reloadOnMount) {
|
|
50
|
+
resource.read();
|
|
51
|
+
} else {
|
|
52
|
+
resource.readIfUnfetched();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return resource;
|
|
56
|
+
}
|
|
57
|
+
return undefined;
|
|
58
|
+
}, [getResource, uri]);
|
|
59
|
+
const [resourceRepresentation, setResourceRepresentation] =
|
|
60
|
+
useState(resource);
|
|
61
|
+
const pastResource = useRef<
|
|
62
|
+
{ resource?: Resource; callback: () => void } | undefined
|
|
63
|
+
>();
|
|
64
|
+
|
|
65
|
+
// Callback function to force the react dom to reload.
|
|
66
|
+
const forceReload = useCallback(
|
|
67
|
+
// Wrap the resource in a proxy so it's techically a different object
|
|
68
|
+
() => {
|
|
69
|
+
if (resource) setResourceRepresentation(new Proxy(resource, {}));
|
|
70
|
+
},
|
|
71
|
+
[resource],
|
|
72
|
+
);
|
|
73
|
+
useEffect(() => {
|
|
74
|
+
// Remove listeners for the previous resource
|
|
75
|
+
if (pastResource.current?.resource) {
|
|
76
|
+
pastResource.current.resource.off(
|
|
77
|
+
"update",
|
|
78
|
+
pastResource.current.callback,
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
// Set a new past resource to the current resource
|
|
82
|
+
pastResource.current = { resource, callback: forceReload };
|
|
83
|
+
if (resource) {
|
|
84
|
+
// Add listener
|
|
85
|
+
resource.on("update", forceReload);
|
|
86
|
+
setResourceRepresentation(new Proxy(resource, {}));
|
|
87
|
+
|
|
88
|
+
// Unsubscribe on unmount
|
|
89
|
+
return () => {
|
|
90
|
+
resource.off("update", forceReload);
|
|
91
|
+
};
|
|
92
|
+
} else {
|
|
93
|
+
setResourceRepresentation(undefined);
|
|
94
|
+
}
|
|
95
|
+
}, [resource]);
|
|
96
|
+
return resourceRepresentation;
|
|
97
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Container, ContainerUri } from "@ldo/solid";
|
|
2
|
+
import { useEffect, useState } from "react";
|
|
3
|
+
import { useResource } from "./useResource";
|
|
4
|
+
import { useLdo } from "./SolidLdoProvider";
|
|
5
|
+
export function useRootContainerFor(uri?: string): Container | undefined {
|
|
6
|
+
const { getResource } = useLdo();
|
|
7
|
+
|
|
8
|
+
const [rootContainerUri, setRootContainerUri] = useState<
|
|
9
|
+
ContainerUri | undefined
|
|
10
|
+
>(undefined);
|
|
11
|
+
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
if (uri) {
|
|
14
|
+
const givenResource = getResource(uri);
|
|
15
|
+
givenResource.getRootContainer().then((result) => {
|
|
16
|
+
if (!result.isError) {
|
|
17
|
+
setRootContainerUri(result.uri);
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
}, [uri]);
|
|
22
|
+
|
|
23
|
+
return useResource(rootContainerUri);
|
|
24
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { SubjectNode } from "@ldo/rdf-utils";
|
|
2
|
+
import {
|
|
3
|
+
ContextUtil,
|
|
4
|
+
JsonldDatasetProxyBuilder,
|
|
5
|
+
} from "@ldo/jsonld-dataset-proxy";
|
|
6
|
+
import type { ShapeType } from "@ldo/ldo";
|
|
7
|
+
import { LdoBuilder } from "@ldo/ldo";
|
|
8
|
+
import type { LdoBase } from "@ldo/ldo";
|
|
9
|
+
import { useLdo } from "./SolidLdoProvider";
|
|
10
|
+
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
11
|
+
import { TrackingProxyContext } from "./util/TrackingProxyContext";
|
|
12
|
+
import { defaultGraph } from "@rdfjs/data-model";
|
|
13
|
+
|
|
14
|
+
export function useSubject<Type extends LdoBase>(
|
|
15
|
+
shapeType: ShapeType<Type>,
|
|
16
|
+
subject: string | SubjectNode,
|
|
17
|
+
): Type;
|
|
18
|
+
export function useSubject<Type extends LdoBase>(
|
|
19
|
+
shapeType: ShapeType<Type>,
|
|
20
|
+
subject?: string | SubjectNode,
|
|
21
|
+
): Type | undefined;
|
|
22
|
+
export function useSubject<Type extends LdoBase>(
|
|
23
|
+
shapeType: ShapeType<Type>,
|
|
24
|
+
subject?: string | SubjectNode,
|
|
25
|
+
): Type | undefined {
|
|
26
|
+
const { dataset } = useLdo();
|
|
27
|
+
|
|
28
|
+
const [forceUpdateCounter, setForceUpdateCounter] = useState(0);
|
|
29
|
+
const forceUpdate = useCallback(
|
|
30
|
+
() => setForceUpdateCounter((val) => val + 1),
|
|
31
|
+
[],
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
// The main linked data object
|
|
35
|
+
const linkedDataObject = useMemo(() => {
|
|
36
|
+
if (!subject) return;
|
|
37
|
+
|
|
38
|
+
// Remove all current subscriptions
|
|
39
|
+
dataset.removeListenerFromAllEvents(forceUpdate);
|
|
40
|
+
|
|
41
|
+
// Rebuild the LdoBuilder from scratch to inject TrackingProxyContext
|
|
42
|
+
const contextUtil = new ContextUtil(shapeType.context);
|
|
43
|
+
const proxyContext = new TrackingProxyContext(
|
|
44
|
+
{
|
|
45
|
+
dataset,
|
|
46
|
+
contextUtil,
|
|
47
|
+
writeGraphs: [defaultGraph()],
|
|
48
|
+
languageOrdering: ["none", "en", "other"],
|
|
49
|
+
},
|
|
50
|
+
forceUpdate,
|
|
51
|
+
);
|
|
52
|
+
const builder = new LdoBuilder(
|
|
53
|
+
new JsonldDatasetProxyBuilder(proxyContext),
|
|
54
|
+
shapeType,
|
|
55
|
+
);
|
|
56
|
+
return builder.fromSubject(subject);
|
|
57
|
+
}, [shapeType, subject, dataset, forceUpdateCounter, forceUpdate]);
|
|
58
|
+
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
// Unregister force update listener upon unmount
|
|
61
|
+
return () => {
|
|
62
|
+
dataset.removeListenerFromAllEvents(forceUpdate);
|
|
63
|
+
};
|
|
64
|
+
}, [shapeType, subject]);
|
|
65
|
+
|
|
66
|
+
return linkedDataObject;
|
|
67
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ArrayProxyTarget,
|
|
3
|
+
SubjectProxyTarget,
|
|
4
|
+
ProxyContextOptions,
|
|
5
|
+
} from "@ldo/jsonld-dataset-proxy";
|
|
6
|
+
import { ProxyContext } from "@ldo/jsonld-dataset-proxy";
|
|
7
|
+
import type { QuadMatch } from "@ldo/rdf-utils";
|
|
8
|
+
import type { SubscribableDataset } from "@ldo/subscribable-dataset";
|
|
9
|
+
import { namedNode } from "@rdfjs/data-model";
|
|
10
|
+
import type { Quad } from "@rdfjs/types";
|
|
11
|
+
|
|
12
|
+
export interface TrackingProxyContextOptions extends ProxyContextOptions {
|
|
13
|
+
dataset: SubscribableDataset<Quad>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export class TrackingProxyContext extends ProxyContext {
|
|
17
|
+
private listener: () => void;
|
|
18
|
+
private subscribableDataset: SubscribableDataset<Quad>;
|
|
19
|
+
|
|
20
|
+
constructor(options: TrackingProxyContextOptions, listener: () => void) {
|
|
21
|
+
super(options);
|
|
22
|
+
this.subscribableDataset = options.dataset;
|
|
23
|
+
this.listener = listener;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Adds the listener to the subscribable dataset while ensuring deduping of the listener
|
|
27
|
+
private addListener(eventName: QuadMatch) {
|
|
28
|
+
const listeners = this.subscribableDataset.listeners(eventName);
|
|
29
|
+
if (!listeners.includes(this.listener)) {
|
|
30
|
+
this.subscribableDataset.on(eventName, this.listener);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
protected createSubjectHandler(): ProxyHandler<SubjectProxyTarget> {
|
|
35
|
+
const baseHandler = super.createSubjectHandler();
|
|
36
|
+
const oldGetFunction = baseHandler.get;
|
|
37
|
+
const newGetFunction: ProxyHandler<SubjectProxyTarget>["get"] = (
|
|
38
|
+
target: SubjectProxyTarget,
|
|
39
|
+
key: string | symbol,
|
|
40
|
+
receiver,
|
|
41
|
+
) => {
|
|
42
|
+
const subject = target["@id"];
|
|
43
|
+
if (typeof key === "symbol") {
|
|
44
|
+
// Do Nothing
|
|
45
|
+
} else if (key === "@id") {
|
|
46
|
+
this.addListener([subject, null, null, null]);
|
|
47
|
+
} else if (!this.contextUtil.isArray(key)) {
|
|
48
|
+
const predicate = namedNode(this.contextUtil.keyToIri(key));
|
|
49
|
+
this.addListener([subject, predicate, null, null]);
|
|
50
|
+
}
|
|
51
|
+
return oldGetFunction && oldGetFunction(target, key, receiver);
|
|
52
|
+
};
|
|
53
|
+
baseHandler.get = newGetFunction;
|
|
54
|
+
baseHandler.set = () => {
|
|
55
|
+
console.warn(
|
|
56
|
+
"You've attempted to set a value on a Linked Data Object from the useSubject, useMatchingSubject, or useMatchingObject hooks. These linked data objects should only be used to render data, not modify it. To modify data, use the `changeData` function.",
|
|
57
|
+
);
|
|
58
|
+
return true;
|
|
59
|
+
};
|
|
60
|
+
return baseHandler;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
protected createArrayHandler(): ProxyHandler<ArrayProxyTarget> {
|
|
64
|
+
const baseHandler = super.createArrayHandler();
|
|
65
|
+
const oldGetFunction = baseHandler.get;
|
|
66
|
+
const newGetFunction: ProxyHandler<ArrayProxyTarget>["get"] = (
|
|
67
|
+
target: ArrayProxyTarget,
|
|
68
|
+
key: string | symbol,
|
|
69
|
+
receiver,
|
|
70
|
+
) => {
|
|
71
|
+
if (qualifiedArrayMethods.has(key)) {
|
|
72
|
+
this.addListener([target[0][0], target[0][1], target[0][2], null]);
|
|
73
|
+
}
|
|
74
|
+
return oldGetFunction && oldGetFunction(target, key, receiver);
|
|
75
|
+
};
|
|
76
|
+
baseHandler.get = newGetFunction;
|
|
77
|
+
return baseHandler;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const qualifiedArrayMethods = new Set([
|
|
82
|
+
"forEach",
|
|
83
|
+
"map",
|
|
84
|
+
"reduce",
|
|
85
|
+
Symbol.iterator,
|
|
86
|
+
"entries",
|
|
87
|
+
"every",
|
|
88
|
+
"filter",
|
|
89
|
+
"find",
|
|
90
|
+
"findIndex",
|
|
91
|
+
"findLast",
|
|
92
|
+
"findLastIndex",
|
|
93
|
+
"includes, indexOf",
|
|
94
|
+
"keys",
|
|
95
|
+
"lastIndexOf",
|
|
96
|
+
"reduceRight",
|
|
97
|
+
"some",
|
|
98
|
+
"values",
|
|
99
|
+
]);
|