Implemented receive response in async/await

pull/4/head
ioedeveloper 5 years ago
parent b45004d1ef
commit 12bc629705
  1. 99
      src/app/files/fileManager.js
  2. 25
      src/app/files/remixDProvider.js
  3. 24
      src/lib/remixd.js

@ -57,8 +57,10 @@ class FileManager extends Plugin {
* @param {string} path path of the file/directory * @param {string} path path of the file/directory
* @param {string} message message to display if path doesn't exist. * @param {string} message message to display if path doesn't exist.
*/ */
_handleExists (path, message) { async _handleExists (path, message) {
if (!this.exists(path)) { const exists = await this.exists(path)
if (!exists) {
throw createError({ code: 'ENOENT', message }) throw createError({ code: 'ENOENT', message })
} }
} }
@ -68,8 +70,10 @@ class FileManager extends Plugin {
* @param {string} path path of the file/directory * @param {string} path path of the file/directory
* @param {string} message message to display if path is not a file. * @param {string} message message to display if path is not a file.
*/ */
_handleIsFile (path, message) { async _handleIsFile (path, message) {
if (!this.isFile(path)) { const isFile = await this.isFile(path)
if (!isFile) {
throw createError({ code: 'EISDIR', message }) throw createError({ code: 'EISDIR', message })
} }
} }
@ -79,8 +83,10 @@ class FileManager extends Plugin {
* @param {string} path path of the file/directory * @param {string} path path of the file/directory
* @param {string} message message to display if path is not a directory. * @param {string} message message to display if path is not a directory.
*/ */
_handleIsDir (path, message) { async _handleIsDir (path, message) {
if (this.isFile(path)) { const isDir = await this.isDirectory(path)
if (!isDir) {
throw createError({ code: 'ENOTDIR', message }) throw createError({ code: 'ENOTDIR', message })
} }
} }
@ -100,11 +106,12 @@ class FileManager extends Plugin {
*/ */
exists (path) { exists (path) {
const provider = this.fileProviderOf(path) const provider = this.fileProviderOf(path)
const result = provider.exists(path, (err, result) => {
return provider.exists(path, (err, result) => {
if (err) return false if (err) return false
return result return result
}) })
return result
} }
/** /**
@ -114,8 +121,21 @@ class FileManager extends Plugin {
*/ */
isFile (path) { isFile (path) {
const provider = this.fileProviderOf(path) const provider = this.fileProviderOf(path)
const result = provider.isFile(path)
return provider.isFile(path) return result
}
/**
* Verify if the path provided is a directory
* @param {string} path path of the directory
* @returns {boolean} true if path is a directory.
*/
isDirectory (path) {
const provider = this.fileProviderOf(path)
const result = provider.isDirectory(path)
return result
} }
/** /**
@ -123,9 +143,9 @@ class FileManager extends Plugin {
* @param {string} path path of the file * @param {string} path path of the file
* @returns {void} * @returns {void}
*/ */
open (path) { async open (path) {
this._handleExists(path, `Cannot open file ${path}`) await this._handleExists(path, `Cannot open file ${path}`)
this._handleIsFile(path, `Cannot open file ${path}`) await this._handleIsFile(path, `Cannot open file ${path}`)
return this.switchFile(path) return this.switchFile(path)
} }
@ -135,9 +155,9 @@ class FileManager extends Plugin {
* @param {string} data content to write on the file * @param {string} data content to write on the file
* @returns {void} * @returns {void}
*/ */
writeFile (path, data) { async writeFile (path, data) {
if (this.exists(path)) { if (await this.exists(path)) {
this._handleIsFile(path, `Cannot write file ${path}`) await this._handleIsFile(path, `Cannot write file ${path}`)
this.setFile(path, data) this.setFile(path, data)
} else { } else {
this.setFile(path, data) this.setFile(path, data)
@ -149,9 +169,9 @@ class FileManager extends Plugin {
* @param {string} path path of the file * @param {string} path path of the file
* @returns {string} content of the file * @returns {string} content of the file
*/ */
readFile (path) { async readFile (path) {
this._handleExists(path, `Cannot read file ${path}`) await this._handleExists(path, `Cannot read file ${path}`)
this._handleIsFile(path, `Cannot read file ${path}`) await this._handleIsFile(path, `Cannot read file ${path}`)
return this.getFile(path) return this.getFile(path)
} }
@ -161,12 +181,13 @@ class FileManager extends Plugin {
* @param {string} dest path of the destrination file * @param {string} dest path of the destrination file
* @returns {void} * @returns {void}
*/ */
copyFile (src, dest) { async copyFile (src, dest) {
this._handleExists(src, `Cannot copy from ${src}`) await this._handleExists(src, `Cannot copy from ${src}`)
this._handleIsFile(src, `Cannot copy from ${src}`) await this._handleIsFile(src, `Cannot copy from ${src}`)
this._handleIsFile(dest, `Cannot paste content into ${dest}`) await this._handleIsFile(dest, `Cannot paste content into ${dest}`)
const content = this.readFile(src) const content = await this.readFile(src)
this.writeFile(dest, content)
await this.writeFile(dest, content)
} }
/** /**
@ -175,9 +196,9 @@ class FileManager extends Plugin {
* @note will not work on a directory, use `rmdir` instead * @note will not work on a directory, use `rmdir` instead
* @returns {void} * @returns {void}
*/ */
unlink (path) { async unlink (path) {
this._handleExists(path, `Cannot remove file ${path}`) await this._handleExists(path, `Cannot remove file ${path}`)
this._handleIsDir(path, `Cannot remove file ${path}`) await this._handleIsDir(path, `Cannot remove file ${path}`)
const provider = this.fileProviderOf(path) const provider = this.fileProviderOf(path)
provider.removeFile(path) provider.removeFile(path)
@ -189,10 +210,10 @@ class FileManager extends Plugin {
* @param {string} newPath new path of the file/directory * @param {string} newPath new path of the file/directory
* @returns {void} * @returns {void}
*/ */
rename (oldPath, newPath) { async rename (oldPath, newPath) {
this.__handleExists(oldPath, `Cannot rename ${oldPath}`) await this.__handleExists(oldPath, `Cannot rename ${oldPath}`)
// todo: should we verify if newPath exists here ? const isFile = await this.isFile(oldPath)
const isFile = this.isFile(oldPath)
this.fileRenamedEvent(oldPath, newPath, !isFile) this.fileRenamedEvent(oldPath, newPath, !isFile)
} }
@ -201,8 +222,8 @@ class FileManager extends Plugin {
* @param {string} path path of the new directory * @param {string} path path of the new directory
* @returns {void} * @returns {void}
*/ */
mkdir (path) { async mkdir (path) {
if (this.exists(path)) { if (await this.exists(path)) {
throw createError({ code: 'EEXIST', message: `Cannot create directory ${path}` }) throw createError({ code: 'EEXIST', message: `Cannot create directory ${path}` })
} }
const provider = this.fileProviderOf(path) const provider = this.fileProviderOf(path)
@ -215,9 +236,9 @@ class FileManager extends Plugin {
* @param {string} path path of the directory * @param {string} path path of the directory
* @returns {string[]} list of the file/directory name in this directory * @returns {string[]} list of the file/directory name in this directory
*/ */
readdir (path) { async readdir (path) {
this._handleExists(path) await this._handleExists(path)
this._handleIsDir(path) await this._handleIsDir(path)
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const provider = this.fileProviderOf(path) const provider = this.fileProviderOf(path)
@ -235,9 +256,9 @@ class FileManager extends Plugin {
* @note will not work on a file, use `unlink` instead * @note will not work on a file, use `unlink` instead
* @returns {void} * @returns {void}
*/ */
rmdir (path) { async rmdir (path) {
this._handleExists(path, `Cannot remove directory ${path}`) await this._handleExists(path, `Cannot remove directory ${path}`)
this._handleIsDir(path, `Cannot remove directory ${path}`) await this._handleIsDir(path, `Cannot remove directory ${path}`)
const provider = this.fileProviderOf(path) const provider = this.fileProviderOf(path)

@ -81,11 +81,12 @@ module.exports = class RemixDProvider {
// //
// this.remixd.exists(path, (error, isValid) => {}) // this.remixd.exists(path, (error, isValid) => {})
exists (path, cb) { async exists (path, cb) {
var unprefixedpath = this.removePrefix(path) const unprefixedpath = this.removePrefix(path)
this._remixd.call('sharedfolder', 'exists', {path: unprefixedpath}, (error, result) => { const callId = await this._remixd.call('sharedfolder', 'exists', {path: unprefixedpath})
cb(error, result) const result = await this._remixd.receiveResponse(callId)
})
return cb(null, result)
} }
get (path, cb) { get (path, cb) {
@ -167,6 +168,20 @@ module.exports = class RemixDProvider {
path = self.removePrefix(path) path = self.removePrefix(path)
self.remixd.dir(path, callback) self.remixd.dir(path, callback)
} }
async isDirectory (path) {
const unprefixedpath = this.removePrefix(path)
const callId = await this._remixd.call('sharedfolder', 'isDirectory', {path: unprefixedpath})
return await this._remixd.receiveResponse(callId)
}
async isFile (path) {
const unprefixedpath = this.removePrefix(path)
const callId = await this._remixd.call('sharedfolder', 'isFile', {path: unprefixedpath})
return await this._remixd.receiveResponse(callId)
}
} }
function remixapi (remixd, self) { function remixapi (remixd, self) {

@ -11,6 +11,7 @@ class Remixd {
this.callid = 0 this.callid = 0
this.socket = null this.socket = null
this.connected = false this.connected = false
this.receiveResponse()
} }
online () { online () {
@ -74,6 +75,17 @@ class Remixd {
}) })
} }
async receiveResponse(requestId) {
return new Promise((resolve, reject) => {
this.event.register('replied', (data) => {
if (data.id === requestId) {
if (data.error) reject(data.error)
else resolve(data.result)
}
})
})
}
errored (event) { errored (event) {
function remixdDialog () { function remixdDialog () {
return yo`<div>Connection to Remixd closed. Localhost connection not available anymore.</div>` return yo`<div>Connection to Remixd closed. Localhost connection not available anymore.</div>`
@ -87,16 +99,24 @@ class Remixd {
} }
call (service, fn, args, callback) { call (service, fn, args, callback) {
return new Promise((resolve, reject) => {
this.ensureSocket((error) => { this.ensureSocket((error) => {
if (error) return callback(error) if (error) {
callback && typeof callback === 'function' && callback(error)
reject(error)
return
}
if (this.socket && this.socket.readyState === this.socket.OPEN) { if (this.socket && this.socket.readyState === this.socket.OPEN) {
var data = this.format(service, fn, args) var data = this.format(service, fn, args)
this.callbacks[data.id] = callback this.callbacks[data.id] = callback
this.socket.send(JSON.stringify(data)) this.socket.send(JSON.stringify(data))
resolve(data.id)
} else { } else {
callback('Socket not ready. state:' + this.socket.readyState) callback && typeof callback === 'function' && callback('Socket not ready. state:' + this.socket.readyState)
reject('Socket not ready. state:' + this.socket.readyState)
} }
}) })
})
} }
ensureSocket (cb) { ensureSocket (cb) {

Loading…
Cancel
Save