Refactored LDO methods into the solid library

main
jaxoncreed 2 years ago
parent 1792ae5ce6
commit 09706ef4d7
  1. 35
      packages/solid-react/src/useLdoMethods.ts
  2. 27
      packages/solid/src/SolidLdoDataset.ts
  3. 2
      packages/solid/src/index.ts
  4. 42
      packages/solid/src/methods.ts
  5. 1
      packages/solid/src/util/RequestBatcher.ts
  6. 6
      packages/solid/test/LeafRequester.test.ts
  7. 5
      packages/solid/test/SolidLdoDataset.test.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<Type extends LdoBase>(
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<SolidLdoDataset["commitChangesToPod"]> {
const changes = transactionChanges(input);
return dataset.commitChangesToPod(changes as DatasetChanges<Quad>);
},
commitData: commitData,
};
}

@ -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<Type extends LdoBase>(
shapeType: ShapeType<Type>,
subject: string | SubjectNode,
...resources: Resource[]
): Type {
const linkedDataObject = this.usingType(shapeType)
.write(...resources.map((r) => r.uri))
.fromSubject(subject);
startTransaction(linkedDataObject);
return linkedDataObject;
}
}

@ -7,3 +7,5 @@ export * from "./resource/Container";
export * from "./resource/Leaf";
export * from "./util/uriTypes";
export * from "./methods";

@ -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<Type extends LdoBase>(
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<SolidLdoDataset["commitChangesToPod"]> {
const changes = transactionChanges(input);
const dataset = getDataset(input) as SolidLdoDataset;
return dataset.commitChangesToPod(changes as DatasetChanges<Quad>);
}

@ -46,6 +46,7 @@ export class RequestBatcher {
}
public isLoading(key: string): boolean {
if (key === ANY_KEY) return !!this.currentlyProcessing;
return this.currentlyProcessing?.name === key;
}

@ -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;

@ -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);
})(),

Loading…
Cancel
Save