Skip to content

Commit c006b4e

Browse files
kodster28RebeccaTamachiro
authored andcommitted
update tags (#20751)
* update tags * unformat
1 parent 5f37cd7 commit c006b4e

File tree

26 files changed

+1139
-1127
lines changed

26 files changed

+1139
-1127
lines changed

src/content/docs/developer-spotlight/tutorials/handle-form-submission-with-astro-resend.mdx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ products:
88
- Workers
99
tags:
1010
- Forms
11-
- Resend
1211
- Astro
1312
languages:
1413
- TypeScript

src/content/docs/durable-objects/examples/alarms-api.mdx

Lines changed: 42 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
---
22
type: example
33
summary: Use the Durable Objects Alarms API to batch requests to a Durable Object.
4-
tags:
5-
- Durable Objects
6-
- Alarms
7-
- API
84
pcx_content_type: example
95
title: Use the Alarms API
106
sidebar:
117
order: 3
128
description: Use the Durable Objects Alarms API to batch requests to a Durable Object.
13-
149
---
1510

1611
import { GlossaryTooltip, WranglerConfig } from "~/components";
@@ -22,57 +17,57 @@ When a request is received and no alarm is set, it sets an alarm for 10 seconds
2217
If no new requests are received, no further alarms will be set until the next request arrives.
2318

2419
```js
25-
import { DurableObject } from 'cloudflare:workers';
20+
import { DurableObject } from "cloudflare:workers";
2621

2722
// Worker
2823
export default {
29-
async fetch(request, env) {
30-
let id = env.BATCHER.idFromName("foo");
31-
return await env.BATCHER.get(id).fetch(request);
32-
},
24+
async fetch(request, env) {
25+
let id = env.BATCHER.idFromName("foo");
26+
return await env.BATCHER.get(id).fetch(request);
27+
},
3328
};
3429

3530
const SECONDS = 10;
3631

3732
// Durable Object
3833
export class Batcher extends DurableObject {
39-
constructor(state, env) {
40-
this.state = state;
41-
this.storage = state.storage;
42-
this.state.blockConcurrencyWhile(async () => {
43-
let vals = await this.storage.list({ reverse: true, limit: 1 });
44-
this.count = vals.size == 0 ? 0 : parseInt(vals.keys().next().value);
45-
});
46-
}
47-
48-
async fetch(request) {
49-
this.count++;
50-
51-
// If there is no alarm currently set, set one for 10 seconds from now
52-
// Any further POSTs in the next 10 seconds will be part of this batch.
53-
let currentAlarm = await this.storage.getAlarm();
54-
if (currentAlarm == null) {
55-
this.storage.setAlarm(Date.now() + (1000 * SECONDS));
56-
}
57-
58-
// Add the request to the batch.
59-
await this.storage.put(this.count, await request.text());
60-
return new Response(JSON.stringify({ queued: this.count }), {
61-
headers: {
62-
"content-type": "application/json;charset=UTF-8",
63-
},
64-
});
65-
}
66-
67-
async alarm() {
68-
let vals = await this.storage.list();
69-
await fetch("http://example.com/some-upstream-service", {
70-
method: "POST",
71-
body: Array.from(vals.values()),
72-
});
73-
await this.storage.deleteAll();
74-
this.count = 0;
75-
}
34+
constructor(state, env) {
35+
this.state = state;
36+
this.storage = state.storage;
37+
this.state.blockConcurrencyWhile(async () => {
38+
let vals = await this.storage.list({ reverse: true, limit: 1 });
39+
this.count = vals.size == 0 ? 0 : parseInt(vals.keys().next().value);
40+
});
41+
}
42+
43+
async fetch(request) {
44+
this.count++;
45+
46+
// If there is no alarm currently set, set one for 10 seconds from now
47+
// Any further POSTs in the next 10 seconds will be part of this batch.
48+
let currentAlarm = await this.storage.getAlarm();
49+
if (currentAlarm == null) {
50+
this.storage.setAlarm(Date.now() + 1000 * SECONDS);
51+
}
52+
53+
// Add the request to the batch.
54+
await this.storage.put(this.count, await request.text());
55+
return new Response(JSON.stringify({ queued: this.count }), {
56+
headers: {
57+
"content-type": "application/json;charset=UTF-8",
58+
},
59+
});
60+
}
61+
62+
async alarm() {
63+
let vals = await this.storage.list();
64+
await fetch("http://example.com/some-upstream-service", {
65+
method: "POST",
66+
body: Array.from(vals.values()),
67+
});
68+
await this.storage.deleteAll();
69+
this.count = 0;
70+
}
7671
}
7772
```
7873

0 commit comments

Comments
 (0)