deox icon indicating copy to clipboard operation
deox copied to clipboard

Handler map factory should handle optimistic action type

Open the-dr-lazy opened this issue 5 years ago • 4 comments

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 avatar Jan 20 '19 21:01 the-dr-lazy

Also in many cases error and cancel have shared logic.

the-dr-lazy avatar Jan 20 '19 21:01 the-dr-lazy

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 */ })

the-dr-lazy avatar Jan 21 '19 08:01 the-dr-lazy

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

kotarella1110 avatar Dec 10 '19 09:12 kotarella1110

Just by using createActionCreator in an object.

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

the-dr-lazy avatar Dec 10 '19 14:12 the-dr-lazy