-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
81 lines (68 loc) · 2.05 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
'use strict'
const { deprecate } = require('util')
const ChannelItem = require('./item')
/**
* The channel class uses a single-direction linked-list
* with two pointers to track the position of input and
* output cursors in a theoretically infinite sequence.
* By shifting the pointers, any event that has had both
* input and output completions will drop out of scope
* and get garbage collected automatically.
*
* Because the functional behaviour of a channel is a
* race between two sides, the internal linked-list will
* always cover only the span between the input and output
* pointers. This design enables optimal memory usage for
* a lossless stream.
*
* To finalize the sequence in a way that mirrors the
* behaviour of iterators, an error or close event will
* create an item in the list which forms a circular
* reference to itself. By doing this, future requests
* beyond the end of the sequence will just repeatedly
* return the final event indicating the done state.
*/
class Channel {
constructor () {
this.input = new ChannelItem()
this.request = this.input
}
give (value) {
const { input } = this
this.input = input.ensureNext()
return input.send(value)
}
next () {
const { request } = this
this.request = request.ensureNext()
return request.consume()
}
giveBack (value) {
const { input, request } = this
// No pending requests or inputs
if (!request.next && !input.next) {
return this.give(value)
}
// If there are pending inputs, create a
// new request and move the request pointer
let item = input
if (request.next) {
item = request.wrap()
this.request = item
}
return item.send(value)
}
error (error) {
return this.input.error(error)
}
close () {
return this.input.close()
}
[Symbol.asyncIterator] () {
return this
}
}
Channel.prototype.take = deprecate(function take () {
return this.next()
}, 'The channel.take() function is deprecated, please use channel.next() or for await loops.')
module.exports = Channel