From 0518c5c7483d8344bdec226a595a6c39a34f346f Mon Sep 17 00:00:00 2001 From: Jackson Morgan Date: Thu, 3 Apr 2025 22:50:12 -0400 Subject: [PATCH] Used subscription for read --- .../src/NextGraphConnectedPlugin.ts | 14 +++----- packages/connected-nextgraph/src/index.ts | 2 ++ .../NextGraphNotificationSubscription.ts | 1 + .../src/resources/NextGraphResource.ts | 34 +++++++++++++------ .../src/results/NoNextGraphStoreError.ts | 8 ----- 5 files changed, 31 insertions(+), 28 deletions(-) delete mode 100644 packages/connected-nextgraph/src/results/NoNextGraphStoreError.ts diff --git a/packages/connected-nextgraph/src/NextGraphConnectedPlugin.ts b/packages/connected-nextgraph/src/NextGraphConnectedPlugin.ts index d84f9bd..e091b2c 100644 --- a/packages/connected-nextgraph/src/NextGraphConnectedPlugin.ts +++ b/packages/connected-nextgraph/src/NextGraphConnectedPlugin.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, ) => NextGraphResource; - createResource( - context: ConnectedContext, - ): Promise; + createResource(context: ConnectedContext): Promise; } export const nextGraphConnectedPlugin: NextGraphConnectedPlugin = { @@ -48,8 +45,8 @@ export const nextGraphConnectedPlugin: NextGraphConnectedPlugin = { createResource: async function ( context: ConnectedContext, options?: NextGraphCreateResourceOptions, - ): Promise { - const storeType = options?.storeType ?? "protected"; + ): Promise { + 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, diff --git a/packages/connected-nextgraph/src/index.ts b/packages/connected-nextgraph/src/index.ts index caba9be..bf2cfc8 100644 --- a/packages/connected-nextgraph/src/index.ts +++ b/packages/connected-nextgraph/src/index.ts @@ -4,3 +4,5 @@ export * from "./NextGraphConnectedPlugin"; export * from "./resources/NextGraphResource"; export * from "./util/isNextGraphUri"; + +export * from "./results/NextGraphReadSuccess"; diff --git a/packages/connected-nextgraph/src/notifications/NextGraphNotificationSubscription.ts b/packages/connected-nextgraph/src/notifications/NextGraphNotificationSubscription.ts index 3391b89..2a34e39 100644 --- a/packages/connected-nextgraph/src/notifications/NextGraphNotificationSubscription.ts +++ b/packages/connected-nextgraph/src/notifications/NextGraphNotificationSubscription.ts @@ -10,6 +10,7 @@ export class NextGraphNotificationSubscription extends NotificationSubscription< private unsub: (() => void) | undefined; protected async open(): Promise { + console.log("THIS WAS OPENED AND IT SHOULDNT BE"); this.unsub = await ng.doc_subscribe( this.resource.uri, this.context.nextgraph.sessionId, diff --git a/packages/connected-nextgraph/src/resources/NextGraphResource.ts b/packages/connected-nextgraph/src/resources/NextGraphResource.ts index 01d6ae2..a25dfda 100644 --- a/packages/connected-nextgraph/src/resources/NextGraphResource.ts +++ b/packages/connected-nextgraph/src/resources/NextGraphResource.ts @@ -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, - this.uri, - ); - // Update the dataset - this.overwriteQuads(sparqlResult); + // Fetch the data once using subscribe + await new Promise(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(); + } + }, + ); + } 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, ); diff --git a/packages/connected-nextgraph/src/results/NoNextGraphStoreError.ts b/packages/connected-nextgraph/src/results/NoNextGraphStoreError.ts deleted file mode 100644 index 2f2b471..0000000 --- a/packages/connected-nextgraph/src/results/NoNextGraphStoreError.ts +++ /dev/null @@ -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."); - } -}