-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (48 loc) · 1.08 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* Module dependencies.
*/
var debug = require('debug')('koa-delay');
var wait = require('co-wait');
/**
* Add a delay before and/or after
* invoking the downstream middleware
*
* @param {Integer} delay(ms) before going downstream
* @param {Integer} delay(ms) before going upstream
*/
module.exports = function(before, after) {
before = ~~before;
after = ~~after;
return function *delay(next) {
if (debug.enabled) {
if (next) {
debug('pause downstream flow for ' + before + 'ms');
} else {
debug('async call duration ' + before + 'ms');
}
}
if (before) {
yield wait(before);
}
if (debug.enabled) {
if (next) {
debug('resume downstream flow');
} else {
debug('async got result');
}
}
if (next) {
// middleware flow: go downstream
yield *next;
}
if (debug.enabled && next) {
debug('pause upstream flow for ' + after + 'ms');
}
if (after) {
yield wait(after);
}
if (debug.enabled && next) {
debug('resume upstream flow');
}
}
}