Skip to content
Myr

counter.ts

This file

Path

example/durable-objects/src/counter.ts

35 lines · 923 bytes · TypeScript

Last changed by

Fixed durable objects example (#46)Yusuke Wadabefore adoptiona22a0eb

History of this file

Take it

Support module workers syntax for Cloudflare Workers (#43)Yusuke Wadabefore adoptionfceed74

lines 1–5why, one step back
1import { Hono } from '../../../src/index'
23export class Counter {4 value: number = 05 state: DurableObjectState

Fixed durable objects example (#46)Yusuke Wadabefore adoptiona22a0eb

line 6why, one step back
6 app: Hono = new Hono()

Support module workers syntax for Cloudflare Workers (#43)Yusuke Wadabefore adoptionfceed74

lines 7–16why, one step back
7
8 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.value

Fixed durable objects example (#46)Yusuke Wadabefore adoptiona22a0eb

line 17why, one step back
17 await this.state.storage?.put('value', this.value)

Support module workers syntax for Cloudflare Workers (#43)Yusuke Wadabefore adoptionfceed74

lines 18–22why, one step back
18 return c.text(currentValue.toString())
19 })2021 this.app.get('/decrement', async (c) => {22 const currentValue = --this.value

Fixed durable objects example (#46)Yusuke Wadabefore adoptiona22a0eb

line 23why, one step back
23 await this.state.storage?.put('value', this.value)

Support module workers syntax for Cloudflare Workers (#43)Yusuke Wadabefore adoptionfceed74

lines 24–32why, one step back
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) {

Fixed durable objects example (#46)Yusuke Wadabefore adoptiona22a0eb

line 33why, one step back
33 return this.app.fetch(request)

Support module workers syntax for Cloudflare Workers (#43)Yusuke Wadabefore adoptionfceed74

lines 34–35why, one step back
34 }
35}