-
Notifications
You must be signed in to change notification settings - Fork 66
Open
Description
Looking at https://github.com/honojs/examples/blob/main/durable-objects/src/counter.ts, I wanted to try something that makes a Durable Object feel more like a stateless application but with a c.state
property. So I came up with this:
const app = new HonoObject()
const getValue = async (storage: DurableObjectStorage) => await storage.get<number>('value') || 0
app.get('/increment', async (c) => {
const { storage } = c.state
const newVal = 1 + await getValue(storage);
storage.put('value', newVal)
return c.text(newVal.toString())
})
app.get('/decrement', async (c) => {
const { storage } = c.state
const newVal = -1 + await getValue(storage);
storage.put('value', newVal)
return c.text(newVal.toString())
})
app.get('/', async c => {
const value = await getValue(c.state.storage)
return c.text(value.toString())
})
export { app as Counter }
OnurGvnc and enfipy
Metadata
Metadata
Assignees
Labels
No labels