-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (39 loc) · 1.31 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
const RPC = require('@microverse-network/core/rpc')
const Database = require('@microverse-network/database')
module.exports = class Tracker extends RPC {
constructor(options = {}) {
super(options)
this.db = new Database({ discoverable: false })
this.descriptions = this.db.get('descriptions', { discoverable: false })
}
handleConnectionClose(connection) {
super.handleConnectionClose(connection)
this.remove(connection.peer)
}
add(description) {
this.debug('add %s on %s', description.module.label, description.node.id)
const { id } = description.module
this.descriptions.update({ id }, description, { upsert: true })
// this.remotes.forEach(client => {
// if (client.$nodeId === description.node.id) return
// this.debug(
// 'offer %s on %s to %s',
// description.module.label,
// description.node.id,
// client.$nodeId,
// )
// client.offer(description)
// })
}
remove(nodeId) {
this.descriptions.remove({ 'node.id': nodeId }, { multi: true })
}
query(selector = {}, options = {}) {
return this.descriptions.find(selector, options)
}
getProtocol(methods = {}) {
methods.add = (...args) => this.add(...args)
methods.query = (...args) => this.query(...args)
return super.getProtocol(methods)
}
}