diff --git a/packages/solid-react/src/useLdoMethods.ts b/packages/solid-react/src/useLdoMethods.ts index e922605..566b973 100644 --- a/packages/solid-react/src/useLdoMethods.ts +++ b/packages/solid-react/src/useLdoMethods.ts @@ -1,10 +1,7 @@ import type { LdoBase, ShapeType } from "@ldo/ldo"; -import { transactionChanges } from "@ldo/ldo"; -import { write } from "@ldo/ldo"; -import { startTransaction } from "@ldo/ldo"; -import type { DatasetChanges, SubjectNode } from "@ldo/rdf-utils"; +import type { SubjectNode } from "@ldo/rdf-utils"; import type { Resource, SolidLdoDataset } from "@ldo/solid"; -import type { Quad } from "@rdfjs/types"; +import { changeData, commitData } from "@ldo/solid"; export interface UseLdoMethods { dataset: SolidLdoDataset; @@ -53,40 +50,18 @@ export function createUseLdoMethods(dataset: SolidLdoDataset): UseLdoMethods { subject: string | SubjectNode, ...resources: Resource[] ): Type { - const linkedDataObject = dataset - .usingType(shapeType) - .write(...resources.map((r) => r.uri)) - .fromSubject(subject); - startTransaction(linkedDataObject); - return linkedDataObject; + return dataset.createData(shapeType, subject, ...resources); }, /** * Begins tracking changes to eventually commit * @param input A linked data object to track changes on * @param resources */ - changeData( - input: Type, - ...resources: Resource[] - ): Type { - // Clone the input and set a graph - const [transactionLdo] = write(...resources.map((r) => r.uri)).usingCopy( - input, - ); - // Start a transaction with the input - startTransaction(transactionLdo); - // Return - return transactionLdo; - }, + changeData: changeData, /** * Commits the transaction to the global dataset, syncing all subscribing * components and Solid Pods */ - commitData( - input: LdoBase, - ): ReturnType { - const changes = transactionChanges(input); - return dataset.commitChangesToPod(changes as DatasetChanges); - }, + commitData: commitData, }; } diff --git a/packages/solid/src/SolidLdoDataset.ts b/packages/solid/src/SolidLdoDataset.ts index 34d8012..7dc4c2b 100644 --- a/packages/solid/src/SolidLdoDataset.ts +++ b/packages/solid/src/SolidLdoDataset.ts @@ -1,5 +1,6 @@ -import { LdoDataset } from "@ldo/ldo"; -import type { DatasetChanges, GraphNode } from "@ldo/rdf-utils"; +import type { LdoBase, ShapeType } from "@ldo/ldo"; +import { LdoDataset, startTransaction } from "@ldo/ldo"; +import type { DatasetChanges, GraphNode, SubjectNode } from "@ldo/rdf-utils"; import type { Dataset, DatasetFactory, Quad } from "@rdfjs/types"; import type { UpdateResult, @@ -17,6 +18,7 @@ import type { SolidLdoDatasetContext } from "./SolidLdoDatasetContext"; import { splitChangesByGraph } from "./util/splitChangesByGraph"; import type { ContainerUri, LeafUri } from "./util/uriTypes"; import { isContainerUri } from "./util/uriTypes"; +import type { Resource } from "./resource/Resource"; export class SolidLdoDataset extends LdoDataset { public context: SolidLdoDatasetContext; @@ -96,4 +98,25 @@ export class SolidLdoDataset extends LdoDataset { ), }; } + + /** + * Shorthand for solidLdoDataset + * .usingType(shapeType) + * .write(...resources.map((r) => r.uri)) + * .fromSubject(subject); + * @param shapeType The shapetype to represent the data + * @param subject A subject URI + * @param resources The resources changes to should written to + */ + createData( + shapeType: ShapeType, + subject: string | SubjectNode, + ...resources: Resource[] + ): Type { + const linkedDataObject = this.usingType(shapeType) + .write(...resources.map((r) => r.uri)) + .fromSubject(subject); + startTransaction(linkedDataObject); + return linkedDataObject; + } } diff --git a/packages/solid/src/index.ts b/packages/solid/src/index.ts index de5e625..544a8b0 100644 --- a/packages/solid/src/index.ts +++ b/packages/solid/src/index.ts @@ -7,3 +7,5 @@ export * from "./resource/Container"; export * from "./resource/Leaf"; export * from "./util/uriTypes"; + +export * from "./methods"; diff --git a/packages/solid/src/methods.ts b/packages/solid/src/methods.ts new file mode 100644 index 0000000..d58c48b --- /dev/null +++ b/packages/solid/src/methods.ts @@ -0,0 +1,42 @@ +import { + startTransaction, + type LdoBase, + write, + transactionChanges, + getDataset, +} from "@ldo/ldo"; +import type { DatasetChanges } from "@ldo/rdf-utils"; +import type { Resource } from "./resource/Resource"; +import type { SolidLdoDataset } from "./SolidLdoDataset"; +import type { Quad } from "@rdfjs/types"; + +/** + * Begins tracking changes to eventually commit + * @param input A linked data object to track changes on + * @param resources + */ +export function changeData( + input: Type, + ...resources: Resource[] +): Type { + // Clone the input and set a graph + const [transactionLdo] = write(...resources.map((r) => r.uri)).usingCopy( + input, + ); + // Start a transaction with the input + startTransaction(transactionLdo); + // Return + return transactionLdo; +} + +/** + * Commits the transaction to the global dataset, syncing all subscribing + * components and Solid Pods + */ +export function commitData( + input: LdoBase, +): ReturnType { + const changes = transactionChanges(input); + const dataset = getDataset(input) as SolidLdoDataset; + return dataset.commitChangesToPod(changes as DatasetChanges); +} diff --git a/packages/solid/src/util/RequestBatcher.ts b/packages/solid/src/util/RequestBatcher.ts index b9711d1..e784069 100644 --- a/packages/solid/src/util/RequestBatcher.ts +++ b/packages/solid/src/util/RequestBatcher.ts @@ -46,6 +46,7 @@ export class RequestBatcher { } public isLoading(key: string): boolean { + if (key === ANY_KEY) return !!this.currentlyProcessing; return this.currentlyProcessing?.name === key; } diff --git a/packages/solid/test/LeafRequester.test.ts b/packages/solid/test/LeafRequester.test.ts index 4504cf8..72dd179 100644 --- a/packages/solid/test/LeafRequester.test.ts +++ b/packages/solid/test/LeafRequester.test.ts @@ -5,6 +5,12 @@ // import { LeafRequester } from "../src/requester/LeafRequester"; // import { namedNode, quad as createQuad } from "@rdfjs/data-model"; +describe("Leaf Requester", () => { + it("trivial", () => { + expect(true).toBe(true); + }); +}); + // describe.skip("Leaf Requester", () => { // let _app: App; // let authFetch: typeof fetch; diff --git a/packages/solid/test/SolidLdoDataset.test.ts b/packages/solid/test/SolidLdoDataset.test.ts index 181d556..efd4360 100644 --- a/packages/solid/test/SolidLdoDataset.test.ts +++ b/packages/solid/test/SolidLdoDataset.test.ts @@ -297,20 +297,17 @@ describe("SolidLdoDataset", () => { const resource = solidLdoDataset.getResource(SAMPLE2_DATA_URI); const container = solidLdoDataset.getResource(TEST_CONTAINER_URI); fetchMock.mockImplementationOnce(async (...args) => { - console.log("before"); await wait(500); - console.log("after"); return authFetch(...args); }); const [result] = await Promise.all([ resource.createAndOverwrite(), (async () => { await wait(100); - console.log("Checking"); expect(resource.isLoading()).toBe(true); expect(resource.isCreating()).toBe(true); expect(resource.isReading()).toBe(false); - expect(resource.isUploading).toBe(false); + expect(resource.isUploading()).toBe(false); expect(resource.isReloading()).toBe(false); expect(resource.isDeleting()).toBe(false); })(),