Skip to content

Commit e04f57c

Browse files
committed
fix: add rtk selector to collaborative stores
1 parent 3db1209 commit e04f57c

File tree

1 file changed

+160
-17
lines changed
  • src/content/docs/realtime/realtimekit/collaborative-stores

1 file changed

+160
-17
lines changed

src/content/docs/realtime/realtimekit/collaborative-stores/index.mdx

Lines changed: 160 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,49 @@ sidebar:
66
order: 7
77
---
88

9+
import RTKSDKSelector from "~/components/realtimekit/RTKSDKSelector/RTKSDKSelector.astro";
10+
import RTKCodeSnippet from "~/components/realtimekit/RTKCodeSnippet/RTKCodeSnippet.astro";
11+
912
The RealtimeKit Stores API allows you to create multiple key-value pair realtime stores. Users can subscribe to changes in a store and receive real-time updates. Data is stored until a [session](/realtime/realtimekit/concepts/meeting/#session) is active.
1013

14+
<RTKSDKSelector />
15+
1116
### Create a Store
1217

1318
You can create a realtime store (changes are synced with other users):
1419

15-
| Param | Type | Description | Required |
16-
| --------- | --- | --- | --- |
17-
| `name` | string | Name of the store | true |
20+
| Param | Type | Description | Required |
21+
| ------ | ------ | ----------------- | -------- |
22+
| `name` | string | Name of the store | true |
1823

1924
To create a store:
2025

21-
```ts
26+
<RTKCodeSnippet id="web-react">
27+
28+
```ts
29+
30+
const stores = useRealtimeKitSelector((m) => m.stores);
31+
const store = stores.create('myStore');
32+
33+
````
34+
</RTKCodeSnippet>
35+
<RTKCodeSnippet id="web-angular">
36+
37+
```ts
38+
2239
const store = meeting.stores.create('myStore');
23-
```
40+
41+
````
42+
43+
</RTKCodeSnippet>
44+
<RTKCodeSnippet id="web-web-components">
45+
46+
```ts
47+
48+
const store = meeting.stores.create('myStore');
49+
50+
````
51+
</RTKCodeSnippet>
2452

2553
:::note
2654
This method must be executed for every user.
@@ -30,55 +58,152 @@ This method must be executed for every user.
3058

3159
You can add, update or delete entires in a store:
3260

33-
| Param | Type | Description | Required |
34-
| ----- | ---------- | ----------------------------------------------------------- | -------- |
61+
| Param | Type | Description | Required |
62+
| ------- | ---------- | ----------------------------------------------------------- | -------- |
3563
| `key` | string | Unique identifier used to store/update a value in the store | Yes |
3664
| `value` | StoreValue | Value that can be stored agains a key | Yes |
3765

66+
<RTKCodeSnippet id="web-react">
3867

3968
```ts
4069
type StoreValue = string | number | object | array;
70+
````
71+
72+
```ts
73+
const stores = useRealtimeKitSelector((m) => m.stores.stores);
74+
const store = stores.get("myStore");
75+
76+
await store.set("user", { name: "John Doe" });
77+
78+
await store.update("user", { age: 34 }); // { name: 'John Doe', age: 34 }
79+
80+
await store.delete("user");
4181
```
82+
83+
</RTKCodeSnippet>
84+
<RTKCodeSnippet id="web-angular">
85+
86+
```ts
87+
type StoreValue = string | number | object | array;
88+
```
89+
4290
```ts
4391
const { stores } = meeting.stores;
44-
const store = stores.get('myStore');
92+
const store = stores.get("myStore");
93+
94+
await store.set("user", { name: "John Doe" });
95+
96+
await store.update("user", { age: 34 }); // { name: 'John Doe', age: 34 }
97+
98+
await store.delete("user");
99+
```
100+
101+
</RTKCodeSnippet>
102+
<RTKCodeSnippet id="web-web-components">
103+
104+
```ts
105+
type StoreValue = string | number | object | array;
106+
```
107+
108+
```ts
109+
const { stores } = meeting.stores;
110+
const store = stores.get("myStore");
45111

46-
await store.set('user', { name: 'John Doe' });
112+
await store.set("user", { name: "John Doe" });
47113

48-
await store.update('user', { age: 34 }); // { name: 'John Doe', age: 34 }
114+
await store.update("user", { age: 34 }); // { name: 'John Doe', age: 34 }
49115

50-
await store.delete('user');
116+
await store.delete("user");
51117
```
52118

119+
</RTKCodeSnippet>
120+
53121
:::note
54122
The `set` method overwrites the existing value, while the `update` method updates the existing value.
55-
123+
56124
For example, if the stored value is `['a', 'b']` and you call `update` with `['c']`, the final value will be `['a', 'b', 'c']`.
57125
:::
58126

59-
60127
### Subscribe to a Store
61128

62129
You can attach event listeners on a store's key, which fire when the value changes.
63130

131+
<RTKCodeSnippet id="web-react">
64132
```ts
65-
const { stores } = meeting.stores;
133+
const stores = useRealtimeKitSelector((m) => m.stores.stores);
66134
const store = stores.get('myStore');
67135
store.subscribe('key', (data) => {
68136
console.log(data);
69137
});
70138

71139
// subscribe to all keys of a store
72-
store.subscribe('*', (data) => {
140+
store.subscribe('\*', (data) => {
141+
console.log(data);
142+
});
143+
144+
store.unsubscribe('key');
145+
146+
````
147+
148+
</RTKCodeSnippet>
149+
150+
<RTKCodeSnippet id="web-angular">
151+
```ts
152+
const { stores } = meeting.stores;
153+
const store = stores.get('myStore');
154+
store.subscribe('key', (data) => {
155+
console.log(data);
156+
});
157+
158+
// subscribe to all keys of a store
159+
store.subscribe('\*', (data) => {
160+
console.log(data);
161+
});
162+
163+
store.unsubscribe('key');
164+
165+
````
166+
167+
</RTKCodeSnippet>
168+
169+
<RTKCodeSnippet id="web-web-components">
170+
```ts
171+
const { stores } = meeting.stores;
172+
const store = stores.get('myStore');
173+
store.subscribe('key', (data) => {
73174
console.log(data);
74175
});
75176

177+
// subscribe to all keys of a store
178+
store.subscribe('\*', (data) => {
179+
console.log(data);
180+
});
181+
76182
store.unsubscribe('key');
77-
```
183+
184+
````
185+
186+
</RTKCodeSnippet>
78187

79188
### Fetch Store Data
80189

81190
You can fetch the data stored in the store:
191+
192+
<RTKCodeSnippet id="web-react">
193+
```ts
194+
const stores = useRealtimeKitSelector((m) => m.stores.stores);
195+
const store = stores.get('myStore');
196+
197+
// fetch value for a specific key
198+
const data = store.get('key');
199+
200+
// fetch all the data in the store
201+
const data = store.getAll();
202+
````
203+
204+
</RTKCodeSnippet>
205+
206+
<RTKCodeSnippet id="web-angular">
82207
```ts
83208
const { stores } = meeting.stores;
84209
const store = stores.get('myStore');
@@ -88,4 +213,22 @@ const data = store.get('key');
88213

89214
// fetch all the data in the store
90215
const data = store.getAll();
91-
```
216+
217+
````
218+
219+
</RTKCodeSnippet>
220+
221+
222+
<RTKCodeSnippet id="web-web-components">
223+
```ts
224+
const { stores } = meeting.stores;
225+
const store = stores.get('myStore');
226+
227+
// fetch value for a specific key
228+
const data = store.get('key');
229+
230+
// fetch all the data in the store
231+
const data = store.getAll();
232+
````
233+
234+
</RTKCodeSnippet>

0 commit comments

Comments
 (0)