From: surya.kumar7 Date: Fri, 18 Oct 2019 11:57:21 +0000 (+0530) Subject: [Addon] Refactor event emitter object handling & operations through it X-Git-Tag: submit/tizen_5.5/20191113.043747^2~3^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=7439dc6ec84a4206b490cec367ee949da467fc7d;p=platform%2Fframework%2Fweb%2Fwrtjs.git [Addon] Refactor event emitter object handling & operations through it 1. While sending event emitter object to add-ons, visibility is limited to only two of its functions - on & off 2. Invalid events are discarded even before it's passed to EventEmitter object 3. Removed an excess dependancy between Runtime & WebApplication while handling event emitter object 4. Removed a bit unused code Change-Id: Ide4b8772eb6881384fd4b2bbd32562826ca4ea85 Signed-off-by: surya.kumar7 --- diff --git a/wrt_app/src/addon_manager.js b/wrt_app/src/addon_manager.js index aedebf46..31c537ac 100644 --- a/wrt_app/src/addon_manager.js +++ b/wrt_app/src/addon_manager.js @@ -25,7 +25,7 @@ const ADDONS_DB_FILE = 'addons_db.json'; const ADDONS_PATH = path.join(__dirname, '..', 'addon', 'browser', 'addonapi.js'); // A set of predefined events for addons -let EventList = [ +const EventList = [ 'newListener', // A listener is added via on() or addListener() 'removeListener', // A listener is removed via off() or removeListener() 'lcPrelaunch', // An app is at just before launching @@ -43,10 +43,10 @@ const {EventEmitter} = require('events'); class AddonManager { constructor() { + this.registerAPIModule(); this.addons_list_ = null; this.addons_ = null; this.evt_emitter_ = null; - this.registerAPIModule(); } registerAPIModule() { @@ -129,7 +129,7 @@ class AddonManager { console.log('activate: ' + addon_path + ' name:' + name); try { let Addon = require(addon_path); - addon = new Addon(this.evt_emitter_); + addon = new Addon(this.wrappedEventEmitter); } catch (e) { console.error('activate - error on require() : ' + e); return; @@ -157,7 +157,7 @@ class AddonManager { console.log('deactivate: path:' + addon_path); try { let Addon = require(addon_path); - addon = new Addon(this.evt_emitter_); + addon = new Addon(this.wrappedEventEmitter); } catch (e) { console.error('deactivate - error on require() : ' + e); } @@ -177,7 +177,6 @@ class AddonManager { for (var name in this.addons_) { this.activate(app, name); } - this.removeInvalidListener(); } deactivateAll(app) { @@ -196,12 +195,21 @@ class AddonManager { initEventListener() { this.evt_emitter_ = new EventEmitter(); - this.evt_emitter_.on('newListener', function(event, listener) { - if (EventList.indexOf(event) != -1) - console.log('Valid Event:' + event); - else { - console.log('Invalid Event:' + event); + this.wrappedEventEmitter = { + on: (eventName, listener) => { + if (EventList.indexOf(eventName) !== -1) + this.evt_emitter_.on(eventName, listener); + else console.log('Invalid Event: ' + eventName); + }, + off: (eventName, listener) => { + if (EventList.indexOf(eventName) !== -1) + this.evt_emitter_.off(eventName, listener); + else console.log('Invalid Event: ' + eventName); } + } + + this.evt_emitter_.on('newListener', function(event, listener) { + console.log('A listener for ' + event + ' has been added'); }); this.evt_emitter_.on('removeListener', function(event, listener) { @@ -209,28 +217,6 @@ class AddonManager { }); } - removeInvalidListener() { - let activeEvents = this.evt_emitter_.eventNames(); - for (var i in activeEvents) { - let item = activeEvents[i]; - if (EventList.indexOf(item) == -1) { - console.log('Found invalid event(' + item + ')'); - if (this.evt_emitter_) - this.evt_emitter_.removeAllListeners(item); - else - console.log('event listener is NOT valid'); - } - } - activeEvents = this.evt_emitter_.eventNames(); - console.log('Remained active events : ' + activeEvents); - } - existEventListeners(event) { - let activeEvents = this.evt_emitter_.eventNames(); - if (activeEvents.indexOf(event) == -1) - return false; - else return true; - } - static getManifestFile() { return MANIFEST_FILE; } diff --git a/wrt_app/src/runtime.js b/wrt_app/src/runtime.js index 6130fcd9..736e68e9 100755 --- a/wrt_app/src/runtime.js +++ b/wrt_app/src/runtime.js @@ -39,7 +39,7 @@ class Runtime { app.on('before-quit', function(event) { console.log('before-quit'); if (!wrt.isElectronApp()) { - _this.webApplication.quit(_this.addonManager.evt_emitter_); + _this.webApplication.quit(); _this.webApplication.finalize(); _this.webApplication = null; } @@ -127,7 +127,7 @@ class Runtime { src = "about:blank"; } _this.webApplication.mainWindow.loadURL(src); - _this.webApplication.prelaunch(_this.addonManager.evt_emitter_, src); + _this.webApplication.prelaunch(src); } else { console.log('Handling app-control event'); if (_this.webApplication.preloadStatus == 'readyToShow') { @@ -169,12 +169,12 @@ class Runtime { wrt.on('suspend', function() { console.log('suspend'); if (_this.webApplication) - _this.webApplication.suspend(_this.addonManager.evt_emitter_); + _this.webApplication.suspend(); }); wrt.on('resume', function() { console.log('resume'); if (_this.webApplication) - _this.webApplication.resume(_this.addonManager.evt_emitter_); + _this.webApplication.resume(); }); wrt.on('low-memory', function() { console.log('low-memory'); @@ -313,7 +313,7 @@ class Runtime { } if (valid) { - _this.webApplication.keyEvent(_this.addonManager.evt_emitter_, key); + _this.webApplication.keyEvent(key); } } } diff --git a/wrt_app/src/web_application.js b/wrt_app/src/web_application.js index d06e0d75..e6e75b55 100755 --- a/wrt_app/src/web_application.js +++ b/wrt_app/src/web_application.js @@ -287,11 +287,11 @@ class WebApplication { "profile log from the initial loading." wrt.showDialog(this.mainWindow.webContents, message); } - suspend(evtEmitter) { + suspend() { console.log('WebApplication : suspend'); - if (evtEmitter) { + if (this.addonEmitter) { console.log('WebApplication : suspend - Found event emitter'); - evtEmitter.emit('lcSuspend', this.mainWindow.id); + this.addonEmitter.emit('lcSuspend', this.mainWindow.id); } else { console.log('WebApplication : suspend - Invalid event emitter'); } @@ -313,14 +313,14 @@ class WebApplication { } this.windowList[this.windowList.length - 1].hide(); } - resume(evtEmitter) { + resume() { console.log('WebApplication : resume'); this.suspended = false; - if (evtEmitter) { + if (this.addonEmitter) { console.log('WebApplication : resume - Found event emitter'); - evtEmitter.emit('lcResume', this.mainWindow.id); + this.addonEmitter.emit('lcResume', this.mainWindow.id); } else { console.log('WebApplication : resume - Invalid event emitter'); } @@ -347,11 +347,11 @@ class WebApplication { window.removeAllListeners(); }); } - quit(evtEmitter) { + quit() { console.log('WebApplication : quit'); - if (evtEmitter) { + if (this.addonEmitter) { console.log('WebApplication : quit - Found event emitter'); - evtEmitter.emit('lcQuit', this.mainWindow.id); + this.addonEmitter.emit('lcQuit', this.mainWindow.id); } else { console.log('WebApplication : quit - Invalid event emitter'); } @@ -389,9 +389,9 @@ class WebApplication { window.destroy(); }); } - keyEvent(evtEmitter, key) { + keyEvent(key) { console.log('WebApplication : keyEvent'); - if (!evtEmitter) { + if (!this.addonEmitter) { console.log('Invalid event emitter for key hook'); return; } @@ -399,22 +399,22 @@ class WebApplication { switch(key) { case "ArrowUp": case "Up": - evtEmitter.emit('hwUpkey', this.mainWindow.id); + this.addonEmitter.emit('hwUpkey', this.mainWindow.id); break; case "ArrowDown": case "Down": - evtEmitter.emit('hwDownkey', this.mainWindow.id); + this.addonEmitter.emit('hwDownkey', this.mainWindow.id); break; default: console.log('No handler for ' + key); break; } } - prelaunch(evtEmitter, origURL) { + prelaunch(origURL) { console.log('WebApplication : prelaunch'); - if (evtEmitter) { + if (this.addonEmitter) { console.log('WebApplication : prelaunch - Found event emitter'); - evtEmitter.emit('lcPrelaunch', this.mainWindow.id, origURL); + this.addonEmitter.emit('lcPrelaunch', this.mainWindow.id, origURL); } else { console.log('WebApplication : prelaunch - Invalid event emitter'); }