Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

gcp-api-queue

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gcp-api-queue - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

136

index.js
function gcpApiQueue(specs) {
"use strict";
// the functions
let startTask;
let startNextTask;
let addTask;
let startQueue;
// the array that holds the queue
let queue = [];
// flag to see if the queue is running
let queueIsRunning = false;
// active tasks
let numActive = 0;
let maxActive = 5;
let startNextTask;
let startTask;
let addTask;
let startQueue;
let queuePromise;
// maximum active tasks
let maxActive = specs.concurrency || 5;
// the placeholder for the resolve of the queue finish promise
let queuePromiseResolve;
// the function to execute on a reject
let onReject = specs.onReject;
// the function to execute on a reject
let onResolve = specs.onResolve;
// start a task
startTask = function (task) {
// increase the active tasks
numActive += 1;
// update the task status
task.status = "active";
task.promise().then(function () {
// handle the task promise
task.promise().then(function (reponse) {
// update the sask status
task.status = "finished";
// clear the task promise
task.promise = undefined;
// decrease the active tasks
numActive -= 1;
// if the queue is empty return the main promise
if (numActive === 0) {
return queuePromiseResolve();
}
startNextTask();
});
};
addTask = function (promise) {
let task = {
position: queue.length,
status: "new",
promise: promise
};
// execute the reject function if provided
if (typeof specs.onResolve === "function") {
onResolve(reponse);
}
queue.push(task);
// start the next available task
return startNextTask();
}).catch(function (err) {
// update the sask status
task.status = "finished";
if (queueIsRunning && numActive < maxActive) {
startTask(task);
}
// clear the task promise
task.promise = undefined;
// decrease the active tasks
numActive -= 1;
// if the queue is empty return the main promise
if (numActive === 0) {
return queuePromiseResolve();
}
// execute the reject function if provided
if (typeof specs.onReject === "function") {
onReject(err);
}
return startNextTask();
});
};
// start the first available task
startNextTask = function () {
// find the first available task
let firstInactiveTaskIndex;

@@ -49,23 +95,49 @@ queue.some(function (task, index) {

// return if there are no tasks left
if (firstInactiveTaskIndex === undefined) {
return;
return false;
}
let task = queue[firstInactiveTaskIndex];
startTask(task);
// start the task
return startTask(queue[firstInactiveTaskIndex]);
};
// add a task to the queue
addTask = function (promise) {
// build the task object
let task = {
position: queue.length,
status: "new",
promise: promise
};
// add the task to the queue
queue.push(task);
// start the task if the maximum concurrent tasks have not been reached
if (queueIsRunning && numActive < maxActive) {
startTask(task);
}
};
// start the queue
startQueue = function () {
// set the flag for the active queue
queueIsRunning = true;
while (numActive <= maxActive) {
startNextTask();
// activate the maximum amount of tasks
while (numActive < maxActive) {
// break loop if there are no tasks to start
if (startNextTask() === false) {
break;
}
}
return queuePromise;
// return the main promise
return new Promise(function (resolve) {
queuePromiseResolve = resolve;
});
};
maxActive = specs.concurrency || 5;
queuePromise = new Promise(function (resolve) {
queuePromiseResolve = resolve;
});
return {

@@ -72,0 +144,0 @@ addTask: addTask,

{
"name": "gcp-api-queue",
"version": "0.1.0",
"version": "0.2.0",
"description": "No dependencies queue for the Google Cloud Platform ",

@@ -5,0 +5,0 @@ "main": "index.js",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc