async-queue-runner 0.2.0 → 0.3.0
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 +1 -0
- package/{_cjs → cjs}/package.json +4 -3
- package/{_esm → esm}/package.json +4 -3
- package/jest.config.js +15 -0
- package/nodemon.json +11 -0
- package/package.json +6 -3
- package/src/action.test.ts +14 -0
- package/src/action.ts +15 -0
- package/{_types/index.d.ts → src/index.ts} +4 -4
- package/src/queue.ts +80 -0
- package/src/runner.test.ts +54 -0
- package/src/runner.ts +37 -0
- package/src/types.ts +19 -0
- package/src/utils.ts +44 -0
- package/tsconfig.cjs.json +10 -0
- package/tsconfig.esm.json +8 -0
- package/tsconfig.json +9 -0
- package/tsconfig.types.json +11 -0
- package/_cjs/action.js +0 -11
- package/_cjs/index.js +0 -9
- package/_cjs/queue.js +0 -62
- package/_cjs/runner.js +0 -31
- package/_cjs/types.js +0 -2
- package/_cjs/utils.js +0 -42
- package/_esm/action.js +0 -7
- package/_esm/index.js +0 -3
- package/_esm/queue.js +0 -58
- package/_esm/runner.js +0 -27
- package/_esm/types.js +0 -1
- package/_esm/utils.js +0 -38
- package/_types/action.d.ts +0 -9
- package/_types/queue.d.ts +0 -19
- package/_types/runner.d.ts +0 -11
- package/_types/types.d.ts +0 -15
- package/_types/utils.d.ts +0 -16
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# node-20-typescript-template
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
{
|
|
2
|
-
"type": "commonjs"
|
|
3
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"type": "commonjs"
|
|
3
|
+
}
|
|
4
|
+
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
{
|
|
2
|
-
"type": "module"
|
|
3
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"type": "module"
|
|
3
|
+
}
|
|
4
|
+
|
package/jest.config.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
|
|
2
|
+
export default {
|
|
3
|
+
transform: {
|
|
4
|
+
'\\.[jt]sx?$': 'ts-jest'
|
|
5
|
+
},
|
|
6
|
+
globals: {
|
|
7
|
+
'ts-jest': {
|
|
8
|
+
useESM: true
|
|
9
|
+
}
|
|
10
|
+
},
|
|
11
|
+
moduleNameMapper: {
|
|
12
|
+
'(.+)\\.js': '$1'
|
|
13
|
+
},
|
|
14
|
+
extensionsToTreatAsEsm: ['.ts']
|
|
15
|
+
};
|
package/nodemon.json
ADDED
package/package.json
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "async-queue-runner",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Library to run in parallel extendable queue of tasks",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"compile": "tsc --project ./tsconfig.json",
|
|
8
|
-
"copy:package:json": "copyfiles ./package.json ./built/ && copyfiles ./package.json ./built/_esm
|
|
8
|
+
"copy:package:json": "copyfiles ./package.json ./built/ && copyfiles ./esm/package.json ./built/_esm && copyfiles ./cjs/package.json ./built/_cjs",
|
|
9
9
|
"build:all": "tsc -p ./tsconfig.esm.json && tsc -p ./tsconfig.cjs.json && tsc -p ./tsconfig.types.json",
|
|
10
10
|
"build": "del-cli ./built/ && npm run build:all && npm run copy:package:json",
|
|
11
|
+
"bump": "npm version minor",
|
|
11
12
|
"watch": "tsc --watch --project ./tsconfig.json",
|
|
12
13
|
"test": "jest"
|
|
13
14
|
},
|
|
15
|
+
"main": "./_cjs/index.js",
|
|
16
|
+
"module": "./_esm/index.js",
|
|
14
17
|
"exports": {
|
|
15
18
|
"./*": {
|
|
16
19
|
"types": "./_types/*.d.ts",
|
|
@@ -19,7 +22,7 @@
|
|
|
19
22
|
"default": "./_esm/*.js"
|
|
20
23
|
}
|
|
21
24
|
},
|
|
22
|
-
"types": "./index.d.ts",
|
|
25
|
+
"types": "./_types/index.d.ts",
|
|
23
26
|
"keywords": [],
|
|
24
27
|
"author": "Eugeny Dementev",
|
|
25
28
|
"license": "MIT",
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { describe, test, expect } from '@jest/globals';
|
|
2
|
+
import { Delay } from './utils.js';
|
|
3
|
+
|
|
4
|
+
describe('Action', () => {
|
|
5
|
+
test('some', async () => {
|
|
6
|
+
const a = new Delay({ delay: 10 });
|
|
7
|
+
|
|
8
|
+
let run = false;
|
|
9
|
+
|
|
10
|
+
await a.execute().then(() => run = true)
|
|
11
|
+
|
|
12
|
+
expect(run).toBe(true);
|
|
13
|
+
})
|
|
14
|
+
});
|
package/src/action.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { IAction, QueueContext } from "./types.js";
|
|
2
|
+
|
|
3
|
+
export type Options = {
|
|
4
|
+
delay?: number,
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export abstract class Action<C> implements IAction {
|
|
8
|
+
delay: number = 0
|
|
9
|
+
|
|
10
|
+
constructor(opts: Options = {}) {
|
|
11
|
+
if (opts.delay) this.delay = opts.delay;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
abstract execute(context: C & QueueContext): Promise<void>
|
|
15
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { IAction, ActionClass, QueueAction, Branches, QueueContext } from './types.js';
|
|
2
|
-
export { AsyncQueue, QueueOpts } from './queue.js';
|
|
3
|
-
export { Action, Options } from './action.js';
|
|
4
|
-
export { util } from './utils.js';
|
|
1
|
+
export { IAction, ActionClass, QueueAction, Branches, QueueContext } from './types.js';
|
|
2
|
+
export { AsyncQueue, QueueOpts } from './queue.js';
|
|
3
|
+
export { Action, Options } from './action.js';
|
|
4
|
+
export { util } from './utils.js';
|
package/src/queue.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { Action } from './action.js';
|
|
2
|
+
import { IAction, QueueAction, QueueContext } from './types.js';
|
|
3
|
+
|
|
4
|
+
export type QueueOpts = {
|
|
5
|
+
actions: QueueAction[],
|
|
6
|
+
name: string
|
|
7
|
+
end: () => void,
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export class AsyncQueue {
|
|
11
|
+
name: string = 'default queue name';
|
|
12
|
+
queue: QueueAction[] = [];
|
|
13
|
+
end: () => void;
|
|
14
|
+
|
|
15
|
+
loopAction = false;
|
|
16
|
+
|
|
17
|
+
context: QueueContext = {
|
|
18
|
+
push: this.push,
|
|
19
|
+
stop: () => { this.loopAction = false; },
|
|
20
|
+
extend: (obj: object) => Object.assign(this.context, obj),
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
constructor(opts: QueueOpts) {
|
|
24
|
+
this.queue = opts.actions;
|
|
25
|
+
this.name = opts.name;
|
|
26
|
+
this.end = opts.end;
|
|
27
|
+
|
|
28
|
+
this.push = this.push.bind(this);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async delay(timeout: number) {
|
|
32
|
+
return new Promise((res) => setTimeout(res, timeout));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async run(context: object) {
|
|
36
|
+
this.loopAction = true;
|
|
37
|
+
Object.assign(this.context, context);
|
|
38
|
+
|
|
39
|
+
try {
|
|
40
|
+
while (this.loopAction) {
|
|
41
|
+
if (this.queue.length === 0) {
|
|
42
|
+
this.loopAction = false;
|
|
43
|
+
console.log('Queue stopped');
|
|
44
|
+
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const item = this.queue.shift();
|
|
49
|
+
|
|
50
|
+
const action = this.processQueueItem(item!);
|
|
51
|
+
|
|
52
|
+
await this.iterate(action!);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
this.end();
|
|
56
|
+
} catch (e) {
|
|
57
|
+
console.log(`Queue(${this.name} failed`);
|
|
58
|
+
console.error(e);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async iterate(action: IAction) {
|
|
63
|
+
const actionName = action.constructor.name || 'some undefined';
|
|
64
|
+
console.log(`Queue(${this.name}): running ${actionName} action`)
|
|
65
|
+
|
|
66
|
+
await action.execute(this.context);
|
|
67
|
+
|
|
68
|
+
await this.delay(action.delay);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
push(actions: QueueAction[]) {
|
|
72
|
+
this.queue.unshift(...actions);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
processQueueItem(item: QueueAction) {
|
|
76
|
+
if (item instanceof Action) {
|
|
77
|
+
return item
|
|
78
|
+
} else return new (item as new (...args: any[]) => IAction);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { describe, test, expect } from '@jest/globals';
|
|
2
|
+
|
|
3
|
+
import { QueueRunner } from './runner.js';
|
|
4
|
+
import { util } from './utils.js';
|
|
5
|
+
import { QueueContext } from './types.js';
|
|
6
|
+
import { Action } from './action.js';
|
|
7
|
+
|
|
8
|
+
function anyAction<C>(execute: (context: C & QueueContext) => Promise<void> | void) {
|
|
9
|
+
class AnyAction extends Action<C> {
|
|
10
|
+
async execute(context: C & QueueContext): Promise<void> {
|
|
11
|
+
return execute(context);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return new AnyAction();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
const order: string[] = []; // queue should fill that array in particular order
|
|
20
|
+
|
|
21
|
+
const testQueue = [
|
|
22
|
+
util.delay(10),
|
|
23
|
+
anyAction(() => { order.push('second') }),
|
|
24
|
+
];
|
|
25
|
+
const testQueue2 = [
|
|
26
|
+
anyAction(() => { order.push('first') }),
|
|
27
|
+
util.delay(15),
|
|
28
|
+
anyAction(() => { order.push('third') }),
|
|
29
|
+
];
|
|
30
|
+
|
|
31
|
+
describe('Runner', () => {
|
|
32
|
+
test('order', async () => {
|
|
33
|
+
return new Promise((res) => {
|
|
34
|
+
const runner = new QueueRunner();
|
|
35
|
+
|
|
36
|
+
runner.addEndListener((name, size) => {
|
|
37
|
+
console.log('end event:', name, size);
|
|
38
|
+
|
|
39
|
+
if (size === 1) {
|
|
40
|
+
expect(order.toString()).toEqual(['first', 'second'].toString());
|
|
41
|
+
return
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
expect(size).toEqual(0);
|
|
45
|
+
expect(order.toString()).toEqual(['first', 'second', 'third'].toString());
|
|
46
|
+
res(null);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
runner.add(testQueue);
|
|
50
|
+
runner.add(testQueue2);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
package/src/runner.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { AsyncQueue } from "./queue.js";
|
|
2
|
+
import { IAction } from "./types.js";
|
|
3
|
+
|
|
4
|
+
type EndListener = (name: string, size: number) => void;
|
|
5
|
+
|
|
6
|
+
export class QueueRunner {
|
|
7
|
+
queues = new Map();
|
|
8
|
+
listeners: EndListener[] = [];
|
|
9
|
+
|
|
10
|
+
add(actions: IAction[], context: object = {}, name: string = this.getName()) {
|
|
11
|
+
const queue = new AsyncQueue({
|
|
12
|
+
name, actions,
|
|
13
|
+
end: () => {
|
|
14
|
+
this.queues.delete(name);
|
|
15
|
+
this.endEvent(name);
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
this.queues.set(name, queue);
|
|
20
|
+
|
|
21
|
+
queue.run(context);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
addEndListener(listener: EndListener) {
|
|
25
|
+
this.listeners.push(listener);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
endEvent(name: string) {
|
|
29
|
+
for (const listener of this.listeners) {
|
|
30
|
+
listener(name, this.queues.size);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
getName() {
|
|
35
|
+
return Math.random().toString(36).substring(2, 10);
|
|
36
|
+
}
|
|
37
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export interface IAction {
|
|
2
|
+
delay: number
|
|
3
|
+
execute: (context: any) => Promise<void>
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export type ActionClass = new (...args: any[]) => IAction;
|
|
7
|
+
|
|
8
|
+
export type QueueAction = IAction | ActionClass;
|
|
9
|
+
|
|
10
|
+
export type Branches = {
|
|
11
|
+
then: QueueAction[]
|
|
12
|
+
else?: QueueAction[]
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type QueueContext = {
|
|
16
|
+
push: (actions: QueueAction[]) => void
|
|
17
|
+
extend: (obj: Partial<object>) => void
|
|
18
|
+
stop: () => void
|
|
19
|
+
};
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Action } from "./action.js";
|
|
2
|
+
import { Branches, QueueContext, IAction } from "./types.js";
|
|
3
|
+
|
|
4
|
+
export class Delay extends Action<null> {
|
|
5
|
+
async execute(): Promise<void> {
|
|
6
|
+
return new Promise(res => {
|
|
7
|
+
setTimeout(res, this.delay);
|
|
8
|
+
})
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const util = {
|
|
13
|
+
delay(timeout: number) {
|
|
14
|
+
return new Delay({ delay: timeout });
|
|
15
|
+
},
|
|
16
|
+
if<C>(condition: (context: C) => Promise<boolean>, branches: Branches) {
|
|
17
|
+
class IfAction extends Action<C> {
|
|
18
|
+
async execute(context: C & QueueContext): Promise<void> {
|
|
19
|
+
const result = await condition(context);
|
|
20
|
+
|
|
21
|
+
if (result) {
|
|
22
|
+
context.push(branches.then);
|
|
23
|
+
} else if (branches.else) {
|
|
24
|
+
context.push(branches.else);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return new IfAction();
|
|
30
|
+
},
|
|
31
|
+
valid<C>(validator: (context: C) => Promise<boolean>, actions: IAction[]) {
|
|
32
|
+
class Validator extends Action<C> {
|
|
33
|
+
async execute(context: C & QueueContext): Promise<void> {
|
|
34
|
+
const valid = await validator(context);
|
|
35
|
+
|
|
36
|
+
if (valid) {
|
|
37
|
+
context.push(actions);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return new Validator();
|
|
43
|
+
},
|
|
44
|
+
}
|
package/tsconfig.json
ADDED
package/_cjs/action.js
DELETED
package/_cjs/index.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.util = exports.Action = exports.AsyncQueue = void 0;
|
|
4
|
-
var queue_js_1 = require("./queue.js");
|
|
5
|
-
Object.defineProperty(exports, "AsyncQueue", { enumerable: true, get: function () { return queue_js_1.AsyncQueue; } });
|
|
6
|
-
var action_js_1 = require("./action.js");
|
|
7
|
-
Object.defineProperty(exports, "Action", { enumerable: true, get: function () { return action_js_1.Action; } });
|
|
8
|
-
var utils_js_1 = require("./utils.js");
|
|
9
|
-
Object.defineProperty(exports, "util", { enumerable: true, get: function () { return utils_js_1.util; } });
|
package/_cjs/queue.js
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AsyncQueue = void 0;
|
|
4
|
-
const action_js_1 = require("./action.js");
|
|
5
|
-
class AsyncQueue {
|
|
6
|
-
name = 'default queue name';
|
|
7
|
-
queue = [];
|
|
8
|
-
end;
|
|
9
|
-
loopAction = false;
|
|
10
|
-
context = {
|
|
11
|
-
push: this.push,
|
|
12
|
-
stop: () => { this.loopAction = false; },
|
|
13
|
-
extend: (obj) => Object.assign(this.context, obj),
|
|
14
|
-
};
|
|
15
|
-
constructor(opts) {
|
|
16
|
-
this.queue = opts.actions;
|
|
17
|
-
this.name = opts.name;
|
|
18
|
-
this.end = opts.end;
|
|
19
|
-
this.push = this.push.bind(this);
|
|
20
|
-
}
|
|
21
|
-
async delay(timeout) {
|
|
22
|
-
return new Promise((res) => setTimeout(res, timeout));
|
|
23
|
-
}
|
|
24
|
-
async run(context) {
|
|
25
|
-
this.loopAction = true;
|
|
26
|
-
Object.assign(this.context, context);
|
|
27
|
-
try {
|
|
28
|
-
while (this.loopAction) {
|
|
29
|
-
if (this.queue.length === 0) {
|
|
30
|
-
this.loopAction = false;
|
|
31
|
-
console.log('Queue stopped');
|
|
32
|
-
break;
|
|
33
|
-
}
|
|
34
|
-
const item = this.queue.shift();
|
|
35
|
-
const action = this.processQueueItem(item);
|
|
36
|
-
await this.iterate(action);
|
|
37
|
-
}
|
|
38
|
-
this.end();
|
|
39
|
-
}
|
|
40
|
-
catch (e) {
|
|
41
|
-
console.log(`Queue(${this.name} failed`);
|
|
42
|
-
console.error(e);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
async iterate(action) {
|
|
46
|
-
const actionName = action.constructor.name || 'some undefined';
|
|
47
|
-
console.log(`Queue(${this.name}): running ${actionName} action`);
|
|
48
|
-
await action.execute(this.context);
|
|
49
|
-
await this.delay(action.delay);
|
|
50
|
-
}
|
|
51
|
-
push(actions) {
|
|
52
|
-
this.queue.unshift(...actions);
|
|
53
|
-
}
|
|
54
|
-
processQueueItem(item) {
|
|
55
|
-
if (item instanceof action_js_1.Action) {
|
|
56
|
-
return item;
|
|
57
|
-
}
|
|
58
|
-
else
|
|
59
|
-
return new item;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
exports.AsyncQueue = AsyncQueue;
|
package/_cjs/runner.js
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.QueueRunner = void 0;
|
|
4
|
-
const queue_js_1 = require("./queue.js");
|
|
5
|
-
class QueueRunner {
|
|
6
|
-
queues = new Map();
|
|
7
|
-
listeners = [];
|
|
8
|
-
add(actions, context = {}, name = this.getName()) {
|
|
9
|
-
const queue = new queue_js_1.AsyncQueue({
|
|
10
|
-
name, actions,
|
|
11
|
-
end: () => {
|
|
12
|
-
this.queues.delete(name);
|
|
13
|
-
this.endEvent(name);
|
|
14
|
-
}
|
|
15
|
-
});
|
|
16
|
-
this.queues.set(name, queue);
|
|
17
|
-
queue.run(context);
|
|
18
|
-
}
|
|
19
|
-
addEndListener(listener) {
|
|
20
|
-
this.listeners.push(listener);
|
|
21
|
-
}
|
|
22
|
-
endEvent(name) {
|
|
23
|
-
for (const listener of this.listeners) {
|
|
24
|
-
listener(name, this.queues.size);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
getName() {
|
|
28
|
-
return Math.random().toString(36).substring(2, 10);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
exports.QueueRunner = QueueRunner;
|
package/_cjs/types.js
DELETED
package/_cjs/utils.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.util = exports.Delay = void 0;
|
|
4
|
-
const action_js_1 = require("./action.js");
|
|
5
|
-
class Delay extends action_js_1.Action {
|
|
6
|
-
async execute() {
|
|
7
|
-
return new Promise(res => {
|
|
8
|
-
setTimeout(res, this.delay);
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
exports.Delay = Delay;
|
|
13
|
-
exports.util = {
|
|
14
|
-
delay(timeout) {
|
|
15
|
-
return new Delay({ delay: timeout });
|
|
16
|
-
},
|
|
17
|
-
if(condition, branches) {
|
|
18
|
-
class IfAction extends action_js_1.Action {
|
|
19
|
-
async execute(context) {
|
|
20
|
-
const result = await condition(context);
|
|
21
|
-
if (result) {
|
|
22
|
-
context.push(branches.then);
|
|
23
|
-
}
|
|
24
|
-
else if (branches.else) {
|
|
25
|
-
context.push(branches.else);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
return new IfAction();
|
|
30
|
-
},
|
|
31
|
-
valid(validator, actions) {
|
|
32
|
-
class Validator extends action_js_1.Action {
|
|
33
|
-
async execute(context) {
|
|
34
|
-
const valid = await validator(context);
|
|
35
|
-
if (valid) {
|
|
36
|
-
context.push(actions);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
return new Validator();
|
|
41
|
-
},
|
|
42
|
-
};
|
package/_esm/action.js
DELETED
package/_esm/index.js
DELETED
package/_esm/queue.js
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import { Action } from './action.js';
|
|
2
|
-
export class AsyncQueue {
|
|
3
|
-
name = 'default queue name';
|
|
4
|
-
queue = [];
|
|
5
|
-
end;
|
|
6
|
-
loopAction = false;
|
|
7
|
-
context = {
|
|
8
|
-
push: this.push,
|
|
9
|
-
stop: () => { this.loopAction = false; },
|
|
10
|
-
extend: (obj) => Object.assign(this.context, obj),
|
|
11
|
-
};
|
|
12
|
-
constructor(opts) {
|
|
13
|
-
this.queue = opts.actions;
|
|
14
|
-
this.name = opts.name;
|
|
15
|
-
this.end = opts.end;
|
|
16
|
-
this.push = this.push.bind(this);
|
|
17
|
-
}
|
|
18
|
-
async delay(timeout) {
|
|
19
|
-
return new Promise((res) => setTimeout(res, timeout));
|
|
20
|
-
}
|
|
21
|
-
async run(context) {
|
|
22
|
-
this.loopAction = true;
|
|
23
|
-
Object.assign(this.context, context);
|
|
24
|
-
try {
|
|
25
|
-
while (this.loopAction) {
|
|
26
|
-
if (this.queue.length === 0) {
|
|
27
|
-
this.loopAction = false;
|
|
28
|
-
console.log('Queue stopped');
|
|
29
|
-
break;
|
|
30
|
-
}
|
|
31
|
-
const item = this.queue.shift();
|
|
32
|
-
const action = this.processQueueItem(item);
|
|
33
|
-
await this.iterate(action);
|
|
34
|
-
}
|
|
35
|
-
this.end();
|
|
36
|
-
}
|
|
37
|
-
catch (e) {
|
|
38
|
-
console.log(`Queue(${this.name} failed`);
|
|
39
|
-
console.error(e);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
async iterate(action) {
|
|
43
|
-
const actionName = action.constructor.name || 'some undefined';
|
|
44
|
-
console.log(`Queue(${this.name}): running ${actionName} action`);
|
|
45
|
-
await action.execute(this.context);
|
|
46
|
-
await this.delay(action.delay);
|
|
47
|
-
}
|
|
48
|
-
push(actions) {
|
|
49
|
-
this.queue.unshift(...actions);
|
|
50
|
-
}
|
|
51
|
-
processQueueItem(item) {
|
|
52
|
-
if (item instanceof Action) {
|
|
53
|
-
return item;
|
|
54
|
-
}
|
|
55
|
-
else
|
|
56
|
-
return new item;
|
|
57
|
-
}
|
|
58
|
-
}
|
package/_esm/runner.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { AsyncQueue } from "./queue.js";
|
|
2
|
-
export class QueueRunner {
|
|
3
|
-
queues = new Map();
|
|
4
|
-
listeners = [];
|
|
5
|
-
add(actions, context = {}, name = this.getName()) {
|
|
6
|
-
const queue = new AsyncQueue({
|
|
7
|
-
name, actions,
|
|
8
|
-
end: () => {
|
|
9
|
-
this.queues.delete(name);
|
|
10
|
-
this.endEvent(name);
|
|
11
|
-
}
|
|
12
|
-
});
|
|
13
|
-
this.queues.set(name, queue);
|
|
14
|
-
queue.run(context);
|
|
15
|
-
}
|
|
16
|
-
addEndListener(listener) {
|
|
17
|
-
this.listeners.push(listener);
|
|
18
|
-
}
|
|
19
|
-
endEvent(name) {
|
|
20
|
-
for (const listener of this.listeners) {
|
|
21
|
-
listener(name, this.queues.size);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
getName() {
|
|
25
|
-
return Math.random().toString(36).substring(2, 10);
|
|
26
|
-
}
|
|
27
|
-
}
|
package/_esm/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/_esm/utils.js
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { Action } from "./action.js";
|
|
2
|
-
export class Delay extends Action {
|
|
3
|
-
async execute() {
|
|
4
|
-
return new Promise(res => {
|
|
5
|
-
setTimeout(res, this.delay);
|
|
6
|
-
});
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
export const util = {
|
|
10
|
-
delay(timeout) {
|
|
11
|
-
return new Delay({ delay: timeout });
|
|
12
|
-
},
|
|
13
|
-
if(condition, branches) {
|
|
14
|
-
class IfAction extends Action {
|
|
15
|
-
async execute(context) {
|
|
16
|
-
const result = await condition(context);
|
|
17
|
-
if (result) {
|
|
18
|
-
context.push(branches.then);
|
|
19
|
-
}
|
|
20
|
-
else if (branches.else) {
|
|
21
|
-
context.push(branches.else);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
return new IfAction();
|
|
26
|
-
},
|
|
27
|
-
valid(validator, actions) {
|
|
28
|
-
class Validator extends Action {
|
|
29
|
-
async execute(context) {
|
|
30
|
-
const valid = await validator(context);
|
|
31
|
-
if (valid) {
|
|
32
|
-
context.push(actions);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
return new Validator();
|
|
37
|
-
},
|
|
38
|
-
};
|
package/_types/action.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { IAction, QueueContext } from "./types.js";
|
|
2
|
-
export type Options = {
|
|
3
|
-
delay?: number;
|
|
4
|
-
};
|
|
5
|
-
export declare abstract class Action<C> implements IAction {
|
|
6
|
-
delay: number;
|
|
7
|
-
constructor(opts?: Options);
|
|
8
|
-
abstract execute(context: C & QueueContext): Promise<void>;
|
|
9
|
-
}
|
package/_types/queue.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { IAction, QueueAction, QueueContext } from './types.js';
|
|
2
|
-
export type QueueOpts = {
|
|
3
|
-
actions: QueueAction[];
|
|
4
|
-
name: string;
|
|
5
|
-
end: () => void;
|
|
6
|
-
};
|
|
7
|
-
export declare class AsyncQueue {
|
|
8
|
-
name: string;
|
|
9
|
-
queue: QueueAction[];
|
|
10
|
-
end: () => void;
|
|
11
|
-
loopAction: boolean;
|
|
12
|
-
context: QueueContext;
|
|
13
|
-
constructor(opts: QueueOpts);
|
|
14
|
-
delay(timeout: number): Promise<unknown>;
|
|
15
|
-
run(context: object): Promise<void>;
|
|
16
|
-
iterate(action: IAction): Promise<void>;
|
|
17
|
-
push(actions: QueueAction[]): void;
|
|
18
|
-
processQueueItem(item: QueueAction): IAction;
|
|
19
|
-
}
|
package/_types/runner.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { IAction } from "./types.js";
|
|
2
|
-
type EndListener = (name: string, size: number) => void;
|
|
3
|
-
export declare class QueueRunner {
|
|
4
|
-
queues: Map<any, any>;
|
|
5
|
-
listeners: EndListener[];
|
|
6
|
-
add(actions: IAction[], context?: object, name?: string): void;
|
|
7
|
-
addEndListener(listener: EndListener): void;
|
|
8
|
-
endEvent(name: string): void;
|
|
9
|
-
getName(): string;
|
|
10
|
-
}
|
|
11
|
-
export {};
|
package/_types/types.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
export interface IAction {
|
|
2
|
-
delay: number;
|
|
3
|
-
execute: (context: any) => Promise<void>;
|
|
4
|
-
}
|
|
5
|
-
export type ActionClass = new (...args: any[]) => IAction;
|
|
6
|
-
export type QueueAction = IAction | ActionClass;
|
|
7
|
-
export type Branches = {
|
|
8
|
-
then: QueueAction[];
|
|
9
|
-
else?: QueueAction[];
|
|
10
|
-
};
|
|
11
|
-
export type QueueContext = {
|
|
12
|
-
push: (actions: QueueAction[]) => void;
|
|
13
|
-
extend: (obj: Partial<object>) => void;
|
|
14
|
-
stop: () => void;
|
|
15
|
-
};
|
package/_types/utils.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { Action } from "./action.js";
|
|
2
|
-
import { Branches, QueueContext, IAction } from "./types.js";
|
|
3
|
-
export declare class Delay extends Action<null> {
|
|
4
|
-
execute(): Promise<void>;
|
|
5
|
-
}
|
|
6
|
-
export declare const util: {
|
|
7
|
-
delay(timeout: number): Delay;
|
|
8
|
-
if<C>(condition: (context: C) => Promise<boolean>, branches: Branches): {
|
|
9
|
-
execute(context: C & QueueContext): Promise<void>;
|
|
10
|
-
delay: number;
|
|
11
|
-
};
|
|
12
|
-
valid<C_1>(validator: (context: C_1) => Promise<boolean>, actions: IAction[]): {
|
|
13
|
-
execute(context: C_1 & QueueContext): Promise<void>;
|
|
14
|
-
delay: number;
|
|
15
|
-
};
|
|
16
|
-
};
|