parent
a659b88d7f
commit
de5d056f7a
@ -0,0 +1,56 @@ |
|||||||
|
import { createNewSetProxy, type SetProxy } from "@ldo/jsonld-dataset-proxy"; |
||||||
|
import type { TrackingProxyContext } from "./TrackingProxyContext"; |
||||||
|
import type { QuadMatch } from "@ldo/rdf-utils"; |
||||||
|
|
||||||
|
export function createTrackingSetProxy( |
||||||
|
proxyContext: TrackingProxyContext, |
||||||
|
quadMatch: QuadMatch, |
||||||
|
isSubjectOriented?: boolean, |
||||||
|
isLangStringSet?: boolean, |
||||||
|
): SetProxy { |
||||||
|
const baseSetProxy = createNewSetProxy( |
||||||
|
quadMatch, |
||||||
|
isSubjectOriented ?? false, |
||||||
|
proxyContext, |
||||||
|
isLangStringSet, |
||||||
|
); |
||||||
|
|
||||||
|
return new Proxy(baseSetProxy, { |
||||||
|
get: (target: SetProxy, key: string | symbol, receiver) => { |
||||||
|
if (trackingMethods.has(key)) { |
||||||
|
proxyContext.addListener(quadMatch); |
||||||
|
} else if (disallowedMethods.has(key)) { |
||||||
|
console.warn( |
||||||
|
"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.", |
||||||
|
); |
||||||
|
} |
||||||
|
return Reflect.get(target, key, receiver); |
||||||
|
}, |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
const trackingMethods = new Set([ |
||||||
|
"has", |
||||||
|
"size", |
||||||
|
"entries", |
||||||
|
"keys", |
||||||
|
"values", |
||||||
|
Symbol.iterator, |
||||||
|
"every", |
||||||
|
"every", |
||||||
|
"some", |
||||||
|
"forEach", |
||||||
|
"map", |
||||||
|
"reduce", |
||||||
|
"toArray", |
||||||
|
"toJSON", |
||||||
|
"difference", |
||||||
|
"intersection", |
||||||
|
"isDisjointFrom", |
||||||
|
"isSubsetOf", |
||||||
|
"isSupersetOf", |
||||||
|
"symmetricDifference", |
||||||
|
"union", |
||||||
|
]); |
||||||
|
|
||||||
|
const disallowedMethods = new Set<string | symbol>(["add", "clear", "delete"]); |
@ -0,0 +1,43 @@ |
|||||||
|
import type { SubjectProxyTarget } from "@ldo/jsonld-dataset-proxy"; |
||||||
|
import { |
||||||
|
createSubjectHandler, |
||||||
|
type SubjectProxy, |
||||||
|
} from "@ldo/jsonld-dataset-proxy"; |
||||||
|
import type { BlankNode, NamedNode } from "@rdfjs/types"; |
||||||
|
import type { TrackingProxyContext } from "./TrackingProxyContext"; |
||||||
|
import { namedNode } from "@rdfjs/data-model"; |
||||||
|
|
||||||
|
export function createTrackingSubjectProxy( |
||||||
|
proxyContext: TrackingProxyContext, |
||||||
|
node: NamedNode | BlankNode, |
||||||
|
): SubjectProxy { |
||||||
|
const baseHandler = createSubjectHandler(proxyContext); |
||||||
|
const oldGetFunction = baseHandler.get; |
||||||
|
const newGetFunction: ProxyHandler<SubjectProxyTarget>["get"] = ( |
||||||
|
target: SubjectProxyTarget, |
||||||
|
key: string | symbol, |
||||||
|
receiver, |
||||||
|
) => { |
||||||
|
const subject = target["@id"]; |
||||||
|
const rdfTypes = proxyContext.getRdfType(subject); |
||||||
|
if (typeof key === "symbol") { |
||||||
|
// Do Nothing
|
||||||
|
} else if (key === "@id") { |
||||||
|
proxyContext.addListener([subject, null, null, null]); |
||||||
|
} else if (!proxyContext.contextUtil.isSet(key, rdfTypes)) { |
||||||
|
const predicate = namedNode( |
||||||
|
proxyContext.contextUtil.keyToIri(key, rdfTypes), |
||||||
|
); |
||||||
|
proxyContext.addListener([subject, predicate, null, null]); |
||||||
|
} |
||||||
|
return oldGetFunction && oldGetFunction(target, key, receiver); |
||||||
|
}; |
||||||
|
baseHandler.get = newGetFunction; |
||||||
|
baseHandler.set = () => { |
||||||
|
console.warn( |
||||||
|
"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.", |
||||||
|
); |
||||||
|
return true; |
||||||
|
}; |
||||||
|
return new Proxy({ "@id": node }, baseHandler) as unknown as SubjectProxy; |
||||||
|
} |
Loading…
Reference in new issue