From d172d68b71c79ab88e484566b7d1f61a2e525cd6 Mon Sep 17 00:00:00 2001 From: yann300 Date: Mon, 8 Aug 2016 16:01:52 +0200 Subject: [PATCH] - change event manager creation process - doc --- src/app.js | 4 ++-- src/app/compiler.js | 4 ++-- src/app/execution-context.js | 4 ++-- src/app/formalVerification.js | 4 ++-- src/lib/eventManager.js | 23 +++++++++++++++++++++++ src/lib/util.js | 5 ----- src/universal-dapp.js | 4 ++-- 7 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/app.js b/src/app.js index 9660f606db..690210b5a7 100644 --- a/src/app.js +++ b/src/app.js @@ -16,7 +16,7 @@ var ExecutionContext = require('./app/execution-context'); var UniversalDApp = require('./universal-dapp.js'); var Debugger = require('./app/debugger'); var FormalVerification = require('./app/formalVerification'); -var util = require('./lib/util'); +var EventManager = require('./lib/eventManager'); // The event listener needs to be registered as early as possible, because the // parent will send the message upon the "load" event. @@ -32,7 +32,7 @@ window.addEventListener('message', function (ev) { */ var run = function () { var self = this; - util.makeEventCapable(this); + this.event = new EventManager(); var storage = new Storage(updateFiles); function loadFiles (files) { diff --git a/src/app/compiler.js b/src/app/compiler.js index 02a432c02f..e5b2e9494c 100644 --- a/src/app/compiler.js +++ b/src/app/compiler.js @@ -5,14 +5,14 @@ var utils = require('./utils'); var Base64 = require('js-base64').Base64; -var util = require('../lib/util'); +var EventManager = require('../lib/eventManager'); /* trigger compilationError, compilationSucceed, compilerLoaded, isCompiling */ function Compiler (editor, queryParams, handleGithubCall, updateFiles) { var self = this; - util.makeEventCapable(this); + this.event = new EventManager(); var compileJSON; var compilerAcceptsMultipleFiles; diff --git a/src/app/execution-context.js b/src/app/execution-context.js index f0b4ac5e60..098e6cbbbc 100644 --- a/src/app/execution-context.js +++ b/src/app/execution-context.js @@ -2,7 +2,7 @@ var $ = require('jquery'); var Web3 = require('web3'); -var util = require('../lib/util'); +var EventManager = require('../lib/eventManager'); var EthJSVM = require('ethereumjs-vm'); var injectedProvider; @@ -24,7 +24,7 @@ vm.stateManager.checkpoint(); function ExecutionContext () { var self = this; - util.makeEventCapable(this); + this.event = new EventManager(); var executionContext = injectedProvider ? 'injected' : 'vm'; this.isVM = function () { diff --git a/src/app/formalVerification.js b/src/app/formalVerification.js index 20e1193ca7..629a402dd0 100644 --- a/src/app/formalVerification.js +++ b/src/app/formalVerification.js @@ -1,11 +1,11 @@ var $ = require('jquery'); -var util = require('../lib/util'); +var EventManager = require('../lib/eventManager'); /* trigger compilationError */ function FormalVerification (outputElement) { - util.makeEventCapable(this); + this.event = new EventManager(); this.outputElement = outputElement; } diff --git a/src/lib/eventManager.js b/src/lib/eventManager.js index 8db570fcb1..29c096dfd1 100644 --- a/src/lib/eventManager.js +++ b/src/lib/eventManager.js @@ -3,6 +3,14 @@ function eventManager () { this.registered = {}; } +/* + * Unregister a listenner. + * Note that if obj is a function. the unregistration will be applied to the dummy obj {}. + * + * @param {String} eventName - the event name + * @param {Object or Func} obj - object that will listen on this event + * @param {Func} func - function of the listenners that will be executed +*/ eventManager.prototype.unregister = function (eventName, obj, func) { if (obj instanceof Function) { func = obj; @@ -17,6 +25,14 @@ eventManager.prototype.unregister = function (eventName, obj, func) { } }; +/* + * Register a new listenner. + * Note that if obj is a function, the function registration will be associated with the dummy object {} + * + * @param {String} eventName - the event name + * @param {Object or Func} obj - object that will listen on this event + * @param {Func} func - function of the listenners that will be executed +*/ eventManager.prototype.register = function (eventName, obj, func) { if (!this.registered[eventName]) { this.registered[eventName] = []; @@ -31,6 +47,13 @@ eventManager.prototype.register = function (eventName, obj, func) { }); }; +/* + * trigger event. + * Every listenner have their associated function executed + * + * @param {String} eventName - the event name + * @param {Array}j - argument that will be passed to the exectued function. +*/ eventManager.prototype.trigger = function (eventName, args) { for (var listener in this.registered[eventName]) { var l = this.registered[eventName][listener]; diff --git a/src/lib/util.js b/src/lib/util.js index b48634393a..67cd4f3066 100644 --- a/src/lib/util.js +++ b/src/lib/util.js @@ -1,14 +1,9 @@ 'use strict'; -var EventManager = require('./eventManager'); module.exports = { extend: function (destination, source) { for (var property in source) { destination[property] = source[property]; } - }, - - makeEventCapable: function (destination) { - destination.event = new EventManager(); } }; diff --git a/src/universal-dapp.js b/src/universal-dapp.js index cef015d792..439cd936f2 100644 --- a/src/universal-dapp.js +++ b/src/universal-dapp.js @@ -6,10 +6,10 @@ var EthJSTX = require('ethereumjs-tx'); var ethJSABI = require('ethereumjs-abi'); var EthJSBlock = require('ethereumjs-block'); var BN = ethJSUtil.BN; -var util = require('./lib/util'); +var EventManager = require('./lib/eventManager'); function UniversalDApp (executionContext, options, txdebugger) { - util.makeEventCapable(this); + this.event = new EventManager(); var self = this; self.options = options || {};