Skip to content

Commit 1a0ea98

Browse files
docs: callable functions
Signed-off-by: David Dal Busco <david.dalbusco@outlook.com>
1 parent 261f4de commit 1a0ea98

4 files changed

Lines changed: 147 additions & 39 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Defines a read-only function that returns data without modifying any state.
2+
3+
import Tabs from "@theme/Tabs";
4+
import TabItem from "@theme/TabItem";
5+
6+
```mdx-code-block
7+
<Tabs groupId="functions">
8+
<TabItem value="Rust">
9+
```
10+
11+
```rust
12+
#[ic_cdk::query]
13+
fn my_function() -> String {
14+
// Your logic here
15+
}
16+
```
17+
18+
```mdx-code-block
19+
</TabItem>
20+
<TabItem value="TypeScript">
21+
```
22+
23+
```typescript
24+
export const myFunction = defineQuery({
25+
args: Schema,
26+
returns: Schema,
27+
handler: ({ args }) => {
28+
// Your logic here
29+
return args;
30+
}
31+
});
32+
```
33+
34+
```mdx-code-block
35+
</TabItem>
36+
</Tabs>
37+
```
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Defines a function that can read and write state.
2+
3+
import Tabs from "@theme/Tabs";
4+
import TabItem from "@theme/TabItem";
5+
6+
```mdx-code-block
7+
<Tabs groupId="functions">
8+
<TabItem value="Rust">
9+
```
10+
11+
```rust
12+
#[ic_cdk::update]
13+
fn my_function() -> String {
14+
// Your logic here
15+
}
16+
```
17+
18+
```mdx-code-block
19+
</TabItem>
20+
<TabItem value="TypeScript">
21+
```
22+
23+
```typescript
24+
export const myFunction = defineUpdate({
25+
args: Schema,
26+
returns: Schema,
27+
handler: async ({ args }) => {
28+
// Your logic here
29+
return args;
30+
}
31+
});
32+
```
33+
34+
```mdx-code-block
35+
</TabItem>
36+
</Tabs>
37+
```

docs/build/functions/development/index.mdx

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,116 +4,140 @@ When you're ready to implement Functions within your Juno Satellite, you'll have
44

55
---
66

7-
## Hooks
7+
## Event-driven Functions
8+
9+
Event-driven functions respond to actions occurring in your Satellite. They are triggered automatically and never invoked directly.
10+
11+
---
12+
13+
### Hooks
814

915
Hooks allow you to define event-driven logic that responds to specific actions within your Satellite. The following is a list of available hooks and their respective use cases.
1016

11-
### on_set_doc
17+
#### on_set_doc
1218

1319
import OnSetDoc from "./components/on-set-doc.mdx";
1420

1521
<OnSetDoc />
1622

17-
### on_set_many_docs
23+
#### on_set_many_docs
1824

1925
import OnSetManyDocs from "./components/on-set-many-docs.mdx";
2026

2127
<OnSetManyDocs />
2228

23-
### on_delete_doc
29+
#### on_delete_doc
2430

2531
import OnDeleteDoc from "./components/on-delete-doc.mdx";
2632

2733
<OnDeleteDoc />
2834

29-
### on_delete_many_docs
35+
#### on_delete_many_docs
3036

3137
import OnDeleteManyDocs from "./components/on-delete-many-docs.mdx";
3238

3339
<OnDeleteManyDocs />
3440

35-
### on_delete_filtered_docs
41+
#### on_delete_filtered_docs
3642

3743
import OnDeleteFilteredDocs from "./components/on-delete-filtered-docs.mdx";
3844

3945
<OnDeleteFilteredDocs />
4046

41-
### on_upload_asset
47+
#### on_upload_asset
4248

4349
import OnUploadAsset from "./components/on-upload-asset.mdx";
4450

4551
<OnUploadAsset />
4652

47-
### on_delete_asset
53+
#### on_delete_asset
4854

4955
import OnDeleteAsset from "./components/on-delete-asset.mdx";
5056

5157
<OnDeleteAsset />
5258

53-
### on_delete_many_assets
59+
#### on_delete_many_assets
5460

5561
import OnDeleteManyAssets from "./components/on-delete-many-assets.mdx";
5662

5763
<OnDeleteManyAssets />
5864

59-
### on_delete_filtered_assets
65+
#### on_delete_filtered_assets
6066

6167
import OnDeleteFilteredAssets from "./components/on-delete-filtered-assets.mdx";
6268

6369
<OnDeleteFilteredAssets />
6470

65-
### on_init
71+
#### on_init
6672

6773
import OnInit from "./components/on-init.mdx";
6874

6975
<OnInit />
7076

71-
### on_post_upgrade
77+
#### on_post_upgrade
7278

7379
import OnPostUpgrade from "./components/on-post-upgrade.mdx";
7480

7581
<OnPostUpgrade />
7682

7783
---
7884

79-
## Assertions
85+
### Assertions
8086

8187
Assertions enable validation checks before specific actions are executed within your Satellite. The following is a list of available assertions and their functionalities.
8288

83-
### assert_set_doc
89+
#### assert_set_doc
8490

8591
import AssertSetDoc from "./components/assert-set-doc.mdx";
8692

8793
<AssertSetDoc />
8894

89-
### assert_delete_doc
95+
#### assert_delete_doc
9096

