Skip to content

Instantly share code, notes, and snippets.

@lmaccherone
Last active March 2, 2023 13:44
Show Gist options
  • Save lmaccherone/b6432da49842be195b6c805ef7f3267b to your computer and use it in GitHub Desktop.
Save lmaccherone/b6432da49842be195b6c805ef7f3267b to your computer and use it in GitHub Desktop.
export class LazyHydrateDemo {
async hydrate() {
if (this.hydrated) return
this.value = await this.state.storage.get('value') ?? 0
this.hydrated = true
}
constructor(state, env) {
this.state = state
this.env = env
this.hydrated = false
}
async fetch(request) {
// Do input checking on request
// Route to endpoints that don't require hydration like OPTIONS
await this.hydrate()
this.value++
await this.state.storage.put('value', this.value)
return new Response(this.value)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment