Skip to content

Commit 22bb12a

Browse files
authored
Adding minor code block fixes (#25225)
1 parent fe5abf3 commit 22bb12a

File tree

2 files changed

+37
-34
lines changed

2 files changed

+37
-34
lines changed

src/content/docs/durable-objects/reference/in-memory-state.mdx

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,24 @@ Variables in a Durable Object will maintain state as long as your Durable Object
1414
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.
1515

1616
```js
17-
export class Counter {
18-
constructor(ctx, env) {
19-
this.ctx = ctx;
20-
// `blockConcurrencyWhile()` ensures no requests are delivered until
21-
// initialization completes.
22-
this.ctx.blockConcurrencyWhile(async () => {
23-
let stored = await this.ctx.storage.get("value");
24-
// After initialization, future reads do not need to access storage.
25-
this.value = stored || 0;
26-
});
27-
}
28-
29-
// Handle HTTP requests from clients.
30-
async fetch(request) {
31-
// use this.value rather than storage
32-
}
17+
import { DurableObject } from "cloudflare:workers";
18+
19+
export class Counter extends DurableObject {
20+
constructor(ctx, env) {
21+
super(ctx, env);
22+
// `blockConcurrencyWhile()` ensures no requests are delivered until
23+
// initialization completes.
24+
this.ctx.blockConcurrencyWhile(async () => {
25+
let stored = await this.ctx.storage.get("value");
26+
// After initialization, future reads do not need to access storage.
27+
this.value = stored || 0;
28+
});
29+
}
30+
31+
// Handle HTTP requests from clients.
32+
async fetch(request) {
33+
// use this.value rather than storage
34+
}
3335
}
3436
```
3537

src/content/docs/kv/concepts/kv-bindings.mdx

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ With this, the deployed Worker will have a `TODO` field in their environment obj
4343

4444
```js
4545
export default {
46-
async fetch(request, env, ctx) {
47-
// Get the value for the "to-do:123" key
48-
// NOTE: Relies on the `TODO` KV binding that maps to the "My Tasks" namespace.
49-
let value = await env.TODO.get("to-do:123");
50-
51-
// Return the value, as is, for the Response
52-
return new Response(value);
53-
},
46+
async fetch(request, env, ctx) {
47+
// Get the value for the "to-do:123" key
48+
// NOTE: Relies on the `TODO` KV binding that maps to the "My Tasks" namespace.
49+
let value = await env.TODO.get("to-do:123");
50+
51+
// Return the value, as is, for the Response
52+
return new Response(value);
53+
},
5454
};
5555
```
5656

@@ -83,15 +83,16 @@ kv_namespaces = [
8383
An example might look like:
8484

8585
```js
86-
export class DurableObject {
87-
constructor(ctx, env) {
88-
this.ctx = ctx;
89-
this.env = env;
90-
}
91-
92-
async fetch(request) {
93-
const valueFromKV = await this.env.NAMESPACE.get("someKey");
94-
return new Response(valueFromKV);
95-
}
86+
import { DurableObject } from "cloudflare:workers";
87+
88+
export class MyDurableObject extends DurableObject {
89+
constructor(ctx, env) {
90+
super(ctx, env);
91+
}
92+
93+
async fetch(request) {
94+
const valueFromKV = await this.env.NAMESPACE.get("someKey");
95+
return new Response(valueFromKV);
96+
}
9697
}
9798
```

0 commit comments

Comments
 (0)