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/src/docs/06-async-actions-and-optimistic-updates.md
+159-1Lines changed: 159 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,4 +5,162 @@ section: "Guide"
5
5
6
6
# Async Actions and Optimistic Updates
7
7
8
-
coming soon.
8
+
When creating async actions there are generally three action types that we need
9
+
10
+
1.**`<ACTION>_STARTED`** represents the action happening client side but has not been verified by the server. When doing optimistic updates
11
+
stores can respond to this action type as if the transaction will be successful, while maintaining enough state to rollback if it eventually fails.
12
+
13
+
2.**`<ACTION>_SUCCESS`** happens after the server has verified that the optimistic update is valid, at this point stores can discard rollback information without worry.
14
+
15
+
3.**`<ACTION>_FAILED`** the server has rejected the update and stores need to rollback.
16
+
17
+
### Implementing an optimistic `cartCheckout()` action
18
+
19
+
The first step is to track checkout rollback information in the **CartStore**
20
+
21
+
#### `stores/CartStore.js`
22
+
23
+
```javascript
24
+
import { Store, toImmutable } from'nuclear-js'
25
+
import {
26
+
CHECKOUT_START,
27
+
CHECKOUT_SUCCESS,
28
+
CHECKOUT_FAILED,
29
+
ADD_TO_CART,
30
+
} from'../action-types'
31
+
32
+
constinitialState=toImmutable({
33
+
itemQty: {},
34
+
pendingCheckout: {},
35
+
})
36
+
37
+
/**
38
+
* CartStore holds the mapping of productId => quantity
39
+
* and also maintains rollback information for the checkout process
0 commit comments