tur-nr / polymer-redux

Polymer bindings for Redux.
https://tur-nr.github.io/polymer-redux
MIT License
440 stars 67 forks source link

Async dispatcher #37

Closed tur-nr closed 7 years ago

tur-nr commented 7 years ago

@cvanleeuwen @kristfal

I have the dispatch passing the polymer-redux dispatch through to middleware actions. This allows you to make use of the action creators list on an element or dispatch any other valid redux action.

Here is a simple implementation, see async.html for a working demo.

Polymer({
    actions: {
        wait() {
            return { type: 'WAIT' };
        },
        complete() {
            return { type: 'COMPLETE' };
        },
        something() {
            return this.dispatch((dispatch) => {
                dispatch('wait'); // polymer-redux dispatch
                setTimeout(() => {
                    dispatch('complete'); // polymer-redux dispatch
                }, 500);
            });
        },
    },
    doSomething() {
        return this.dispatch((dispatch) => {
            dispatch('wait'); // polymer-redux dispatch
            setTimeout(() => {
                dispatch('complete'); // polymer-redux dispatch
            }, 500);
        });
    },
    doSomethingInAction() {
        return this.dispatch('something');
    }
});

Also I want to note that dispatch functions that return an action (inline action creator) will be depreciated. It's a little confusing and makes me anxious 😟

this.dispatch(() => {
    return { type: 'DEPRECIATED' };
});
coveralls commented 7 years ago

Coverage Status

Coverage decreased (-4.3%) to 95.652% when pulling 981dd0b2078a5986e6020647cf1ba58f9f4f6e1f on async-dispatcher into 95f0735134b51a7af7a539da9917268ea5f6afe5 on master.

kristfal commented 7 years ago

Looks perfect! Thanks for adding this feature. I'll work with this branch and give you any feedback if I find anything.

👍

kristfal commented 7 years ago

@tur-nr found one issue. Actions defined in behaviors are not picked up by the async dispatcher. I'll dig into it and see if I can patch it.

kristfal commented 7 years ago

Here is a fix to extract actions from behaviors in the dispatchReduxAction method. It should probably not override the actions defined in the root element as this quick fix does, but works though.

function dispatchReduxAction(element, store, args) {
        var action = args[0];
        var actions = element.actions;

        // Quick fix: Extract actions from attached behaviors
        element.behaviors.forEach(function(behavior) {if (behavior.actions) {
            Object.assign(actions, behavior.actions);
        }});

        args = castArgumentsToArray(args);

        ...
tur-nr commented 7 years ago

@kristfal this will merge the behaviors actions into the elements, and will do so every time a dispatch is called on that element. It would be best not to merge I think.

coveralls commented 7 years ago

Coverage Status

Coverage remained the same at 100.0% when pulling f20fa121e4638dee4ba6a3679efff4ccccc64f2a on async-dispatcher into 95f0735134b51a7af7a539da9917268ea5f6afe5 on master.