Skip to content

subscribeAction: before and after calls do not wait if we have a promise or await statement inside them. #1572

Open
@StijnVandermeulen1

Description

@StijnVandermeulen1

What problem does this feature solve?

If you subscribe to an action an return a promise or use an await statement, the code does not wait for the request to be complete. This is a problem when you implement a component as a plugin. Communication between components can be handled by the subscribeAction. But retrieving data and pushing it into the state using the afore mentioned methods will not result in any data since the service does not wait for it to be completed.

This feature would be very usefull for components as plugins that can be fully compartmentalized and plugged into different applications. Configuration retrieval can then be defined in the subscribeAction allowing different implementations over multiple applications.

What does the proposed API look like?

Example for before action:

Current code in src/store.js line 132:

try {

  this._actionSubscribers
    .filter(sub => sub.before)
    .forEach(sub => sub.before(action, this.state))
}

Should be:

try {

      async function asyncForEach(array, callback) {
         for (let index = 0; index < array.length; index  ) {
            await callback(array[index], index, array);
       }
     }

     await asyncForEach(this._actionSubscribers
       .filter(function (sub) { return sub.before; }), async function (sub) {
            return await sub.before(action, this$1.state); 
       });
    }

dispatch function should be async as wel.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions