prism-observable
Advanced tools
Comparing version 2.0.1 to 2.0.2
{ | ||
"name": "prism-observable", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "dependencies": {}, |
@@ -1,21 +0,13 @@ | ||
# @tutu/prism-observable | ||
# prism-observable | ||
Утилита для использования redux-observable вместе с prism. Оборачивает epics, позволяя им работать с префикснутыми actions, а также с частью store, относящейся к компоненту. | ||
This package fixes the problem of multiple same epics. You can read more about epics here https://redux-observable.js.org. | ||
By default epics can listen certain actions, but if those actions came from same components epic could not differ it. | ||
### Repository | ||
That's why we should pass some identifier (prefix or smth else) to totaly define correct action. | ||
https://stash.tutu.ru/projects/frontend/repos/prism-observable/browse | ||
### Clone | ||
```sh | ||
$ git clone ssh://git@depot.tutu.ru:7999/frontend/prism-observable.git | ||
``` | ||
### Install | ||
Устанавливаем @tutu/prism-observable в качестве зависимости | ||
```sh | ||
$ npm install @tutu/prism-observable --save | ||
$ npm install prism-observable --save | ||
``` | ||
@@ -27,15 +19,41 @@ | ||
import { combineEpics } from 'redux-observable'; | ||
import { wrapEpic } from '@tutu/prism-observable'; | ||
import { wrapEpic } from 'prism-observable'; | ||
import fetchStations from '../Station/epic'; | ||
import fetchDates from '../Date/epic'; | ||
// the target epic | ||
const fetchCities = action$ => { | ||
return action$.ofType('FETCH') | ||
.debounceTime(300) | ||
.switchMap(({ url }) => | ||
ajax({url: url, crossDomain: true}) | ||
.map(result => ({ | ||
type: 'FULFILL', | ||
payload: result.response | ||
})) | ||
); | ||
} | ||
// the function identifier | ||
// it listen all possible actions apply to regexp | ||
const getPrefix = action => { | ||
const match = action.type.match(/^routes\.[\d].(from|to)/); | ||
return match ? match[0] : null; | ||
} | ||
export default combineEpics( | ||
wrapEpic(fetchStations, 'Arrival', s => s.arrival), | ||
wrapEpic(fetchStations, 'Departure', s => s.departure), | ||
wrapEpic(fetchDates, 'Date'), // третий аргумент — селектор — опционален | ||
wrapEpic(fetchCities, getPrefix), | ||
); | ||
``` | ||
В данном случае один и тот же epic используется для 2 разных компонентов. Например, если по умолчанию эпик fetch умеет принимать action type `'Fetch'`, то после оборачивания — `'Departure.Fetch'`, `'Arrival.Fetch'`. | ||
The function `getPrefix` makes possible to listen actions such as `routes.1.from.FETCH`, `routes.0.to.FETCH`... After epic makes it's deal `wrapEpic` wraps the output action by appending the prefix. It case `routes.1.from.FETCH` you will get `routes.1.from.FULFILL` | ||
### API | ||
The signature is `wrapEpic(epic, getPrefix, selector)`; | ||
| Name | Type | Required | Description | | ||
|----------------|---------------------|-----------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ||
| `epic` | `function` | `true` | The target epic to wrap | | ||
| `getPrefix` | `function | string` | `true` | If you pass a function all actions will be passed through it and prefix will be gained by that function. But you can pass a static prefix as a string. | | ||
| `selector` | `function` | `false` | The state slice selector for the second epic argument. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
73162
59