Skip to content

Commit b5f3ce8

Browse files
gefjoncoolreader18
andauthored
Add docs for procedures (#3766)
# Description of Changes In the spirit of our planned move to concept-based documentation rather than language-based documentation, I've chosen to add a quick section to the overview, and then a new page for documentation about procedures. I have not updated any tutorials or reference pages. # API and ABI breaking changes N/a - docs # Expected complexity level and risk 1 - docs # Testing None. --------- Co-authored-by: Noa <coolreader18@gmail.com>
1 parent 4588f77 commit b5f3ce8

19 files changed

+631
-0
lines changed

docs/docs/01-intro/01-overview.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,124 @@ or at a specific time.
289289
</TabItem>
290290
</Tabs>
291291

292+
### Procedure
293+
294+
A **procedure** is a function exported by a [database](#database), similar to a [reducer](#reducer).
295+
Connected [clients](#client-side-sdks) can call procedures.
296+
Procedures can perform additional operations not possible in reducers, including making HTTP requests to external services.
297+
However, procedures don't automatically run in database transactions,
298+
and must manually open and commit a transaction in order to read from or modify the database state.
299+
300+
Procedures are currently in beta, and their API may change in upcoming SpacetimeDB releases.
301+
302+
<Tabs groupId="syntax" queryString>
303+
<TabItem value="rust" label="Rust">
304+
305+
Because procedures are unstable, Rust modules that define them must opt in to the `unstable` feature in their `Cargo.toml`:
306+
307+
```toml
308+
[dependencies]
309+
spacetimedb = { version = "1.x", features = ["unstable"] }
310+
```
311+
312+
Then, that module can define a procedure:
313+
314+
```rust
315+
#[spacetimedb::procedure]
316+
pub fn make_request(ctx: &mut spacetimedb::ProcedureContext) -> String {
317+
// ...
318+
}
319+
```
320+
321+
And a Rust [client](#client) can call that procedure:
322+
323+
```rust
324+
fn main() {
325+
// ...setup code, then...
326+
ctx.procedures.make_request();
327+
}
328+
```
329+
330+
A Rust [client](#client) can also register a callback to run when a procedure call finishes, which will be invoked with that procedure's return value:
331+
332+
```rust
333+
fn main() {
334+
// ...setup code, then...
335+
ctx.procedures.make_request_then(|ctx, res| {
336+
match res {
337+
Ok(string) => log::info!("Procedure `make_request` returned {string}"),
338+
Err(e) => log::error!("Procedure `make_request` failed! {e:?}"),
339+
}
340+
})
341+
}
342+
```
343+
344+
</TabItem>
345+
<TabItem value="csharp" label="C#">
346+
347+
C# modules currently cannot define procedures. Support for defining procedures in C# modules will be released shortly.
348+
349+
A C# [client](#client) can call a procedure defined by a Rust or TypeScript module:
350+
351+
```csharp
352+
void Main()
353+
{
354+
// ...setup code, then...
355+
ctx.Procedures.MakeRequest();
356+
}
357+
```
358+
359+
A C# [client](#client) can also register a callback to run when a procedure call finishes, which will be invoked with that procedure's return value:
360+
361+
```csharp
362+
void Main()
363+
{
364+
// ...setup code, then...
365+
ctx.Procedures.MakeRequestThen((ctx, res) =>
366+
{
367+
if (res.IsSuccess)
368+
{
369+
Log.Debug($"Procedure `make_request` returned {res.Value!}");
370+
}
371+
else
372+
{
373+
throw new Exception($"Procedure `make_request` failed: {res.Error!}");
374+
}
375+
});
376+
}
377+
```
378+
379+
</TabItem>
380+
<TabItem value="typescript" label="TypeScript">
381+
382+
A procedure can be defined in a TypeScript module:
383+
384+
```typescript
385+
spacetimedb.procedure("make_request", t.string(), ctx => {
386+
// ...
387+
})
388+
```
389+
390+
And a TypeScript [client](#client) can call that procedure:
391+
392+
```typescript
393+
ctx.procedures.makeRequest();
394+
```
395+
396+
A Rust [client](#client) can also register a callback to run when a procedure call finishes, which will be invoked with that procedure's return value:
397+
398+
```typescript
399+
ctx.procedures.makeRequest().then(
400+
res => console.log(`Procedure make_request returned ${res}`),
401+
err => console.error(`Procedure make_request failed! ${err}`),
402+
);
403+
```
404+
405+
</TabItem>
406+
</Tabs>
407+
408+
See [Procedures](/procedures) for more details about procedures.
409+
292410
### Client
293411

294412
A **client** is an application that connects to a [database](#database). A client logs in using an [identity](#identity) and receives an [connection id](#connectionid) to identify the connection. After that, it can call [reducers](#reducer) and query public [tables](#table).

0 commit comments

Comments
 (0)