flyd icon indicating copy to clipboard operation
flyd copied to clipboard

Use ajax with scanMerge

Open xgrommx opened this issue 9 years ago • 8 comments

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 avatar Jul 29 '15 04:07 xgrommx

@paldepind Can you help me?

xgrommx avatar Aug 05 '15 02:08 xgrommx

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.

paldepind avatar Aug 05 '15 07:08 paldepind

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

xgrommx avatar Aug 05 '15 14:08 xgrommx

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

paldepind avatar Aug 06 '15 19:08 paldepind

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

xgrommx avatar Aug 07 '15 10:08 xgrommx

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

xgrommx avatar Aug 08 '15 00:08 xgrommx

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.

paldepind avatar Aug 08 '15 10:08 paldepind

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

xgrommx avatar Aug 08 '15 13:08 xgrommx