Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
tonycuadra committed Mar 23, 2022
1 parent 69614c8 commit 86afa18
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
27 changes: 26 additions & 1 deletion docs/advanced/thunks.md
Expand Up @@ -28,4 +28,29 @@ store:dispatch(function(store)
end)
```

Thunks are a simple way to introduce more complex processing of `action` objects, but you may want to consider creating custom [`middleware`](middleware.md) for complex features instead of relying on thunks alone.
Thunks are a simple way to introduce more complex processing of `action` objects, but you may want to consider creating custom [`middleware`](middleware.md) for complex features instead of relying on thunks alone.

It is also possible to inject a custom argument into the thunk middleware. This is useful for cases like using an API service layer that could be swapped out for a mock service in tests. This is accomplished by using the `Rodux.makeThunkMiddleware` API instead:

```lua
local myThunkMiddleware = Rodux.makeThunkMiddleware(myCustomArg)
local store = Rodux.Store.new(reducer, initialState, {
myThunkMiddleware,
})

store:dispatch(function(store, myCustomArg)
print("Hello from a thunk with extra argument:", myCustomArg)
store:dispatch({
type = "thunkAction"
})
end)
```

If multiple values need to be passed in, they can be combined into a single table:

```lua
local myThunkMiddleware = Rodux.makeThunkMiddleware({
[RoactNetworking] = networking,
AvatarEditorService = AvatarEditorService,
})
```
16 changes: 6 additions & 10 deletions docs/api-reference.md
Expand Up @@ -264,7 +264,12 @@ store:dispatch(function(store)
end)
```

It is also possible to inject a custom argument into the thunk middleware. This is useful for cases like using an API service layer that could be swapped out for a mock service in tests. This is accomplished by using the `Rodux.makeThunkMiddleware` API instead:
### Rodux.makeThunkMiddleware (unreleased)
```
Rodux.makeThunkMiddleware(extraArgument) -> thunkMiddleware
```

A function that creates a thunk middleware that injects a custom argument when invoking thunks (in addition to the store itself). This is useful for cases like using an API service layer that could be swapped out for a mock service in tests.

```lua
local myThunkMiddleware = Rodux.makeThunkMiddleware(myCustomArg)
Expand All @@ -278,12 +283,3 @@ store:dispatch(function(store, myCustomArg)
})
end)
```

If multiple values need to be passed in, they can be combined into a single table:

```lua
local myThunkMiddleware = Rodux.makeThunkMiddleware({
[RoactNetworking] = networking,
AvatarEditorService = AvatarEditorService,
})
```

0 comments on commit 86afa18

Please sign in to comment.