kefirjs / kefir

A Reactive Programming library for JavaScript
https://kefirjs.github.io/kefir/
MIT License
1.87k stars 97 forks source link

Deprecate .awaiting? #145

Closed rpominov closed 9 years ago

rpominov commented 9 years ago

If you think of it, a.awaiting(b) is just Kefir.merge([a.map(() => true), b.map(() => false)]), so it's not a big deal to do ad-hoc. Also notice that it completely discards values from a and b replacing them with booleans, but often we need those values, for instance:

var streamOfSearchResuts = streamOfSearchQueryChanges.flatMapLatest(sendQueryToServer)
var html = Kefir.merge([
  streamOfSearchQueryChanges.map(() => "loading ..."),
  streamOfSearchResuts.map(result => resultToString(result))
])

// see full example http://jsfiddle.net/fwo0toLx/6/

Honestly I've never used .awaiting myself.

Migration

Before

foo.awaiting(bar)

After

// Strict equivalent
Kefir.merge([foo.map(() => true), bar.map(() => false)])
  .skipDuplicates()
  .toProperty(() => false)

// Loose equivalent
Kefir.merge([foo.map(() => true), bar.map(() => false)])

// More useful equivalent
Kefir.merge([
  foo.map(x => "something more useful than bool & based on x"), 
  bar.map(y => "something more useful than bool & based on y")
])
shamansir commented 9 years ago

Just to mention, I am using .awaiting as a limit for filterBy:

Kefir.fromEvents(..., 'click')
     .map(stopPropagation)
     .filterBy(outerClicks.awaiting(someProcessInProgress))
     .map(extractPos)...
rpominov commented 8 years ago

@shamansir I usually do the following in a situation like this:

const clicks = Kefir.fromEvents(..., 'click').map(stopPropagation)
outerClicks.flatMap(() => clicks.takeUntilBy(someProcessInProgress)).map(extractPos)...

In other words:

- foo.filterBy(bar.awaiting(baz))
+ bar.flatMap(() => foo.takeUntilBy(baz))
rpominov commented 8 years ago

@shamansir Oh, and btw, your example may work differently after/if we fix https://github.com/rpominov/kefir/issues/167 (stopPropagation side effect will be called only when filterBy holds true. I.e. filterBy+awaiting will work exactly like flatMap+takeUntilBy work now.)

shamansir commented 8 years ago

Got it, thanks! Seems I should update my code again :).

And yeah, looks like #167 is a right decision.