@ldo/solid-react 0.0.1-alpha.32 → 1.0.0-alpha.2

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.
Files changed (165) hide show
  1. package/README.md +127 -3
  2. package/dist/SolidAuthContext.js.map +1 -1
  3. package/dist/UnauthenticatedSolidLdoProvider.js +1 -2
  4. package/dist/UnauthenticatedSolidLdoProvider.js.map +1 -1
  5. package/dist/createBrowserSolidReactMethods.d.ts +11 -0
  6. package/dist/createBrowserSolidReactMethods.js +79 -0
  7. package/dist/createBrowserSolidReactMethods.js.map +1 -0
  8. package/dist/defaultIntance.d.ts +5 -0
  9. package/dist/defaultIntance.js +10 -0
  10. package/dist/defaultIntance.js.map +1 -0
  11. package/dist/index.d.ts +2 -9
  12. package/dist/index.js +2 -11
  13. package/dist/index.js.map +1 -1
  14. package/dist/jsonld-dataset-proxy/src/ContextUtil.d.ts +17 -0
  15. package/dist/jsonld-dataset-proxy/src/ContextUtil.js +108 -0
  16. package/dist/jsonld-dataset-proxy/src/ContextUtil.js.map +1 -0
  17. package/dist/jsonld-dataset-proxy/src/LdoJsonldContext.d.ts +8 -0
  18. package/dist/jsonld-dataset-proxy/src/LdoJsonldContext.js +3 -0
  19. package/dist/jsonld-dataset-proxy/src/LdoJsonldContext.js.map +1 -0
  20. package/dist/jsonld-dataset-proxy/src/ProxyContext.d.ts +31 -0
  21. package/dist/jsonld-dataset-proxy/src/ProxyContext.js +63 -0
  22. package/dist/jsonld-dataset-proxy/src/ProxyContext.js.map +1 -0
  23. package/dist/jsonld-dataset-proxy/src/language/languageTypes.d.ts +2 -0
  24. package/dist/jsonld-dataset-proxy/src/language/languageTypes.js +3 -0
  25. package/dist/jsonld-dataset-proxy/src/language/languageTypes.js.map +1 -0
  26. package/dist/jsonld-dataset-proxy/src/language/languageUtils.d.ts +14 -0
  27. package/dist/jsonld-dataset-proxy/src/language/languageUtils.js +73 -0
  28. package/dist/jsonld-dataset-proxy/src/language/languageUtils.js.map +1 -0
  29. package/dist/jsonld-dataset-proxy/src/setProxy/ObjectSetProxy.d.ts +17 -0
  30. package/dist/jsonld-dataset-proxy/src/setProxy/ObjectSetProxy.js +31 -0
  31. package/dist/jsonld-dataset-proxy/src/setProxy/ObjectSetProxy.js.map +1 -0
  32. package/dist/jsonld-dataset-proxy/src/setProxy/SetProxy.d.ts +29 -0
  33. package/dist/jsonld-dataset-proxy/src/setProxy/SetProxy.js +72 -0
  34. package/dist/jsonld-dataset-proxy/src/setProxy/SetProxy.js.map +1 -0
  35. package/dist/jsonld-dataset-proxy/src/setProxy/SubjectSetProxy.d.ts +15 -0
  36. package/dist/jsonld-dataset-proxy/src/setProxy/SubjectSetProxy.js +38 -0
  37. package/dist/jsonld-dataset-proxy/src/setProxy/SubjectSetProxy.js.map +1 -0
  38. package/dist/jsonld-dataset-proxy/src/setProxy/WildcardObjectSetProxy.d.ts +20 -0
  39. package/dist/jsonld-dataset-proxy/src/setProxy/WildcardObjectSetProxy.js +49 -0
  40. package/dist/jsonld-dataset-proxy/src/setProxy/WildcardObjectSetProxy.js.map +1 -0
  41. package/dist/jsonld-dataset-proxy/src/setProxy/WildcardSubjectSetProxy.d.ts +19 -0
  42. package/dist/jsonld-dataset-proxy/src/setProxy/WildcardSubjectSetProxy.js +31 -0
  43. package/dist/jsonld-dataset-proxy/src/setProxy/WildcardSubjectSetProxy.js.map +1 -0
  44. package/dist/jsonld-dataset-proxy/src/setProxy/createNewSetProxy.d.ts +5 -0
  45. package/dist/jsonld-dataset-proxy/src/setProxy/createNewSetProxy.js +27 -0
  46. package/dist/jsonld-dataset-proxy/src/setProxy/createNewSetProxy.js.map +1 -0
  47. package/dist/jsonld-dataset-proxy/src/setProxy/ldSet/BasicLdSet.d.ts +36 -0
  48. package/dist/jsonld-dataset-proxy/src/setProxy/ldSet/BasicLdSet.js +193 -0
  49. package/dist/jsonld-dataset-proxy/src/setProxy/ldSet/BasicLdSet.js.map +1 -0
  50. package/dist/jsonld-dataset-proxy/src/setProxy/ldSet/LdSet.d.ts +30 -0
  51. package/dist/jsonld-dataset-proxy/src/setProxy/ldSet/LdSet.js +3 -0
  52. package/dist/jsonld-dataset-proxy/src/setProxy/ldSet/LdSet.js.map +1 -0
  53. package/dist/jsonld-dataset-proxy/src/subjectProxy/SubjectProxy.d.ts +14 -0
  54. package/dist/jsonld-dataset-proxy/src/subjectProxy/SubjectProxy.js +3 -0
  55. package/dist/jsonld-dataset-proxy/src/subjectProxy/SubjectProxy.js.map +1 -0
  56. package/dist/jsonld-dataset-proxy/src/subjectProxy/createSubjectHandler.d.ts +6 -0
  57. package/dist/jsonld-dataset-proxy/src/subjectProxy/createSubjectHandler.js +75 -0
  58. package/dist/jsonld-dataset-proxy/src/subjectProxy/createSubjectHandler.js.map +1 -0
  59. package/dist/jsonld-dataset-proxy/src/subjectProxy/deleteFromDataset.d.ts +3 -0
  60. package/dist/jsonld-dataset-proxy/src/subjectProxy/deleteFromDataset.js +25 -0
  61. package/dist/jsonld-dataset-proxy/src/subjectProxy/deleteFromDataset.js.map +1 -0
  62. package/dist/jsonld-dataset-proxy/src/subjectProxy/getValueForKey.d.ts +5 -0
  63. package/dist/jsonld-dataset-proxy/src/subjectProxy/getValueForKey.js +41 -0
  64. package/dist/jsonld-dataset-proxy/src/subjectProxy/getValueForKey.js.map +1 -0
  65. package/dist/jsonld-dataset-proxy/src/subjectProxy/isSubjectProxy.d.ts +4 -0
  66. package/dist/jsonld-dataset-proxy/src/subjectProxy/isSubjectProxy.js +25 -0
  67. package/dist/jsonld-dataset-proxy/src/subjectProxy/isSubjectProxy.js.map +1 -0
  68. package/dist/jsonld-dataset-proxy/src/types.d.ts +9 -0
  69. package/dist/jsonld-dataset-proxy/src/types.js +11 -0
  70. package/dist/jsonld-dataset-proxy/src/types.js.map +1 -0
  71. package/dist/jsonld-dataset-proxy/src/util/NodeSet.d.ts +9 -0
  72. package/dist/jsonld-dataset-proxy/src/util/NodeSet.js +35 -0
  73. package/dist/jsonld-dataset-proxy/src/util/NodeSet.js.map +1 -0
  74. package/dist/jsonld-dataset-proxy/src/util/RawObject.d.ts +9 -0
  75. package/dist/jsonld-dataset-proxy/src/util/RawObject.js +3 -0
  76. package/dist/jsonld-dataset-proxy/src/util/RawObject.js.map +1 -0
  77. package/dist/jsonld-dataset-proxy/src/util/addObjectToDataset.d.ts +8 -0
  78. package/dist/jsonld-dataset-proxy/src/util/addObjectToDataset.js +83 -0
  79. package/dist/jsonld-dataset-proxy/src/util/addObjectToDataset.js.map +1 -0
  80. package/dist/jsonld-dataset-proxy/src/util/getNodeFromRaw.d.ts +6 -0
  81. package/dist/jsonld-dataset-proxy/src/util/getNodeFromRaw.js +47 -0
  82. package/dist/jsonld-dataset-proxy/src/util/getNodeFromRaw.js.map +1 -0
  83. package/dist/jsonld-dataset-proxy/src/util/nodeToJsonldRepresentation.d.ts +7 -0
  84. package/dist/jsonld-dataset-proxy/src/util/nodeToJsonldRepresentation.js +75 -0
  85. package/dist/jsonld-dataset-proxy/src/util/nodeToJsonldRepresentation.js.map +1 -0
  86. package/dist/solid-react/src/BrowserSolidLdoProvider.d.ts +2 -0
  87. package/dist/solid-react/src/BrowserSolidLdoProvider.js +63 -0
  88. package/dist/solid-react/src/BrowserSolidLdoProvider.js.map +1 -0
  89. package/dist/solid-react/src/SolidAuthContext.d.ts +14 -0
  90. package/dist/solid-react/src/SolidAuthContext.js +10 -0
  91. package/dist/solid-react/src/SolidAuthContext.js.map +1 -0
  92. package/dist/solid-react/src/SolidLdoProvider.d.ts +8 -0
  93. package/dist/solid-react/src/SolidLdoProvider.js +31 -0
  94. package/dist/solid-react/src/SolidLdoProvider.js.map +1 -0
  95. package/dist/solid-react/src/UnauthenticatedSolidLdoProvider.d.ts +2 -0
  96. package/dist/solid-react/src/UnauthenticatedSolidLdoProvider.js +41 -0
  97. package/dist/solid-react/src/UnauthenticatedSolidLdoProvider.js.map +1 -0
  98. package/dist/solid-react/src/index.d.ts +9 -0
  99. package/dist/solid-react/src/index.js +28 -0
  100. package/dist/solid-react/src/index.js.map +1 -0
  101. package/dist/solid-react/src/useLdoMethods.d.ts +12 -0
  102. package/dist/solid-react/src/useLdoMethods.js +20 -0
  103. package/dist/solid-react/src/useLdoMethods.js.map +1 -0
  104. package/dist/solid-react/src/useMatchObject.d.ts +3 -0
  105. package/dist/solid-react/src/useMatchObject.js +13 -0
  106. package/dist/solid-react/src/useMatchObject.js.map +1 -0
  107. package/dist/solid-react/src/useMatchSubject.d.ts +3 -0
  108. package/dist/solid-react/src/useMatchSubject.js +13 -0
  109. package/dist/solid-react/src/useMatchSubject.js.map +1 -0
  110. package/dist/solid-react/src/useResource.d.ts +12 -0
  111. package/dist/solid-react/src/useResource.js +63 -0
  112. package/dist/solid-react/src/useResource.js.map +1 -0
  113. package/dist/solid-react/src/useRootContainer.d.ts +3 -0
  114. package/dist/solid-react/src/useRootContainer.js +26 -0
  115. package/dist/solid-react/src/useRootContainer.js.map +1 -0
  116. package/dist/solid-react/src/useSubject.d.ts +5 -0
  117. package/dist/solid-react/src/useSubject.js +15 -0
  118. package/dist/solid-react/src/useSubject.js.map +1 -0
  119. package/dist/solid-react/src/useSubscribeToResource.d.ts +1 -0
  120. package/dist/solid-react/src/useSubscribeToResource.js +40 -0
  121. package/dist/solid-react/src/useSubscribeToResource.js.map +1 -0
  122. package/dist/solid-react/src/util/TrackingProxyContext.d.ts +16 -0
  123. package/dist/solid-react/src/util/TrackingProxyContext.js +27 -0
  124. package/dist/solid-react/src/util/TrackingProxyContext.js.map +1 -0
  125. package/dist/solid-react/src/util/TrackingSetProxy.d.ts +4 -0
  126. package/dist/solid-react/src/util/TrackingSetProxy.js +44 -0
  127. package/dist/solid-react/src/util/TrackingSetProxy.js.map +1 -0
  128. package/dist/solid-react/src/util/TrackingSubjectProxy.d.ts +4 -0
  129. package/dist/solid-react/src/util/TrackingSubjectProxy.js +31 -0
  130. package/dist/solid-react/src/util/TrackingSubjectProxy.js.map +1 -0
  131. package/dist/solid-react/src/util/useTrackingProxy.d.ts +3 -0
  132. package/dist/solid-react/src/util/useTrackingProxy.js +34 -0
  133. package/dist/solid-react/src/util/useTrackingProxy.js.map +1 -0
  134. package/dist/useMatchObject.d.ts +2 -2
  135. package/dist/useMatchSubject.d.ts +2 -2
  136. package/dist/useRootContainerFor.d.ts +4 -0
  137. package/dist/useRootContainerFor.js +25 -0
  138. package/dist/useRootContainerFor.js.map +1 -0
  139. package/dist/util/TrackingProxyContext.d.ts +6 -5
  140. package/dist/util/TrackingProxyContext.js +6 -54
  141. package/dist/util/TrackingProxyContext.js.map +1 -1
  142. package/dist/util/TrackingSetProxy.d.ts +4 -0
  143. package/dist/util/TrackingSetProxy.js +44 -0
  144. package/dist/util/TrackingSetProxy.js.map +1 -0
  145. package/dist/util/TrackingSubjectProxy.d.ts +4 -0
  146. package/dist/util/TrackingSubjectProxy.js +31 -0
  147. package/dist/util/TrackingSubjectProxy.js.map +1 -0
  148. package/package.json +14 -13
  149. package/src/SolidAuthContext.ts +3 -0
  150. package/src/UnauthenticatedSolidLdoProvider.tsx +4 -2
  151. package/src/createBrowserSolidReactMethods.tsx +132 -0
  152. package/src/defaultIntance.ts +19 -0
  153. package/src/index.ts +2 -12
  154. package/src/useRootContainerFor.ts +46 -0
  155. package/src/BrowserSolidLdoProvider.tsx +0 -97
  156. package/src/SolidLdoProvider.tsx +0 -55
  157. package/src/useLdoMethods.ts +0 -84
  158. package/src/useMatchObject.ts +0 -21
  159. package/src/useMatchSubject.ts +0 -21
  160. package/src/useResource.ts +0 -114
  161. package/src/useRootContainer.ts +0 -31
  162. package/src/useSubject.ts +0 -30
  163. package/src/useSubscribeToResource.ts +0 -52
  164. package/src/util/TrackingProxyContext.ts +0 -110
  165. package/src/util/useTrackingProxy.ts +0 -55
