sc-manager/client/module.js

76 lines
1.4 KiB
JavaScript
Raw Normal View History

const m = require('mithril')
2020-09-08 07:53:42 +00:00
const defaults = require('./defaults')
const socket = require('./socket')
const core = {
name: '...loading...',
title: '',
apps: [],
db: {},
status: {},
}
socket.on('core.config', function(res) {
core.apps = []
let keys = Object.keys(res)
for (let key of keys) {
if (typeof(res[key]) !== 'object') {
core[key] = res[key]
}
}
for (let appName of res.applications) {
core.apps.push({
name: appName,
config: res[appName],
})
}
m.redraw()
})
socket.on('core.status', function(res) {
core.status = res
m.redraw()
})
2020-09-08 07:53:42 +00:00
module.exports = function Module(module) {
return defaults(module, {
init: function() {},
oninit: function(vnode) {
this._listeners = []
this.core = core
2020-09-08 07:53:42 +00:00
this.init(vnode)
},
_listeners: null,
_socketOn: function(cb) {
socket.on('connect', () => cb())
if (socket.connected) {
cb()
}
},
on: function(name, cb) {
for (let i = 0; i < this._listeners.length; i++) {
if (this._listeners[i][0] === name) {
return
}
}
2020-09-08 07:53:42 +00:00
this._listeners.push([name, cb])
socket.on(name, cb)
},
remove: function() {},
onremove: function() {
this.remove()
if (!this._listeners) return
for (let i = 0; i < this._listeners.length; i++) {
socket.off(this._listeners[i][0], this._listeners[i][1])
2020-09-08 07:53:42 +00:00
}
},
})
}