From afa87c96262929f1d0cbf98597855497e9e8c6f7 Mon Sep 17 00:00:00 2001 From: yann300 Date: Thu, 11 Aug 2016 20:44:24 +0200 Subject: [PATCH] register event in inner class --- src/app.js | 41 ++++++++++------------------------- src/app/compiler.js | 12 +++++----- src/app/debugger.js | 10 ++++++++- src/app/execution-context.js | 2 +- src/app/formalVerification.js | 27 ++++++++++++++--------- src/app/renderer.js | 18 ++++++++++++++- src/lib/util.js | 9 -------- src/universal-dapp.js | 3 +++ test/compiler-test.js | 3 ++- 9 files changed, 66 insertions(+), 59 deletions(-) delete mode 100644 src/lib/util.js diff --git a/src/app.js b/src/app.js index 690210b5a7..872ff22270 100644 --- a/src/app.js +++ b/src/app.js @@ -28,7 +28,7 @@ window.addEventListener('message', function (ev) { } }, false); /* - trigger selectTab + trigger tabChanged */ var run = function () { var self = this; @@ -411,7 +411,6 @@ var run = function () { setEditorSize(hidingRHP ? 0 : storage.getEditorSize()); $('.toggleRHP i').toggleClass('fa-angle-double-right', !hidingRHP); $('.toggleRHP i').toggleClass('fa-angle-double-left', hidingRHP); - if (!hidingRHP) compiler.compile(); }); // ----------------- editor resize --------------- @@ -436,9 +435,9 @@ var run = function () { $('#output').append($('
').append($('
').text('Loading github.com/' + root + '/' + path + ' ...')));
     return $.getJSON('https://api.github.com/repos/' + root + '/contents/' + path, cb);
   }
-  var transactionDebugger = new Debugger('#debugger');
-  var executionContext = new ExecutionContext();
 
+  var executionContext = new ExecutionContext();
+  var transactionDebugger = new Debugger('#debugger', executionContext.event);
   transactionDebugger.addProvider('VM', executionContext.vm());
   transactionDebugger.switchProvider('VM');
   transactionDebugger.addProvider('INTERNAL', executionContext.web3());
@@ -456,44 +455,26 @@ var run = function () {
     transactionDebugger.debug(data);
   });
 
-  var renderer = new Renderer(editor, executionContext.web3(), updateFiles, udapp, executionContext);
-  var formalVerification = new FormalVerification($('#verificationView'));
-
-  formalVerification.event.register('compilationError', this, function (message, container, noAnnotations) {
-    renderer.error(message, container, noAnnotations);
-  });
-
   var compiler = new Compiler(editor, queryParams, handleGithubCall, updateFiles);
+  var formalVerification = new FormalVerification($('#verificationView'), compiler.event);
+  var renderer = new Renderer(editor, executionContext.web3(), updateFiles, udapp, executionContext, formalVerification.event, compiler.event); // eslint-disable-line
 
   executionContext.event.register('contextChanged', this, function (context) {
-    $('#output').empty();
-    context = context === 'vm' ? 'VM' : context;
-    context = context === 'injected' ? 'EXTERNAL' : context;
-    context = context === 'web3' ? 'INTERNAL' : context;
-    transactionDebugger.switchProvider(context);
     compiler.compile();
   });
+
   executionContext.event.register('web3EndpointChanged', this, function (context) {
-    $('#output').empty();
     compiler.compile();
   });
+
+  executionContext.event.register('compilerLoaded', this, function (context) {
+    compiler.compile();
+  });
+
   compiler.event.register('compilerLoaded', this, function (version) {
     setVersionText(version);
     compiler.compile();
   });
