@ldo/solid-react 1.0.0-alpha.1 → 1.0.0-alpha.3
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 +10 -10
- package/dist/SolidAuthContext.js.map +1 -1
- package/dist/UnauthenticatedSolidLdoProvider.js +1 -2
- package/dist/UnauthenticatedSolidLdoProvider.js.map +1 -1
- package/dist/createBrowserSolidReactMethods.d.ts +11 -0
- package/dist/createBrowserSolidReactMethods.js +79 -0
- package/dist/createBrowserSolidReactMethods.js.map +1 -0
- package/dist/defaultIntance.d.ts +5 -0
- package/dist/defaultIntance.js +10 -0
- package/dist/defaultIntance.js.map +1 -0
- package/dist/index.d.ts +2 -9
- package/dist/index.js +2 -11
- package/dist/index.js.map +1 -1
- package/dist/useRootContainerFor.d.ts +4 -0
- package/dist/useRootContainerFor.js +25 -0
- package/dist/useRootContainerFor.js.map +1 -0
- package/package.json +14 -13
- package/src/SolidAuthContext.ts +3 -0
- package/src/UnauthenticatedSolidLdoProvider.tsx +4 -2
- package/src/createBrowserSolidReactMethods.tsx +132 -0
- package/src/defaultIntance.ts +19 -0
- package/src/index.ts +2 -12
- package/src/useRootContainerFor.ts +46 -0
- package/src/BrowserSolidLdoProvider.tsx +0 -97
- package/src/SolidLdoProvider.tsx +0 -55
- package/src/useLdoMethods.ts +0 -84
- package/src/useMatchObject.ts +0 -21
- package/src/useMatchSubject.ts +0 -21
- package/src/useResource.ts +0 -114
- package/src/useRootContainer.ts +0 -31
- package/src/useSubject.ts +0 -30
- package/src/useSubscribeToResource.ts +0 -52
- package/src/util/TrackingProxyContext.ts +0 -61
- package/src/util/TrackingSetProxy.ts +0 -56
- package/src/util/TrackingSubjectProxy.ts +0 -43
- package/src/util/useTrackingProxy.ts +0 -55
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
import React, { useCallback, useEffect, useMemo, useState } from "react";
|
|
2
|
-
import type { FunctionComponent, PropsWithChildren } from "react";
|
|
3
|
-
import type { LoginOptions, SessionInfo } from "./SolidAuthContext";
|
|
4
|
-
import { SolidAuthContext } from "./SolidAuthContext";
|
|
5
|
-
import {
|
|
6
|
-
getDefaultSession,
|
|
7
|
-
handleIncomingRedirect,
|
|
8
|
-
login as libraryLogin,
|
|
9
|
-
logout as libraryLogout,
|
|
10
|
-
fetch as libraryFetch,
|
|
11
|
-
} from "@inrupt/solid-client-authn-browser";
|
|
12
|
-
import { SolidLdoProvider } from "./SolidLdoProvider";
|
|
13
|
-
|
|
14
|
-
const PRE_REDIRECT_URI = "PRE_REDIRECT_URI";
|
|
15
|
-
|
|
16
|
-
export const BrowserSolidLdoProvider: FunctionComponent<PropsWithChildren> = ({
|
|
17
|
-
children,
|
|
18
|
-
}) => {
|
|
19
|
-
const [session, setSession] = useState<SessionInfo>(getDefaultSession().info);
|
|
20
|
-
const [ranInitialAuthCheck, setRanInitialAuthCheck] = useState(false);
|
|
21
|
-
|
|
22
|
-
const runInitialAuthCheck = useCallback(async () => {
|
|
23
|
-
if (!window.localStorage.getItem(PRE_REDIRECT_URI)) {
|
|
24
|
-
window.localStorage.setItem(PRE_REDIRECT_URI, window.location.href);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
await handleIncomingRedirect({
|
|
28
|
-
restorePreviousSession: true,
|
|
29
|
-
});
|
|
30
|
-
// Set timout to ensure this happens after the redirect
|
|
31
|
-
setTimeout(() => {
|
|
32
|
-
setSession({ ...getDefaultSession().info });
|
|
33
|
-
window.history.replaceState(
|
|
34
|
-
{},
|
|
35
|
-
"",
|
|
36
|
-
window.localStorage.getItem(PRE_REDIRECT_URI),
|
|
37
|
-
);
|
|
38
|
-
window.localStorage.removeItem(PRE_REDIRECT_URI);
|
|
39
|
-
|
|
40
|
-
setRanInitialAuthCheck(true);
|
|
41
|
-
}, 0);
|
|
42
|
-
}, []);
|
|
43
|
-
|
|
44
|
-
const login = useCallback(async (issuer: string, options?: LoginOptions) => {
|
|
45
|
-
const cleanUrl = new URL(window.location.href);
|
|
46
|
-
cleanUrl.hash = "";
|
|
47
|
-
cleanUrl.search = "";
|
|
48
|
-
const fullOptions = {
|
|
49
|
-
redirectUrl: cleanUrl.toString(),
|
|
50
|
-
clientName: "Solid App",
|
|
51
|
-
oidcIssuer: issuer,
|
|
52
|
-
...options,
|
|
53
|
-
};
|
|
54
|
-
window.localStorage.setItem(PRE_REDIRECT_URI, window.location.href);
|
|
55
|
-
await libraryLogin(fullOptions);
|
|
56
|
-
setSession({ ...getDefaultSession().info });
|
|
57
|
-
}, []);
|
|
58
|
-
|
|
59
|
-
const logout = useCallback(async () => {
|
|
60
|
-
await libraryLogout();
|
|
61
|
-
setSession({ ...getDefaultSession().info });
|
|
62
|
-
}, []);
|
|
63
|
-
|
|
64
|
-
const signUp = useCallback(
|
|
65
|
-
async (issuer: string, options?: LoginOptions) => {
|
|
66
|
-
// The typings on @inrupt/solid-client-authn-core have not yet been updated
|
|
67
|
-
// TODO: remove this ts-ignore when they are updated.
|
|
68
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
69
|
-
// @ts-ignore
|
|
70
|
-
return login(issuer, { ...options, prompt: "create" });
|
|
71
|
-
},
|
|
72
|
-
[login],
|
|
73
|
-
);
|
|
74
|
-
|
|
75
|
-
useEffect(() => {
|
|
76
|
-
runInitialAuthCheck();
|
|
77
|
-
}, []);
|
|
78
|
-
|
|
79
|
-
const solidAuthFunctions = useMemo(
|
|
80
|
-
() => ({
|
|
81
|
-
runInitialAuthCheck,
|
|
82
|
-
login,
|
|
83
|
-
logout,
|
|
84
|
-
signUp,
|
|
85
|
-
session,
|
|
86
|
-
ranInitialAuthCheck,
|
|
87
|
-
fetch: libraryFetch,
|
|
88
|
-
}),
|
|
89
|
-
[login, logout, ranInitialAuthCheck, runInitialAuthCheck, session, signUp],
|
|
90
|
-
);
|
|
91
|
-
|
|
92
|
-
return (
|
|
93
|
-
<SolidAuthContext.Provider value={solidAuthFunctions}>
|
|
94
|
-
<SolidLdoProvider>{children}</SolidLdoProvider>
|
|
95
|
-
</SolidAuthContext.Provider>
|
|
96
|
-
);
|
|
97
|
-
};
|
package/src/SolidLdoProvider.tsx
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
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/useLdoMethods.ts
DELETED
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
import type { LdoBase, ShapeType } from "@ldo/ldo";
|
|
2
|
-
import type { SubjectNode } from "@ldo/rdf-utils";
|
|
3
|
-
import type {
|
|
4
|
-
Resource,
|
|
5
|
-
SolidLdoDataset,
|
|
6
|
-
SolidLdoTransactionDataset,
|
|
7
|
-
} from "@ldo/solid";
|
|
8
|
-
import { changeData, commitData } from "@ldo/solid";
|
|
9
|
-
|
|
10
|
-
export interface UseLdoMethods {
|
|
11
|
-
dataset: SolidLdoDataset;
|
|
12
|
-
getResource: SolidLdoDataset["getResource"];
|
|
13
|
-
getSubject<Type extends LdoBase>(
|
|
14
|
-
shapeType: ShapeType<Type>,
|
|
15
|
-
subject: string | SubjectNode,
|
|
16
|
-
): Type;
|
|
17
|
-
createData<Type extends LdoBase>(
|
|
18
|
-
shapeType: ShapeType<Type>,
|
|
19
|
-
subject: string | SubjectNode,
|
|
20
|
-
resource: Resource,
|
|
21
|
-
...additionalResources: Resource[]
|
|
22
|
-
): Type;
|
|
23
|
-
changeData<Type extends LdoBase>(
|
|
24
|
-
input: Type,
|
|
25
|
-
resource: Resource,
|
|
26
|
-
...additionalResources: Resource[]
|
|
27
|
-
): Type;
|
|
28
|
-
commitData(
|
|
29
|
-
input: LdoBase,
|
|
30
|
-
): ReturnType<SolidLdoTransactionDataset["commitToPod"]>;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export function createUseLdoMethods(dataset: SolidLdoDataset): UseLdoMethods {
|
|
34
|
-
return {
|
|
35
|
-
dataset: dataset,
|
|
36
|
-
/**
|
|
37
|
-
* Gets a resource
|
|
38
|
-
*/
|
|
39
|
-
getResource: dataset.getResource.bind(dataset),
|
|
40
|
-
/**
|
|
41
|
-
* Returns a Linked Data Object for a subject
|
|
42
|
-
* @param shapeType The shape type for the data
|
|
43
|
-
* @param subject Subject Node
|
|
44
|
-
* @returns A Linked Data Object
|
|
45
|
-
*/
|
|
46
|
-
getSubject<Type extends LdoBase>(
|
|
47
|
-
shapeType: ShapeType<Type>,
|
|
48
|
-
subject: string | SubjectNode,
|
|
49
|
-
): Type {
|
|
50
|
-
return dataset.usingType(shapeType).fromSubject(subject);
|
|
51
|
-
},
|
|
52
|
-
/**
|
|
53
|
-
* Begins tracking changes to eventually commit for a new subject
|
|
54
|
-
* @param shapeType The shape type that defines the created data
|
|
55
|
-
* @param subject The RDF subject for a Linked Data Object
|
|
56
|
-
* @param resources Any number of resources to which this data should be written
|
|
57
|
-
* @returns A Linked Data Object to modify and commit
|
|
58
|
-
*/
|
|
59
|
-
createData<Type extends LdoBase>(
|
|
60
|
-
shapeType: ShapeType<Type>,
|
|
61
|
-
subject: string | SubjectNode,
|
|
62
|
-
resource: Resource,
|
|
63
|
-
...additionalResources: Resource[]
|
|
64
|
-
): Type {
|
|
65
|
-
return dataset.createData(
|
|
66
|
-
shapeType,
|
|
67
|
-
subject,
|
|
68
|
-
resource,
|
|
69
|
-
...additionalResources,
|
|
70
|
-
);
|
|
71
|
-
},
|
|
72
|
-
/**
|
|
73
|
-
* Begins tracking changes to eventually commit
|
|
74
|
-
* @param input A linked data object to track changes on
|
|
75
|
-
* @param resources
|
|
76
|
-
*/
|
|
77
|
-
changeData: changeData,
|
|
78
|
-
/**
|
|
79
|
-
* Commits the transaction to the global dataset, syncing all subscribing
|
|
80
|
-
* components and Solid Pods
|
|
81
|
-
*/
|
|
82
|
-
commitData: commitData,
|
|
83
|
-
};
|
|
84
|
-
}
|
package/src/useMatchObject.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import type { LdoBase, LdSet, ShapeType } from "@ldo/ldo";
|
|
2
|
-
import type { QuadMatch } from "@ldo/rdf-utils";
|
|
3
|
-
import type { LdoBuilder } from "@ldo/ldo";
|
|
4
|
-
import { useCallback } from "react";
|
|
5
|
-
import { useTrackingProxy } from "./util/useTrackingProxy";
|
|
6
|
-
|
|
7
|
-
export function useMatchObject<Type extends LdoBase>(
|
|
8
|
-
shapeType: ShapeType<Type>,
|
|
9
|
-
subject?: QuadMatch[0] | string,
|
|
10
|
-
predicate?: QuadMatch[1] | string,
|
|
11
|
-
graph?: QuadMatch[3] | string,
|
|
12
|
-
): LdSet<Type> {
|
|
13
|
-
const matchObject = useCallback(
|
|
14
|
-
(builder: LdoBuilder<Type>) => {
|
|
15
|
-
return builder.matchObject(subject, predicate, graph);
|
|
16
|
-
},
|
|
17
|
-
[subject, predicate, graph],
|
|
18
|
-
);
|
|
19
|
-
|
|
20
|
-
return useTrackingProxy(shapeType, matchObject);
|
|
21
|
-
}
|
package/src/useMatchSubject.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import type { LdoBase, LdSet, ShapeType } from "@ldo/ldo";
|
|
2
|
-
import type { QuadMatch } from "@ldo/rdf-utils";
|
|
3
|
-
import type { LdoBuilder } from "@ldo/ldo";
|
|
4
|
-
import { useCallback } from "react";
|
|
5
|
-
import { useTrackingProxy } from "./util/useTrackingProxy";
|
|
6
|
-
|
|
7
|
-
export function useMatchSubject<Type extends LdoBase>(
|
|
8
|
-
shapeType: ShapeType<Type>,
|
|
9
|
-
predicate?: QuadMatch[1] | string,
|
|
10
|
-
object?: QuadMatch[2] | string,
|
|
11
|
-
graph?: QuadMatch[3] | string,
|
|
12
|
-
): LdSet<Type> {
|
|
13
|
-
const matchSubject = useCallback(
|
|
14
|
-
(builder: LdoBuilder<Type>) => {
|
|
15
|
-
return builder.matchSubject(predicate, object, graph);
|
|
16
|
-
},
|
|
17
|
-
[predicate, object, graph],
|
|
18
|
-
);
|
|
19
|
-
|
|
20
|
-
return useTrackingProxy(shapeType, matchSubject);
|
|
21
|
-
}
|
package/src/useResource.ts
DELETED
|
@@ -1,114 +0,0 @@
|
|
|
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
|
-
subscribe?: boolean;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export function useResource(
|
|
18
|
-
uri: ContainerUri,
|
|
19
|
-
options?: UseResourceOptions,
|
|
20
|
-
): Container;
|
|
21
|
-
export function useResource(uri: LeafUri, options?: UseResourceOptions): Leaf;
|
|
22
|
-
export function useResource(
|
|
23
|
-
uri: string,
|
|
24
|
-
options?: UseResourceOptions,
|
|
25
|
-
): Leaf | Container;
|
|
26
|
-
export function useResource(
|
|
27
|
-
uri?: ContainerUri,
|
|
28
|
-
options?: UseResourceOptions,
|
|
29
|
-
): Container | undefined;
|
|
30
|
-
export function useResource(
|
|
31
|
-
uri?: LeafUri,
|
|
32
|
-
options?: UseResourceOptions,
|
|
33
|
-
): Leaf | undefined;
|
|
34
|
-
export function useResource(
|
|
35
|
-
uri?: string,
|
|
36
|
-
options?: UseResourceOptions,
|
|
37
|
-
): Leaf | Container | undefined;
|
|
38
|
-
export function useResource(
|
|
39
|
-
uri?: string,
|
|
40
|
-
options?: UseResourceOptions,
|
|
41
|
-
): Leaf | Container | undefined {
|
|
42
|
-
const { getResource } = useLdo();
|
|
43
|
-
const subscriptionIdRef = useRef<string | undefined>();
|
|
44
|
-
|
|
45
|
-
// Get the resource
|
|
46
|
-
const resource = useMemo(() => {
|
|
47
|
-
if (uri) {
|
|
48
|
-
const resource = getResource(uri);
|
|
49
|
-
// Run read operations if necissary
|
|
50
|
-
if (!options?.suppressInitialRead) {
|
|
51
|
-
if (options?.reloadOnMount) {
|
|
52
|
-
resource.read();
|
|
53
|
-
} else {
|
|
54
|
-
resource.readIfUnfetched();
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
return resource;
|
|
58
|
-
}
|
|
59
|
-
return undefined;
|
|
60
|
-
}, [getResource, uri]);
|
|
61
|
-
const [resourceRepresentation, setResourceRepresentation] =
|
|
62
|
-
useState(resource);
|
|
63
|
-
const pastResource = useRef<
|
|
64
|
-
{ resource?: Resource; callback: () => void } | undefined
|
|
65
|
-
>();
|
|
66
|
-
|
|
67
|
-
useEffect(() => {
|
|
68
|
-
if (options?.subscribe) {
|
|
69
|
-
resource
|
|
70
|
-
?.subscribeToNotifications()
|
|
71
|
-
.then((subscriptionId) => (subscriptionIdRef.current = subscriptionId));
|
|
72
|
-
} else if (subscriptionIdRef.current) {
|
|
73
|
-
resource?.unsubscribeFromNotifications(subscriptionIdRef.current);
|
|
74
|
-
}
|
|
75
|
-
return () => {
|
|
76
|
-
if (subscriptionIdRef.current)
|
|
77
|
-
resource?.unsubscribeFromNotifications(subscriptionIdRef.current);
|
|
78
|
-
};
|
|
79
|
-
}, [resource, options?.subscribe]);
|
|
80
|
-
|
|
81
|
-
// Callback function to force the react dom to reload.
|
|
82
|
-
const forceReload = useCallback(
|
|
83
|
-
// Wrap the resource in a proxy so it's techically a different object
|
|
84
|
-
() => {
|
|
85
|
-
if (resource) setResourceRepresentation(new Proxy(resource, {}));
|
|
86
|
-
},
|
|
87
|
-
[resource],
|
|
88
|
-
);
|
|
89
|
-
|
|
90
|
-
useEffect(() => {
|
|
91
|
-
// Remove listeners for the previous resource
|
|
92
|
-
if (pastResource.current?.resource) {
|
|
93
|
-
pastResource.current.resource.off(
|
|
94
|
-
"update",
|
|
95
|
-
pastResource.current.callback,
|
|
96
|
-
);
|
|
97
|
-
}
|
|
98
|
-
// Set a new past resource to the current resource
|
|
99
|
-
pastResource.current = { resource, callback: forceReload };
|
|
100
|
-
if (resource) {
|
|
101
|
-
// Add listener
|
|
102
|
-
resource.on("update", forceReload);
|
|
103
|
-
setResourceRepresentation(new Proxy(resource, {}));
|
|
104
|
-
|
|
105
|
-
// Unsubscribe on unmount
|
|
106
|
-
return () => {
|
|
107
|
-
resource.off("update", forceReload);
|
|
108
|
-
};
|
|
109
|
-
} else {
|
|
110
|
-
setResourceRepresentation(undefined);
|
|
111
|
-
}
|
|
112
|
-
}, [resource]);
|
|
113
|
-
return resourceRepresentation;
|
|
114
|
-
}
|
package/src/useRootContainer.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import type { Container, ContainerUri } from "@ldo/solid";
|
|
2
|
-
import { useEffect, useState } from "react";
|
|
3
|
-
import type { UseResourceOptions } from "./useResource";
|
|
4
|
-
import { useResource } from "./useResource";
|
|
5
|
-
import { useLdo } from "./SolidLdoProvider";
|
|
6
|
-
|
|
7
|
-
export function useRootContainerFor(
|
|
8
|
-
uri?: string,
|
|
9
|
-
options?: UseResourceOptions,
|
|
10
|
-
): Container | undefined {
|
|
11
|
-
const { getResource } = useLdo();
|
|
12
|
-
|
|
13
|
-
const [rootContainerUri, setRootContainerUri] = useState<
|
|
14
|
-
ContainerUri | undefined
|
|
15
|
-
>(undefined);
|
|
16
|
-
|
|
17
|
-
useEffect(() => {
|
|
18
|
-
if (uri) {
|
|
19
|
-
const givenResource = getResource(uri);
|
|
20
|
-
givenResource.getRootContainer().then((result) => {
|
|
21
|
-
if (!result.isError) {
|
|
22
|
-
setRootContainerUri(result.uri);
|
|
23
|
-
}
|
|
24
|
-
});
|
|
25
|
-
} else {
|
|
26
|
-
setRootContainerUri(undefined);
|
|
27
|
-
}
|
|
28
|
-
}, [uri]);
|
|
29
|
-
|
|
30
|
-
return useResource(rootContainerUri, options);
|
|
31
|
-
}
|
package/src/useSubject.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import type { SubjectNode } from "@ldo/rdf-utils";
|
|
2
|
-
import type { ShapeType } from "@ldo/ldo";
|
|
3
|
-
import type { LdoBuilder } from "@ldo/ldo";
|
|
4
|
-
import type { LdoBase } from "@ldo/ldo";
|
|
5
|
-
import { useCallback } from "react";
|
|
6
|
-
|
|
7
|
-
import { useTrackingProxy } from "./util/useTrackingProxy";
|
|
8
|
-
|
|
9
|
-
export function useSubject<Type extends LdoBase>(
|
|
10
|
-
shapeType: ShapeType<Type>,
|
|
11
|
-
subject: string | SubjectNode,
|
|
12
|
-
): Type;
|
|
13
|
-
export function useSubject<Type extends LdoBase>(
|
|
14
|
-
shapeType: ShapeType<Type>,
|
|
15
|
-
subject?: string | SubjectNode,
|
|
16
|
-
): Type | undefined;
|
|
17
|
-
export function useSubject<Type extends LdoBase>(
|
|
18
|
-
shapeType: ShapeType<Type>,
|
|
19
|
-
subject?: string | SubjectNode,
|
|
20
|
-
): Type | undefined {
|
|
21
|
-
const fromSubject = useCallback(
|
|
22
|
-
(builder: LdoBuilder<Type>) => {
|
|
23
|
-
if (!subject) return;
|
|
24
|
-
return builder.fromSubject(subject);
|
|
25
|
-
},
|
|
26
|
-
[subject],
|
|
27
|
-
);
|
|
28
|
-
|
|
29
|
-
return useTrackingProxy(shapeType, fromSubject);
|
|
30
|
-
}
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { useLdo } from "./SolidLdoProvider";
|
|
2
|
-
import { useEffect, useRef } from "react";
|
|
3
|
-
|
|
4
|
-
export function useSubscribeToResource(...uris: string[]): void {
|
|
5
|
-
const { dataset } = useLdo();
|
|
6
|
-
const currentlySubscribed = useRef<Record<string, string>>({});
|
|
7
|
-
useEffect(() => {
|
|
8
|
-
const resources = uris.map((uri) => dataset.getResource(uri));
|
|
9
|
-
const previousSubscriptions = { ...currentlySubscribed.current };
|
|
10
|
-
Promise.all<void>(
|
|
11
|
-
resources.map(async (resource) => {
|
|
12
|
-
if (!previousSubscriptions[resource.uri]) {
|
|
13
|
-
// Prevent multiple triggers from created subscriptions while waiting
|
|
14
|
-
// for connection
|
|
15
|
-
currentlySubscribed.current[resource.uri] = "AWAITING";
|
|
16
|
-
// Read and subscribe
|
|
17
|
-
await resource.readIfUnfetched();
|
|
18
|
-
currentlySubscribed.current[resource.uri] =
|
|
19
|
-
await resource.subscribeToNotifications();
|
|
20
|
-
} else {
|
|
21
|
-
delete previousSubscriptions[resource.uri];
|
|
22
|
-
}
|
|
23
|
-
}),
|
|
24
|
-
).then(async () => {
|
|
25
|
-
// Unsubscribe from all remaining previous subscriptions
|
|
26
|
-
await Promise.all(
|
|
27
|
-
Object.entries(previousSubscriptions).map(
|
|
28
|
-
async ([resourceUri, subscriptionId]) => {
|
|
29
|
-
// Unsubscribe
|
|
30
|
-
delete currentlySubscribed.current[resourceUri];
|
|
31
|
-
const resource = dataset.getResource(resourceUri);
|
|
32
|
-
await resource.unsubscribeFromNotifications(subscriptionId);
|
|
33
|
-
},
|
|
34
|
-
),
|
|
35
|
-
);
|
|
36
|
-
});
|
|
37
|
-
}, [uris]);
|
|
38
|
-
|
|
39
|
-
// Cleanup Subscriptions
|
|
40
|
-
useEffect(() => {
|
|
41
|
-
return () => {
|
|
42
|
-
Promise.all(
|
|
43
|
-
Object.entries(currentlySubscribed.current).map(
|
|
44
|
-
async ([resourceUri, subscriptionId]) => {
|
|
45
|
-
const resource = dataset.getResource(resourceUri);
|
|
46
|
-
await resource.unsubscribeFromNotifications(subscriptionId);
|
|
47
|
-
},
|
|
48
|
-
),
|
|
49
|
-
);
|
|
50
|
-
};
|
|
51
|
-
}, []);
|
|
52
|
-
}
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
ProxyContextOptions,
|
|
3
|
-
SubjectProxy,
|
|
4
|
-
SetProxy,
|
|
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 type { BlankNode, NamedNode, Quad } from "@rdfjs/types";
|
|
10
|
-
import { createTrackingSubjectProxy } from "./TrackingSubjectProxy";
|
|
11
|
-
import { createTrackingSetProxy } from "./TrackingSetProxy";
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* @internal
|
|
15
|
-
* Options to be passed to the tracking proxy
|
|
16
|
-
*/
|
|
17
|
-
export interface TrackingProxyContextOptions extends ProxyContextOptions {
|
|
18
|
-
dataset: SubscribableDataset<Quad>;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* @internal
|
|
23
|
-
* This proxy exists to ensure react components rerender at the right time. It
|
|
24
|
-
* keeps track of every key accessed in a Linked Data Object and only when the
|
|
25
|
-
* dataset is updated with that key does it rerender the react component.
|
|
26
|
-
*/
|
|
27
|
-
export class TrackingProxyContext extends ProxyContext {
|
|
28
|
-
private listener: () => void;
|
|
29
|
-
private subscribableDataset: SubscribableDataset<Quad>;
|
|
30
|
-
|
|
31
|
-
constructor(options: TrackingProxyContextOptions, listener: () => void) {
|
|
32
|
-
super(options);
|
|
33
|
-
this.subscribableDataset = options.dataset;
|
|
34
|
-
this.listener = listener;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// Adds the listener to the subscribable dataset while ensuring deduping of the listener
|
|
38
|
-
public addListener(eventName: QuadMatch) {
|
|
39
|
-
const listeners = this.subscribableDataset.listeners(eventName);
|
|
40
|
-
if (!listeners.includes(this.listener)) {
|
|
41
|
-
this.subscribableDataset.on(eventName, this.listener);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
protected createNewSubjectProxy(node: NamedNode | BlankNode): SubjectProxy {
|
|
46
|
-
return createTrackingSubjectProxy(this, node);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
protected createNewSetProxy(
|
|
50
|
-
quadMatch: QuadMatch,
|
|
51
|
-
isSubjectOriented?: boolean,
|
|
52
|
-
isLangStringSet?: boolean,
|
|
53
|
-
): SetProxy {
|
|
54
|
-
return createTrackingSetProxy(
|
|
55
|
-
this,
|
|
56
|
-
quadMatch,
|
|
57
|
-
isSubjectOriented,
|
|
58
|
-
isLangStringSet,
|
|
59
|
-
);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { createNewSetProxy, type SetProxy } from "@ldo/jsonld-dataset-proxy";
|
|
2
|
-
import type { TrackingProxyContext } from "./TrackingProxyContext";
|
|
3
|
-
import type { QuadMatch } from "@ldo/rdf-utils";
|
|
4
|
-
|
|
5
|
-
export function createTrackingSetProxy(
|
|
6
|
-
proxyContext: TrackingProxyContext,
|
|
7
|
-
quadMatch: QuadMatch,
|
|
8
|
-
isSubjectOriented?: boolean,
|
|
9
|
-
isLangStringSet?: boolean,
|
|
10
|
-
): SetProxy {
|
|
11
|
-
const baseSetProxy = createNewSetProxy(
|
|
12
|
-
quadMatch,
|
|
13
|
-
isSubjectOriented ?? false,
|
|
14
|
-
proxyContext,
|
|
15
|
-
isLangStringSet,
|
|
16
|
-
);
|
|
17
|
-
|
|
18
|
-
return new Proxy(baseSetProxy, {
|
|
19
|
-
get: (target: SetProxy, key: string | symbol, receiver) => {
|
|
20
|
-
if (trackingMethods.has(key)) {
|
|
21
|
-
proxyContext.addListener(quadMatch);
|
|
22
|
-
} else if (disallowedMethods.has(key)) {
|
|
23
|
-
console.warn(
|
|
24
|
-
"You've attempted to modify 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.",
|
|
25
|
-
);
|
|
26
|
-
}
|
|
27
|
-
return Reflect.get(target, key, receiver);
|
|
28
|
-
},
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const trackingMethods = new Set([
|
|
33
|
-
"has",
|
|
34
|
-
"size",
|
|
35
|
-
"entries",
|
|
36
|
-
"keys",
|
|
37
|
-
"values",
|
|
38
|
-
Symbol.iterator,
|
|
39
|
-
"every",
|
|
40
|
-
"every",
|
|
41
|
-
"some",
|
|
42
|
-
"forEach",
|
|
43
|
-
"map",
|
|
44
|
-
"reduce",
|
|
45
|
-
"toArray",
|
|
46
|
-
"toJSON",
|
|
47
|
-
"difference",
|
|
48
|
-
"intersection",
|
|
49
|
-
"isDisjointFrom",
|
|
50
|
-
"isSubsetOf",
|
|
51
|
-
"isSupersetOf",
|
|
52
|
-
"symmetricDifference",
|
|
53
|
-
"union",
|
|
54
|
-
]);
|
|
55
|
-
|
|
56
|
-
const disallowedMethods = new Set<string | symbol>(["add", "clear", "delete"]);
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import type { SubjectProxyTarget } from "@ldo/jsonld-dataset-proxy";
|
|
2
|
-
import {
|
|
3
|
-
createSubjectHandler,
|
|
4
|
-
type SubjectProxy,
|
|
5
|
-
} from "@ldo/jsonld-dataset-proxy";
|
|
6
|
-
import type { BlankNode, NamedNode } from "@rdfjs/types";
|
|
7
|
-
import type { TrackingProxyContext } from "./TrackingProxyContext";
|
|
8
|
-
import { namedNode } from "@rdfjs/data-model";
|
|
9
|
-
|
|
10
|
-
export function createTrackingSubjectProxy(
|
|
11
|
-
proxyContext: TrackingProxyContext,
|
|
12
|
-
node: NamedNode | BlankNode,
|
|
13
|
-
): SubjectProxy {
|
|
14
|
-
const baseHandler = createSubjectHandler(proxyContext);
|
|
15
|
-
const oldGetFunction = baseHandler.get;
|
|
16
|
-
const newGetFunction: ProxyHandler<SubjectProxyTarget>["get"] = (
|
|
17
|
-
target: SubjectProxyTarget,
|
|
18
|
-
key: string | symbol,
|
|
19
|
-
receiver,
|
|
20
|
-
) => {
|
|
21
|
-
const subject = target["@id"];
|
|
22
|
-
const rdfTypes = proxyContext.getRdfType(subject);
|
|
23
|
-
if (typeof key === "symbol") {
|
|
24
|
-
// Do Nothing
|
|
25
|
-
} else if (key === "@id") {
|
|
26
|
-
proxyContext.addListener([subject, null, null, null]);
|
|
27
|
-
} else if (!proxyContext.contextUtil.isSet(key, rdfTypes)) {
|
|
28
|
-
const predicate = namedNode(
|
|
29
|
-
proxyContext.contextUtil.keyToIri(key, rdfTypes),
|
|
30
|
-
);
|
|
31
|
-
proxyContext.addListener([subject, predicate, null, null]);
|
|
32
|
-
}
|
|
33
|
-
return oldGetFunction && oldGetFunction(target, key, receiver);
|
|
34
|
-
};
|
|
35
|
-
baseHandler.get = newGetFunction;
|
|
36
|
-
baseHandler.set = () => {
|
|
37
|
-
console.warn(
|
|
38
|
-
"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.",
|
|
39
|
-
);
|
|
40
|
-
return true;
|
|
41
|
-
};
|
|
42
|
-
return new Proxy({ "@id": node }, baseHandler) as unknown as SubjectProxy;
|
|
43
|
-
}
|