CompileTab: refactor event listeners

pull/3094/head
serapath 7 years ago
parent 4366e2e5ed
commit eaeaae9f3e
  1. 64
      src/app/tabs/compile-tab.js

@ -22,48 +22,38 @@ class CompileTab {
self.data = { self.data = {
autoCompile: self._opts.config.get('autoCompile') || false, autoCompile: self._opts.config.get('autoCompile') || false,
compileTimeout: null, compileTimeout: null,
contractsDetails: {} contractsDetails: {},
speed: 100
} }
var appAPI = self._api var appAPI = self._api
var appEvents = self._events
appEvents.editor.register('contentChanged', () => { self._events.editor.register('contentChanged', scheduleCompilation)
scheduleCompilation() self._events.editor.register('sessionSwitched', scheduleCompilation)
})
appEvents.editor.register('sessionSwitched', () => {
scheduleCompilation()
})
function scheduleCompilation () { function scheduleCompilation () {
if (!opts.config.get('autoCompile')) { if (!self._opts.config.get('autoCompile')) return
return if (self.data.compileTimeout) window.clearTimeout(self.data.compileTimeout)
} self.data.compileTimeout = window.setTimeout(() => appAPI.runCompiler(), 300)
}
if (compileTimeout) { self._events.compiler.register('compilationDuration', function tabHighlighting (speed) {
window.clearTimeout(compileTimeout) if (self.data.speed > 1000) {
} var msg = `Last compilation took ${speed}ms. We suggest to turn off autocompilation.`
compileTimeout = window.setTimeout(() => { warnCompilationSlow.setAttribute('title', msg)
appAPI.runCompiler()
}, 300)
}
appEvents.compiler.register('compilationDuration', function tabHighlighting (speed) {
if (speed > 1000) {
warnCompilationSlow.setAttribute('title', `Last compilation took ${speed}ms. We suggest to turn off autocompilation.`)
warnCompilationSlow.style.display = 'inline-block' warnCompilationSlow.style.display = 'inline-block'
} else { } else {
warnCompilationSlow.style.display = 'none' warnCompilationSlow.style.display = 'none'
} }
}) })
appEvents.editor.register('contentChanged', function changedFile () { self._events.editor.register('contentChanged', function changedFile () {
var compileTab = document.querySelector('.compileView') var compileTab = document.querySelector('.compileView')
compileTab.style.color = styles.colors.red compileTab.style.color = styles.colors.red
compileIcon.classList.add(`${css.bouncingIcon}`) compileIcon.classList.add(`${css.bouncingIcon}`)
}) })
appEvents.compiler.register('loadingCompiler', function start () { self._events.compiler.register('loadingCompiler', function start () {
compileIcon.classList.add(`${css.spinningIcon}`) compileIcon.classList.add(`${css.spinningIcon}`)
warnCompilationSlow.style.display = 'none' warnCompilationSlow.style.display = 'none'
compileIcon.setAttribute('title', 'compiler is loading, please wait a few moments.') compileIcon.setAttribute('title', 'compiler is loading, please wait a few moments.')
}) })
appEvents.compiler.register('compilationFinished', function finish () { self._events.compiler.register('compilationFinished', function finish () {
var compileTab = document.querySelector('.compileView') var compileTab = document.querySelector('.compileView')
compileTab.style.color = styles.colors.black compileTab.style.color = styles.colors.black
compileIcon.style.color = styles.colors.black compileIcon.style.color = styles.colors.black
@ -71,19 +61,19 @@ class CompileTab {
compileIcon.classList.remove(`${css.bouncingIcon}`) compileIcon.classList.remove(`${css.bouncingIcon}`)
compileIcon.setAttribute('title', 'idle') compileIcon.setAttribute('title', 'idle')
}) })
appEvents.compiler.register('compilationStarted', function start () { self._events.compiler.register('compilationStarted', function start () {
errorContainer.innerHTML = '' errorContainer.innerHTML = ''
compileIcon.classList.remove(`${css.bouncingIcon}`) compileIcon.classList.remove(`${css.bouncingIcon}`)
compileIcon.classList.add(`${css.spinningIcon}`) compileIcon.classList.add(`${css.spinningIcon}`)
compileIcon.setAttribute('title', 'compiling...') compileIcon.setAttribute('title', 'compiling...')
}) })
appEvents.compiler.register('compilerLoaded', function loaded () { self._events.compiler.register('compilerLoaded', function loaded () {
compileIcon.classList.remove(`${css.spinningIcon}`) compileIcon.classList.remove(`${css.spinningIcon}`)
compileIcon.setAttribute('title', '') compileIcon.setAttribute('title', '')
}) })
appEvents.compiler.register('compilationFinished', function (success, data, source) { self._events.compiler.register('compilationFinished', function (success, data, source) {
// reset the contractMetadata list (used by the publish action) // reset the contractMetadata list (used by the publish action)
contractsDetails = {} self.data.contractsDetails = {}
// refill the dropdown list // refill the dropdown list
getContractNames(success, data) getContractNames(success, data)
// hightlight the tab if error // hightlight the tab if error
@ -96,27 +86,27 @@ class CompileTab {
var error = false var error = false
if (data['error']) { if (data['error']) {
error = true error = true
opts.renderer.error(data['error'].formattedMessage, $(errorContainer), {type: data['error'].severity}) self._opts.renderer.error(data['error'].formattedMessage, $(errorContainer), {type: data['error'].severity})
} }
if (data['errors']) { if (data['errors']) {
if (data['errors'].length) error = true if (data['errors'].length) error = true
data['errors'].forEach(function (err) { data['errors'].forEach(function (err) {
opts.renderer.error(err.formattedMessage, $(errorContainer), {type: err.severity}) self._opts.renderer.error(err.formattedMessage, $(errorContainer), {type: err.severity})
}) })
} }
if (!error) { if (!error) {
if (data.contracts) { if (data.contracts) {
opts.compiler.visitContracts((contract) => { self._opts.compiler.visitContracts((contract) => {
opts.renderer.error(contract.name, $(errorContainer), {type: 'success'}) self._opts.renderer.error(contract.name, $(errorContainer), {type: 'success'})
}) })
} }
} }
}) })
appEvents.staticAnalysis.register('staticAnaysisWarning', (count) => { self._events.staticAnalysis.register('staticAnaysisWarning', (count) => {
if (count) { if (count) {
opts.renderer.error(`Static Analysis raised ${count} warning(s) that requires your attention.`, $(errorContainer), { self._opts.renderer.error(`Static Analysis raised ${count} warning(s) that requires your attention.`, $(errorContainer), {
type: 'warning', type: 'warning',
click: () => appAPI.switchTab('staticanalysisView') click: () => self._api.switchTab('staticanalysisView')
}) })
} }
}) })
@ -141,8 +131,6 @@ class CompileTab {
appAPI.runCompiler() appAPI.runCompiler()
}) })
var compileTimeout = null
// ----------------- autoCompile ----------------- // ----------------- autoCompile -----------------
var autoCompileInput = compileContainer.querySelector('#autoCompile') var autoCompileInput = compileContainer.querySelector('#autoCompile')
var autoCompile = false var autoCompile = false

Loading…
Cancel
Save