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: README.md
+65-12Lines changed: 65 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ When using Redux with Redux-Sagas, asynchronous options end up being fairly comp
8
8
9
9
Other libraries exist for handling asynchronous actions, usually by including middlewares. However, if you are already using the Redux-Saga middleware, it adds an extra layer of complication to add a new middleware on top of Redux-Sagas.
10
10
11
-
**Async-Ops** uses the Redux-Saga middleware to handle asynchronous operations, unifying all types of async calls under a small number of actionTypes which are handled by a single saga. Whenever you want to add a new asynchronous operation, all that you need to do is create an operation method using whatever technology you like (we like to use the Javascript `fetch` API for simplicity), register that operation, and fire the Async-Ops actions with the registered name and, boom.
11
+
**Async-Ops** uses the Redux-Saga middleware to handle asynchronous operations, unifying all types of async calls under a small number of actionTypes which are handled by a single saga. Whenever you want to add a new asynchronous operation, all that you need to do is create an operation function using whatever technology you like (we like to use the Javascript `fetch` API for simplicity), register that operation, and fire the Async-Ops actions with the registered name and, boom.
12
12
13
13
## Usage
14
14
Async-Ops is available on npm with the following command:
@@ -18,7 +18,7 @@ Async-Ops is available on npm with the following command:
The `register`method registers an `operation`method and a `mockOperation`method under a given name. Operations must be registered prior to being used by the application.
21
+
The `register`function registers an `operation`function and a `mockOperation`function under a given name. Operations must be registered prior to being used by the application.
The `callOperation`method retrieves the `operation` registered at the provided name and then calls it, passing in `..args` to the called method.
43
+
The `callOperation`function retrieves the `operation` registered at the provided name and then calls it, passing in `..args` to the called function.
44
44
45
45
#### Arguments
46
46
@@ -50,7 +50,7 @@ The `callOperation` method retrieves the `operation` registered at the provided
50
50
51
51
52
52
#### Return : Promise(result)
53
-
`callOperation` returns the result of the previously registered `operation`method (or its `mockOperation`) after invoking it with the provided `...args`. If the `operation` result is not a `Promise`, it will be wrapped in a resolved `Promise`.
53
+
`callOperation` returns the result of the previously registered `operation`function (or its `mockOperation`) after invoking it with the provided `...args`. If the `operation` result is not a `Promise`, it will be wrapped in a resolved `Promise`.
54
54
55
55
#### Example
56
56
```javascript
@@ -66,18 +66,18 @@ const x = await callOperation('fetchData', 1) // x = 2
66
66
```
67
67
68
68
### `enableMock() : Function`
69
-
The `enableMock`method causes `callOperation` to use the `mockOperation`method rather than the `operation`method. The current mock status is set in the client's Local Storage.
69
+
The `enableMock`function causes `callOperation` to use the `mockOperation`function rather than the `operation`function. The current mock status is set in the client's Local Storage.
70
70
71
71
### `disableMock() : Function`
72
-
The `enableMock`method causes `callOperation` to use the `mockOperation`method rather than the `operation`method. The current mock status is set in the client's Local Storage.
72
+
The `enableMock`function causes `callOperation` to use the `mockOperation`function rather than the `operation`function. The current mock status is set in the client's Local Storage.
73
73
74
74
### `actions : Object`
75
75
76
-
The `actions` object contains action creator methods which create the actions which are used by the Async-Ops `saga` to automatically run and process Async-Ops operations.
76
+
The `actions` object contains action creator functions which create the actions which are used by the Async-Ops `saga` to automatically run and process Async-Ops operations.
These three methods are helper methods. They return a match function which takes an action Object and matches the actionType, name, and channel. This can used for Redux-Saga matching and React-Redux reducer matching.
129
+
These three functions are helper functions. They return a match function which takes an action Object and matches the actionType, name, and channel. This can used for Redux-Saga matching and React-Redux reducer matching.
130
130
131
131
#### Arguments
132
132
@@ -135,4 +135,57 @@ These three methods are helper methods. They return a match function which take
135
135
**`channel : String [optional]`** A channel name to be matched.
136
136
137
137
#### Return : Function(action : Object)
138
-
The method returned from the helper methods takes an action object and returns either `true` if the action matches the provided info or `false` if the action does not.
138
+
The function returned from the helper functions takes an action object and returns either `true` if the action matches the provided info or `false` if the action does not.
139
+
140
+
### `reducer : Function`
141
+
142
+
The `reducer` is a Redux reducer function that can added to an app's reducer to keep the store updated with the status about the latest async-ops calls. It must be put under the key 'asyncops' which can be imported from the async-ops package as `STORE_DOMAIN`.
143
+
144
+
### `loadingSelector : Function`
145
+
146
+
This selector function can be used to determine the loading state of an asynchronous operation.
147
+
148
+
#### Arguments
149
+
150
+
**`name : String [required]`** The name of the operation.
151
+
152
+
**`channel : String [optional]`** The name of the channel.
153
+
154
+
#### Return : Function(action : Object)
155
+
The function returned from the selector functions takes a state object and returns either `true` if the asynchronous operation is currently loading or `false` if it is not.
156
+
157
+
#### Example
158
+
```javascript
159
+
import { loadingSelector } from'async-ops'
160
+
161
+
constmapStateToProps=state=> ({
162
+
testOpIsLoading:loadingSelector('testOp')(state)
163
+
})
164
+
```
165
+
166
+
export const loadingSelector = (name, channel) => state =>
167
+
statusSelector(name, channel).loading || false
168
+
169
+
export const errorSelector = (name, channel) => state =>
170
+
statusSelector(name, channel).error || null
171
+
### `errorSelector : Function`
172
+
173
+
This selector function can be used to retrieve error data from an asynchronous operation.
174
+
175
+
#### Arguments
176
+
177
+
**`name : String [required]`** The name of the operation.
178
+
179
+
**`channel : String [optional]`** The name of the channel.
180
+
181
+
#### Return : Function(action : Object)
182
+
The function returned from the selector functions takes a state object and returns either null if the asynchronous operation did not error or an error object.
0 commit comments