@@ -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
- }
@@ -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,110 +0,0 @@
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
- /**
13
- * @internal
14
- * Options to be passed to the tracking proxy
15
- */
16
- export interface TrackingProxyContextOptions extends ProxyContextOptions {
17
- dataset: SubscribableDataset<Quad>;
18
- }
19
-
20
- /**
21
- * @internal
22
- * This proxy exists to ensure react components rerender at the right time. It
23
- * keeps track of every key accessed in a Linked Data Object and only when the
24
- * dataset is updated with that key does it rerender the react component.
25
- */
26
- export class TrackingProxyContext extends ProxyContext {
27
- private listener: () => void;
28
- private subscribableDataset: SubscribableDataset<Quad>;
29
-
30
- constructor(options: TrackingProxyContextOptions, listener: () => void) {
31
- super(options);
32
- this.subscribableDataset = options.dataset;
33
- this.listener = listener;
34
- }
35
-
36
- // Adds the listener to the subscribable dataset while ensuring deduping of the listener
37
- private addListener(eventName: QuadMatch) {
38
- const listeners = this.subscribableDataset.listeners(eventName);
39
- if (!listeners.includes(this.listener)) {
40
- this.subscribableDataset.on(eventName, this.listener);
41
- }
42
- }
43
-
44
- protected createSubjectHandler(): ProxyHandler<SubjectProxyTarget> {
45
- const baseHandler = super.createSubjectHandler();
46
- const oldGetFunction = baseHandler.get;
47
- const newGetFunction: ProxyHandler<SubjectProxyTarget>["get"] = (
48
- target: SubjectProxyTarget,
49
- key: string | symbol,
50
- receiver,
51
- ) => {
52
- const subject = target["@id"];
53
- const rdfTypes = this.getRdfType(subject);
54
- if (typeof key === "symbol") {
55
- // Do Nothing
56
- } else if (key === "@id") {
57
- this.addListener([subject, null, null, null]);
58
- } else if (!this.contextUtil.isArray(key, rdfTypes)) {
59
- const predicate = namedNode(this.contextUtil.keyToIri(key, rdfTypes));
60
- this.addListener([subject, predicate, null, null]);
61
- }
62
- return oldGetFunction && oldGetFunction(target, key, receiver);
63
- };
64
- baseHandler.get = newGetFunction;
65
- baseHandler.set = () => {
66
- console.warn(
67
- "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.",
68
- );
69
- return true;
70
- };
71
- return baseHandler;
72
- }
73
-
74
- protected createArrayHandler(): ProxyHandler<ArrayProxyTarget> {
75
- const baseHandler = super.createArrayHandler();
76
- const oldGetFunction = baseHandler.get;
77
- const newGetFunction: ProxyHandler<ArrayProxyTarget>["get"] = (
78
- target: ArrayProxyTarget,
79
- key: string | symbol,
80
- receiver,
81
- ) => {
82
- if (qualifiedArrayMethods.has(key)) {
83
- this.addListener([target[0][0], target[0][1], target[0][2], null]);
84
- }
85
- return oldGetFunction && oldGetFunction(target, key, receiver);
86
- };
87
- baseHandler.get = newGetFunction;
88
- return baseHandler;
89
- }
90
- }
91
-
92
- const qualifiedArrayMethods = new Set([
93
- "forEach",
94
- "map",
95
- "reduce",
96
- Symbol.iterator,
97
- "entries",
98
- "every",
99
- "filter",
100
- "find",
101
- "findIndex",
102
- "findLast",
103
- "findLastIndex",
104
- "includes, indexOf",
105
- "keys",
106
- "lastIndexOf",
107
- "reduceRight",
108
- "some",
109
- "values",
110
- ]);
@@ -1,55 +0,0 @@
1
- import {
2
- ContextUtil,
3
- JsonldDatasetProxyBuilder,
4
- } from "@ldo/jsonld-dataset-proxy";
5
- import { LdoBuilder } from "@ldo/ldo";
6
- import type { LdoBase, ShapeType } from "@ldo/ldo";
7
- import { useCallback, useEffect, useMemo, useState } from "react";
8
- import { TrackingProxyContext } from "./TrackingProxyContext";
9
- import { defaultGraph } from "@rdfjs/data-model";
10
- import { useLdo } from "../SolidLdoProvider";
11
-
12
- export function useTrackingProxy<Type extends LdoBase, ReturnType>(
13
- shapeType: ShapeType<Type>,
14
- createLdo: (builder: LdoBuilder<Type>) => ReturnType,
15
- ): ReturnType {
16
- const { dataset } = useLdo();
17
-
18
- const [forceUpdateCounter, setForceUpdateCounter] = useState(0);
19
- const forceUpdate = useCallback(
20
- () => setForceUpdateCounter((val) => val + 1),
21
- [],
22
- );
23
-
24
- // The main linked data object
25
- const linkedDataObject = useMemo(() => {
26
- // Remove all current subscriptions
27
- dataset.removeListenerFromAllEvents(forceUpdate);
28
-
29
- // Rebuild the LdoBuilder from scratch to inject TrackingProxyContext
30
- const contextUtil = new ContextUtil(shapeType.context);
31
- const proxyContext = new TrackingProxyContext(
32
- {
33
- dataset,
34
- contextUtil,
35
- writeGraphs: [defaultGraph()],
36
- languageOrdering: ["none", "en", "other"],
37
- },
38
- forceUpdate,
39
- );
40
- const builder = new LdoBuilder(
41
- new JsonldDatasetProxyBuilder(proxyContext),
42
- shapeType,
43
- );
44
- return createLdo(builder);
45
- }, [shapeType, dataset, forceUpdateCounter, forceUpdate, createLdo]);
46
-
47
- useEffect(() => {
48
- // Unregister force update listener upon unmount
49
- return () => {
50
- dataset.removeListenerFromAllEvents(forceUpdate);
51
- };
52
- }, [shapeType]);
53
-
54
- return linkedDataObject;
55
- }