/
stream.js
249 lines (213 loc) · 5.35 KB
/
stream.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import { Mutex } from 'async-mutex'
import { debounce, throttle } from 'throttle-debounce'
import { Component } from 'react'
import objectHash from 'object-hash'
export class Stream extends Component {
get method () {
return this.props.method || function () {}
}
get request () {
return this.props.request
}
get response () {
return this.props.response
}
static defaultProps = {
request: {},
response: (queue, data) => {
queue.length = 0
queue.push(data)
},
fallback: null,
children: (data, index) => null,
}
state = {
queue: null,
}
componentDidMount () {
this.invoke()
}
componentWillUnmount () {}
invoke = async () => {
const stream = await this.method(this.request)
const queue = []
stream.on('data', response => {
this.props.response(queue, response)
this.setStateDebounce({ queue })
})
}
setStateDebounce = debounce(50, this.setState)
setStateDebounce = throttle(50, this.setState)
render () {
if (this.state.queue == null) {
return this.props.fallback
}
return this.state.queue.map(this.props.children)
}
}
const deepFilterEqual = (a, b, opts = { exclude: [] }) => {
const { exclude } = opts
if (!a) {
return true
}
if (typeof a !== typeof b) {
return false
}
switch (typeof a) {
case 'object':
if (Array.isArray(a)) {
return a.every(av => b.some(bv => deepFilterEqual(av, bv)))
}
return Object.keys(a).every(
k =>
exclude.some(excludeKey => excludeKey === k) ||
deepFilterEqual(a[k], b[k])
)
default:
return a === b
}
}
export class StreamPagination extends Stream {
queue = []
cursor = ''
count = 0
loading = true
retry = () => {
this.loading = true
this.forceUpdate()
this.cursor = ''
this.queue = []
this.invokeHashTable = {}
this.invoke()
}
add = change => {
const { newValue } = change
// delete item from list if not equal with filter
if (!deepFilterEqual(this.filter, newValue)) {
this.delete({ ...change, oldValue: newValue })
return
}
// if item exist, update it
const index = this.queue.findIndex(item => newValue.id === item.id)
if (index !== -1) {
this.queue.splice(index, 1, newValue)
return
}
// else find where to put the new item
const cursorField = this.paginate.sortedBy || 'id'
const cursor = newValue[cursorField]
for (let topIndex in this.queue) {
let itemTop = this.queue[topIndex]
let infTop = cursor <= itemTop[cursorField]
if (infTop) {
this.queue.splice(topIndex, 0, newValue)
return
}
let bottomIndex = this.queue.length - topIndex - 1
let itemBottom = this.queue[bottomIndex]
let supBottom = cursor > itemBottom[cursorField]
if (supBottom) {
if (bottomIndex + 1 !== this.queue.length || change.force) {
this.queue.splice(bottomIndex + 1, 0, newValue)
return
}
return
}
}
// if forced to add, push it
if (change.force) {
this.queue.push(newValue)
return
}
if (this.queue.length < this.paginate.first) {
this.invoke()
}
}
update = change => {
this.add(change)
}
delete = change => {
const { oldValue } = change
const index = this.queue.findIndex(item => item.id === oldValue.id)
if (index === -1) {
return
}
this.queue.splice(index, 1)
}
observe = async change => {
this[change.type](change)
this.smartForceUpdate()
}
smartForceUpdateMutex = new Mutex()
smartForceUpdate = async () => {
if (this.smartForceUpdateMutex.isLocked()) {
return
}
const release = await this.smartForceUpdateMutex.acquire()
this.forceUpdateDebounced(release)
}
forceUpdateDebounced = debounce(16, this.forceUpdate)
get request () {
return { filter: this.filter, paginate: this.paginate }
}
get filter () {
if (this.props.filter) {
return this.props.filter
}
return {}
}
get paginate () {
if (this.props.paginate) {
return this.props.paginate({
cursor: this.cursor,
count: this.queue.length,
})
}
return {
first: 10,
after: this.cursor,
}
}
invokeHashTable = {}
invoke = async () => {
const queue = []
const request = this.request
const requestHash = objectHash(request)
if (this.invokeHashTable[requestHash]) {
return
}
this.invokeHashTable[requestHash] = true
this.loading = true
const stream = await this.method(request)
stream.on('data', response => {
queue.push(response)
this.observe({
type: 'add',
newValue: response,
name: response.id,
force: true,
})
})
stream.on('end', () => {
this.loading = false
if (queue.length !== 0) {
this.cursor = queue[queue.length - 1][this.paginate.sortedBy || 'id']
}
this.smartForceUpdate()
this.invokeHashTable[requestHash] = false
})
}
render () {
if (this.props.fallback && this.loading && this.queue.length === 0) {
return this.props.fallback
}
return this.props.children({
queue: this.queue,
paginate: this.invoke,
count: this.queue.length,
loading: this.loading,
retry: this.retry,
})
}
}
export default Stream