Skip to content

Commit 2c8c3f5

Browse files
committed
Updated Documentation to include new Selector logic
1 parent 8ca1a9d commit 2c8c3f5

File tree

2 files changed

+70
-15
lines changed

2 files changed

+70
-15
lines changed

README.md

Lines changed: 65 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ When using Redux with Redux-Sagas, asynchronous options end up being fairly comp
88

99
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.
1010

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.
1212

1313
## Usage
1414
Async-Ops is available on npm with the following command:
@@ -18,7 +18,7 @@ Async-Ops is available on npm with the following command:
1818

1919
## API
2020
### `register(name:String, operation:Function, mockOperation: Function) : Function`
21-
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.
2222

2323
#### Arguments
2424

@@ -40,7 +40,7 @@ register('fetchData', service, mock)
4040
```
4141

4242
### `callOperation(name:String, ...args) : Function`
43-
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.
4444

4545
#### Arguments
4646

@@ -50,7 +50,7 @@ The `callOperation` method retrieves the `operation` registered at the provided
5050

5151

5252
#### 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`.
5454

5555
#### Example
5656
```javascript
@@ -66,18 +66,18 @@ const x = await callOperation('fetchData', 1) // x = 2
6666
```
6767

6868
### `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.
7070

7171
### `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.
7373

7474
### `actions : Object`
7575

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.
7777

7878
### `actions.asyncOperationStart : Function(name:String | options:Object, ...args)`
7979

80-
The `asyncOperationStart` method creates an action which starts an async operation.
80+
The `asyncOperationStart` function creates an action which starts an async operation.
8181

8282
#### Arguments
8383

@@ -90,10 +90,10 @@ The `asyncOperationStart` method creates an action which starts an async operati
9090
**`...args`** The arguments to be passed to the `operation` when it is called.
9191

9292
#### Return : Object
93-
The `asyncOperationStart` method returns a Redux-formatted action object with the following properties:
93+
The `asyncOperationStart` function returns a Redux-formatted action object with the following properties:
9494
* *`type : String`* This is always set to `'ASYNC_OPERATION'`
9595
* *`...args : Array[arg1, arg2, ...]`* an array of the args provided.
96-
* *`...options`* any other option values provided to the method
96+
* *`...options`* any other option values provided to the function
9797

9898
#### Example
9999
```javascript
@@ -126,7 +126,7 @@ sagaMiddleware.run(saga)
126126

127127
### `isAsyncOperation, isAsyncComplete, isAsyncFailure : Function(name:String, channel:String)`
128128

129-
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.
130130

131131
#### Arguments
132132

@@ -135,4 +135,57 @@ These three methods are helper methods. They return a match function which take
135135
**`channel : String [optional]`** A channel name to be matched.
136136

137137
#### 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+
const mapStateToProps = 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.
183+
184+
#### Example
185+
```javascript
186+
import { errorSelector } from 'async-ops'
187+
188+
const mapStateToProps = state => ({
189+
testOpError: errorSelector('testOp')(state)
190+
})
191+
```

src/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as actionTypes from './actionTypes'
44
import saga from './saga'
55
import { call as callOperation, register } from './operations'
66
import * as reducerHelpers from './helpers'
7-
import reducer from './reducer'
7+
import reducer, { STORE_DOMAIN } from './reducer'
88
import * as selectors from './selectors'
99

1010
const isAsyncOperation = reducerHelpers.isAsyncOperation
@@ -24,7 +24,8 @@ export {
2424
isAsyncComplete,
2525
isAsyncFailure,
2626
reducer,
27-
selectors
27+
selectors,
28+
STORE_DOMAIN
2829
}
2930

3031
export default {
@@ -40,5 +41,6 @@ export default {
4041
isAsyncComplete,
4142
isAsyncFailure,
4243
reducer,
43-
selectors
44+
selectors,
45+
STORE_DOMAIN
4446
}

0 commit comments

Comments
 (0)