the-dr-lazy / deox

Functional Type-safe Flux Standard Utilities
https://deox.js.org
MIT License
206 stars 12 forks source link

Handler map factory should handle optimistic action type #4

Open the-dr-lazy opened 5 years ago

the-dr-lazy commented 5 years ago

According to #2, createHandlerMap should be capable to handle optimistic action creators.

Detailed Description

Yet, optimistic actions should be handled separately with multiple createHandlerMap call. So there would be a good point to reduce boilerplates. Although, cancel is not part of observer protocol but this type practically is important and should be handled in many cases (isFetching entity, etc.) in the reducers.

Possible Implementation

createHandlerMap(addFoo, {
  next(state, action) {},
  error(state, action) {},
  complete(state, action) {},
  cancel(state, action) {}
})
the-dr-lazy commented 5 years ago

Also in many cases error and cancel have shared logic.

the-dr-lazy commented 5 years ago

A solution for shared logic is to handle cancel and error separately:

createHandlerMap(addFoo, {
  next(state, action) {},
  complete(state, action) {},
})
createHandlerMap([addFoo.error, addFoo.cancel], (state, action) => { /* shared logic */ })
kotarella1110 commented 4 years ago

Hey @thebrodmann again! How do you create action creator in this case?

the-dr-lazy commented 4 years ago

Just by using createActionCreator in an object.

const addFoo = {
  next: createActionCreator('NEXT'),
  error: createActionCreator('ERROR'),
  cancel: createActionCreator('CANCEL'),
  complete: createActionCreator('COMPLETE'),
}