esamattis / immer-reducer

Type-safe and terse reducers with Typescript for React Hooks and Redux
http://npm.im/immer-reducer
MIT License
225 stars 15 forks source link

Document/suggest intergration between immer-reducer and redux-saga #14

Closed FrozenKiwi closed 5 years ago

FrozenKiwi commented 5 years ago

https://github.com/epeli/immer-reducer/issues/3 provides sample code for integrating with redux-observable, however I have been unable to figure out how to get redux-saga

This is both a request for assistance, or a place to document how to achieve this for anyone who gets as stuck as I have trying to figure how to do this nicely with redux-saga.

My ideal scenario:

define & use saga's similar to regular reducers

interface IAction {
   ....
  runSaga(val: string): Iterable<any>;
}

class ActionImp  extends ImmerReducer<ContainerState>
  implements IActions {

  *setSagaResults(result: any) {
    this.draftState.result = result
  }

  *runSaga(val: string) {
    const results = yield call(myApi, val)
    yield put(setSagaResult, results) // ??? how to remap this call appropriately transparently to the user
  }

class MyComponent<Props> {
  onBtnPress() {
    this.props.runSaga("myString")
  }
}

I'll update this issue with thoughts, and please let me know if there is anything you can think of that would help guide me.

esamattis commented 5 years ago

As I said in #10 the class should be considered to be only a replacement for the reducer function with the switch-case statement. So you should not put anything into the class what you would not put inside the reducer fuction.

I added some type guard helpers (isAction(), isActionFrom()) in the lastest release which makes it easier to work with redux-saga.

There's also a simple redux-saga example in the readme now.

esamattis commented 5 years ago

I've also pushed redux-saga example to the todoapp example

https://github.com/epeli/typescript-redux-todoapp/blob/saga/src/redux/sagas.tsx

Nothing fancy. The saga just injects a default value for new todos after a small delay.

esamattis commented 5 years ago

Made some further updates for redux-saga users in 0.6.0:

https://github.com/epeli/immer-reducer/releases/tag/0.6.0

Also updated the todoapp example with them https://github.com/epeli/typescript-redux-todoapp/blob/83fe39b6a43fdd2495ebda1f6553fc4380a85c39/src/redux/sagas.tsx

esamattis commented 5 years ago

Closing. Feel free to ask if there's still any questions.