paldepind / flyd

The minimalistic but powerful, modular, functional reactive programming library in JavaScript.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Immutable array to stream

sammosampson opened this issue · comments

Not an issue just a question, so apologies if this is the wrong place that.

Does anyone know how I would get the values from an immutable array into a stream and when the array changes (or rather, as its immutable, becomes a different array with the same values as previous but plus a new one) add the only the new value to the stream. I am guessing some kind of periodical sampling is required as there is no 'event' that occurs when new items arrive.

@sammosampson, Hello. I don't think something so specific is already exist.
You can look over list of modules. It includes third-party modules too. You can also look over npm, but for now community is so small so any third-package module hits the list first.
Consider creating a module for your needs, leveraging Immutable and flyd libs.

commented

Not sure if I'm understanding exactly what you're asking for. An immutable array won't change ever -- you'll be getting new arrays. Are you considering a stream of the values from the array? Or the array itself? What's the source of the immutable array? Are you thinking something like this:

const arr = [1, 2, 3];
const s = someModule(arr); // someModule takes an array and produces a stream of the values
// s: 1---2---3---
s([1, 2, 3, 4]);
// s: ---4---

That is, s is given the full array, and only produces the new values?

I am currently using the 'every' module to periodically call a function that gets an immutable array and maps it into a stream, but what I then end up with is s([1,2,3]...[1,2,3,4]...) but what I really want is s(1...2...3...1...2...3...4) or absolutely ideally s(1...2...3...4).

So I suppose I am really asking is there an easy way to get from:
s([1,2,3]...[1,2,3,4]...) to s(1...2...3...1...2...3...4) or ideally s(1...2...3...4)

commented

I mean, you can just spread your array results with a combine.

var print = flyd.on(x => console.log(x));
var spread = (s) => flyd.combine((x, self) => x().forEach(a => setTimeout(() => self(a), 0)), [s]); // Your spread module
var source = flyd.stream();

var s = spread(source);
print(s);

source([1, 2, 3])([4, 5, 6])([1, 2, 3, 4]);
// s: 1--2--3--4--5--6--1--2--3--4--

There's currently a "bug" when updating the same stream multiple times in one combine event, so I have to use setTimeout to push each update at the end of the call stack.

Thx, the spread function is exactly what I am looking for

Guys, any idea why I get '_flyd2.default.combine is not a function' when running the above code

If i do this:
console.log(flyd.combine);

I get undefined but if I do the the same for:

console.log(flyd.on); I get:

function (a0,a1){return fn.apply(this,arguments);} WTF?

commented

Flyd functions are curried by default -- so flyd.on.toString() won't have the value you expect -- the fn in that snippet is the captured curry function (it's using Ramda's R.curry)

For the former (flyd.combine), what version of flyd are you using? How are you including it in your project?

i just nom’d it in, its 0.2.2

On 27 Apr 2016, at 22:28, Dante notifications@github.com wrote:

Flyd functions are curried by default -- so flyd.on.toString() won't have the value you expect -- the fn in that snippet is the captured curry function (it's using Ramda's R.curry)

For the former, what version of flyd are you using? How are you including it in your project?


You are receiving this because you modified the open/close state.
Reply to this email directly or view it on GitHub #112 (comment)

npm that is. Strange this if I look at flyd.js in the node module I can see the flyd.combine method in the source

commented

_flyd2.default.combine Looks like you're importing it through babel?

import flyd from 'flyd';

console.log(flyd.combine);
console.log(flyd.on);

Something like that?

Yep

On 27 Apr 2016, at 22:46, Dante notifications@github.com wrote:

_flyd2.default.combine Looks like you're importing it through babel?

import flyd from 'flyd';

console.log(flyd.combine);
console.log(flyd.on);
Something like that?


You are receiving this because you modified the open/close state.
Reply to this email directly or view it on GitHub #112 (comment)

This is what removes combine from the flyd object:

import keepWhen from 'flyd-keepwhen';

If I remove that import combine reappears

commented

Oh interesting. I have't tried to import modules through ES2015 import yet -- might need to do something about that.

It seems any module I add kills combine

On 27 Apr 2016, at 22:51, Dante notifications@github.com wrote:

Oh interesting. I have't tried to import modules through ES2015 import yet -- might need to do something about that.


You are receiving this because you modified the open/close state.
Reply to this email directly or view it on GitHub #112 (comment)

Its ok for now, i just copied all the code for the modules I need locally

commented

Thankfully, the modules are small. Should add this as a bug -- I'll look into it when I can.

There's currently a "bug" when updating the same stream multiple times in one combine event, so I have to use setTimeout to push each update at the end of the call stack.

What bug is this? Is it fixed?

commented

It's how the combine pushes values down -- if you want to produce multiple events from one combine trigger, you need to create a new callstack for each one. Use setTimeout to do that. This is due to the atomic update guarantee when flushing updates.