You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/docs/modules/c-sharp/index.md
+62-15Lines changed: 62 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -295,30 +295,77 @@ public static void PrintInfo(ReducerContext e)
295
295
}
296
296
```
297
297
298
-
`[SpacetimeDB.Reducer]` also generates a function to schedule the given reducer in the future.
299
298
300
-
Since it's not possible to generate extension methods on existing methods, the codegen will instead add a `Schedule`-prefixed method colocated in the same namespace as the original method instead. The generated method will accept `DateTimeOffset` argument for the time when the reducer should be invoked, followed by all the arguments of the reducer itself, except those that have type `ReducerContext`.
299
+
### Scheduler Tables
300
+
Tables can be used to schedule a reducer calls either at a specific timestamp or at regular intervals.
301
301
302
302
```csharp
303
-
// Example reducer:
304
-
[SpacetimeDB.Reducer]
305
-
publicstaticvoidAdd(stringname, intage) { ... }
303
+
publicstaticpartialclassTimers
304
+
{
305
+
306
+
// The `Scheduled` attribute links this table to a reducer.
Copy file name to clipboardExpand all lines: docs/docs/modules/rust/index.md
+55-21Lines changed: 55 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -167,8 +167,6 @@ struct Person {
167
167
168
168
### Defining reducers
169
169
170
-
`#[spacetimedb(reducer)]` optionally takes a single argument, which is a frequency at which the reducer will be automatically called by the database.
171
-
172
170
`#[spacetimedb(reducer)]` is always applied to top level Rust functions. They can take arguments of types known to SpacetimeDB (just like fields of structs must be known to SpacetimeDB), and either return nothing, or return a `Result<(), E: Debug>`.
173
171
174
172
```rust
@@ -192,39 +190,75 @@ struct Item {
192
190
193
191
Note that reducers can call non-reducer functions, including standard library functions.
194
192
195
-
Reducers that are called periodically take an additional macro argument specifying the frequency at which they will be invoked. Durations are parsed according to https://docs.rs/humantime/latest/humantime/fn.parse_duration.html and will usually be a number of milliseconds or seconds.
196
193
197
-
Both of these examples are invoked every second.
194
+
There are several macros which modify the semantics of a column, which are applied to the members of the table struct. `#[unique]` and `#[autoinc]` are covered below, describing how those attributes affect the semantics of inserting, filtering, and so on.
198
195
199
-
```rust
200
-
#[spacetimedb(reducer, repeat = 1s)]
201
-
fnevery_second() {}
196
+
#[SpacetimeType]
202
197
203
-
#[spacetimedb(reducer, repeat = 1000ms)]
204
-
fnevery_thousand_milliseconds() {}
205
-
```
198
+
#[sats]
206
199
207
-
Finally, reducers can also receive a ReducerContext object, or the Timestamp at which they are invoked, just by taking parameters of those types first.
200
+
### Defining Scheduler Tables
201
+
Tables can be used to schedule a reducer calls either at a specific timestamp or at regular intervals.
208
202
209
203
```rust
210
-
#[spacetimedb(reducer, repeat = 1s)]
211
-
fntick_timestamp(time:Timestamp) {
212
-
println!("tick at {time}");
204
+
// The `scheduled` attribute links this table to a reducer.
205
+
#[spacetimedb(table, scheduled(send_message))]
206
+
structSendMessageTimer {
207
+
text:String,
213
208
}
209
+
```
214
210
215
-
#[spacetimedb(reducer, repeat = 500ms)]
216
-
fntick_ctx(ctx:ReducerContext) {
217
-
println!("tick at {}", ctx.timestamp)
211
+
The `scheduled` attribute adds a couple of default fields and expands as follows:
212
+
```rust
213
+
#[spacetimedb(table)]
214
+
structSendMessageTimer {
215
+
text:String, // original field
216
+
#[primary]
217
+
#[autoinc]
218
+
scheduled_id:u64, // identifier for internal purpose
219
+
scheduled_at:ScheduleAt, //schedule details
220
+
}
221
+
222
+
pubenumScheduleAt {
223
+
/// A specific time at which the reducer is scheduled.
224
+
/// Value is a UNIX timestamp in microseconds.
225
+
Time(u64),
226
+
/// A regular interval at which the repeated reducer is scheduled.
227
+
/// Value is a duration in microseconds.
228
+
Interval(u64),
218
229
}
219
230
```
220
231
221
-
Note that each distinct time a repeating reducer is invoked, a seperate schedule is created for that reducer. So invoking `every_second` three times from the spacetimedb cli will result in the reducer being called times times each second.
232
+
Managing timers with scheduled table is as simple as inserting or deleting rows from table.
233
+
```rust
234
+
#[spacetimedb(reducer)]
222
235
223
-
There are several macros which modify the semantics of a column, which are applied to the members of the table struct. `#[unique]` and `#[autoinc]` are covered below, describing how those attributes affect the semantics of inserting, filtering, and so on.
236
+
// Reducers linked to the scheduler table should have their first argument as `ReducerContext`
237
+
// and the second as an instance of the table struct it is linked to.
1. Add the following code to lib.rs. We are using a special attribute argument called repeat which will automatically schedule the reducer to run every 1000ms.
101
+
1. Add the following code to lib.rs. As we want to schedule `resource_spawn_agent` to run later, It will require to implement a scheduler table.
2.Since this reducer uses `rand::Rng` we need add include it.Add this `use` statement to the top of lib.rs.
161
167
162
168
```rust
163
169
use rand::Rng;
164
170
```
165
171
166
-
3.Even though our reducer is set to repeat, we still need to schedule it the first time. Add the following code to the end of the `init` reducer. You can use this `schedule!` macro to schedule any reducer to run in the future after a certain amount of time.
172
+
3.Add the following code to the end of the `init` reducerto set the reducer to repeat at every regular interval.
4.Next we need to generate our client code and publish the module.Since we changed the schema we need to make sure we include the `--clear-database` flag.Run the following commands from your Server directory:
0 commit comments