paldepind / flyd

The minimalistic but powerful, modular, functional reactive programming library in JavaScript.
MIT License
1.56k stars 85 forks source link

Starting and stopping a stream based on values from another stream #109

Closed sammosampson closed 8 years ago

sammosampson commented 8 years ago

For example I want to start an 'every' pulse stream when another stream changes value and pause it when the other stream changes to another value. Any ideas on the best way to do this?

c-dante commented 8 years ago

Streams in flyd are always active (to use kefir terms) -- that is, they're more like a property than a stream.

There is no way to "enable" or "disable" a stream, but you do have a ton of control through the combine api.

From what I gleam off your request, you can do something like:

const debug = (name, s) => flyd.on((x) => console.log(name, x), s);
const a = flyd.stream(); // Start undefined
const b = flyd.stream(0); // Start at 0

const c = flyd.combine((a, b, self, changed) => {
  if (b() % 2 === 0) {
    c(a())
  }
}, [a, b]);

debug('a', a);
debug('b', b);
debug('c', c);
a(0);
b(1);
a(10)(15);
b(2);
a(1)(2)(3);

/* Expected:
b 0
a 0
c 0
b 1
a 10 // Notice that c skips 10 because b was odd during this call
a 15
b 2
c 15
a 1
c 1
a 2
c 2
a 3
c 3
*/

Again, not 100% on your use case or what you directly mean, but I'm demonstrating a way to use flyd.combine for some interesting logic. In this example, if b is even than c takes on a's value, otherwise c is "frozen".

You could also keep c's state outside in a closure, if that's easier to work with.

sammosampson commented 8 years ago

Excellent this is exactly what I need thank you for this.

Chris

On 19 Apr 2016, at 20:59, Dante notifications@github.com wrote:

Streams in flyd are always active (to use kefir terms) -- that is, they're more like a property than a stream.

There is no way to "enable" or "disable" a stream, but you do have a ton of control through the combine api.

From what I gleam off your request, you can do something like:

const debug = (name, s) => flyd.on((x) => console.log(name, x), s); const a = flyd.stream(); // Start undefined const b = flyd.stream(0); // Start at 0

const c = flyd.combine((a, b, self, changed) => { if (b() % 2 === 0) { c(a()) } }, [a, b]);

debug('a', a); debug('b', b); debug('c', c); a(0); b(1); a(10)(15); b(2); a(1)(2)(3);

/* Expected: b 0 a 0 c 0 b 1 a 10 // Notice that c skips 10 because b was odd during this call a 15 b 2 c 15 a 1 c 1 a 2 c 2 a 3 c 3 */ Again, not 100% on your use case or what you directly mean, but I'm demonstrating a way to use flyd.combine for some interesting logic. In this example, if b is even than c takes on a's value, otherwise c is "frozen".

You could also keep c's state outside in a closure, if that's easier to work with.

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub https://github.com/paldepind/flyd/issues/109#issuecomment-212102304

StreetStrider commented 8 years ago

flyd-keepwhen flyd-cacheUntil

c-dante commented 8 years ago

If this is resolved, can you close this issue @sammosampson or @paldepind?