Skip to content

Commit 2bdab23

Browse files
author
John Edward
committed
working group queue
1 parent b62b279 commit 2bdab23

File tree

2 files changed

+86
-18
lines changed

2 files changed

+86
-18
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "express-sync-middleware",
2+
"name": "express-request-queue",
33
"version": "1.0.0",
44
"description": "Make synchronous calls to your express routes.",
55
"repository": "https://github.com/jeescu/express-sync-middleware",

src/index.js

Lines changed: 85 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,117 @@
33
/**
44
* Queue
55
*/
6-
class SyncMiddleware {
7-
constructor(config) {
8-
this.config = config;
6+
7+
class Queue {
8+
constructor() {
9+
this.fn = null;
910
this.stacks = [];
1011
}
11-
12+
1213
/**
1314
* @method _async
1415
* @description wraps callable request
1516
* and added to the stack
1617
*
1718
* @param {Function} fn, xp request function
1819
*/
20+
1921
sync = (fn) => {
22+
this.fn = fn;
23+
2024
return (req, res, next) => {
21-
this.stacks.push({
22-
fn,
23-
http: { req, res, next }
24-
});
25+
this.push(fn, { req, res, next });
26+
this.emit();
27+
}
28+
}
2529

26-
if (this.stacks.length == 1) {
27-
this._emitSync();
28-
}
30+
setFn = (fn) => {
31+
this.fn = fn;
32+
}
33+
34+
/**
35+
* @method push
36+
* @description adds request to the stack
37+
*
38+
* @param {Function} fn, xp request function
39+
* @param {*} http, xp req, res, next objects
40+
*/
41+
42+
push = (http) => {
43+
this.stacks.push({ http });
44+
}
45+
46+
/**
47+
* @method emit
48+
* @description emits worker method '_emitSync'
49+
*/
50+
51+
emit = () => {
52+
if (this.stacks.length == 1) {
53+
this._emitSync();
2954
}
3055
}
3156

3257
/**
3358
* @method _emitSync
3459
* @description calls the next queued request
3560
*/
61+
3662
_emitSync = async () => {
3763
const queuedRequest = this.stacks[0];
38-
64+
3965
if (queuedRequest) {
4066
const { req, res, next } = queuedRequest['http'];
41-
const requestCall = queuedRequest['fn'];
42-
43-
await requestCall(req, res, next);
44-
67+
68+
await this.fn(req, res, next);
69+
4570
this.stacks.shift();
4671
this._emitSync();
4772
}
4873
}
4974
}
5075

51-
export default SyncMiddleware;
76+
/**
77+
* Request Queue
78+
*/
79+
class RequestQueue {
80+
constructor(config) {
81+
this.queues = {};
82+
this.config = config;
83+
}
84+
85+
run = (fn) => {
86+
if (this.config) {
87+
// unique queue
88+
if (this.config.unique) {
89+
if (!this.config.from || !this.config.name) {
90+
throw new Error('Additional config elements are required: from, to');
91+
}
92+
93+
return this._groupRun(fn);
94+
}
95+
96+
} else {
97+
// normal queue
98+
return new Queue().sync(fn);
99+
}
100+
}
101+
102+
_groupRun = (fn) => {
103+
return (req, res, next) => {
104+
const id = req[this.config.from][this.config.name];
105+
106+
if (this.queues[id]) {
107+
this.queues[id].push({ req, res, next });
108+
this.queues[id].emit();
109+
} else {
110+
this.queues[id] = new Queue();
111+
this.queues[id].setFn(fn);
112+
this.queues[id].push({ req, res, next });
113+
this.queues[id].emit();
114+
}
115+
}
116+
}
117+
}
118+
119+
export default RequestQueue;

0 commit comments

Comments
 (0)