Used subscription for read

main
Jackson Morgan 5 months ago
parent a199f2514b
commit 0518c5c748
  1. 14
      packages/connected-nextgraph/src/NextGraphConnectedPlugin.ts
  2. 2
      packages/connected-nextgraph/src/index.ts
  3. 1
      packages/connected-nextgraph/src/notifications/NextGraphNotificationSubscription.ts
  4. 30
      packages/connected-nextgraph/src/resources/NextGraphResource.ts
  5. 8
      packages/connected-nextgraph/src/results/NoNextGraphStoreError.ts

@ -3,7 +3,6 @@ import type { NextGraphUri } from "./types";
import { NextGraphResource } from "./resources/NextGraphResource";
import ng from "nextgraph";
import { isNextGraphUri } from "./util/isNextGraphUri";
import { NoNextGraphStoreError } from "./results/NoNextGraphStoreError";
export interface NextGraphConnectedContext {
sessionId?: string;
@ -13,7 +12,7 @@ export interface NextGraphConnectedContext {
}
export interface NextGraphCreateResourceOptions {
storeType?: "public" | "protected" | "private";
storeType?: "public" | "protected" | "private" | "group" | "dialog";
storeRepo?: string;
}
@ -30,9 +29,7 @@ export interface NextGraphConnectedPlugin
uri: NextGraphUri,
context: ConnectedContext<this[]>,
) => NextGraphResource;
createResource(
context: ConnectedContext<this[]>,
): Promise<NextGraphResource | NoNextGraphStoreError>;
createResource(context: ConnectedContext<this[]>): Promise<NextGraphResource>;
}
export const nextGraphConnectedPlugin: NextGraphConnectedPlugin = {
@ -48,8 +45,8 @@ export const nextGraphConnectedPlugin: NextGraphConnectedPlugin = {
createResource: async function (
context: ConnectedContext<NextGraphConnectedPlugin[]>,
options?: NextGraphCreateResourceOptions,
): Promise<NextGraphResource | NoNextGraphStoreError> {
const storeType = options?.storeType ?? "protected";
): Promise<NextGraphResource> {
const storeType = options?.storeType;
const storeRepo =
options?.storeRepo ??
(storeType === "protected"
@ -59,9 +56,6 @@ export const nextGraphConnectedPlugin: NextGraphConnectedPlugin = {
: storeType === "private"
? context.nextgraph.privateStoreId
: undefined);
if (!storeRepo) {
return new NoNextGraphStoreError();
}
const nuri: NextGraphUri = await ng.doc_create(
context.nextgraph.sessionId,

@ -4,3 +4,5 @@ export * from "./NextGraphConnectedPlugin";
export * from "./resources/NextGraphResource";
export * from "./util/isNextGraphUri";
export * from "./results/NextGraphReadSuccess";

@ -10,6 +10,7 @@ export class NextGraphNotificationSubscription extends NotificationSubscription<
private unsub: (() => void) | undefined;
protected async open(): Promise<void> {
console.log("THIS WAS OPENED AND IT SHOULDNT BE");
this.unsub = await ng.doc_subscribe(
this.resource.uri,
this.context.nextgraph.sessionId,

@ -123,15 +123,25 @@ export class NextGraphResource
this.loading = true;
this.emit("update");
// Get the data
const sparqlResult = await ng.sparql_query(
this.context.nextgraph.sessionId,
`CONSTRUCT { ?s ?p ?o } WHERE { GRAPH <${this.uri}> { ?s ?p ?o } }`,
undefined,
// Fetch the data once using subscribe
await new Promise<void>(async (resolve, reject) => {
let unsub: () => void;
try {
unsub = await ng.doc_subscribe(
this.uri,
this.context.nextgraph.sessionId,
async (response: NextGraphNotificationMessage) => {
if (response.V0.State) {
unsub();
await this.onNotification(response);
resolve();
}
},
);
// Update the dataset
this.overwriteQuads(sparqlResult);
} catch (err) {
reject(err);
}
});
// Update statuses
const result = new NextGraphReadSuccess(this, false);
@ -214,7 +224,11 @@ export class NextGraphResource
}
protected async onNotification(response: NextGraphNotificationMessage) {
if (response.V0.State?.graph) {
if (response.V0.State) {
if (!response.V0.State.graph) {
this.overwriteQuads([]);
return;
}
const json_str = new TextDecoder().decode(
response.V0.State.graph.triples,
);

@ -1,8 +0,0 @@
import { ErrorResult } from "@ldo/connected";
export class NoNextGraphStoreError extends ErrorResult {
type = "noNextGraphStore" as const;
constructor(message?: string) {
super(message ?? "No NextGraph store was provided.");
}
}
Loading…
Cancel
Save