From a4fe11ba08074e692678cf7a2aa3a07e60c6cb8d Mon Sep 17 00:00:00 2001 From: Laurin Weger Date: Mon, 20 Oct 2025 01:54:39 +0200 Subject: [PATCH] signals ngSession promise --- .../src/app/pages/index.astro | 2 +- sdk/js/signals/src/connector/initNg.ts | 27 ++++++----- .../src/connector/ormConnectionHandler.ts | 46 ++++++++----------- 3 files changed, 37 insertions(+), 38 deletions(-) diff --git a/sdk/js/examples/multi-framework-signals/src/app/pages/index.astro b/sdk/js/examples/multi-framework-signals/src/app/pages/index.astro index 05b7cf3..71993e9 100644 --- a/sdk/js/examples/multi-framework-signals/src/app/pages/index.astro +++ b/sdk/js/examples/multi-framework-signals/src/app/pages/index.astro @@ -27,7 +27,7 @@ const title = "Multi-framework app"; console.log(await ng.locales()); let info = await ng.client_info(); console.log(info.V0.details); - initNg(ng); + initNg(ng, event.session); }, true, [] diff --git a/sdk/js/signals/src/connector/initNg.ts b/sdk/js/signals/src/connector/initNg.ts index 95d937a..b644f08 100644 --- a/sdk/js/signals/src/connector/initNg.ts +++ b/sdk/js/signals/src/connector/initNg.ts @@ -1,15 +1,20 @@ import * as NG from "@ng-org/lib-wasm"; -export let ng: typeof NG; - -export function initNg( - ngImpl: typeof NG, - session: { - session_id: unknown; - protected_store_id: unknown; - private_store_id: unknown; - public_store_id: unknown; +type Session = { + session_id: unknown; + protected_store_id: unknown; + private_store_id: unknown; + public_store_id: unknown; +}; + +let resolveNgSession: (value: { ng: typeof NG; session: Session }) => void; + +export const ngSession = new Promise<{ ng: typeof NG; session: Session }>( + (resolve) => { + resolveNgSession = resolve; } -) { - ng = ngImpl; +); + +export function initNg(ngImpl: typeof NG, session: Session) { + resolveNgSession({ ng: ngImpl, session }); } diff --git a/sdk/js/signals/src/connector/ormConnectionHandler.ts b/sdk/js/signals/src/connector/ormConnectionHandler.ts index 2ecdb90..47e8cb5 100644 --- a/sdk/js/signals/src/connector/ormConnectionHandler.ts +++ b/sdk/js/signals/src/connector/ormConnectionHandler.ts @@ -1,7 +1,7 @@ import type { Diff as Patches, Scope } from "../types.ts"; import { applyDiff } from "./applyDiff.ts"; -import { ng } from "./initNg.ts"; +import { ngSession } from "./initNg.ts"; import { deepSignal, @@ -11,7 +11,6 @@ import { import type { DeepPatch, DeepSignalObject, - WatchPatchCallback, WatchPatchEvent, } from "@ng-org/alien-deepsignals"; import type { ShapeType, BaseType } from "@ng-org/shex-orm"; @@ -27,7 +26,6 @@ export class OrmConnection { /*** Identifier as a combination of shape type and scope. Prevents duplications. */ private identifier: string; ready: boolean; - sessionId: number; suspendDeepWatcher: boolean; readyPromise: Promise; // Promise that resolves once initial data has been applied. @@ -56,9 +54,6 @@ export class OrmConnection { idGenerator: this.generateSubjectIri, }); - // TODO: - this.sessionId = 1; - // Schedule cleanup of the connection when the signal object is GC'd. OrmConnection.cleanupSignalRegistry?.register( this.signalObject, @@ -74,14 +69,11 @@ export class OrmConnection { this.resolveReady = resolve; }); - new Promise(async () => { - // Establish connection to wasm land. - await new Promise((resolve) => setTimeout(resolve, 100)); - + ngSession.then(({ ng, session }) => { ng.orm_start( scope, shapeType, - this.sessionId, + session.session_id, this.onBackendMessage ); }); @@ -98,8 +90,6 @@ export class OrmConnection { shapeType: ShapeType, scope: Scope ): OrmConnection { - // if (!ng) throw new Error("initNg was not called yet."); - const scopeKey = canonicalScope(scope); // Unique identifier for a given shape type and scope. @@ -108,13 +98,15 @@ export class OrmConnection { // If we already have an object for this shape+scope, // return it and just increase the reference count. // Otherwise, create new one. - const connection = - OrmConnection.idToEntry.get(identifier) ?? - new OrmConnection(shapeType, scope); - - connection.refCount += 1; - - return connection; + const existingConnection = OrmConnection.idToEntry.get(identifier); + if (existingConnection) { + existingConnection.refCount += 1; + return existingConnection; + } else { + const newConnection = new OrmConnection(shapeType, scope); + OrmConnection.idToEntry.set(identifier, newConnection); + return newConnection; + } } public release() { @@ -131,12 +123,14 @@ export class OrmConnection { const ormPatches = deepPatchesToDiff(patches); - ng.orm_update( - this.scope, - this.shapeType.shape, - ormPatches, - this.sessionId - ); + ngSession.then(({ ng, session }) => { + ng.orm_update( + this.scope, + this.shapeType.shape, + ormPatches, + session.session_id + ); + }); } private onBackendMessage(...message: any) {