forked from redux-saga/redux-saga
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchannel.js
219 lines (180 loc) · 5.12 KB
/
channel.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import test from 'tape';
import { emitter, channel, eventChannel, END, UNDEFINED_INPUT_ERROR } from '../src/internal/channel'
import { buffers } from '../src/internal/buffers'
const eq = x => y => x === y
test('emitter', assert => {
assert.plan(1);
const em = emitter()
const actual = []
const unsub1 = em.subscribe(e => actual.push(`1:${e}`))
const unsub2 = em.subscribe(e => actual.push(`2:${e}`))
em.emit('e1')
unsub1()
em.emit('e2')
unsub2()
em.emit('e3')
const expected = ['1:e1', '2:e1', '2:e2']
assert.deepEqual(actual, expected, 'emitter should notify subscribers')
});
test('Unbuffered channel', assert => {
let chan = channel(buffers.none())
let actual = []
const logger = () => (ac) => actual.push(ac)
try {
chan.put(undefined)
} catch (e) {
assert.equal(e.message, UNDEFINED_INPUT_ERROR, 'channel should reject undefined messages')
}
chan = channel(buffers.none())
chan.take(logger(), eq(1))
const cb = logger()
chan.take(cb, eq(1))
chan.put(1)
assert.deepEqual(actual, [1], 'channel must notify takers')
cb.cancel()
chan.put(1)
assert.deepEqual(actual, [1], 'channel must discard cancelled takes')
actual = []
chan.take(logger())
chan.take(logger())
chan.close()
assert.deepEqual(actual, [END, END], 'closing a channel must resolve all takers with END ')
actual = []
chan.take(logger())
assert.deepEqual(actual, [END], 'closed channel must resolve new takers with END')
chan.put('action-after-end')
assert.deepEqual(actual, [END], 'channel must reject messages after being closed')
assert.end()
});
test('buffered channel', assert => {
const buffer = []
const spyBuffer = {
isEmpty: () => !buffer.length,
put: (it) => buffer.push(it),
take: () => buffer.shift()
}
let chan = channel(spyBuffer)
let log = []
const taker = () => (ac) => log.push(ac)
const state = () => [chan.__closed__, chan.__takers__, buffer, log]
var t1 = taker()
chan.take(t1)
assert.deepEqual(
state(),
[
/* closed? */ false,
/* takers */ [t1],
/* buffer */ [],
/* log */ []
],
'channel must queue pending takers if there are no buffered messages')
const t2 = taker()
chan.take(t2)
chan.put(1)
assert.deepEqual(
state(),
[
/* closed? */ false,
/* takers */ [t2],
/* buffer */ [],
/* log */ [1]
],
'channel must resolve the oldest penfing taker with a new message')
chan.put(2)
chan.put(3)
chan.put(4)
//try {
// chan.put(5)
//} catch(err) {
// assert.equal(err.message, BUFFER_OVERFLOW)
//}
assert.deepEqual(
state(),
[
/* closed? */ false,
/* takers */ [],
/* buffer */ [3,4],
/* log */ [1,2]
],
'channel must buffer new messages if there are no takers')
chan.take(taker())
assert.deepEqual(
state(),
[
/* closed? */ false,
/* takers */ [],
/* buffer */ [4],
/* log */ [1,2,3]
],
'channel must resolve new takers if there are buffered messages')
chan.close()
assert.deepEqual(
state(),
[
/* closed? */ true,
/* takers */ [],
/* buffer */ [4],
/* log */ [1,2,3]
],
'channel must set closed state to true')
chan.close()
assert.deepEqual(
state(),
[
/* closed? */ true,
/* takers */ [],
/* buffer */ [4],
/* log */ [1,2,3]
],
'closing an already closed channel should be noop')
chan.put('hi')
chan.put('I said hi')
assert.deepEqual(
state(),
[
/* closed? */ true,
/* takers */ [],
/* buffer */ [4],
/* log */ [1,2,3]
],
'putting on an already closed channel should be noop')
chan.take(taker())
assert.deepEqual(
state(),
[
/* closed? */ true,
/* takers */ [],
/* buffer */ [],
/* log */ [1,2,3,4]
],
'closed channel must resolve new takers with any buffered message')
chan.take(taker())
assert.deepEqual(
state(),
[
/* closed? */ true,
/* takers */ [],
/* buffer */ [],
/* log */ [1,2,3,4, END]
],
'closed channel must resolve new takers with END if there are no buffered message')
assert.end()
});
test('event channel', assert => {
const em = emitter()
let chan = eventChannel(em.subscribe)
let actual = []
chan.take((ac) => actual.push(ac))
em.emit('action-1')
assert.deepEqual(actual, ['action-1'], 'eventChannel must notify takers on a new action')
em.emit('action-1')
assert.deepEqual(actual, ['action-1'], 'eventChannel must notify takers only once')
actual = []
chan.take((ac) => actual.push(ac), ac => ac === 'action-xxx')
chan.close()
assert.deepEqual(actual, [END], 'eventChannel must notify all pending takers on END')
actual = []
chan.take((ac) => actual.push(ac), ac => ac === 'action-yyy')
assert.deepEqual(actual, [END], 'eventChannel must notify all new takers if closed')
assert.end()
});