Skip to content

Commit 65a7cc1

Browse files
committed
docs: layers
1 parent 04a5c63 commit 65a7cc1

1 file changed

Lines changed: 63 additions & 0 deletions

File tree

docs/guide/data/cache.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,66 @@ You can also use the `store.<modelName>.clearItem` method:
8888
```ts
8989
store.User.clearItem('abc')
9090
```
91+
92+
## Layers
93+
94+
A cache layer is a way to create a temporary state modification that can be easily reverted. This is how [optimistic updates](./mutation.md#optimistic-updates) are implemented.
95+
96+
To create a new layer, use the `addLayer` method:
97+
98+
```ts
99+
store.$cache.addLayer({
100+
id: 'some-layer-id',
101+
state: {
102+
Messages: {
103+
'some-message-id': {
104+
$overrideKey: 'some-message-id',
105+
text: 'This is an optimistic message',
106+
},
107+
},
108+
},
109+
deleteItems: {},
110+
optimistic: true, // Optional
111+
prevent: { // Optional
112+
update: false,
113+
delete: false,
114+
},
115+
skip: false, // Optional
116+
})
117+
```
118+
119+
In this example, the layer will override the `text` property of the `Messages` item with id `some-message-id`.
120+
121+
::: tip
122+
If the layer contains records that do not exist in the cache, it will act as if those records were created.
123+
:::
124+
125+
```ts
126+
store.$cache.addLayer({
127+
id: 'some-layer-id',
128+
state: {},
129+
deleteItems: {
130+
Messages: new Set(['some-message-id']),
131+
},
132+
})
133+
```
134+
135+
In this second example, the layer will delete the `Messages` item with id `some-message-id`.
136+
137+
::: tip
138+
If multiple layers, they are applied in the order they were added.
139+
:::
140+
141+
To get a layer, use the `getLayer` method:
142+
143+
```ts
144+
const layer = store.$cache.getLayer('some-layer-id')
145+
```
146+
147+
To remove a layer, use the `removeLayer` method:
148+
149+
```ts
150+
store.$cache.removeLayer('some-layer-id')
151+
```
152+
153+
It will effectively rollback all the changes applied by the layer.

0 commit comments

Comments
 (0)