Skip to content

Latest commit

 

History

History
75 lines (56 loc) · 1.39 KB

README.md

File metadata and controls

75 lines (56 loc) · 1.39 KB

Mirror Saga

Adds Redux Sagas to Mirror.js.

NO LONGER MAINTAINED. MAY HAVE ISSUES.

Example

See a basic counter example.

Setup

Install dependencies.

npm install mirror-saga redux-saga

In your mirror setup, apply mirrorSaga.

import mirrorSaga from 'mirror-saga'

mirror.defaults(mirrorSaga())

// OR optionally pass in mirror default options
// mirror.defaults(mirrorSaga(options))

Use

Add model effects as generators (*function) and they will be treated as sagas by middleware.

// import saga effects from 'redux-saga' directly
import { delay } from 'redux-saga'

mirror.model({
  name: 'app',
  // ...
  effects: {
    *runASaga(payload) {
      yield delay(1000)
      yield actions.app.doSomething(payload)
    },
  }
})

Effects that are not generators will run normally using async/await.

import { call, take } from 'redux-saga'

const asyncDelay = (time) => new Promise((resolve, reject) => (
  setTimeout(() => resolve(), time))
)

export default {
  name: 'app',
  effects: {
    // runs as async function
    async effectExample() {
      await asyncDelay(1000)
      actions.app.increment()
    },
    // runs as saga
    *sagaExample() {
      yield take('authenticated')
      yield call(api, params)
      yield actions.app.increment()
    },
  }
}