counter.ts
This file
Path
examples/durable-objects/src/counter.ts
35 lines · 923 bytes · TypeScript
Last changed by
refactor: refactor something (#65)Yusuke Wadabefore adoption336afc8
History of this fileTake it
refactor: refactor something (#65)Yusuke Wadabefore adoption336afc8
lines 1–35why, one step back1import { Hono } from '../../../src/index'23export class Counter {4 value: number = 05 state: DurableObjectState6 app: Hono = new Hono()78 constructor(state: DurableObjectState) {9 this.state = state10 this.state.blockConcurrencyWhile(async () => {11 const stored = await this.state.storage?.get<number>('value')12 this.value = stored || 013 })1415 this.app.get('/increment', async (c) => {16 const currentValue = ++this.value17 await this.state.storage?.put('value', this.value)18 return c.text(currentValue.toString())19 })2021 this.app.get('/decrement', async (c) => {22 const currentValue = --this.value23 await this.state.storage?.put('value', this.value)24 return c.text(currentValue.toString())25 })2627 this.app.get('/', async (c) => {28 return c.text(this.value.toString())29 })30 }3132 async fetch(request: Request) {33 return this.app.fetch(request)34 }35}