From 7d944721f02cfb48f9ac96bd2706d4c742a8f719 Mon Sep 17 00:00:00 2001 From: yann300 Date: Wed, 19 Oct 2016 11:41:02 +0200 Subject: [PATCH] fix anonymous function --- src/code/disassembler.js | 2 +- src/lib/eventManager.js | 17 +++++++++++------ src/ui/CodeListView.js | 4 ++-- src/ui/StepDetail.js | 6 +++--- src/ui/TxBrowser.js | 2 +- test/eventManager.js | 35 +++++++++++++++++++++++++++++++++++ test/tests.js | 1 + 7 files changed, 54 insertions(+), 13 deletions(-) create mode 100644 test/eventManager.js diff --git a/src/code/disassembler.js b/src/code/disassembler.js index 68bbc6cf72..60ce1f40a3 100644 --- a/src/code/disassembler.js +++ b/src/code/disassembler.js @@ -7,7 +7,7 @@ var createExpressions = function (instructions) { var expressions = [] var labels = 0 for (var i = 0; i < instructions.length; i++) { - let expr = instructions[i] + var expr = instructions[i] expr.functional = false if (expr.name === 'JUMPDEST') { expr.label = 'label' + (++labels) diff --git a/src/lib/eventManager.js b/src/lib/eventManager.js index 8a5839effb..616e0f3732 100644 --- a/src/lib/eventManager.js +++ b/src/lib/eventManager.js @@ -2,6 +2,7 @@ function eventManager () { this.registered = {} + this.anonymous = {} } /* @@ -13,15 +14,16 @@ function eventManager () { * @param {Func} func - function of the listenners that will be executed */ eventManager.prototype.unregister = function (eventName, obj, func) { + if (!this.registered[eventName]) { + return + } if (obj instanceof Function) { func = obj - obj = {} + obj = this.anonymous } for (var reg in this.registered[eventName]) { - if (this.registered[eventName][reg] && - this.registered[eventName][reg].obj === obj && (!func || this.registered[eventName][reg].func === func)) { + if (this.registered[eventName][reg].obj === obj && this.registered[eventName][reg].func === func) { this.registered[eventName].splice(reg, 1) - return } } } @@ -40,7 +42,7 @@ eventManager.prototype.register = function (eventName, obj, func) { } if (obj instanceof Function) { func = obj - obj = {} + obj = this.anonymous } this.registered[eventName].push({ obj: obj, @@ -56,9 +58,12 @@ eventManager.prototype.register = function (eventName, obj, func) { * @param {Array}j - argument that will be passed to the exectued function. */ eventManager.prototype.trigger = function (eventName, args) { + if (!this.registered[eventName]) { + return + } for (var listener in this.registered[eventName]) { var l = this.registered[eventName][listener] - l.func.apply(l.obj, args) + l.func.apply(l.obj === this.anonymous ? {} : l.obj, args) } } diff --git a/src/ui/CodeListView.js b/src/ui/CodeListView.js index 22b9ca79e3..d355dbb751 100644 --- a/src/ui/CodeListView.js +++ b/src/ui/CodeListView.js @@ -21,8 +21,8 @@ CodeListView.prototype.render = function () { CodeListView.prototype.init = function () { var self = this - this.codeManager.register('changed', this, this.changed) - this.parent.register('traceUnloaded', this, function () { + this.codeManager.event.register('changed', this, this.changed) + this.parent.event.register('traceUnloaded', this, function () { self.changed([], '', -1) }) } diff --git a/src/ui/StepDetail.js b/src/ui/StepDetail.js index ab1feeaa79..ac752e5a51 100644 --- a/src/ui/StepDetail.js +++ b/src/ui/StepDetail.js @@ -19,17 +19,17 @@ StepDetail.prototype.render = function () { StepDetail.prototype.init = function () { var self = this - this.parent.register('traceUnloaded', this, function () { + this.parent.event.register('traceUnloaded', this, function () { self.detail = initDetail() self.basicPanel.update(self.detail) }) - this.parent.register('newTraceLoaded', this, function () { + this.parent.event.register('newTraceLoaded', this, function () { self.detail = initDetail() self.basicPanel.update(self.detail) }) - this.parent.register('indexChanged', this, function (index) { + this.parent.event.register('indexChanged', this, function (index) { if (index < 0) return self.detail['vm trace step'] = index diff --git a/src/ui/TxBrowser.js b/src/ui/TxBrowser.js index 71091f5671..b19701dfe8 100644 --- a/src/ui/TxBrowser.js +++ b/src/ui/TxBrowser.js @@ -74,7 +74,7 @@ TxBrowser.prototype.update = function (error, tx) { info.from = tx.from info.to = tx.to info.hash = tx.hash - this.trigger('newTraceRequested', [this.blockNumber, this.txNumber, tx]) + this.event.trigger('newTraceRequested', [this.blockNumber, this.txNumber, tx]) } else { var mes = '' info.from = mes diff --git a/test/eventManager.js b/test/eventManager.js new file mode 100644 index 0000000000..1e497504cf --- /dev/null +++ b/test/eventManager.js @@ -0,0 +1,35 @@ +'use strict' +var tape = require('tape') +var EventManager = require('../src/lib/eventManager') +tape('eventManager', function (t) { + t.test('eventManager', function (st) { + var events = new EventManager() + var listenner = {} + + var trace = '' + listenner.listen = function (data1) { + trace += data1 + } + var registeredFunction = function (data) { + trace += data + } + events.register('event1', listenner, listenner.listen) + events.register('event2', registeredFunction) + events.trigger('event1', ['event1']) + events.trigger('event2', ['event2']) + st.equal(trace, 'event1event2') + + events.unregister('event1', listenner.listen) + st.equal(events.registered['event1'].length, 1) + st.equal(events.registered['event2'].length, 1) + + events.unregister('event1', listenner, listenner.listen) + st.equal(events.registered['event1'].length, 0) + st.equal(events.registered['event2'].length, 1) + + events.unregister('event2', registeredFunction) + st.equal(events.registered['event1'].length, 0) + st.equal(events.registered['event2'].length, 0) + st.end() + }) +}) diff --git a/test/tests.js b/test/tests.js index 2886ff2985..38820e4fe0 100644 --- a/test/tests.js +++ b/test/tests.js @@ -5,3 +5,4 @@ require('./codeManager.js') require('./util.js') require('./astwalker.js') require('./disassembler.js') +require('./eventManager.js')