paldepind / flyd

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

Use ajax with scanMerge #57

Open xgrommx opened 9 years ago

xgrommx commented 9 years ago

Hello, How can I use ajax response with scanMerge? For example:

const fromPromise = (promise) => {
    var s = flyd.stream();

    promise.then(v => s(v));

    return flyd.immediate(flyd.stream([s], function () {
        return s();
    }));
};

const ajaxStream = (key) => fromPromise(emulateAjaxWithDelay(key));

const personStream = flatMap(person => {
    return scanMerge(
        [
            [changeFirstName, (person, firstName) => {
                console.log(person);
                console.log(firstName);
                return person.set('firstName', firstName);
            }],
            [changeLastName, (person, lastName) => person.set('lastName', lastName)],
            [changeCountry, (person, country) => person.setIn(['country', 'name'], country)],
            [addFriend, (person, friend) => person.set('friends', person.get('friends').push(friend))],
            [removeFriend, (person, friendIndex) => person.set('friends', person.get('friends').splice(friendIndex, 1))],
            [save, (person, _) => saveToLocalStorage('person', person)],
            [undo, (person, historyPerson) => historyPerson],
            [redo, (person, futurePerson) => futurePerson]
        ],
        person
    )
}, ajaxStream('person'));

flyd.on(p => console.log(p), personStream);

But this code doesn't work. Person has value in flatMap but scanMerge doesn't emit value. My RxJS version here https://github.com/xgrommx/react-rx-flux/blob/master/src/store.js#L24

xgrommx commented 9 years ago

@paldepind Can you help me?

paldepind commented 9 years ago

What is the value of person inside flatMap. And what is the value in the stream created with scanMerge? scanMerge should contain the initial value.

I think it might be because the stream created with flatMap does not emit existing values in the stream it flattens. Only values emitted afterwards are emitted.

xgrommx commented 9 years ago

person is Immutable.js which was created from a plain javascript object.

paldepind commented 9 years ago

And if you check the value of the stream returned by scanMerge does it contain the person?

xgrommx commented 9 years ago

@paldepind Yes, I'll create a new branch on my repo and upload my experiment with flyd

xgrommx commented 9 years ago

This is a new branch with flyd https://github.com/xgrommx/react-rx-flux/tree/flyd

paldepind commented 9 years ago

I'm not sure what I'm supposed to see.

Can you confirm if this is correct?

I think it might be because the stream created with flatMap does not emit existing values in the stream it flattens. Only values emitted afterwards are emitted.

I'd prefer if you could reduce this problem to it's essence and a smaller code example.

xgrommx commented 9 years ago

@paldepind But almost the same approach working with Rx, Kefir or Bacon.