forked from nodezoo/nodezoo-info
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfo.js
52 lines (37 loc) · 932 Bytes
/
info.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
'use strict'
var Cache = require('lru-cache')
var opts = {
plugin: 'nodezoo-info',
role: 'info',
size: 99999,
wait: 222
}
module.exports = function (options) {
var seneca = this
var extend = seneca.util.deepextend
opts = extend(opts, options)
opts.cache = Cache(options.size)
seneca.add({role: opts.role, cmd: 'get'}, get)
seneca.add({role: opts.role, res: 'part'}, update)
return {
name: opts.plugin
}
}
function get (msg, done) {
var seneca = this
var name = msg.name
seneca.act({role: opts.role, req: 'part', name: name, update: msg.update})
function respond () {
done(null, (opts.cache.get(name) || {}))
}
setTimeout(respond, opts.wait)
}
function update (msg, done) {
done()
var name = msg.name
var data = opts.cache.get(name) || {}
data[msg.part] = msg.data
data.name = name
opts.cache.set(name, data)
this.act('role:info,info:updated', {data: data})
}