Skip to content

Commit 57a9efa

Browse files
Update README.md
1 parent 00d054d commit 57a9efa

File tree

1 file changed

+29
-31
lines changed

1 file changed

+29
-31
lines changed

README.md

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,6 @@ As a consequence, of the fix, the library also introduce an elegant approach in
1010
A `deferred action` is an `action` that is not immediately dispatched, but rather scheduled to be dispatched later.
1111
In contrast with `reducers` (that given an action and the state, provides the new state), a `scheduler` is a function that given the current context and a deferred action, can execute (user defined) side effects, and return (or not) a cleanup/cancellation function associated with.
1212

13-
## Installation
14-
15-
```console
16-
$ yarn add rescript-react-restate
17-
```
18-
19-
or
20-
21-
```console
22-
$ npm install --save rescript-react-restate
23-
```
24-
25-
Then add `rescript-react-restate` to your `bsconfig.json` `bs-dependencies` field.
26-
2713
## Handling side effects (Asynchronous actions, logging, etc.)
2814

2915
`Restate` powers up reducers by allow them not just update state, but also defer an action to be dispatched later if is desired. This is useful when you want to handle side effects (like logging, network requests, etc.) after the state has been updated.
@@ -44,36 +30,23 @@ type deferredAction =
4430
| LogIncrement
4531
| LogDecrement
4632
47-
// Because of implementation details related on how we clean up the side effects, we need to provide a way to identify each deferred action. In other words, deferred actions, must have a unique identifier.
48-
module DeferredAction: Restate.HasDeferredAction with type t = deferredAction = {
49-
type t = deferredAction
50-
let variantId = action =>
51-
switch action {
52-
| LogIncrement => "LogIncrement"
53-
| LogDecrement => "LogDecrement"
54-
}
55-
}
56-
57-
// Instantiate the reducer with your deferred action type
58-
module RestateReducer = Restate.MakeReducer(DeferredAction)
59-
6033
// A Reducer now can update the state and schedule deferred actions (if they need to)
6134
let reducer = (state, action) =>
6235
switch action {
6336
| Increment =>
64-
RestateReducer.UpdateWithDeferred(
37+
Restate.UpdateWithDeferred(
6538
state + 1,
6639
LogIncrement,
6740
)
6841
| Decrement =>
69-
RestateReducer.UpdateWithDeferred(
42+
Restate.UpdateWithDeferred(
7043
state - 1,
7144
LogDecrement,
7245
)
7346
}
7447
7548
// A Scheduler handle deferred actions by triggering side effects and returning a cleanup function (if necessary)
76-
let scheduler: (RestateReducer.self<state, action>, deferredAction) => option<unit=>unit> =
49+
let scheduler: (Restate.self<state, action, deferredAction>, deferredAction) => option<unit=>unit> =
7750
(self, deferredAction) =>
7851
switch deferredAction {
7952
| LogIncrement =>
@@ -88,7 +61,7 @@ let scheduler: (RestateReducer.self<state, action>, deferredAction) => option<un
8861
8962
@react.component
9063
let make = () => {
91-
let (state, send, _defer) = RestateReducer.useReducer(reducer, scheduler, 0)
64+
let (state, send, _defer) = Restate.useReducer(reducer, scheduler, 0)
9265
<div>
9366
{state->React.int}
9467
<button onClick={_ => send(Decrement)}> {"-"->React.string} </button>
@@ -101,3 +74,28 @@ let make = () => {
10174

10275
If you'd rather initialize state lazily (if there's some computation you don't want executed at every render for instance), use `useReducerWithMapState` where the first argument is a function taking `unit` and returning the initial state.
10376

77+
## Installation
78+
79+
```console
80+
$ yarn add rescript-react-restate
81+
```
82+
83+
or
84+
85+
```console
86+
$ npm install --save rescript-react-restate
87+
```
88+
89+
Then add `rescript-react-restate` to your `bsconfig.json` `bs-dependencies` field.
90+
91+
## Development
92+
93+
```console
94+
nix develop
95+
96+
# Run examples server:
97+
yarn dev
98+
99+
# Build and watch rescript:
100+
yarn re:watch
101+
```

0 commit comments

Comments
 (0)