timequeue 2.0.1 → 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  A queue with custom concurrency and time limits. Inspired by [async/queue](https://caolan.github.io/async/v3/docs.html#queue), but also with variable number of arguments in the worker, events, and with optional time limits.
4
4
 
5
- [![Dependency Status](https://david-dm.org/fent/timequeue.js.svg)](https://david-dm.org/fent/timequeue.js)
5
+ ![Depfu](https://img.shields.io/depfu/fent/timequeue.js)
6
6
  [![codecov](https://codecov.io/gh/fent/timequeue.js/branch/master/graph/badge.svg)](https://codecov.io/gh/fent/timequeue.js)
7
7
 
8
8
  # Usage
@@ -16,8 +16,8 @@ const worker = (arg1, arg2, callback) => {
16
16
 
17
17
  // Worker can be an async function.
18
18
  const worker = async (arg1) => {
19
- await anotherSyncFunction(arg1);
20
- return await andAnotherOne();
19
+ let result1 = await anotherSyncFunction(arg1);
20
+ return andAnotherOne(result1);
21
21
  };
22
22
 
23
23
  // create a queue with max 5 concurrency every second
@@ -60,32 +60,33 @@ Creates a new instance of a queue. Worker must be a function with a callback for
60
60
  // if callback was given to that task, it will be called with the error,
61
61
  // otherwise, the returned promise should be `caught`.
62
62
  , timeout: 0
63
+
64
+ // You can pass a custom store to share tasks between several queues.
65
+ // Default is MemoryStore from `src/mem-store.ts`.
66
+ // Look at `example/redis-store.js` for an example that saves tasks onto redis.
67
+ , store: MemoryStore
63
68
  }
64
69
  ```
65
70
 
66
71
  All of these options can later be edited on the queue instance.
67
72
 
68
73
  ### TimeQueue#active
69
-
70
74
  How many tasks are currently active.
71
75
 
72
76
  ### TimeQueue#intransit
73
-
74
77
  If you use the `every` option to queue up tasks, this property will be delayed from being updated until there are free spots open for new tasks to begin. `active` will be updated as soon as a task finishes, even if the next one is just a timeout around the corner.
75
78
 
76
- ### TimeQueue#queued
77
-
78
- How many tasks are currently in the queue.
79
-
80
79
  ### TimeQueue#finished
81
-
82
80
  How many tasks have finished in total.
83
81
 
84
- ### TimeQueue#push(data..., [callback])
82
+ ### async TimeQueue#push(data..., [callback])
85
83
  Pushes a new task to the queue. Any number of arguments can be given. An optional callback can also be given as the last parameter. The callback will be called when the task is finished or if there was any error running the worker.
86
84
 
87
85
  If the queue is full, pushed tasks will be ignored.
88
86
 
87
+ ### async TimeQueue#store.getQueued()
88
+ How many tasks are currently in the queue.
89
+
89
90
  ### TimeQueue#isFull()
90
91
  Returns true if queue is full.
91
92
 
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /// <reference types="node" />
2
- declare const EventEmitter: any;
2
+ import { EventEmitter } from 'events';
3
3
  declare namespace TimeQueue {
4
4
  type Worker = (...args: any[]) => void | Promise<any>;
5
5
  interface Options {
@@ -7,10 +7,18 @@ declare namespace TimeQueue {
7
7
  every?: number;
8
8
  maxQueued?: number;
9
9
  timeout?: number;
10
+ store?: Store;
10
11
  }
11
12
  interface TaskError extends Error {
12
13
  args: any[];
13
14
  }
15
+ interface Store {
16
+ isEmpty: () => Promise<boolean>;
17
+ getQueued: () => Promise<number>;
18
+ getNextTask: () => Promise<any[]>;
19
+ pushTask: (...args: any[]) => void;
20
+ clear: () => void;
21
+ }
14
22
  }
15
23
  declare class TimeQueue extends EventEmitter {
16
24
  worker: TimeQueue.Worker;
@@ -18,12 +26,11 @@ declare class TimeQueue extends EventEmitter {
18
26
  every: number;
19
27
  maxQueued: number;
20
28
  timeout: number;
21
- private _workerAsync;
22
- private _queue;
29
+ private _isWorkerAsync;
23
30
  private _timers;
31
+ store: TimeQueue.Store;
24
32
  active: number;
25
33
  intransit: number;
26
- queued: number;
27
34
  finished: number;
28
35
  static TaskError: {
29
36
  new (message?: string): {
@@ -32,7 +39,7 @@ declare class TimeQueue extends EventEmitter {
32
39
  message: string;
33
40
  stack?: string;
34
41
  };
35
- captureStackTrace(targetObject: Object, constructorOpt?: Function): void;
42
+ captureStackTrace(targetObject: object, constructorOpt?: Function): void;
36
43
  prepareStackTrace?: (err: Error, stackTraces: NodeJS.CallSite[]) => any;
37
44
  stackTraceLimit: number;
38
45
  };
@@ -55,12 +62,6 @@ declare class TimeQueue extends EventEmitter {
55
62
  * @return {Promise?}
56
63
  */
57
64
  push(...args: any[]): Promise<unknown>;
58
- /**
59
- * Returns true if queue is full.
60
- *
61
- * @return {boolean}
62
- */
63
- isFull(): boolean;
64
65
  /**
65
66
  * Starts a task
66
67
  *
@@ -71,7 +72,7 @@ declare class TimeQueue extends EventEmitter {
71
72
  * Called when a task finishes. Looks at the queue and processes the next
72
73
  * waiting task.
73
74
  */
74
- _next(): void;
75
+ _next(): Promise<void>;
75
76
  /**
76
77
  * Empties the queue and kills the timers.
77
78
  * Active tasks will still be completed.
package/dist/index.js CHANGED
@@ -8,8 +8,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
- const EventEmitter = require('events').EventEmitter;
12
- class TimeQueue extends EventEmitter {
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ const events_1 = require("events");
15
+ const mem_store_1 = __importDefault(require("./mem-store"));
16
+ class TimeQueue extends events_1.EventEmitter {
13
17
  /**
14
18
  * @constructor
15
19
  * @extends {EventEmitter}
@@ -22,18 +26,21 @@ class TimeQueue extends EventEmitter {
22
26
  */
23
27
  constructor(worker, options = {}) {
24
28
  super();
29
+ // How many tasks are currently active.
30
+ this.active = 0;
31
+ // How many tasks are still being waited on,
32
+ // in case the `every` option was used.
33
+ this.intransit = 0;
34
+ // How many tasks have finished.
35
+ this.finished = 0;
25
36
  this.worker = worker;
26
- this._workerAsync = worker.constructor.name == 'AsyncFunction';
37
+ this._isWorkerAsync = worker.constructor.name == 'AsyncFunction';
27
38
  this.concurrency = options.concurrency || 1;
28
39
  this.every = options.every || 0;
29
40
  this.maxQueued = options.maxQueued || Infinity;
30
41
  this.timeout = options.timeout || 0;
31
- this._queue = [];
42
+ this.store = options.store || new mem_store_1.default({ maxQueued: this.maxQueued });
32
43
  this._timers = [];
33
- this.active = 0;
34
- this.intransit = 0;
35
- this.queued = 0;
36
- this.finished = 0;
37
44
  }
38
45
  /**
39
46
  * Pushes a task onto the queue.
@@ -43,46 +50,36 @@ class TimeQueue extends EventEmitter {
43
50
  * @return {Promise?}
44
51
  */
45
52
  push(...args) {
46
- // Returns a promise no `callback` is given.
47
- if (this._workerAsync && args.length === this.worker.length ||
48
- !this._workerAsync && args.length < this.worker.length) {
49
- return new Promise((resolve, reject) => {
50
- // Add any missing arguments.
51
- if (!this._workerAsync) {
52
- while (args.length < this.worker.length - 1) {
53
- args.push(undefined);
53
+ return __awaiter(this, void 0, void 0, function* () {
54
+ // Returns a promise when no `callback` is given.
55
+ if (this._isWorkerAsync && args.length === this.worker.length ||
56
+ !this._isWorkerAsync && args.length < this.worker.length) {
57
+ return new Promise((resolve, reject) => {
58
+ // Add any missing arguments.
59
+ if (!this._isWorkerAsync) {
60
+ while (args.length < this.worker.length - 1) {
61
+ args.push(undefined);
62
+ }
54
63
  }
55
- }
56
- this.push(...args, (err, results) => {
57
- if (err)
58
- return reject(err);
59
- resolve(results);
64
+ TimeQueue.prototype.push.call(this, ...args, (err, results) => {
65
+ if (err)
66
+ return reject(err);
67
+ resolve(results);
68
+ });
60
69
  });
61
- });
62
- }
63
- if (this.isFull()) {
64
- return;
65
- }
66
- if (this.intransit < this.concurrency) {
67
- this.intransit++;
68
- this.active++;
69
- if (this.intransit === this.concurrency) {
70
- this.emit('full');
71
70
  }
72
- this._process(args);
73
- }
74
- else {
75
- this._queue.push(args);
76
- this.queued++;
77
- }
78
- }
79
- /**
80
- * Returns true if queue is full.
81
- *
82
- * @return {boolean}
83
- */
84
- isFull() {
85
- return this.maxQueued === this.queued;
71
+ if (this.intransit < this.concurrency) {
72
+ this.intransit++;
73
+ this.active++;
74
+ if (this.intransit === this.concurrency) {
75
+ this.emit('full');
76
+ }
77
+ yield this._process(args);
78
+ }
79
+ else {
80
+ yield this.store.pushTask(args);
81
+ }
82
+ });
86
83
  }
87
84
  /**
88
85
  * Starts a task
@@ -94,11 +91,11 @@ class TimeQueue extends EventEmitter {
94
91
  const callback = args.pop();
95
92
  let finished = false;
96
93
  let every = ~~this.every;
97
- let timedOut;
94
+ let tookLongerThanEvery;
98
95
  if (every) {
99
- timedOut = false;
96
+ tookLongerThanEvery = false;
100
97
  this._timers.push(setTimeout(() => {
101
- timedOut = true;
98
+ tookLongerThanEvery = true;
102
99
  this._timers.shift();
103
100
  if (finished) {
104
101
  this._next();
@@ -106,7 +103,7 @@ class TimeQueue extends EventEmitter {
106
103
  }, every));
107
104
  }
108
105
  else {
109
- timedOut = true;
106
+ tookLongerThanEvery = true;
110
107
  }
111
108
  // If `timeout` option is set, set a timeout to check the task doesn't lag.
112
109
  let taskTimedOut = false;
@@ -131,7 +128,7 @@ class TimeQueue extends EventEmitter {
131
128
  this.active--;
132
129
  callback(err, result);
133
130
  finished = true;
134
- if (timedOut) {
131
+ if (tookLongerThanEvery) {
135
132
  this._next();
136
133
  }
137
134
  };
@@ -144,7 +141,7 @@ class TimeQueue extends EventEmitter {
144
141
  }, timeout);
145
142
  }
146
143
  // Call the worker.
147
- if (this._workerAsync) {
144
+ if (this._isWorkerAsync) {
148
145
  try {
149
146
  taskCallback(null, yield this.worker(...args));
150
147
  }
@@ -165,29 +162,30 @@ class TimeQueue extends EventEmitter {
165
162
  * waiting task.
166
163
  */
167
164
  _next() {
168
- if (this.intransit <= this.concurrency && this._queue.length) {
169
- this.queued--;
170
- this.active++;
171
- this._process(this._queue.shift());
172
- if (this._queue.length === 0) {
173
- this.emit('empty');
165
+ return __awaiter(this, void 0, void 0, function* () {
166
+ let task;
167
+ if (this.intransit <= this.concurrency && (task = yield this.store.getNextTask())) {
168
+ this.active++;
169
+ yield this._process(task);
170
+ if (yield this.store.isEmpty()) {
171
+ this.emit('empty');
172
+ }
174
173
  }
175
- }
176
- else if (--this.intransit === 0) {
177
- this.emit('drain');
178
- }
174
+ else if (--this.intransit === 0) {
175
+ this.emit('drain');
176
+ }
177
+ });
179
178
  }
180
179
  /**
181
180
  * Empties the queue and kills the timers.
182
181
  * Active tasks will still be completed.
183
182
  */
184
183
  die() {
185
- this._queue = [];
184
+ this.store.clear();
186
185
  this._timers.forEach(clearTimeout);
187
186
  this._timers = [];
188
187
  this.intransit = 0;
189
188
  this.active = 0;
190
- this.queued = 0;
191
189
  }
192
190
  }
193
191
  TimeQueue.TaskError = class TaskError extends Error {
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,MAAM,YAAY,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC,YAAY,CAAC;AAepD,MAAM,SAAU,SAAQ,YAAY;IA6BlC;;;;;;;;;OASG;IACH,YAAY,MAAwB,EAAE,UAA6B,EAAE;QACnE,KAAK,EAAE,CAAC;QAER,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,YAAY,GAAG,MAAM,CAAC,WAAW,CAAC,IAAI,IAAI,eAAe,CAAC;QAC/D,IAAI,CAAC,WAAW,GAAG,OAAO,CAAC,WAAW,IAAI,CAAC,CAAC;QAC5C,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,IAAI,CAAC,CAAC;QAChC,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,QAAQ,CAAC;QAC/C,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,CAAC,CAAC;QACpC,IAAI,CAAC,MAAM,GAAG,EAAE,CAAC;QACjB,IAAI,CAAC,OAAO,GAAG,EAAE,CAAC;QAClB,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;QAChB,IAAI,CAAC,SAAS,GAAG,CAAC,CAAC;QACnB,IAAI,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AAM,CAAC,GAAG,KAAK,CAAC,CAAC;aACvB;QACH,CAAC;KAAA;IAGD;;;OAGG;IACG,KAAK;;YACT,IAAI,IAAI,CAAC;YACT,IAAI,IAAI,CAAC,SAAS,IAAI,IAAI,CAAC,WAAW,IAAI,CAAC,IAAI,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,WAAW,EAAE,CAAC,EAAE;gBACjF,IAAI,CAAC,MAAM,EAAE,CAAC;gBACd,MAAM,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;gBAE1B,IAAI,MAAM,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,EAAE;oBAC9B,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;iBACpB;aAEF;iBAAM,IAAI,EAAE,IAAI,CAAC,SAAS,KAAK,CAAC,EAAE;gBACjC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;aACpB;QACH,CAAC;KAAA;IAGD;;;OAGG;IACH,GAAG;QACD,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;QACnB,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC;QACnC,IAAI,CAAC,OAAO,GAAG,EAAE,CAAC;QAClB,IAAI,CAAC,SAAS,GAAG,CAAC,CAAC;QACnB,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;IAClB,CAAC;;AAjLa,mBAAS,GAAG,MAAM,SAAU,SAAQ,KAAK;CAEtD,CAAA;AAkLH,iBAAS,SAAS,CAAC"}
@@ -0,0 +1,13 @@
1
+ export declare class Options {
2
+ maxQueued?: number;
3
+ }
4
+ export default class MemoryStore {
5
+ maxQueued: number;
6
+ private _queue;
7
+ constructor(options: Options);
8
+ isEmpty(): Promise<boolean>;
9
+ getQueued(): Promise<number>;
10
+ getNextTask(): Promise<any[]>;
11
+ pushTask(args: any[]): Promise<void>;
12
+ clear(): Promise<void>;
13
+ }
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.Options = void 0;
13
+ class Options {
14
+ }
15
+ exports.Options = Options;
16
+ class MemoryStore {
17
+ constructor(options) {
18
+ this.maxQueued = options.maxQueued;
19
+ this._queue = [];
20
+ }
21
+ isEmpty() {
22
+ return __awaiter(this, void 0, void 0, function* () {
23
+ return this._queue.length === 0;
24
+ });
25
+ }
26
+ getQueued() {
27
+ return __awaiter(this, void 0, void 0, function* () {
28
+ return this._queue.length;
29
+ });
30
+ }
31
+ getNextTask() {
32
+ return __awaiter(this, void 0, void 0, function* () {
33
+ return this._queue.shift();
34
+ });
35
+ }
36
+ pushTask(args) {
37
+ return __awaiter(this, void 0, void 0, function* () {
38
+ if (this._queue.length < this.maxQueued) {
39
+ this._queue.push(args);
40
+ }
41
+ });
42
+ }
43
+ clear() {
44
+ return __awaiter(this, void 0, void 0, function* () {
45
+ this._queue = [];
46
+ });
47
+ }
48
+ }
49
+ exports.default = MemoryStore;
50
+ //# sourceMappingURL=mem-store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mem-store.js","sourceRoot":"","sources":["../src/mem-store.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,MAAa,OAAO;CAEnB;AAFD,0BAEC;AACD,MAAqB,WAAW;IAG9B,YAAY,OAAgB;QAC1B,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC;QACnC,IAAI,CAAC,MAAM,GAAG,EAAE,CAAC;IACnB,CAAC;IACK,OAAO;;YACX,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,KAAK,CAAC,CAAC;QAClC,CAAC;KAAA;IACK,SAAS;;YACb,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC;QAC5B,CAAC;KAAA;IACK,WAAW;;YACf,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;QAC7B,CAAC;KAAA;IACK,QAAQ,CAAC,IAAW;;YACxB,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,GAAG,IAAI,CAAC,SAAS,EAAE;gBACvC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;aACxB;QACH,CAAC;KAAA;IACK,KAAK;;YACT,IAAI,CAAC,MAAM,GAAG,EAAE,CAAC;QACnB,CAAC;KAAA;CACF;AAxBD,8BAwBC"}
package/package.json CHANGED
@@ -6,33 +6,31 @@
6
6
  "flow",
7
7
  "time"
8
8
  ],
9
- "version": "2.0.1",
9
+ "version": "3.0.1",
10
10
  "repository": {
11
11
  "type": "git",
12
12
  "url": "git://github.com/fent/timequeue.js.git"
13
13
  },
14
- "author": "fent (https://github.com/fent)",
14
+ "author": "fent <fentbox@gmail.com> (https://github.com/fent)",
15
15
  "main": "./dist/index.js",
16
- "types": "./dist/index.d.ts",
17
16
  "files": [
18
17
  "dist"
19
18
  ],
20
19
  "scripts": {
20
+ "prepare": "tsc -p tsconfig.build.json",
21
21
  "build": "tsc -p tsconfig.build.json",
22
- "test": "nyc --extension .ts --reporter=lcov --reporter=text-summary mocha -- --require ts-node/register test/*-test.ts"
23
- },
24
- "directories": {
25
- "lib": "./lib"
22
+ "test": "nyc --extension .ts --reporter=lcov --reporter=text-summary npm run test:unit",
23
+ "test:unit": "mocha -- --require ts-node/register test/*-test.ts"
26
24
  },
27
25
  "devDependencies": {
28
- "@types/mocha": "^5.2.7",
29
- "@types/node": "^12.12.16",
30
- "@types/sinon": "^7.5.1",
31
- "mocha": "^6.2.0",
26
+ "@types/mocha": "^7.0.0",
27
+ "@types/node": "^13.1.6",
28
+ "@types/sinon": "^9.0.8",
29
+ "mocha": "^7.0.1",
32
30
  "nyc": "^15.0.0",
33
- "sinon": "^8.0.0",
34
- "ts-node": "^8.5.4",
35
- "typescript": "^3.7.3"
31
+ "sinon": "^9.0.0",
32
+ "ts-node": "^10.4.0",
33
+ "typescript": "^4.5.4"
36
34
  },
37
35
  "engines": {
38
36
  "node": ">=10"