const m = require('mithril') const defaults = require('./defaults') const socket = require('./socket') const core = { name: '...loading...', title: '', apps: [], db: {}, status: {}, } window.core = core 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() }) module.exports = function Module(module) { return defaults(module, { init: function() {}, oninit: function(vnode) { this._listeners = [] this.core = core 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 } } 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]) } }, }) }