-  compiler.event.register('compilationError', this, function (data) {
-    renderer.error(data);
-  });
-  compiler.event.register('compilationSucceed', this, function (data, source) {
-    if (!hidingRHP) {
-      renderer.contracts(data, source);
-      formalVerification.compilationFinished(data);
-    }
-  });
-  compiler.event.register('isCompiling', this, function () {
-    $('#output').empty();
-    formalVerification.compiling();
-  });
 
   function setVersionText (text) {
     $('#version').text(text);
diff --git a/src/app/compiler.js b/src/app/compiler.js
index e5b2e9494c..436e2bb1bb 100644
--- a/src/app/compiler.js
+++ b/src/app/compiler.js
@@ -8,7 +8,7 @@ var Base64 = require('js-base64').Base64;
 var EventManager = require('../lib/eventManager');
 
 /*
-  trigger compilationError, compilationSucceed, compilerLoaded, isCompiling
+  trigger compilationFinished, compilerLoaded, compilationStarted
 */
 function Compiler (editor, queryParams, handleGithubCall, updateFiles) {
   var self = this;
@@ -44,7 +44,7 @@ function Compiler (editor, queryParams, handleGithubCall, updateFiles) {
 
   var compile = function (missingInputs) {
     editor.clearAnnotations();
-    self.event.trigger('isCompiling', []);
+    self.event.trigger('compilationStarted', []);
     var input = editor.getValue();
     editor.setCacheFileContent(input);
 
@@ -52,7 +52,7 @@ function Compiler (editor, queryParams, handleGithubCall, updateFiles) {
     files[utils.fileNameFromKey(editor.getCacheFile())] = input;
     gatherImports(files, missingInputs, function (input, error) {
       if (input === null) {
-        this.event.trigger('compilationError', [error]);
+        this.event.trigger('compilationFinished', [false, error, editor.getValue()]);
       } else {
         var optimize = queryParams.get().optimize;
         compileJSON(input, optimize ? 1 : 0);
@@ -102,14 +102,14 @@ function Compiler (editor, queryParams, handleGithubCall, updateFiles) {
     var noFatalErrors = true; // ie warnings are ok
 
     if (data['error'] !== undefined) {
-      self.event.trigger('compilationError', [data['error']]);
+      self.event.trigger('compilationFinished', [false, [data['error']], editor.getValue()]);
       if (utils.errortype(data['error']) !== 'warning') {
         noFatalErrors = false;
       }
     }
     if (data['errors'] !== undefined) {
+      self.event.trigger('compilationFinished', [false, data['errors'], editor.getValue()]);
       data['errors'].forEach(function (err) {
-        self.event.trigger('compilationError', [err]);
         if (utils.errortype(err) !== 'warning') {
           noFatalErrors = false;
         }
@@ -119,7 +119,7 @@ function Compiler (editor, queryParams, handleGithubCall, updateFiles) {
     if (missingInputs !== undefined && missingInputs.length > 0) {
       compile(missingInputs);
     } else if (noFatalErrors) {
-      self.event.trigger('compilationSucceed', [data, editor.getValue()]);
+      self.event.trigger('compilationFinished', [true, data, editor.getValue()]);
     }
   }
 
diff --git a/src/app/debugger.js b/src/app/debugger.js
index b44432558a..e7e6ac0d16 100644
--- a/src/app/debugger.js
+++ b/src/app/debugger.js
@@ -1,9 +1,17 @@
 var remix = require('ethereum-remix');
 
-function Debugger (id) {
+function Debugger (id, executionContextEvent) {
   this.el = document.querySelector(id);
   this.debugger = new remix.ui.Debugger();
   this.el.appendChild(this.debugger.render());
+
+  var self = this;
+  executionContextEvent.register('contextChanged', this, function (context) {
+    context = context === 'vm' ? 'VM' : context;
+    context = context === 'injected' ? 'EXTERNAL' : context;
+    context = context === 'web3' ? 'INTERNAL' : context;
+    self.switchProvider(context);
+  });
 }
 
 Debugger.prototype.debug = function (receipt) {
diff --git a/src/app/execution-context.js b/src/app/execution-context.js
index 098e6cbbbc..cc6d218128 100644
--- a/src/app/execution-context.js
+++ b/src/app/execution-context.js
@@ -19,7 +19,7 @@ var vm = new EthJSVM(null, null, { activatePrecompiles: true, enableHomestead: t
 vm.stateManager.checkpoint();
 
 /*
-  trigger contextChanged
+  trigger contextChanged, web3EndpointChanged
 */
 
 function ExecutionContext () {
diff --git a/src/app/formalVerification.js b/src/app/formalVerification.js
index 629a402dd0..0b729db91a 100644
--- a/src/app/formalVerification.js
+++ b/src/app/formalVerification.js
@@ -2,23 +2,28 @@ var $ = require('jquery');
 var EventManager = require('../lib/eventManager');
 
 /*
-  trigger compilationError
+  trigger compilationFinished
 */
-function FormalVerification (outputElement) {
+function FormalVerification (outputElement, compilerEvent) {
   this.event = new EventManager();
   this.outputElement = outputElement;
-}
-
-FormalVerification.prototype.compiling = function () {
-  $('#formalVerificationInput', this.outputElement)
+  var self = this;
+  compilerEvent.register('compilationFinished', this, function (success, data, source) {
+    if (success) {
+      self.compilationFinished(data);
+    }
+  });
+  compilerEvent.register('compilationStarted', this, function () {
+    $('#formalVerificationInput', self.outputElement)
     .val('')
     .hide();
-  $('#formalVerificationErrors').empty();
-};
+    $('#formalVerificationErrors').empty();
+  });
+}
 
 FormalVerification.prototype.compilationFinished = function (compilationResult) {
   if (compilationResult.formal === undefined) {
-    this.event.trigger('compilationError', ['Formal verification not supported by this compiler version.', $('#formalVerificationErrors'), true]);
+    this.event.trigger('compilationFinished', [false, 'Formal verification not supported by this compiler version.', $('#formalVerificationErrors'), true]);
   } else {
     if (compilationResult.formal['why3'] !== undefined) {
       $('#formalVerificationInput', this.outputElement).val(
@@ -30,8 +35,10 @@ FormalVerification.prototype.compilationFinished = function (compilationResult)
     if (compilationResult.formal.errors !== undefined) {
       var errors = compilationResult.formal.errors;
       for (var i = 0; i < errors.length; i++) {
-        this.event.trigger('compilationError', [errors[i], $('#formalVerificationErrors'), true]);
+        this.event.trigger('compilationFinished', [false, errors[i], $('#formalVerificationErrors'), true]);
       }
+    } else {
+      this.event.trigger('compilationFinished', [true, null, null, true]);
     }
   }
 };
diff --git a/src/app/renderer.js b/src/app/renderer.js
index 3058be48db..5dd9158acb 100644
--- a/src/app/renderer.js
+++ b/src/app/renderer.js
@@ -3,12 +3,28 @@ var $ = require('jquery');
 var utils = require('./utils');
 var uiHelper = require('./ui-helper');
 
-function Renderer (editor, web3, updateFiles, udapp, executionContext) {
+function Renderer (editor, web3, updateFiles, udapp, executionContext, formalVerificationEvent, compilerEvent) {
   this.editor = editor;
   this.web3 = web3;
   this.updateFiles = updateFiles;
   this.udapp = udapp;
   this.executionContext = executionContext;
+  var self = this;
+  formalVerificationEvent.register('compilationFinished', this, function (success, message, container, noAnnotations) {
+    if (!success) {
+      self.error(message, container, noAnnotations);
+    }
+  });
+  compilerEvent.register('compilationFinished', this, function (success, data, source) {
+    $('#output').empty();
+    if (success) {
+      self.contracts(data, source);
+    } else {
+      data.forEach(function (err) {
+        self.error(err);
+      });
+    }
+  });
 }
 
 Renderer.prototype.error = function (message, container, noAnnotations) {
diff --git a/src/lib/util.js b/src/lib/util.js
deleted file mode 100644
index 67cd4f3066..0000000000
--- a/src/lib/util.js
+++ /dev/null
@@ -1,9 +0,0 @@
-'use strict';
-
-module.exports = {
-  extend: function (destination, source) {
-    for (var property in source) {
-      destination[property] = source[property];
-    }
-  }
-};
diff --git a/src/universal-dapp.js b/src/universal-dapp.js
index 439cd936f2..a20903c213 100644
--- a/src/universal-dapp.js
+++ b/src/universal-dapp.js
@@ -8,6 +8,9 @@ var EthJSBlock = require('ethereumjs-block');
 var BN = ethJSUtil.BN;
 var EventManager = require('./lib/eventManager');
 
+/*
+  trigger debugRequested
+*/
 function UniversalDApp (executionContext, options, txdebugger) {
   this.event = new EventManager();
   var self = this;
diff --git a/test/compiler-test.js b/test/compiler-test.js
index e189bd0cea..8144f22188 100644
--- a/test/compiler-test.js
+++ b/test/compiler-test.js
@@ -1,6 +1,7 @@
 var test = require('tape');
 
 var Compiler = require('../src/app/compiler');
+var EventManager = require('../src/lib/eventManager');
 
 test('compiler.compile smoke', function (t) {
   t.plan(1);
@@ -9,7 +10,7 @@ test('compiler.compile smoke', function (t) {
   var getCacheFile = function () { return 'fakeCacheFile'; };
   var fakeEditor = {onChangeSetup: noop, clearAnnotations: noop, getValue: noop, setCacheFileContent: noop, getCacheFile: getCacheFile};
   var fakeQueryParams = {get: function () { return {}; }};
-  var compiler = new Compiler(fakeEditor, fakeQueryParams, null, null);
+  var compiler = new Compiler(fakeEditor, fakeQueryParams, null, null, new EventManager());
   compiler.setCompileJSON(noop);
   compiler.compile();
   t.ok(compiler);