forked from kappa-db/kappa-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kappa.js
318 lines (269 loc) · 7.67 KB
/
kappa.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
const thunky = require('thunky')
const { EventEmitter } = require('events')
const Status = {
Ready: 0,
Running: 1,
Paused: 2
}
module.exports = class Kappa extends EventEmitter {
/**
* Create a kappa core.
* @constructor
*/
constructor (opts = {}) {
super()
this.flows = {}
// APIs
this.view = {}
this.source = {}
}
// This is here for backwards compatibility.
get api () { return this.view }
use (name, source, view, opts = {}) {
opts.status = opts.status || this.status
opts.context = opts.context || this
const flow = new Flow(name, source, view, opts)
this.flows[name] = flow
this.view[name] = flow.view
this.source[name] = flow.source
flow.on('error', err => this.emit('error', err, flow))
this.emit('flow', name)
if (this.status !== Status.Paused) flow.open()
return flow
}
pause () {
this.status = Status.Paused
Object.values(this.flows).forEach(flow => flow.pause())
}
resume () {
if (this.status !== Status.Paused) return
Object.values(this.flows).forEach(flow => flow.resume())
this.status = Status.Ready
}
reset (name, cb) {
const flow = this.flows[name]
if (!flow) return cb(new Error('Unknown flow: ' + name))
flow.reset(cb)
}
ready (names, cb) {
if (typeof names === 'function') return this.ready(null, names)
if (typeof names === 'string') names = [names]
if (!names) names = Object.keys(this.flows)
// wait a tick
process.nextTick(() => {
let pending = names.length
if (!pending) return cb()
for (const name of names) {
const flow = this.flows[name]
if (!flow) return cb(new Error('Unknown flow: ' + name))
flow.ready(done)
}
function done () {
if (--pending === 0) cb()
}
})
}
close (cb) {
let flows = Object.values(this.flows)
let pending = flows.length
if (!pending) done()
// TODO: Propagate errors?
flows.forEach(flow => flow.close(done))
function done () {
if (--pending === 0) cb()
}
}
}
class Flow extends EventEmitter {
constructor (name, source, view, opts) {
super()
this.opts = opts
this.name = name
this._view = view
this._source = source
this.context = opts.context
this.status = opts.status || Status.Ready
// Assign view and source apis
this.view = {}
this.source = {}
this.view.ready = cb => this.ready(cb)
if (view.api) {
for (let [key, value] of Object.entries(view.api)) {
this.view[key] = bindFn(value, this, this.context)
}
delete view.api
}
if (source.api) {
for (let [key, value] of Object.entries(source.api)) {
this.source[key] = bindFn(value, this, this.context)
}
delete source.api
}
// Create the list of funtions through which messages run between pull and map.
this._transform = new Pipeline()
if (this._source.transform) this._transform.push(this._source.transform.bind(this._source))
if (this.opts.transform) this._transform.push(this.opts.transform)
if (this._view.transform) this._transform.push(this._view.transform.bind(this._view))
if (this._view.filter) this._transform.push(this._view.filter.bind(this._view))
this._opened = false
this.open = thunky(this._open.bind(this))
}
get version () {
return this._view.version || 1
}
_open (cb = noop) {
if (this._opened) return cb()
const self = this
let pending = 1
if (this._view.open) ++pending && this._view.open(this, onopen)
if (this._source.open) ++pending && this._source.open(this, onopen)
onopen()
function onopen () {
if (--pending !== 0) return
if (self._source.fetchVersion) {
self._source.fetchVersion((err, version) => {
if (err) return ondone()
if (!version) return self._source.storeVersion(self.version, ondone)
if (version !== self.version) {
self.reset(() => self._source.storeVersion(self.version, ondone))
} else ondone()
})
} else ondone()
}
function ondone () {
self._opened = true
self._run()
cb()
}
}
close (cb) {
const self = this
this.pause()
this._closing = true
if (this.status === Status.Running) this.once('ready', close)
else close()
function close () {
let pending = 1
if (self._source.close) ++pending && self._source.close(cb)
if (self._view.close) ++pending && self._view.close(cb)
done()
function done () {
if (--pending !== 0) return
self._closing = false
self._opened = false
cb()
}
}
}
ready (cb, waitForSource) {
const self = this
if (!this._opened) return this.open(() => this.ready(cb))
setImmediate(() => {
if (this.source.ready) this.source.ready(onsourceready)
else onsourceready()
})
function onsourceready () {
process.nextTick(() => {
if (self.status === Status.Ready) process.nextTick(cb)
else self.once('ready', cb)
})
}
}
pause () {
this.status = Status.Paused
}
resume () {
if (this.status !== Status.Paused) return
this.status = Status.Ready
if (!this._opened) this.open()
else this._run()
}
reset (cb = noop) {
const self = this
this.pause()
let pending = 1
process.nextTick(() => {
if (this._view.clearIndex) ++pending && this._view.clearIndex(done)
if (this._source.reset) ++pending && this._source.reset(done)
done()
})
function done () {
if (--pending !== 0) return
self.resume()
cb()
}
}
update () {
if (!this._opened) return
this.incomingUpdate = true
process.nextTick(this._run.bind(this))
}
_run () {
const self = this
if (!this._opened) throw new Error('Flow is not opened.')
if (this.status === Status.Running) return
if (this.status === Status.Paused) return
this.status = Status.Running
this._source.pull(onbatch)
function onbatch (result) {
if (self.status === Status.Paused) return close()
if (!result || !result.messages.length) return close(null, result)
const { messages = [], finished, onindexed } = result
// TODO: Handle timeout / error?
self._transform.run(messages, messages => {
if (!messages.length) return close(null, { messages, finished, onindexed })
self._view.map(messages, () => {
close(null, { messages, finished, onindexed })
})
})
}
function close (err, result) {
if (err) self.emit('error', err)
if (!result) return finish(true)
const { messages, finished, onindexed } = result
if (messages.length && self._view.indexed) {
self._view.indexed(messages)
}
if (onindexed) onindexed(() => finish(finished))
else finish(finished)
}
function finish (finished) {
self.status = Status.Ready
if (self._closing) return self.emit('ready')
if (self.incomingUpdate || !finished) {
self.incomingUpdate = false
process.nextTick(self._run.bind(self))
} else {
self.emit('ready')
}
}
}
}
// Utils
function bindFn (value, ...binds) {
if (typeof value === 'function') value = value.bind(...binds)
return value
}
class Pipeline {
constructor () {
this.fns = []
}
push (fn) {
this.fns.push(fn)
}
run (messages, final) {
runThrough(messages, this.fns, final)
}
}
function runThrough (state, fns, final) {
fns = fns.filter(f => f)
next(state)
function next (state) {
const fn = fns.shift()
if (!fn) return final(state)
fn(state, nextState => {
process.nextTick(next, nextState)
})
}
}
function noop () {}