/** * TODO here: * Create try catch in start and stop: modules are not being unloaded/reloaded if there is exception in stop * Improve fields, solve redundancy, on "on" and "after" * Maybe use the XPrefix to create an endpoint named events or fetch a channel * channel should work for emit aswell * * Separate vars in several contexts * * For the endpoint when bundle is stopped we could * * */ var log = require('hlogger').createLogger('bundle-context'); /** * Check method in bundleActivator if not check as bundleMethod */ function solveMethod(instance,name) { if(instance.bundleActivator != undefined && instance.bundleActivator[name]!= undefined ) { return (context) => {instance.bundleActivator[name](context)}; } var subName = "bundle" + name[0].toUpperCase() + name.slice(1); if(instance[subName] != undefined ) { return (context) => {instance[subName](context)}; } return null; } // Maybe create channel code here and completelly remove on here // // This will contain the listeners for Activator class BundleContext /*extends XEventEmitter*/ { constructor(conf) { /*super();*/ var info = {}; ({id:info.id, name:info.name, manager:info.manager, instance:info.instance, modulePath:info.modulePath, pkgInfo:info.pkgInfo} = conf); //info = fromFields(["id","name","manager","instance","modulePath","pkgInfo"],conf); info.state = 0; info.loadDate = new Date(); this.info = info; this._bundleContext = { intervals:[], timeouts:[], instanceStart: null, instanceStop: null }; var dummyFunc = () => {}; this._bundleContext.instanceStart = solveMethod(this.instance,"start") || dummyFunc; this._bundleContext.instanceStop = solveMethod(this.instance,"stop") || dummyFunc; // New contextual events } get events() { if(this._events == undefined) { this._events = this.manager.eventual.createContext(); } return this._events; } get id() { return this.info.id; } get modulePath() { return this.info.modulePath; } get name() { return this.info.name; } get instance() { return this.info.instance; } get manager() { return this.info.manager; } get version() { return this.info.version; } start() { if(this.info.state == 1) { log.warn("Instance is turned on trying to turn off first"); this.stop(); } try { this._bundleContext.instanceStart(this); } catch(e) { log.error("Bundle start error: " + this.name + "\n",e); } this.info.state = 1; this.info.startDate = new Date(); this.events.channel(this.name).emit('start'); } stop() { if(this.info.state == 0) return; //this.toggleOff("start"); this.events.channel(this.name).emit("stop"); try { this._bundleContext.instanceStop(this); } catch( e) { log.error("Bundle stop error: " + this.name + "\n",e); } this.clearIntervals(); this.clearTimeouts(); this.events.destroy(); //this.manager.events.removeListener(this); this.info.state = 0; } /*channel(...names) { return this.events.channel(...names); }*/ /*emit(name,...args) { return this.events.emit(this.name + ":" +name,...args); } propagate(name,nameto,...args) { return this.events.propagate(this.name + ":" + name, this.name + ":" + nameto,...args); } on(name,cb) { // Only create events if does not exists, maybe with a getter this.events.on(name,cb); return this; } after(name,cb) { // Priorities this.events.on(name,cb,1000); return this; } // Watcher watch(name,cb) { this.events.watch(name,cb); } set(name,value) { this.events.set(this.name + ":" + name, value); } unset(name) { this.events.unset(this.name + ":" + name); } */ // manager Wrappers /** * @param {array} args * @param {function} callback */ with(args,callback) { return this.manager.with(args,callback); } /** * @param {string} match * @param {variable} ...args */ call(name,...args) { return this.manager.call(name,...args); } // Auto clear helpers // setInterval(cb,n) { var ret = setInterval(cb,n); this._bundleContext.intervals.push(ret); return ret; } clearIntervals() { this._bundleContext.intervals.forEach((v) => { clearInterval(v); }); } setTimeout(cb,n) { var ret = setTimeout(cb,n); this._bundleContext.timeouts.push(ret); return ret; } clearTimeouts() { this._bundleContext.timeouts.forEach((v) => { clearTimeout(v); }); } } module.exports = BundleContext;