9197
import AssertDeleteDoc from "./components/assert-delete-doc.mdx";
9298

9399
<AssertDeleteDoc />
94100

95-
### assert_upload_asset
101+
#### assert_upload_asset
96102

97103
import AssertUploadAsset from "./components/assert-upload-asset.mdx";
98104

99105
<AssertUploadAsset />
100106

101-
### assert_delete_asset
107+
#### assert_delete_asset
102108

103109
import AssertDeleteAsset from "./components/assert-delete-asset.mdx";
104110

105111
<AssertDeleteAsset />
106112

107113
---
108114

109-
## When the Functions Run
115+
### When Hooks and Assertions Run
110116

111117
import Collections from "./components/collections.mdx";
112118

113119
<Collections />
114120

115121
---
116122

123+
## Callable Functions
124+
125+
Callable functions (also referenced as custom functions) are explicitly invoked — from your frontend or from other canisters.
126+
127+
### Query
128+
129+
import Query from "./components/query.mdx";
130+
131+
<Query />
132+
133+
### Update
134+
135+
import Update from "./components/update.mdx";
136+
137+
<Update />
138+
139+
---
140+
117141
## Further Resources
118142

119143
import Resources from "./components/further-resources.mdx";

docs/build/functions/index.md

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,28 @@ keywords:
1616

1717
Functions are a set of features enabling developers to extend the native capabilities of [Satellites](../../terminology.mdx#satellite) using Rust or TypeScript. They let you define serverless behaviors directly within your containerized environment.
1818

19-
Triggered by events like document and asset operations, they allow you to automatically run backend code or assertions. These functions are shipped with your container and don’t require you to manage or scale your own servers.
19+
There are two types of functions:
2020

21-
---
22-
23-
## How does it work?
24-
25-
Functions are defined using hooks that automatically respond to document and asset events such as create, update, or delete. This allows you to embed dynamic behavior directly into your container.
21+
- [Event-driven functions](#event-driven-functions): triggered automatically in response to actions such as a document being created or an asset being uploaded.
22+
- [Callable functions](#callable-functions): explicitly invoked from your frontend or from other services.
2623

27-
A naive schema representation of a hook that is triggered when a document is set:
28-
29-
![Functions hooks flow](../../img/functions.png)
24+
---
3025

31-
### Asynchronous Hook Spawning
26+
## Event-driven Functions
3227

33-
When a Function is triggered, it spawns hooks asynchronously, operating independently of the caller's action. This means that the execution of the hooks is initiated without waiting for a synchronous response, ensuring that the flow of update calls to the Satellite remains unhindered. Consequently, callers may receive feedback on their actions before the corresponding hooks have commenced their execution.
28+
Event-driven functions respond to actions occurring in your Satellite. They are triggered automatically and never invoked directly.
3429

35-
### Error-Resilient Execution
30+
### Hooks
3631

37-
Hooks are initiated only when there are no errors in the preceding operations. This ensures a robust and dependable execution flow, promoting reliability and consistency in the functioning of Functions.
32+
Hooks respond to specific actions within your Satellite. They run asynchronously, independently of the caller's request - meaning the caller may receive a response before the hook has finished executing.
3833

39-
### Optional
34+
![Functions hooks flow](../../img/functions.png)
4035

41-
Custom hooks are not active by default. You need to opt in to enable event-driven execution of your own logic.
36+
:::note
4237

43-
---
38+
Hooks are only initiated when the preceding operation completes without errors.
4439

45-
## Available Hooks
40+
:::
4641

4742
| Hook | Provider | Description |
4843
| --------------------------- | --------- | --------------------------------------------------------------- |
@@ -58,11 +53,9 @@ Custom hooks are not active by default. You need to opt in to enable event-drive
5853
| `on_init` | Satellite | Called during the initialization of the Satellite. |
5954
| `on_post_upgrade` | Satellite | Invoked after the Satellite has been upgraded to a new version. |
6055

61-
---
62-
63-
## Assertions
56+
### Assertions
6457

65-
In addition to hooks, developers have the option to expand the native rule set of their Satellites by creating custom assertions. These assertions can be implemented similarly to hooks, with the key difference being that they are synchronous and must return a result indicating the outcome of the assertion.
58+
Assertions run synchronously before an operation is executed. They allow you to validate or reject actions before any data is written.
6659

6760
| Assertion | Provider | Description |
6861
| --------------------- | --------- | --------------------------------------------- |
@@ -73,6 +66,23 @@ In addition to hooks, developers have the option to expand the native rule set o
7366

7467
---
7568

69+
## Callable Functions
70+
71+
Callable functions are explicitly invoked — from your frontend or from other services. They expose query and update endpoints directly from your Satellite.
72+
73+
:::tip
74+
75+
This is conceptually similar to exposing your own endpoints on an API.
76+
77+
:::
78+
79+
| Type | Description |
80+
| -------- | --------------------------------------------------------------- |
81+
| `query` | A read-only function that returns data without modifying state. |
82+
| `update` | A function that can read and write state. |
83+
84+
---
85+
7686
## Rust vs. TypeScript
7787

7888
You can write serverless functions in either [Rust](./development/rust.mdx) or [TypeScript](./development/typescript.mdx), depending on your needs and project goals.

0 commit comments

Comments
 (0)