From a35c726692707ff774039948da89aa0190c48dda Mon Sep 17 00:00:00 2001 From: Marcos Candeia Date: Tue, 15 Oct 2024 19:04:20 -0300 Subject: [PATCH] Fix initialization issue Signed-off-by: Marcos Candeia --- src/actors/runtime.ts | 12 +++++------- src/actors/state.ts | 10 +++++----- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/actors/runtime.ts b/src/actors/runtime.ts index fbcdfbc..bf3f5e1 100644 --- a/src/actors/runtime.ts +++ b/src/actors/runtime.ts @@ -1,11 +1,11 @@ import { type ServerSentEventMessage, ServerSentEventStream } from "@std/http"; import { ACTOR_ID_HEADER_NAME, ACTOR_ID_QS_NAME } from "./proxy.ts"; import { ActorState } from "./state.ts"; +import type { ActorStorage } from "./storage.ts"; import { DenoKvActorStorage } from "./storage/denoKv.ts"; +import { S3ActorStorage } from "./storage/s3.ts"; import { EVENT_STREAM_RESPONSE_HEADER } from "./stream.ts"; import { isUpgrade, makeWebSocket } from "./util/channels/channel.ts"; -import { S3ActorStorage } from "./storage/s3.ts"; -import type { ActorStorage } from "./storage.ts"; /** * Represents an actor. @@ -65,7 +65,7 @@ export interface ActorInvoker { /** * A promise that resolves when the actor is initialized. */ - initialization: PromiseWithResolvers; + initialization: Promise; } /** @@ -108,10 +108,8 @@ export class ActorRuntime { return; } this.actorsConstructors.forEach((Actor) => { - const initialization = Promise.withResolvers(); const storage = this.getActorStorage(actorId, Actor.name); const state = new ActorState({ - initialization, storage, }); const actor = new Actor( @@ -120,7 +118,7 @@ export class ActorRuntime { this.actors.set(Actor.name, { actor, state, - initialization, + initialization: state.initialization, }); }); this.initilized = true; @@ -180,7 +178,7 @@ export class ActorRuntime { }, ); } - await initialization.promise; + await initialization; const res = await (methodImpl as Function).bind(actor)( ...Array.isArray(args) ? args : [args], ); diff --git a/src/actors/state.ts b/src/actors/state.ts index 839693b..e1446d8 100644 --- a/src/actors/state.ts +++ b/src/actors/state.ts @@ -1,7 +1,6 @@ import type { ActorStorage } from "./storage.ts"; export interface ActorStateOptions { - initialization: PromiseWithResolvers; storage: ActorStorage; } /** @@ -9,13 +8,14 @@ export interface ActorStateOptions { */ export class ActorState { public storage: ActorStorage; + public initialization: Promise = Promise.resolve(); constructor(private options: ActorStateOptions) { this.storage = options.storage; } - async blockConcurrencyWhile(callback: () => Promise): Promise { - return await callback().finally(() => { - this.options.initialization.resolve(); - }); + blockConcurrencyWhile(callback: () => Promise): Promise { + const result = callback(); + this.initialization = result.then(() => {}); + return result; } }