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

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

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

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