Websocket client requests

uiCheck
ioedeveloper 5 years ago committed by yann300
parent add05a54af
commit 422efde77b
  1. 2
      embark/index.js
  2. 14
      src/app.js
  3. 107
      src/app/files/remixDProvider.js
  4. 26
      src/app/files/remixd-handle.js

@ -13,7 +13,7 @@ const DEFAULT_OPTIONS = {
module.exports = (embark) => {
// plugin options
const readOnly = embark.pluginConfig.readOnly || false
const {protocol, host, port} = merge.recursive(DEFAULT_OPTIONS, embark.pluginConfig.remixIde)
const { protocol, host, port } = merge.recursive(DEFAULT_OPTIONS, embark.pluginConfig.remixIde)
// globals
const remixIdeUrl = `${protocol}://${host}` + `${port ? `:${port}` : ''}`

@ -5,7 +5,7 @@ var csjs = require('csjs-inject')
var yo = require('yo-yo')
var remixLib = require('remix-lib')
var registry = require('./global/registry')
var Remixd = require('./lib/remixd')
// var Remixd = require('./lib/remixd')
var loadFileFromParent = require('./loadFilesFromParent')
var { OffsetToLineColumnConverter } = require('./lib/offsetToLineColumnConverter')
var QueryParams = require('./lib/query-params')
@ -147,13 +147,13 @@ class App {
self._components.filesProviders['browser'] = new FileProvider('browser')
registry.put({api: self._components.filesProviders['browser'], name: 'fileproviders/browser'})
var remixd = new Remixd(65520)
registry.put({api: remixd, name: 'remixd'})
remixd.event.register('system', (message) => {
if (message.error) toolTip(message.error)
})
// var remixd = new Remixd(65520)
// registry.put({api: remixd, name: 'remixd'})
// remixd.event.register('system', (message) => {
// if (message.error) toolTip(message.error)
// })
self._components.filesProviders['localhost'] = new RemixDProvider(remixd, self.appManager)
self._components.filesProviders['localhost'] = new RemixDProvider(self.appManager)
registry.put({api: self._components.filesProviders['localhost'], name: 'fileproviders/localhost'})
registry.put({api: self._components.filesProviders, name: 'fileproviders'})

@ -3,11 +3,10 @@ var EventManager = require('../../lib/events')
var pathtool = require('path')
module.exports = class RemixDProvider {
constructor (remixd, appManager) {
constructor (appManager) {
this.event = new EventManager()
this._remixd = remixd
this._appManager = appManager
this.remixd = remixapi(remixd, this)
this.remixd = remixapi(appManager, this)
this.type = 'localhost'
this.error = { 'EEXIST': 'File already exists' }
this._isReady = false
@ -16,39 +15,39 @@ module.exports = class RemixDProvider {
this.filesContent = {}
this.files = {}
var remixdEvents = ['connecting', 'connected', 'errored', 'closed']
remixdEvents.forEach((value) => {
remixd.event.register(value, (event) => {
this.event.trigger(value, [event])
})
})
remixd.event.register('notified', (data) => {
if (data.scope === 'sharedfolder') {
if (data.name === 'created') {
this.init(() => {
this.event.trigger('fileAdded', [this.type + '/' + data.value.path, data.value.isReadOnly, data.value.isFolder])
})
} else if (data.name === 'removed') {
this.init(() => {
this.event.trigger('fileRemoved', [this.type + '/' + data.value.path])
})
} else if (data.name === 'changed') {
this._appManager.call('remixd', 'get', {path: data.value}, (error, content) => {
if (error) {
console.log(error)
} else {
var path = this.type + '/' + data.value
this.filesContent[path] = content
this.event.trigger('fileExternallyChanged', [path, content])
}
})
} else if (data.name === 'rootFolderChanged') {
// new path has been set, we should reset
this.event.trigger('folderAdded', [this.type + '/'])
}
}
})
// var remixdEvents = ['connecting', 'connected', 'errored', 'closed']
// remixdEvents.forEach((value) => {
// remixd.event.register(value, (event) => {
// this.event.trigger(value, [event])
// })
// })
// remixd.event.register('notified', (data) => {
// if (data.scope === 'sharedfolder') {
// if (data.name === 'created') {
// this.init(() => {
// this.event.trigger('fileAdded', [this.type + '/' + data.value.path, data.value.isReadOnly, data.value.isFolder])
// })
// } else if (data.name === 'removed') {
// this.init(() => {
// this.event.trigger('fileRemoved', [this.type + '/' + data.value.path])
// })
// } else if (data.name === 'changed') {
// this._appManager.call('remixd', 'get', {path: data.value}, (error, content) => {
// if (error) {
// console.log(error)
// } else {
// var path = this.type + '/' + data.value
// this.filesContent[path] = content
// this.event.trigger('fileExternallyChanged', [path, content])
// }
// })
// } else if (data.name === 'rootFolderChanged') {
// // new path has been set, we should reset
// this.event.trigger('folderAdded', [this.type + '/'])
// }
// }
// })
}
isConnected () {
@ -61,10 +60,15 @@ module.exports = class RemixDProvider {
}
async init (cb) {
await this._appManager.call('remixd', 'sharedFolder', { currentSharedFolder })
this._appManager.call('remixd', 'folderIsReadOnly', {}).then((result) => {
this._isReady = true
this._readOnlyMode = await this._appManager.call('remixd', 'folderIsReadOnly', {})
this._readOnlyMode = result
console.log('this._readOnlyMode: ', this._readOnlyMode)
cb()
cb && cb()
}).catch((error) => {
cb && cb(error)
})
}
// @TODO: refactor all `this._remixd.call(....)` uses into `this.remixd[api](...)`
@ -79,10 +83,12 @@ module.exports = class RemixDProvider {
async exists (path, cb) {
const unprefixedpath = this.removePrefix(path)
const callId = await this._appManager.call('remixd', 'exists', {path: unprefixedpath})
const result = await this._appManager.receiveResponse(callId)
return cb(null, result)
this._appManager.call('remixd', 'exists', {path: unprefixedpath}).then((result) => {
return cb && cb(null, result)
}).catch((error) => {
return cb && cb(error)
})
}
getNormalizedName (path) {
@ -190,33 +196,38 @@ module.exports = class RemixDProvider {
}
}
function remixapi (remixd, self) {
function remixapi (appManager, self) {
const read = (path, callback) => {
path = '' + (path || '')
path = pathtool.join('./', path)
remixd.call('remixd', 'get', { path }, (error, content) => callback(error, content))
appManager.call('remixd', 'get', { path }, (error, content) => callback(error, content))
}
const write = (path, content, callback) => {
path = '' + (path || '')
path = pathtool.join('./', path)
remixd.call('remixd', 'set', { path, content }, (error, result) => callback(error, result))
appManager.call('remixd', 'set', { path, content }, (error, result) => callback(error, result))
}
const rename = (path, newpath, callback) => {
path = '' + (path || '')
path = pathtool.join('./', path)
remixd.call('remixd', 'rename', { oldPath: path, newPath: newpath }, (error, result) => callback(error, result))
appManager.call('remixd', 'rename', { oldPath: path, newPath: newpath }, (error, result) => callback(error, result))
}
const remove = (path, callback) => {
path = '' + (path || '')
path = pathtool.join('./', path)
remixd.call('remixd', 'remove', { path }, (error, result) => callback(error, result))
appManager.call('remixd', 'remove', { path }, (error, result) => callback(error, result))
}
const dir = (path, callback) => {
path = '' + (path || '')
path = pathtool.join('./', path)
remixd.call('remixd', 'resolveDirectory', { path }, (error, filesList) => callback(error, filesList))
appManager.call('remixd', 'resolveDirectory', { path }).then((filesList) => {
console.log('filesList: ', filesList)
callback(null, filesList)
}).catch((error) => {
callback(error)
})
}
const exit = () => { remixd.close() }
const api = { read, write, rename, remove, dir, exit, event: remixd.event }
const api = { read, write, rename, remove, dir, exit, event: self.event }
return api
}

@ -21,7 +21,7 @@ var css = csjs`
const profile = {
name: 'remixd',
url: 'ws://127.0.0.1:65520',
methods: ['folderIsReadOnly'],
methods: ['folderIsReadOnly', 'resolveDirectory'],
events: [],
description: 'Using Remixd daemon, allow to access file system',
kind: 'other',
@ -43,8 +43,7 @@ export class RemixdHandle extends WebsocketPlugin {
})
}
async activate () {
await super.activate()
activate () {
this.connectToLocalhost()
}
@ -58,7 +57,7 @@ export class RemixdHandle extends WebsocketPlugin {
*
* @param {String} txHash - hash of the transaction
*/
connectToLocalhost () {
async connectToLocalhost () {
let connection = (error) => {
if (error) {
console.log(error)
@ -68,13 +67,12 @@ export class RemixdHandle extends WebsocketPlugin {
)
this.canceled()
} else {
this.locahostProvider.init()
this.fileSystemExplorer.ensureRoot()
}
}
if (this.locahostProvider.isConnected()) {
this.locahostProvider.close((error) => {
if (error) console.log(error)
})
this.deactivate()
} else if (!isElectron()) {
// warn the user only if he/she is in the browser context
modalDialog(
@ -82,7 +80,12 @@ export class RemixdHandle extends WebsocketPlugin {
remixdDialog(),
{ label: 'Connect',
fn: () => {
this.locahostProvider.init((error) => connection(error))
try {
super.activate()
setTimeout(() => { connection() }, 3000)
} catch(error) {
connection(error)
}
}
},
{ label: 'Cancel',
@ -92,7 +95,12 @@ export class RemixdHandle extends WebsocketPlugin {
}
)
} else {
this.locahostProvider.init((error) => connection(error))
try {
super.activate()
setTimeout(() => { connection() }, 3000)
} catch(error) {
connection(error)
}
}
}
}

Loading…
Cancel
Save