0no-co / wonka

🎩 A tiny but capable push & pull stream library for TypeScript and Flow
MIT License
709 stars 29 forks source link

pairwise operator #36

Closed sxd1140 closed 4 years ago

sxd1140 commented 4 years ago

Is have pairwise operator in wonka?

kitten commented 4 years ago

Hiya, for the sake of not having to cross-check this operator:

That may give me more information on what you're trying to get out of wonka đź‘Ť Are you referring to it as an Rx operator? Would love to make sure that wonka supports every necessary use-case, but I'd love to keep the operators to a minimum, since I think it already has all the operators needed to fulfill lots of usecases

sxd1140 commented 4 years ago

I need handle scroller change event, so need current scroller position and last scroller position to know scrolling direction.

kitten commented 4 years ago

Sounds like you were thinking of the combine operator, but if you have a single stream of position events you could also use the scan operator to transform a single value into a stream of { last, current } or sth similar

sxd1140 commented 4 years ago

Thanks, I look scan operator.

scan((acc, val) => acc + val) in Example. But in d.ts file https://github.com/kitten/wonka/blob/acc4e71e338ca178ad6da5748d7ffa6c3a3446c8/src/operators/wonka_operator_scan.d.ts#L3

What the second param I should pass?

kitten commented 4 years ago

Hm, seems like that example has a typo! As can be seen in the Reason example for it (and in the typings as you’ve already pointed out) there is a second argument indeed.

Like for reduce with Arrays that second argument is for the initial value of the accumulation, but it’s not optional for scan

sxd1140 commented 4 years ago

Ok. But seem like scan just pass one value.

pipe(
   fromArray([1, 2, 3, 4, 5, 6]),
   scan((last, current) => current, 0),
   subscribe( [`I need two value here, last and current`] => {}),
);
kitten commented 4 years ago

You can just store the previous value using scan temporarily, then calculate your scroll trajectory separately:

  scan(
    ({ current: prev }, current) => ({ prev, current }),
    { current: null }
  ),
  map(({ prev, current }) => prev !== null ? current - prev : 0)
sxd1140 commented 4 years ago

I got it. Thanks a lot.