remove fileManager ref from the recorder modulr

fix#476
yann300 4 years ago
parent edbc6f3de7
commit be03cdd59a
  1. 48
      apps/remix-ide/src/app/tabs/runTab/model/recorder.js
  2. 24
      apps/remix-ide/src/app/tabs/runTab/recorder.js
  3. 4
      apps/remix-ide/src/app/udapp/run-tab.js

@ -12,12 +12,11 @@ var helper = require('../../../../lib/helper.js')
* *
*/ */
class Recorder { class Recorder {
constructor (blockchain, fileManager) { constructor (blockchain) {
var self = this var self = this
self.event = new EventManager() self.event = new EventManager()
self.blockchain = blockchain self.blockchain = blockchain
self.data = { _listen: true, _replay: false, journal: [], _createdContracts: {}, _createdContractsReverse: {}, _usedAccounts: {}, _abis: {}, _contractABIReferences: {}, _linkReferences: {} } self.data = { _listen: true, _replay: false, journal: [], _createdContracts: {}, _createdContractsReverse: {}, _usedAccounts: {}, _abis: {}, _contractABIReferences: {}, _linkReferences: {} }
this.fileManager = fileManager
this.blockchain.event.register('initiatingTransaction', (timestamp, tx, payLoad) => { this.blockchain.event.register('initiatingTransaction', (timestamp, tx, payLoad) => {
if (tx.useCall) return if (tx.useCall) return
@ -278,23 +277,26 @@ class Recorder {
return address return address
} }
runScenario (file, continueCb, promptCb, alertCb, confirmationCb, logCallBack, cb) { runScenario (json, continueCb, promptCb, alertCb, confirmationCb, logCallBack, cb) {
if (!file) { if (!json) {
return cb('file must be defined') return cb('a json content must be provided')
} }
if (!file.match('.json$')) { if (typeof json === 'string') {
return cb('A scenario file is required. Please make sure a scenario file is currently displayed in the editor. The file must be of type JSON. Use the "Save Transactions" Button to generate a new Scenario File.') try {
json = JSON.parse(json)
} catch (e) {
return cb('A scenario file is required. It must be json formatted')
} }
this.fileManager.readFile(file).then((json) => { }
try { try {
var obj = JSON.parse(json) var txArray = json.transactions || []
var txArray = obj.transactions || [] var accounts = json.accounts || []
var accounts = obj.accounts || [] var options = json.options || {}
var options = obj.options || {} var abis = json.abis || {}
var abis = obj.abis || {} var linkReferences = json.linkReferences || {}
var linkReferences = obj.linkReferences || {}
} catch (e) { } catch (e) {
return cb('Invalid Scenario File, please try again') return cb('Invalid Scenario File. Please try again')
} }
if (!txArray.length) { if (!txArray.length) {
@ -304,24 +306,8 @@ class Recorder {
this.run(txArray, accounts, options, abis, linkReferences, confirmationCb, continueCb, promptCb, alertCb, logCallBack, (abi, address, contractName) => { this.run(txArray, accounts, options, abis, linkReferences, confirmationCb, continueCb, promptCb, alertCb, logCallBack, (abi, address, contractName) => {
cb(null, abi, address, contractName) cb(null, abi, address, contractName)
}) })
}).catch((error) => cb(error))
}
saveScenario (promptCb, cb) {
var txJSON = JSON.stringify(this.getAll(), null, 2)
var path = this.fileManager.currentPath()
promptCb(path, input => {
var fileProvider = this.fileManager.fileProviderOf(path)
if (!fileProvider) return
var newFile = path + '/' + input
helper.createNonClashingName(newFile, fileProvider, (error, newFile) => {
if (error) return cb('Failed to create file. ' + newFile + ' ' + error)
if (!fileProvider.set(newFile, txJSON)) return cb('Failed to create file ' + newFile)
this.fileManager.open(newFile)
})
})
} }
} }
module.exports = Recorder module.exports = Recorder

@ -18,7 +18,8 @@ const profile = {
class RecorderUI extends Plugin { class RecorderUI extends Plugin {
constructor (blockchain, recorder, logCallBack, config) { constructor (blockchain, fileManager, recorder, logCallBack, config) {
this.fileManager = fileManager
this.blockchain = blockchain this.blockchain = blockchain
this.recorder = recorder this.recorder = recorder
this.logCallBack = logCallBack this.logCallBack = logCallBack
@ -76,14 +77,16 @@ class RecorderUI extends Plugin {
const confirmationCb = this.getConfirmationCb(modalDialog, confirmDialog) const confirmationCb = this.getConfirmationCb(modalDialog, confirmDialog)
this.fileManager.readFile(file).then((json) => {
// TODO: there is still a UI dependency to remove here, it's still too coupled at this point to remove easily // TODO: there is still a UI dependency to remove here, it's still too coupled at this point to remove easily
this.recorder.runScenario(file, continueCb, promptCb, alertCb, confirmationCb, this.logCallBack, (error, abi, address, contractName) => { this.recorder.runScenario(json, continueCb, promptCb, alertCb, confirmationCb, this.logCallBack, (error, abi, address, contractName) => {
if (error) { if (error) {
return modalDialogCustom.alert(error) return modalDialogCustom.alert(error)
} }
this.event.trigger('newScenario', [abi, address, contractName]) this.event.trigger('newScenario', [abi, address, contractName])
}) })
}).catch((error) => cb(error))
} }
getConfirmationCb (modalDialog, confirmDialog) { getConfirmationCb (modalDialog, confirmDialog) {
@ -119,7 +122,7 @@ class RecorderUI extends Plugin {
} }
triggerRecordButton () { triggerRecordButton () {
this.recorder.saveScenario( this.saveScenario(
(path, cb) => { (path, cb) => {
modalDialogCustom.prompt('Save transactions as scenario', 'Transactions will be saved in a file under ' + path, 'scenario.json', cb) modalDialogCustom.prompt('Save transactions as scenario', 'Transactions will be saved in a file under ' + path, 'scenario.json', cb)
}, },
@ -129,6 +132,21 @@ class RecorderUI extends Plugin {
) )
} }
saveScenario (promptCb, cb) {
var txJSON = JSON.stringify(this.getAll(), null, 2)
var path = this.fileManager.currentPath()
promptCb(path, input => {
var fileProvider = this.fileManager.fileProviderOf(path)
if (!fileProvider) return
var newFile = path + '/' + input
helper.createNonClashingName(newFile, fileProvider, (error, newFile) => {
if (error) return cb('Failed to create file. ' + newFile + ' ' + error)
if (!fileProvider.set(newFile, txJSON)) return cb('Failed to create file ' + newFile)
this.fileManager.open(newFile)
})
})
}
} }
module.exports = RecorderUI module.exports = RecorderUI

@ -118,13 +118,13 @@ export class RunTab extends LibraryPlugin {
renderRecorder (udappUI, fileManager, config, logCallback) { renderRecorder (udappUI, fileManager, config, logCallback) {
this.recorderCount = yo`<span>0</span>` this.recorderCount = yo`<span>0</span>`
const recorder = new Recorder(this.blockchain, fileManager) const recorder = new Recorder(this.blockchain)
recorder.event.register('recorderCountChange', (count) => { recorder.event.register('recorderCountChange', (count) => {
this.recorderCount.innerText = count this.recorderCount.innerText = count
}) })
this.event.register('clearInstance', recorder.clearAll.bind(recorder)) this.event.register('clearInstance', recorder.clearAll.bind(recorder))
this.recorderInterface = new RecorderUI(this.blockchain, recorder, logCallback, config) this.recorderInterface = new RecorderUI(this.blockchain, fileManager, recorder, logCallback, config)
this.recorderInterface.event.register('newScenario', (abi, address, contractName) => { this.recorderInterface.event.register('newScenario', (abi, address, contractName) => {
var noInstancesText = this.noInstancesText var noInstancesText = this.noInstancesText

Loading…
Cancel
Save