From 3d49a7504b646f671fe30d221b4a0351a3623c3f Mon Sep 17 00:00:00 2001 From: yann300 Date: Wed, 25 Aug 2021 10:44:25 +0200 Subject: [PATCH] simplify "openFile" and make it async --- apps/remix-ide/src/app/files/fileManager.js | 50 +++++++++++---------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/apps/remix-ide/src/app/files/fileManager.js b/apps/remix-ide/src/app/files/fileManager.js index 68ac44199e..a5618995a9 100644 --- a/apps/remix-ide/src/app/files/fileManager.js +++ b/apps/remix-ide/src/app/files/fileManager.js @@ -177,7 +177,7 @@ class FileManager extends Plugin { } await this._handleExists(path, `Cannot open file ${path}`) await this._handleIsFile(path, `Cannot open file ${path}`) - return this.openFile(path) + await this.openFile(path) } catch (e) { throw new Error(e) } @@ -601,8 +601,11 @@ class FileManager extends Plugin { this.events.emit('noFileSelected') } - openFile (file) { - const _openFile = (file) => { + async openFile (file) { + if (!file) { + this.emit('noFileSelected') + this.events.emit('noFileSelected') + } else { this.saveCurrentFile() let resolved try { @@ -614,26 +617,27 @@ class FileManager extends Plugin { const provider = resolved.provider this._deps.config.set('currentFile', file) this.openedFiles[file] = file - provider.get(file, (error, content) => { - if (error) { - console.log(error) - } else { - if (provider.isReadOnly(file)) { - this.editor.openReadOnly(file, content) - } else { - this.editor.open(file, content) - } - // TODO: Only keep `this.emit` (issue#2210) - this.emit('currentFileChanged', file) - this.events.emit('currentFileChanged', file) - } - }) - } - if (file) return _openFile(file) - else { - this.emit('noFileSelected') - this.events.emit('noFileSelected') - } + await (() => { + return new Promise((resolve, reject) => { + provider.get(file, (error, content) => { + if (error) { + console.log(error) + reject(error) + } else { + if (provider.isReadOnly(file)) { + this.editor.openReadOnly(file, content) + } else { + this.editor.open(file, content) + } + // TODO: Only keep `this.emit` (issue#2210) + this.emit('currentFileChanged', file) + this.events.emit('currentFileChanged', file) + resolve() + } + }) + }) + })() + } } /**