@@ -21,6 +21,9 @@ Parameters:
21
21
- ` fn ` (string): The name of the action to be executed.
22
22
- ` ...args ` (unknown[ ] ): Additional arguments to pass to the function.
23
23
24
+ Returns:
25
+ - ` Promise<string> ` : A unique identifier for the scheduled event.
26
+
24
27
### ` c.schedule.at(timestamp, fn, ...args) `
25
28
26
29
Schedules a function to be executed at a specific timestamp. This function persists across actor restarts, upgrades, or crashes.
@@ -31,6 +34,41 @@ Parameters:
31
34
- ` fn ` (string): The name of the action to be executed.
32
35
- ` ...args ` (unknown[ ] ): Additional arguments to pass to the function.
33
36
37
+ Returns:
38
+ - ` Promise<string> ` : A unique identifier for the scheduled event.
39
+
40
+ ### ` c.schedule.list() `
41
+
42
+ Lists all scheduled events for the actor.
43
+
44
+ Returns:
45
+ - ` Promise<Alarm[]> ` : An array of scheduled alarms, where each alarm has the following properties:
46
+ - ` id ` (string): The unique identifier of the alarm
47
+ - ` createdAt ` (number): The timestamp when the alarm was created
48
+ - ` triggersAt ` (number): The timestamp when the alarm will trigger
49
+ - ` fn ` (string): The name of the action to be executed
50
+ - ` args ` (unknown[ ] ): The arguments to pass to the function
51
+
52
+ ### ` c.schedule.get(alarmId) `
53
+
54
+ Gets details about a specific scheduled event.
55
+
56
+ Parameters:
57
+ - ` alarmId ` (string): The unique identifier of the alarm to retrieve.
58
+
59
+ Returns:
60
+ - ` Promise<Alarm | undefined> ` : The alarm details if found, undefined otherwise.
61
+
62
+ ### ` c.schedule.cancel(alarmId) `
63
+
64
+ Cancels a scheduled event.
65
+
66
+ Parameters:
67
+ - ` alarmId ` (string): The unique identifier of the alarm to cancel.
68
+
69
+ Returns:
70
+ - ` Promise<void> `
71
+
34
72
## Scheduling Private Actions
35
73
36
74
Currently, scheduling can only trigger public actions. If the scheduled action is private, it needs to be secured with something like a token.
@@ -46,7 +84,7 @@ const reminderService = actor({
46
84
},
47
85
48
86
actions: {
49
- setReminder : (c , userId , message , delayMs ) => {
87
+ setReminder : async (c , userId , message , delayMs ) => {
50
88
const reminderId = crypto .randomUUID ();
51
89
52
90
// Store the reminder in state
@@ -57,7 +95,89 @@ const reminderService = actor({
57
95
};
58
96
59
97
// Schedule the sendReminder action to run after the delay
60
- c .after (delayMs , " sendReminder" , reminderId );
98
+ // Store the alarmId for potential cancellation
99
+ const alarmId = await c .schedule .after (delayMs , " sendReminder" , reminderId );
100
+
101
+ return { reminderId , alarmId };
102
+ },
103
+
104
+ cancelReminder : async (c , reminderId ) => {
105
+ const reminder = c .state .reminders [reminderId ];
106
+ if (! reminder ) return { success: false };
107
+
108
+ // Cancel the scheduled reminder
109
+ await c .schedule .cancel (reminder .alarmId );
110
+
111
+ // Clean up the reminder
112
+ delete c .state .reminders [reminderId ];
113
+
114
+ return { success: true };
115
+ },
116
+
117
+ sendReminder : (c , reminderId ) => {
118
+ const reminder = c .state .reminders [reminderId ];
119
+ if (! reminder ) return ;
120
+
121
+ // Find the user's connection if they're online
122
+ const userConn = c .conns .find (
123
+ conn => conn .state .userId === reminder .userId
124
+ );
125
+
126
+ if (userConn ) {
127
+ // Send the reminder to the user
128
+ userConn .send (" reminder" , {
129
+ message: reminder .message ,
130
+ scheduledAt: reminder .scheduledFor
131
+ });
132
+ } else {
133
+ // If user is offline, store reminder for later delivery
134
+ // ...
135
+ }
136
+
137
+ // Clean up the processed reminder
138
+ delete c .state .reminders [reminderId ];
139
+ }
140
+ }
141
+ });
142
+ ```
143
+
144
+ ## Testing Schedules
145
+
146
+ ``` typescript
147
+ import { actor } from " actor-core" ;
148
+
149
+ const reminderService = actor ({
150
+ state: {
151
+ reminders: {}
152
+ },
153
+
154
+ actions: {
155
+ setReminder : async (c , userId , message , delayMs ) => {
156
+ const reminderId = crypto .randomUUID ();
157
+
158
+ // Store the reminder in state
159
+ c .state .reminders [reminderId ] = {
160
+ userId ,
161
+ message ,
162
+ scheduledFor: Date .now () + delayMs
163
+ };
164
+
165
+ // Schedule the sendReminder action to run after the delay
166
+ // Store the alarmId for potential cancellation
167
+ const alarmId = await c .schedule .after (delayMs , " sendReminder" , reminderId );
168
+
169
+ return { reminderId , alarmId };
170
+ },
171
+
172
+ cancelReminder : async (c , reminderId ) => {
173
+ const reminder = c .state .reminders [reminderId ];
174
+ if (! reminder ) return { success: false };
175
+
176
+ // Cancel the scheduled reminder
177
+ await c .schedule .cancel (reminder .alarmId );
178
+
179
+ // Clean up the reminder
180
+ delete c .state .reminders [reminderId ];
61
181
62
182
return { reminderId };
63
183
},
0 commit comments