Skip to content

Commit d9e7768

Browse files
committed
Adding fetch and alarm to base class chapter.
Actioning other PR comments.
1 parent 5a9944d commit d9e7768

File tree

3 files changed

+52
-18
lines changed

3 files changed

+52
-18
lines changed

src/content/docs/durable-objects/api/base.mdx

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,38 @@ sidebar:
77

88
import { Render, Tabs, TabItem, GlossaryTooltip, Type, MetaInfo } from "~/components";
99

10-
{/* DEFINITION OF DO BASE CLASS TBC */}
10+
The `DurableObject` base class is an abstract class which all Durable Objects inherit from. This base class provides a set of optional methods, frequently referred to as handler methods, which can respond to events, for example a webSocketMessage when using the [WebSocket Hibernation API](#/durable-objects/best-practices/websockets/#websocket-hibernation-api). To provide a concrete example, here is a Durable Object `MyDurableObject` which extends `DurableObject` and implements the fetch handler to return "Hello, World!" to the calling Worker.
11+
12+
<TypeScriptExample>
13+
```ts
14+
export class MyDurableObject extends DurableObject {
15+
constructor(ctx: DurableObjectState, env: Env) {
16+
super(ctx, env);
17+
}
18+
19+
async fetch(request: Request) {
20+
return new Response("Hello, World!");
21+
}
22+
}
23+
```
24+
</TypeScriptExample>
1125

1226
## Methods
1327

28+
### `fetch`
29+
30+
- <code>fetch(<Type text="Request"/>)</code>: <Type text="Response"/> | <Type text="Promise <Response>"/>
31+
32+
- Takes an HTTP request object and returns an HTTP response object. This method allows the Durable Object to emulate an HTTP server where a Worker with a binding to that object is the client.
33+
34+
- This method can be `async`.
35+
36+
### `alarm`
37+
38+
- <code>alarm()</code>: <Type text="Promise <void>"/>
39+
40+
- See [Alarms](/durable-objects/api/alarms/).
41+
1442
### `webSocketMessage`
1543

1644
- <code> webSocketMessage(ws <Type text="WebSocket" />, message{" "} <Type text="string | ArrayBuffer" />)</code>: <Type text="void" />
@@ -39,7 +67,13 @@ import { Render, Tabs, TabItem, GlossaryTooltip, Type, MetaInfo } from "~/compon
3967

4068
## Properties
4169

42-
{/* Properties of DO Base Class TBC */}
70+
### `DurableObjectState`
71+
72+
See [`DurableObjectState` documentation](/durable-objects/api/state/).
73+
74+
### `Env`
75+
76+
A list of bindings which are available to the Durable Object.
4377

4478
## Related resources
4579

src/content/docs/durable-objects/api/state.mdx

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -267,22 +267,6 @@ export class MyDurableObject extends DurableObject {
267267

268268
- None.
269269

270-
## Extended methods
271-
272-
### `serializeAttachment`
273-
274-
- <code> serializeAttachment(value <Type text="any" />)</code>: <Type text="void" />
275-
276-
- Keeps a copy of `value` in memory (not on disk) to survive hibernation. The value can be any type supported by the [structured clone algorithm](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm), which is true of most types.
277-
278-
- If you modify `value` after calling this method, those changes will not be retained unless you call this method again. The serialized size of `value` is limited to 2,048 bytes, otherwise this method will throw an error. If you need larger values to survive hibernation, use the [Storage API](/durable-objects/api/storage-api/) and pass the corresponding key to this method so it can be retrieved later.
279-
280-
### `deserializeAttachment`
281-
282-
- `deserializeAttachment()`: <Type text='any' />
283-
284-
- Retrieves the most recent value passed to `serializeAttachment()`, or `null` if none exists.
285-
286270
## Properties
287271

288272
### `id`

src/content/docs/durable-objects/best-practices/websockets.mdx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,22 @@ If you are using older versions, note that while hibernatable WebSocket events s
379379

380380
:::
381381

382+
## Extended methods
383+
384+
### `serializeAttachment`
385+
386+
- <code> serializeAttachment(value <Type text="any" />)</code>: <Type text="void" />
387+
388+
- Keeps a copy of `value` in memory (not on disk) to survive hibernation. The value can be any type supported by the [structured clone algorithm](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm), which is true of most types.
389+
390+
- If you modify `value` after calling this method, those changes will not be retained unless you call this method again. The serialized size of `value` is limited to 2,048 bytes, otherwise this method will throw an error. If you need larger values to survive hibernation, use the [Storage API](/durable-objects/api/storage-api/) and pass the corresponding key to this method so it can be retrieved later.
391+
392+
### `deserializeAttachment`
393+
394+
- `deserializeAttachment()`: <Type text='any' />
395+
396+
- Retrieves the most recent value passed to `serializeAttachment()`, or `null` if none exists.
397+
382398
## Related resources
383399

384400
- [Mozilla Developer Network's (MDN) documentation on the WebSocket class](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket)

0 commit comments

Comments
 (0)