-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (35 loc) · 1.2 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
const soWatchify = require('@lwmqn/smartobject-watchify')
module.exports = function smartbase (so, plugins) {
const iidEvents = {}
so = soWatchify(so)
so.hal = so.hal || {}
// prepare hal and setup functions
for (const name in plugins) {
if (Object.prototype.hasOwnProperty.call(plugins, name)) { so.hal[name] = plugins[name].handle }
}
// initialize the smart object
for (const name in plugins) {
if (Object.prototype.hasOwnProperty.call(plugins, name)) {
const plugin = plugins[name]
const { oid } = plugin
const { iid } = plugin
const { resources } = plugin
const { setup } = plugin
const iidEvent = `${oid}/${iid}/`
iidEvents[iidEvent] = name
so.init(oid, iid, resources, function () {
if (typeof setup === 'function') { setup.call(this) }
})
}
}
for (const ievt in iidEvents) {
if (Object.prototype.hasOwnProperty.call(iidEvents, ievt)) {
// Bridge to alias event
so.onChange(ievt, (rRec) => { // { iPath, rid: rid, cVal: currentValue, pVal: oldValue }
const bridgedEvt = `${iidEvents[rRec.iPath]}/${rRec.rid}`
so._emit(bridgedEvt, rRec.cVal, rRec.pVal)
})
}
}
return so
}