ncthbrt / react-use-elmish

A React hook for unified purely-functional state and effect management
MIT License
57 stars 4 forks source link

Using ROP-style effect creators? #2

Closed iyegoroff closed 4 years ago

iyegoroff commented 4 years ago

Hi @ncthbrt ! What do you think about railway oriented programming?

Enforcing users to always implement "failure path" looks like an interesting idea to me. For example, signatures of fromFunction & fromPromise could be changed this way:

export function fromPromise<Action, T, E>(
  promise: () => Promise<T>,
  ofSuccess: (value: T) => Action,
  ofError: (error: E) => Action
): Effect<Action>

export function fromPromise<Action, T, E>(
  promise: () => Promise<T>,
  ofError: (error: E) => Action
): Effect<Action>

ofError action is being dispatched when function/promise throws an exception and ofSuccess otherwise. Elmish message creator functions follow similar interface - https://github.com/elmish/elmish/blob/12648f57092ac6c3dca383e1c25995882a646447/src/cmd.fs#L47

ncthbrt commented 4 years ago

Hi @ncthbrt ! What do you think about railway oriented programming?

I'm a big fan of RoP. This is actually a pretty nice suggestion.

Another approach I tried internally was creating a wrapper around Promise that only allowed for then (Vows), but I didn't want to make it public as it's a non-standard abstraction, and added a certain amount of overhead as you had to convert back and forth between promises and vows.

I'm game for making this change.

Will require a major version bump however

ncthbrt commented 4 years ago

@iyegoroff: Update on this. I'm happy to implement this myself, but it'll only be in a few weeks time. Got a deadline for another personal project in the first week of December, so can't really devote the time that this deserves till after this (know it isn't a huge feature, but it's more of a context switching thing)

If you're willing, I'd happily accept a PR for ROP effect creators

iyegoroff commented 4 years ago

Sure, probably I could look at it on this weekend.

Also I just played a little with this module and I really like the idea of bringing elmish approach to mainstream land with Typescript. I think that 'react-use-elmish' has huge potential!

ncthbrt commented 4 years ago

@iyegoroff Offtopic (and currently under documented), but also created this companion library: https://github.com/ncthbrt/react-elmish-router

ncthbrt commented 4 years ago

Thanks a lot for your work @iyegoroff I've just published the new version to npm

iyegoroff commented 4 years ago

That's great! Thanks!