From a8bf23f600abae459be1cf0d008025c0c279ee9a Mon Sep 17 00:00:00 2001 From: Marcos Candeia Date: Mon, 2 Dec 2024 17:12:22 -0300 Subject: [PATCH] Fix deno usage Signed-off-by: Marcos Candeia --- examples/cf/counter-actor/package-lock.json | 8 ++--- examples/cf/counter-actor/package.json | 2 +- examples/cf/counter-actor/src/counter.ts | 38 +++++++++++++++++++++ examples/cf/counter-actor/src/index.ts | 8 ++++- src/actors/hono/middleware.ts | 3 +- src/actors/runtime.ts | 4 ++- 6 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 examples/cf/counter-actor/src/counter.ts diff --git a/examples/cf/counter-actor/package-lock.json b/examples/cf/counter-actor/package-lock.json index 3641f13..ecfb7fe 100644 --- a/examples/cf/counter-actor/package-lock.json +++ b/examples/cf/counter-actor/package-lock.json @@ -8,7 +8,7 @@ "name": "counter-actor", "version": "0.0.0", "dependencies": { - "@deco/actors": "npm:@jsr/deco__actors@^0.14.0-beta.5", + "@deco/actors": "npm:@jsr/deco__actors@^0.14.0-beta.6", "hono": "^4.6.12" }, "devDependencies": { @@ -150,9 +150,9 @@ }, "node_modules/@deco/actors": { "name": "@jsr/deco__actors", - "version": "0.14.0-beta.5", - "resolved": "https://npm.jsr.io/~/11/@jsr/deco__actors/0.14.0-beta.5.tgz", - "integrity": "sha512-O80Ik1Gk6EKWbpFQflP00gr3oGO3zvVgiNgMqJzH/DBz8Ztyxl6wYF5p4BwY7BmqTF2VFSeiJhT28DlSCXzx9w==", + "version": "0.14.0-beta.6", + "resolved": "https://npm.jsr.io/~/11/@jsr/deco__actors/0.14.0-beta.6.tgz", + "integrity": "sha512-Y1YwywC7TE7P4duInAp1CYw7AEQvA4LKk45XhvnJn6I2LyjIRhZBzz+h3X3HUQrmb+ynB2j1mHKcfUuugNTAaQ==", "dependencies": { "@cloudflare/workers-types": "^4.20241127.0", "@jsr/hono__hono": "^4.6.2", diff --git a/examples/cf/counter-actor/package.json b/examples/cf/counter-actor/package.json index 2621cfc..b89928a 100644 --- a/examples/cf/counter-actor/package.json +++ b/examples/cf/counter-actor/package.json @@ -15,7 +15,7 @@ "@cloudflare/workers-types": "^4.20241127.0" }, "dependencies": { - "@deco/actors": "npm:@jsr/deco__actors@^0.14.0-beta.5", + "@deco/actors": "npm:@jsr/deco__actors@^0.14.0-beta.6", "hono": "^4.6.12" } } diff --git a/examples/cf/counter-actor/src/counter.ts b/examples/cf/counter-actor/src/counter.ts new file mode 100644 index 0000000..ab8cf09 --- /dev/null +++ b/examples/cf/counter-actor/src/counter.ts @@ -0,0 +1,38 @@ +import { ActorState } from "@deco/actors"; +import { WatchTarget } from "@deco/actors/watch"; + +export class Counter { + private count: number; + private watchTarget = new WatchTarget(); + public metadata?: { extraSum: number }; + + constructor(protected state: ActorState) { + this.count = 0; + state.blockConcurrencyWhile(async () => { + this.count = await this.state.storage.get("counter") ?? 0; + }); + } + + + async increment(): Promise { + this.count++; + await this.state.storage.put("counter", this.count); + this.watchTarget.notify(this.count); + return this.count; + } + + async decrement(): Promise { + this.count--; + await this.state.storage.put("counter", this.count); + this.watchTarget.notify(this.count); + return this.count; + } + + getCount(): number { + return this.count + (this.metadata?.extraSum ?? 0); + } + + watch(): AsyncIterableIterator { + return this.watchTarget.subscribe(); + } +} \ No newline at end of file diff --git a/examples/cf/counter-actor/src/index.ts b/examples/cf/counter-actor/src/index.ts index 1ea708c..94da074 100644 --- a/examples/cf/counter-actor/src/index.ts +++ b/examples/cf/counter-actor/src/index.ts @@ -1,6 +1,12 @@ +import { ActorCfRuntime, Env } from "@deco/actors/cf"; +import { withActors } from "@deco/actors/hono"; import { Hono } from "hono"; +import { Counter } from "./counter.ts"; export { ActorDurableObject } from "@deco/actors/cf"; -const app = new Hono(); +const app = new Hono<{ Bindings: Env }>(); + +const runtime = new ActorCfRuntime([Counter]); +app.use(withActors(runtime)) app.get("/", (c) => c.text("Hello Cloudflare Workers!")); diff --git a/src/actors/hono/middleware.ts b/src/actors/hono/middleware.ts index d70587f..fea83d1 100644 --- a/src/actors/hono/middleware.ts +++ b/src/actors/hono/middleware.ts @@ -1,4 +1,5 @@ import type { MiddlewareHandler } from "@hono/hono"; +import process from "node:process"; import type { ActorFetcher } from "../runtime.ts"; /** @@ -16,7 +17,7 @@ export const withActors = ( } if (path.endsWith(`${basePath}/__restart`)) { console.log("Restarting actors..."); - Deno.exit(1); + process.exit(1); } const response = await fetcher.fetch(ctx.req.raw, ctx.env); ctx.res = response; diff --git a/src/actors/runtime.ts b/src/actors/runtime.ts index 3bf77fe..c55ffd2 100644 --- a/src/actors/runtime.ts +++ b/src/actors/runtime.ts @@ -94,7 +94,9 @@ export class ActorRuntime implements ActorFetcher { constructor( protected actorsConstructors: Array, ) { - this.websocketHandler = Deno?.upgradeWebSocket; + this.websocketHandler = typeof Deno === "object" + ? Deno?.upgradeWebSocket + : undefined; } setWebSocketHandler(