https://github.com/dash-os/saga-event-observer
Use saga-observables to provide a simple way to create window event listeners for your sagas
https://github.com/dash-os/saga-event-observer
Last synced: 3 months ago
JSON representation
Use saga-observables to provide a simple way to create window event listeners for your sagas
- Host: GitHub
- URL: https://github.com/dash-os/saga-event-observer
- Owner: Dash-OS
- Created: 2017-05-30T09:54:32.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-05-30T10:11:36.000Z (about 9 years ago)
- Last Synced: 2025-01-31T23:16:21.606Z (over 1 year ago)
- Language: JavaScript
- Size: 4.88 KB
- Stars: 1
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# saga-event-observer
Uses [saga-observable](https://www.npmjs.com/package/saga-observable) to provide a
simple way to create window event listeners from sagas.
### Installation
```
yarn add saga-event-observer
```
**or**
```
npm install --save saga-event-observer
```
### Simple Example
```js
import { put, fork } = 'redux-saga/effects'
import eventObserver from 'saga-event-observer'
function* mySaga(foo, bar, ctx) {
const task = yield fork(
eventObserverSaga,
'device-orientation-observer', // uid
[ 'orientationchange' , { passive: true } ], // event args
{
onEvent: handleEvent,
onError: handleError,
// optionally use [ context, fn ] for binding context.
onCancel: [ ctx, handleCancellation ]
}, // optional lifecycle events, called as saga if possible.
/* pass through args are sent to the handlers */
foo, bar
)
}
function* handleEvent(event, value, uid, ...passThroughArgs) {
yield put({
type: 'WINDOW_EVENT',
event, // 'orientationchange'
value, // Event
uid, // 'device-orientation-observer'
passThroughArgs, // [ foo, bar ]
})
}
function* handleError(event, error, uid, ...passThroughArgs) {
/* ... handle error ... */
}
function* handleCancellation(event, uid, ...passThroughArgs) {
/* ... handle cancellation ... */
}
```