From cd7746f8fb2a4f77983cc14b6ea7afd4f6886773 Mon Sep 17 00:00:00 2001 From: sor4chi Date: Tue, 19 Sep 2023 23:31:50 +0900 Subject: [PATCH 1/3] feat: wrap hono object constructer's callback with blockConcurrencyWhile for initialize-safe --- packages/hono-do/src/index.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/hono-do/src/index.ts b/packages/hono-do/src/index.ts index 4356d52..65b99cf 100644 --- a/packages/hono-do/src/index.ts +++ b/packages/hono-do/src/index.ts @@ -14,7 +14,10 @@ export function generateHonoObject< BasePath extends string = "/", >( basePath: string, - cb: (app: Hono, state: DurableObjectState) => void, + cb: ( + app: Hono, + state: DurableObjectState, + ) => void | Promise, ) { const honoObject = function ( this: HonoObject, @@ -22,7 +25,9 @@ export function generateHonoObject< ) { const app = new Hono().basePath(basePath); this.app = app; - cb(app, state); + state.blockConcurrencyWhile(async () => { + await cb(app, state); + }); }; honoObject.prototype.fetch = function ( From 7f36ed055778f8576f0508bbe934cfa2467868f4 Mon Sep 17 00:00:00 2001 From: sor4chi Date: Wed, 20 Sep 2023 01:37:22 +0900 Subject: [PATCH 2/3] refactor(counter): replace async/await with synchronous code --- examples/counter/src/counter.ts | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/examples/counter/src/counter.ts b/examples/counter/src/counter.ts index 8821fc0..ece4908 100644 --- a/examples/counter/src/counter.ts +++ b/examples/counter/src/counter.ts @@ -1,22 +1,20 @@ import { generateHonoObject } from "hono-do"; -export const Counter = generateHonoObject("/counter", (app, state) => { +export const Counter = generateHonoObject("/counter", async (app, state) => { const { storage } = state; + let value = (await storage.get("value")) ?? 0; - app.post("/increment", async (c) => { - const newVal = 1 + ((await storage.get("value")) || 0); - storage.put("value", newVal); - return c.text(newVal.toString()); + app.post("/increment", (c) => { + storage.put("value", value++); + return c.text(value.toString()); }); - app.post("/decrement", async (c) => { - const newVal = -1 + ((await storage.get("value")) || 0); - storage.put("value", newVal); - return c.text(newVal.toString()); + app.post("/decrement", (c) => { + storage.put("value", value--); + return c.text(value.toString()); }); - app.get("/", async (c) => { - const value = (await storage.get("value")) || 0; + app.get("/", (c) => { return c.text(value.toString()); }); }); From 7fc995476cc27fd48194b07f395d4fa0bce070df Mon Sep 17 00:00:00 2001 From: sor4chi Date: Wed, 27 Sep 2023 01:55:59 +0900 Subject: [PATCH 3/3] chore: changeset --- .changeset/shiny-bees-explode.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/shiny-bees-explode.md diff --git a/.changeset/shiny-bees-explode.md b/.changeset/shiny-bees-explode.md new file mode 100644 index 0000000..9463cb9 --- /dev/null +++ b/.changeset/shiny-bees-explode.md @@ -0,0 +1,5 @@ +--- +"hono-do": minor +--- + +wrap initialize function of durable object with blockConcurrencyWhile