Skip to content

Commit 8f9a85c

Browse files
committed
Quick review + removing TypeScriptExample to avoid
breaking the docs.
1 parent 12d91d9 commit 8f9a85c

File tree

1 file changed

+77
-104
lines changed

1 file changed

+77
-104
lines changed

src/content/docs/durable-objects/examples/reference-do-name-using-init.mdx

Lines changed: 77 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -10,88 +10,76 @@ sidebar:
1010
description: Access the name from within a Durable Object using RpcTarget.
1111
---
1212

13-
import {
14-
TabItem,
15-
Tabs,
16-
GlossaryTooltip,
17-
TypeScriptExample,
18-
} from "~/components";
13+
import { TabItem, Tabs, GlossaryTooltip, TypeScriptExample } from "~/components";
1914

20-
Once you create a Durable Object using [`idFromName`](/durable-objects/api/namespace/#idfromname), you cannot access the Durable Object [`name`](/durable-objects/api/id/#name) within the Durable Object class itself. Moreover, if you want to set some metadata for the Durable Object, you can't do it directly in the Durable Object. You will have to pass the metadata to every method you call on the Durable Object. This is not ideal.
15+
Once you create a Durable Object using [`idFromName`](/durable-objects/api/namespace/#idfromname), you cannot access the Durable Object [`name`](/durable-objects/api/id/#name) within the Durable Object class itself. Moreover, if you want to set some metadata for the Durable Object, you cannot do it directly in the Durable Object. Instead, you have to pass the metadata to every method you call on the Durable Object, which is not ideal.
2116

22-
A solution to this is using an `RpcTarget` class. . You can return an instance of this class from the the Durable Object class and use it to configure the Durable Object metadata.
17+
A solution to this is using an `RpcTarget` class. You can return an instance of this class from the the Durable Object class, and use it to configure the Durable Object metadata.
2318

24-
Based on your needs, you can either store the metadata in the `RpcTarget` class that will be temporary, or use Durable Object storage to store the metadata and reference it in the `RpcTarget` class.
19+
Based on your needs, you can either store the metadata in the `RpcTarget` class will be temporary, or use Durable Object storage to store the metadata and reference it in the `RpcTarget` class.
2520

26-
This example does not persist the Durable Object metadata. It demonstrates how to
21+
This example does not persist the Durable Object metadata. It demonstrates how to:
2722

28-
1. create an `RpcTarget` class
29-
2. set the Durable Object metadata (name in this example) in the `RpcTarget` class
30-
3. pass the metadata (name in this example) to a Durable Object method
31-
4. cleans up the `RpcTarget` class after use
23+
1. Create an `RpcTarget` class
24+
2. Set the Durable Object metadata (name in this example) in the `RpcTarget` class
25+
3. Pass the metadata (name in this example) to a Durable Object method
26+
4. Cleans up the `RpcTarget` class after use
3227

3328
<TypeScriptExample>
3429
```ts
3530
import { DurableObject, RpcTarget } from 'cloudflare:workers';
3631

37-
/**
38-
* Create an RpcDO class that extends RpcTarget
39-
* Use this class to set the Durable Object metadata
40-
* Pass the metadata in the Durable Object methods
41-
* @param mainDo - The main Durable Object class
42-
* @param doName - The name of the Durable Object
43-
*/
32+
// * Create an RpcDO class that extends RpcTarget
33+
// * Use this class to set the Durable Object metadata
34+
// * Pass the metadata in the Durable Object methods
35+
// * @param mainDo - The main Durable Object class
36+
// * @param doName - The name of the Durable Object
4437

4538
export class RpcDO extends RpcTarget {
4639
constructor(private mainDo: MyDurableObject, private doName: string) {
4740
super();
4841
}
4942

50-
/**
51-
* Pass the name to the Durable Object method
52-
* @param name - The name to pass to the Durable Object method
53-
*/
43+
// * Pass the name to the Durable Object method
44+
// * @param name - The name to pass to the Durable Object method
45+
5446
async passDoName(name: string): Promise<string> {
5547

5648
// Call the Durable Object method and pass the name and the Durable Object name
5749
return this.mainDo.getDoName(name, this.doName);
5850
}
5951

60-
/**
61-
* Call the Durable Object method without passing the Durable Object name
62-
* @param name - The name to pass to the Durable Object method
63-
*/
52+
// * Call the Durable Object method without passing the Durable Object name
53+
// * @param name - The name to pass to the Durable Object method
54+
6455
async noDoName(name: string) {
6556
return this.mainDo.noName(name);
6657
}
6758

6859
}
6960

70-
/**
71-
* Create a Durable Object class
72-
* You can use the RpcDO class to set the Durable Object metadata
73-
*/
61+
// * Create a Durable Object class
62+
// * You can use the RpcDO class to set the Durable Object metadata
7463

7564
export class MyDurableObject extends DurableObject<Env> {
7665
constructor(ctx: DurableObjectState, env: Env) {
7766
super(ctx, env);
7867
}
7968

80-
/**
81-
* Initialize the RpcDO class
82-
* You can set the Durable Object metadata here
83-
* It reutrns an instance of the RpcDO class
84-
* @param doName - The name of the Durable Object
85-
*/
69+
70+
// * Initialize the RpcDO class
71+
// * You can set the Durable Object metadata here
72+
// * It returns an instance of the RpcDO class
73+
// * @param doName - The name of the Durable Object
74+
8675
async setMetaData(doName: string) {
8776
return new RpcDO(this, doName);
8877
}
8978

90-
/**
91-
* Function that takes the name and the Durable Object name
92-
* @param name - The name to pass to the Durable Object method
93-
* @param doName - The name of the Durable Object
94-
*/
79+
// * Function that takes the name and the Durable Object name
80+
// * @param name - The name to pass to the Durable Object method
81+
// * @param doName - The name of the Durable Object
82+
9583
async getDoName(name: string, doName: string): Promise<string> {
9684
console.log({
9785
name: name,
@@ -100,19 +88,16 @@ export class RpcDO extends RpcTarget {
10088
return `Hello, ${name}! The name of this DO is ${doName}`;
10189
}
10290

103-
/**
104-
* Function that is not in the RpcTarget
105-
* Not every function has to be in the RpcTarget
106-
*/
91+
// * Function that is not in the RpcTarget
92+
// * Not every function has to be in the RpcTarget
10793

10894
private async notInRpcTarget() {
10995
return 'This is not in the RpcTarget';
11096
}
11197

112-
/**
113-
* Function that takes the name and doesn't use the Durable Object name
114-
* @param name - The name to pass to the Durable Object method
115-
*/
98+
// * Function that takes the name and doesn't use the Durable Object name
99+
// * @param name - The name to pass to the Durable Object method
100+
116101
async noName(name: string) {
117102
// Call the private function that is not in the RpcTarget
118103
console.log(this.notInRpcTarget());
@@ -127,10 +112,10 @@ export default {
127112
let id: DurableObjectId = env.MY_DURABLE_OBJECT.idFromName(new URL(request.url).pathname);
128113
let stub = env.MY_DURABLE_OBJECT.get(id);
129114

130-
/**
131-
* Set the Durable Object metadata using the RpcTarget
132-
* Notice that no await is needed here
133-
*/
115+
116+
// * Set the Durable Object metadata using the RpcTarget
117+
// * Notice that no await is needed here
118+
134119
const rpcTarget = stub.setMetaData(id.name ?? 'default');
135120

136121
// Call the Durable Object method using the RpcTarget.
@@ -148,7 +133,7 @@ export default {
148133
console.error({
149134
message: 'RpcTarget could not be cleaned up.',
150135
error: String(e),
151-
errorPropertirs: e,
136+
errorProperties: e,
152137
});
153138
}
154139

@@ -160,74 +145,67 @@ export default {
160145
```
161146
</TypeScriptExample>
162147

148+
163149
This example persists the Durable Object metadata. It demonstrates similar steps as the previous example, but uses Durable Object storage to store the metadata.
164150

165-
<TypeScriptExample>
151+
166152
```ts
167153
import { DurableObject, RpcTarget } from 'cloudflare:workers';
168154

169-
/**
170-
* Create an RpcDO class that extends RpcTarget
171-
* Use this class to set the Durable Object metadata
172-
* Pass the metadata in the Durable Object methods
173-
* @param mainDo - The main Durable Object class
174-
* @param doName - The name of the Durable Object
175-
*/
155+
// * Create an RpcDO class that extends RpcTarget
156+
// * Use this class to set the Durable Object metadata
157+
// * Pass the metadata in the Durable Object methods
158+
// * @param mainDo - The main Durable Object class
159+
// * @param doName - The name of the Durable Object
176160

177161
export class RpcDO extends RpcTarget {
178162
constructor(private mainDo: MyDurableObject, private doName: string) {
179163
super();
180164
}
181165

182-
/**
183-
* Pass the name to the Durable Object method
184-
* @param name - The name to pass to the Durable Object method
185-
*/
166+
// * Pass the name to the Durable Object method
167+
// * @param name - The name to pass to the Durable Object method
168+
186169
async passDoName(name: string): Promise<string> {
187170

188171
// Call the Durable Object method and pass the name and the Durable Object name
189172
return this.mainDo.getDoName(name, this.doName);
190173
}
191174

192-
/**
193-
* Call the Durable Object method without passing the Durable Object name
194-
* @param name - The name to pass to the Durable Object method
195-
*/
175+
// * Call the Durable Object method without passing the Durable Object name
176+
// * @param name - The name to pass to the Durable Object method
177+
196178
async noDoName(name: string) {
197179
return this.mainDo.noName(name);
198180
}
199181

200182
}
201183

202-
/**
203-
* Create a Durable Object class
204-
* You can use the RpcDO class to set the Durable Object metadata
205-
*/
184+
// * Create a Durable Object class
185+
// * You can use the RpcDO class to set the Durable Object metadata
206186

207187
export class MyDurableObject extends DurableObject<Env> {
208188
constructor(ctx: DurableObjectState, env: Env) {
209189
super(ctx, env);
210190
}
211191

212-
/**
213-
* Initialize the RpcDO class
214-
* You can set the Durable Object metadata here
215-
* It reutrns an instance of the RpcDO class
216-
* @param doName - The name of the Durable Object
217-
*/
192+
// * Initialize the RpcDO class
193+
// * You can set the Durable Object metadata here
194+
// * It returns an instance of the RpcDO class
195+
// * @param doName - The name of the Durable Object
196+
218197
async setMetaData(doName: string) {
219198
// Use DO storage to store the Durable Object name
220199
await this.ctx.storage.put('doName', doName);
221200
return new RpcDO(this, doName);
222201
}
223202

224-
/**
225-
* Function that takes the name and the Durable Object name
226-
* @param name - The name to pass to the Durable Object method
227-
* @param doName - The name of the Durable Object
228-
*/
203+
// * Function that takes the name and the Durable Object name
204+
// * @param name - The name to pass to the Durable Object method
205+
// * @param doName - The name of the Durable Object
206+
229207
async getDoName(name: string, doName: string): Promise<string> {
230-
// If doName is not availabe from the RpcTarget class, get if from the storage
208+
// If doName is not available from the RpcTarget class, get if from the storage
231209
doName = doName ?? await this.ctx.storage.get('doName');
232210
console.log({
233211
name: name,
@@ -236,19 +214,16 @@ export class RpcDO extends RpcTarget {
236214
return `Hello, ${name}! The name of this DO is ${doName}`;
237215
}
238216

239-
/**
240-
* Function that is not in the RpcTarget
241-
* Not every function has to be in the RpcTarget
242-
*/
217+
// * Function that is not in the RpcTarget
218+
// * Not every function has to be in the RpcTarget
243219

244220
private async notInRpcTarget() {
245221
return 'This is not in the RpcTarget';
246222
}
247223

248-
/**
249-
* Function that takes the name and doesn't use the Durable Object name
250-
* @param name - The name to pass to the Durable Object method
251-
*/
224+
// * Function that takes the name and doesn't use the Durable Object name
225+
// * @param name - The name to pass to the Durable Object method
226+
252227
async noName(name: string) {
253228
// Call the private function that is not in the RpcTarget
254229
console.log(this.notInRpcTarget());
@@ -263,10 +238,9 @@ export default {
263238
let id: DurableObjectId = env.MY_DURABLE_OBJECT.idFromName(new URL(request.url).pathname);
264239
let stub = env.MY_DURABLE_OBJECT.get(id);
265240

266-
/**
267-
* Set the Durable Object metadata using the RpcTarget
268-
* Notice that no await is needed here
269-
*/
241+
// * Set the Durable Object metadata using the RpcTarget
242+
// * Notice that no await is needed here
243+
270244
const rpcTarget = stub.setMetaData(id.name ?? 'default');
271245

272246
// Call the Durable Object method using the RpcTarget.
@@ -284,7 +258,7 @@ export default {
284258
console.error({
285259
message: 'RpcTarget could not be cleaned up.',
286260
error: String(e),
287-
errorPropertirs: e,
261+
errorProperties: e,
288262
});
289263
}
290264

@@ -293,5 +267,4 @@ export default {
293267

294268
} satisfies ExportedHandler<Env>;
295269

296-
```
297-
</TypeScriptExample>
270+
```

0 commit comments

Comments
 (0)