Last active
March 2, 2023 13:44
-
-
Save lmaccherone/b6432da49842be195b6c805ef7f3267b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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