Skip to content

Commit

Permalink
Fix initialization issue
Browse files Browse the repository at this point in the history
Signed-off-by: Marcos Candeia <[email protected]>
  • Loading branch information
mcandeia committed Oct 15, 2024
1 parent aa9776d commit a35c726
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
12 changes: 5 additions & 7 deletions src/actors/runtime.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -65,7 +65,7 @@ export interface ActorInvoker {
/**
* A promise that resolves when the actor is initialized.
*/
initialization: PromiseWithResolvers<void>;
initialization: Promise<void>;
}

/**
Expand Down Expand Up @@ -108,10 +108,8 @@ export class ActorRuntime {
return;
}
this.actorsConstructors.forEach((Actor) => {
const initialization = Promise.withResolvers<void>();
const storage = this.getActorStorage(actorId, Actor.name);
const state = new ActorState({
initialization,
storage,
});
const actor = new Actor(
Expand All @@ -120,7 +118,7 @@ export class ActorRuntime {
this.actors.set(Actor.name, {
actor,
state,
initialization,
initialization: state.initialization,
});
});
this.initilized = true;
Expand Down Expand Up @@ -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],
);
Expand Down
10 changes: 5 additions & 5 deletions src/actors/state.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import type { ActorStorage } from "./storage.ts";

export interface ActorStateOptions {
initialization: PromiseWithResolvers<void>;
storage: ActorStorage;
}
/**
* Represents the state of an actor.
*/
export class ActorState {
public storage: ActorStorage;
public initialization: Promise<void> = Promise.resolve();
constructor(private options: ActorStateOptions) {
this.storage = options.storage;
}

async blockConcurrencyWhile<T>(callback: () => Promise<T>): Promise<T> {
return await callback().finally(() => {
this.options.initialization.resolve();
});
blockConcurrencyWhile<T>(callback: () => Promise<T>): Promise<T> {
const result = callback();
this.initialization = result.then(() => {});
return result;
}
}

0 comments on commit a35c726

Please sign in to comment.