Skip to content

Commit

Permalink
Merge pull request #3 from sor4chi/feat/wrap-with-block-concurrency-w…
Browse files Browse the repository at this point in the history
…hile

feat: wrap constructor callback with block concurrency while
  • Loading branch information
sor4chi authored Sep 26, 2023
2 parents dc30540 + 7fc9954 commit edea88f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/shiny-bees-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"hono-do": minor
---

wrap initialize function of durable object with blockConcurrencyWhile
20 changes: 9 additions & 11 deletions examples/counter/src/counter.ts
Original file line number Diff line number Diff line change
@@ -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<number>("value")) ?? 0;

app.post("/increment", async (c) => {
const newVal = 1 + ((await storage.get<number>("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<number>("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<number>("value")) || 0;
app.get("/", (c) => {
return c.text(value.toString());
});
});
9 changes: 7 additions & 2 deletions packages/hono-do/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@ export function generateHonoObject<
BasePath extends string = "/",
>(
basePath: string,
cb: (app: Hono<E, S, BasePath>, state: DurableObjectState) => void,
cb: (
app: Hono<E, S, BasePath>,
state: DurableObjectState,
) => void | Promise<void>,
) {
const honoObject = function (
this: HonoObject<E, S, BasePath>,
state: DurableObjectState,
) {
const app = new Hono<E, S, BasePath>().basePath(basePath);
this.app = app;
cb(app, state);
state.blockConcurrencyWhile(async () => {
await cb(app, state);
});
};

honoObject.prototype.fetch = function (
Expand Down

0 comments on commit edea88f

Please sign in to comment.