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

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

@ -11,6 +11,7 @@ class Remixd {
this.callid = 0
this.socket = null
this.connected = false
this.receiveResponse()
}
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) {
function remixdDialog () {
return yo`<div>Connection to Remixd closed. Localhost connection not available anymore.</div>`
@ -87,16 +99,24 @@ class Remixd {
}
call (service, fn, args, callback) {
return new Promise((resolve, reject) => {
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) {
var data = this.format(service, fn, args)
this.callbacks[data.id] = callback
this.socket.send(JSON.stringify(data))
resolve(data.id)
} 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) {

Loading…
Cancel
Save