Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80538 views
1
var test = require('tap').test
2
var inf = require('./inflight.js')
3
4
5
function req (key, cb) {
6
cb = inf(key, cb)
7
if (cb) setTimeout(function () {
8
cb(key)
9
cb(key)
10
})
11
return cb
12
}
13
14
test('basic', function (t) {
15
var calleda = false
16
var a = req('key', function (k) {
17
t.notOk(calleda)
18
calleda = true
19
t.equal(k, 'key')
20
if (calledb) t.end()
21
})
22
t.ok(a, 'first returned cb function')
23
24
var calledb = false
25
var b = req('key', function (k) {
26
t.notOk(calledb)
27
calledb = true
28
t.equal(k, 'key')
29
if (calleda) t.end()
30
})
31
32
t.notOk(b, 'second should get falsey inflight response')
33
})
34
35
test('timing', function (t) {
36
var expect = [
37
'method one',
38
'start one',
39
'end one',
40
'two',
41
'tick',
42
'three'
43
]
44
var i = 0
45
46
function log (m) {
47
t.equal(m, expect[i], m + ' === ' + expect[i])
48
++i
49
if (i === expect.length)
50
t.end()
51
}
52
53
function method (name, cb) {
54
log('method ' + name)
55
process.nextTick(cb)
56
}
57
58
var one = inf('foo', function () {
59
log('start one')
60
var three = inf('foo', function () {
61
log('three')
62
})
63
if (three) method('three', three)
64
log('end one')
65
})
66
67
method('one', one)
68
69
var two = inf('foo', function () {
70
log('two')
71
})
72
if (two) method('one', two)
73
74
process.nextTick(log.bind(null, 'tick'))
75
})
76
77
test('parameters', function (t) {
78
t.plan(8)
79
80
var a = inf('key', function (first, second, third) {
81
t.equal(first, 1)
82
t.equal(second, 2)
83
t.equal(third, 3)
84
})
85
t.ok(a, 'first returned cb function')
86
87
var b = inf('key', function (first, second, third) {
88
t.equal(first, 1)
89
t.equal(second, 2)
90
t.equal(third, 3)
91
})
92
t.notOk(b, 'second should get falsey inflight response')
93
94
setTimeout(function () {
95
a(1, 2, 3)
96
})
97
})
98
99