-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathindex.js
128 lines (107 loc) · 2.92 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
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
var inherits = require('inherits')
var EventEmitter = require('events').EventEmitter
var multifeed = require('multifeed')
var indexer = require('multifeed-index')
module.exports = Kappa
function Kappa (storage, opts) {
if (!(this instanceof Kappa)) return new Kappa(storage, opts)
if (!opts) opts = {}
this._logs = opts.multifeed || multifeed(storage, opts)
this._indexes = {}
this.api = {}
}
inherits(Kappa, EventEmitter)
Kappa.prototype.use = function (name, version, view) {
var self = this
if (typeof version !== 'number') {
view = version
version = undefined
}
var idx = indexer(Object.assign({}, view, {
log: this._logs,
version: version,
maxBatch: view.maxBatch || 10,
batch: view.map
}))
idx.on('error', function (err) {
self.emit('error', err)
})
if (view.indexed) idx.on('indexed', view.indexed)
this._indexes[name] = idx
this.api[name] = {}
this.api[name].ready = idx.ready.bind(idx)
for (var key in view.api) {
if (typeof view.api[key] === 'function') this.api[name][key] = view.api[key].bind(idx, this)
else this.api[name][key] = view.api[key]
}
}
Kappa.prototype.feeds = function () {
return this._logs.feeds()
}
Kappa.prototype.ready = function (viewNames, cb) {
if (typeof viewNames === 'function') {
cb = viewNames
viewNames = []
}
if (typeof viewNames === 'string') viewNames = [viewNames]
if (viewNames.length === 0) {
viewNames = Object.keys(this._indexes)
}
var pending = viewNames.length + 1
var self = this
this._logs.ready(function () {
for (var i = 0; i < viewNames.length; i++) {
self._indexes[viewNames[i]].ready(done)
}
done()
})
function done () {
if (!--pending) cb()
}
}
Kappa.prototype.pause = function (viewNames, cb) {
if (typeof viewNames === 'function') {
cb = viewNames
viewNames = []
}
cb = cb || noop
if (!viewNames) viewNames = []
if (typeof viewNames === 'string') viewNames = [viewNames]
if (viewNames.length === 0) {
viewNames = Object.keys(this._indexes)
}
var pending = viewNames.length + 1
var self = this
this._logs.ready(function () {
for (var i = 0; i < viewNames.length; i++) {
self._indexes[viewNames[i]].pause(done)
}
done()
})
function done () {
if (!--pending) cb()
}
}
Kappa.prototype.resume = function (viewNames) {
if (!viewNames) viewNames = []
if (typeof viewNames === 'string') viewNames = [viewNames]
if (viewNames.length === 0) {
viewNames = Object.keys(this._indexes)
}
var self = this
this._logs.ready(function () {
for (var i = 0; i < viewNames.length; i++) {
self._indexes[viewNames[i]].resume()
}
})
}
Kappa.prototype.writer = function (name, cb) {
this._logs.writer(name, cb)
}
Kappa.prototype.feed = function (key) {
return this._logs.feed(key)
}
Kappa.prototype.replicate = function (isInitiator, opts) {
return this._logs.replicate(isInitiator, opts)
}
function noop () {}