From 4be5fcd51500b924d366a47298827ccd18ceba80 Mon Sep 17 00:00:00 2001 From: Jun Lee Date: Wed, 17 Sep 2025 10:16:05 +0100 Subject: [PATCH] Adding minor code block fixes --- .../reference/in-memory-state.mdx | 34 +++++++++-------- src/content/docs/kv/concepts/kv-bindings.mdx | 37 ++++++++++--------- 2 files changed, 37 insertions(+), 34 deletions(-) diff --git a/src/content/docs/durable-objects/reference/in-memory-state.mdx b/src/content/docs/durable-objects/reference/in-memory-state.mdx index a56df7a73d6b41..5b02be3249ac4a 100644 --- a/src/content/docs/durable-objects/reference/in-memory-state.mdx +++ b/src/content/docs/durable-objects/reference/in-memory-state.mdx @@ -14,22 +14,24 @@ Variables in a Durable Object will maintain state as long as your Durable Object A common pattern is to initialize a Durable Object from [persistent storage](/durable-objects/api/storage-api/) and set instance variables the first time it is accessed. Since future accesses are routed to the same Durable Object, it is then possible to return any initialized values without making further calls to persistent storage. ```js -export class Counter { - constructor(ctx, env) { - this.ctx = ctx; - // `blockConcurrencyWhile()` ensures no requests are delivered until - // initialization completes. - this.ctx.blockConcurrencyWhile(async () => { - let stored = await this.ctx.storage.get("value"); - // After initialization, future reads do not need to access storage. - this.value = stored || 0; - }); - } - - // Handle HTTP requests from clients. - async fetch(request) { - // use this.value rather than storage - } +import { DurableObject } from "cloudflare:workers"; + +export class Counter extends DurableObject { + constructor(ctx, env) { + super(ctx, env); + // `blockConcurrencyWhile()` ensures no requests are delivered until + // initialization completes. + this.ctx.blockConcurrencyWhile(async () => { + let stored = await this.ctx.storage.get("value"); + // After initialization, future reads do not need to access storage. + this.value = stored || 0; + }); + } + + // Handle HTTP requests from clients. + async fetch(request) { + // use this.value rather than storage + } } ``` diff --git a/src/content/docs/kv/concepts/kv-bindings.mdx b/src/content/docs/kv/concepts/kv-bindings.mdx index f40f24b97efbfd..66ba312c7126bb 100644 --- a/src/content/docs/kv/concepts/kv-bindings.mdx +++ b/src/content/docs/kv/concepts/kv-bindings.mdx @@ -43,14 +43,14 @@ With this, the deployed Worker will have a `TODO` field in their environment obj ```js export default { - async fetch(request, env, ctx) { - // Get the value for the "to-do:123" key - // NOTE: Relies on the `TODO` KV binding that maps to the "My Tasks" namespace. - let value = await env.TODO.get("to-do:123"); - - // Return the value, as is, for the Response - return new Response(value); - }, + async fetch(request, env, ctx) { + // Get the value for the "to-do:123" key + // NOTE: Relies on the `TODO` KV binding that maps to the "My Tasks" namespace. + let value = await env.TODO.get("to-do:123"); + + // Return the value, as is, for the Response + return new Response(value); + }, }; ``` @@ -83,15 +83,16 @@ kv_namespaces = [ An example might look like: ```js -export class DurableObject { - constructor(ctx, env) { - this.ctx = ctx; - this.env = env; - } - - async fetch(request) { - const valueFromKV = await this.env.NAMESPACE.get("someKey"); - return new Response(valueFromKV); - } +import { DurableObject } from "cloudflare:workers"; + +export class MyDurableObject extends DurableObject { + constructor(ctx, env) { + super(ctx, env); + } + + async fetch(request) { + const valueFromKV = await this.env.NAMESPACE.get("someKey"); + return new Response(valueFromKV); + } } ```