enhance

ac-apps-minimal-enhance-workers

enhance/workers.js

This module allows you to concurrently execute multiple asynchronous requests, making it ideal for handling long lists of tasks. You can create a list of tasks using the app.workers.task method, and then start them all at once using the app.workers.do method. The promise returned by this module will only be resolved once all of the tasks have been completed. This enables you to effectively manage and process large volumes of data in an efficient and timely manner.

Example:
app.workers.task(function() { /* task one */ }, "task-name");
app.workers.task(function() { /* task two */ }, "task-name");
app.workers.task(function() { /* task three */ }, "task-name");
await app.workers.do("task-name");

A module for ac-bundle-app, published by ac-bundle-module.

Install from:

https://www.npmjs.com/package/ac-apps-minimal-enhance-workers
https://github.com/animatedcreativity/ac-apps-minimal-enhance-workers

Available Methods: start

start

function() {
  app.workers = {
    index: 0,
    list: {},
    task: function(callback, taskName) {
      app.workers.index += 1;
      app.workers.list[app.workers.index] = {taskName: taskName, callback: callback};
    },
    count: function(taskName) {
      var count = 0;
      for (var index in app.workers.list) {
        var item = app.workers.list[index];
        if (item.name === taskName) count += 1;
      }
      return count;
    },
    do: async function(taskName) {
      return new Promise(function(resolve, reject) {
        for (var index in app.workers.list) {
          (async function(index) {
            var item = app.workers.list[index];
            if (taskName === item.taskName) {
              await item.callback();
              delete app.workers.list[index];
              if (app.workers.count(taskName) <= 0) resolve(true);
            }
          })(index);
        }
      });
    }
  };
}

Happy Coding!

MIT License: https://choosealicense.com/licenses/mit/

Copyright 2022 Animated Creativity

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Leave a Reply

Your email address will not be published. Required fields are marked *