Revert "Resolved merge conflict"

This reverts commit 2ddb2d7460fd16f57c3951fa4d7f37140067bf41.
pull/11/head
ioedeveloper 5 years ago
parent 6b3651b43d
commit a496a50c02
  1. 262
      apps/remix-ide/src/lib/remixd.js

@ -1,145 +1,145 @@
'use strict' // 'use strict'
var EventManager = require('../lib/events') // var EventManager = require('../lib/events')
var modalDialog = require('../app/ui/modaldialog') // var modalDialog = require('../app/ui/modaldialog')
var yo = require('yo-yo') // var yo = require('yo-yo')
class Remixd { // class Remixd {
constructor (port) { // constructor (port) {
this.event = new EventManager() // this.event = new EventManager()
this.port = port // this.port = port
this.callbacks = {} // this.callbacks = {}
this.callid = 0 // this.callid = 0
this.socket = null // this.socket = null
this.connected = false // this.connected = false
this.receiveResponse() // this.receiveResponse()
} // }
online () { // online () {
return this.socket !== null // return this.socket !== null
} // }
close () { // close () {
if (this.socket) { // if (this.socket) {
this.socket.close() // this.socket.close()
this.socket = null // this.socket = null
} // }
} // }
start (cb) { // start (cb) {
if (this.socket) { // if (this.socket) {
try { // try {
this.socket.close() // this.socket.close()
} catch (e) {} // } catch (e) {}
} // }
this.event.trigger('connecting', []) // this.event.trigger('connecting', [])
this.socket = new WebSocket('ws://localhost:' + this.port, 'echo-protocol') // eslint-disable-line // this.socket = new WebSocket('ws://localhost:' + this.port, 'echo-protocol') // eslint-disable-line
this.socket.addEventListener('open', (event) => { // this.socket.addEventListener('open', (event) => {
this.connected = true // this.connected = true
this.event.trigger('connected', [event]) // this.event.trigger('connected', [event])
cb() // cb()
}) // })
this.socket.addEventListener('message', (event) => { // this.socket.addEventListener('message', (event) => {
var data = JSON.parse(event.data) // var data = JSON.parse(event.data)
if (data.type === 'reply') { // if (data.type === 'reply') {
if (this.callbacks[data.id]) { // if (this.callbacks[data.id]) {
this.callbacks[data.id](data.error, data.result) // this.callbacks[data.id](data.error, data.result)
delete this.callbacks[data.id] // delete this.callbacks[data.id]
} // }
this.event.trigger('replied', [data]) // this.event.trigger('replied', [data])
} else if (data.type === 'notification') { // } else if (data.type === 'notification') {
this.event.trigger('notified', [data]) // this.event.trigger('notified', [data])
} else if (data.type === 'system') { // } else if (data.type === 'system') {
if (data.error) { // if (data.error) {
this.event.trigger('system', [{ // this.event.trigger('system', [{
error: data.error // error: data.error
}]) // }])
} // }
} // }
}) // })
this.socket.addEventListener('error', (event) => { // this.socket.addEventListener('error', (event) => {
this.errored(event) // this.errored(event)
cb(event) // cb(event)
}) // })
this.socket.addEventListener('close', (event) => { // this.socket.addEventListener('close', (event) => {
if (event.wasClean) { // if (event.wasClean) {
this.connected = false // this.connected = false
this.event.trigger('closed', [event]) // this.event.trigger('closed', [event])
} else { // } else {
this.errored(event) // this.errored(event)
} // }
this.socket = null // this.socket = null
}) // })
} // }
async receiveResponse (requestId) { // async receiveResponse (requestId) {
return new Promise((resolve, reject) => { // return new Promise((resolve, reject) => {
this.event.register('replied', (data) => { // this.event.register('replied', (data) => {
if (data.id === requestId) { // if (data.id === requestId) {
if (data.error) reject(data.error) // if (data.error) reject(data.error)
else resolve(data.result) // 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>`
} // }
if (this.connected) { // if (this.connected) {
modalDialog('Lost connection to Remixd!', remixdDialog(), {}, {label: ''}) // modalDialog('Lost connection to Remixd!', remixdDialog(), {}, {label: ''})
} // }
this.connected = false // this.connected = false
this.socket = null // this.socket = null
this.event.trigger('errored', [event]) // this.event.trigger('errored', [event])
} // }
call (service, fn, args, callback) { // call (service, fn, args, callback) {
return new Promise((resolve, reject) => { // return new Promise((resolve, reject) => {
this.ensureSocket((error) => { // this.ensureSocket((error) => {
if (error) { // if (error) {
callback && typeof callback === 'function' && callback(error) // callback && typeof callback === 'function' && callback(error)
reject(error) // reject(error)
return // 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) // resolve(data.id)
} else { // } else {
callback && typeof callback === 'function' && 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) // reject('Socket not ready. state:' + this.socket.readyState)
} // }
}) // })
}) // })
} // }
ensureSocket (cb) { // ensureSocket (cb) {
if (this.socket) return cb(null, this.socket) // if (this.socket) return cb(null, this.socket)
this.start((error) => { // this.start((error) => {
if (error) { // if (error) {
cb(error) // cb(error)
} else { // } else {
cb(null, this.socket) // cb(null, this.socket)
} // }
}) // })
} // }
format (service, fn, args) { // format (service, fn, args) {
var data = { // var data = {
id: this.callid, // id: this.callid,
service: service, // service: service,
fn: fn, // fn: fn,
args: args // args: args
} // }
this.callid++ // this.callid++
return data // return data
} // }
} // }
module.exports = Remixd // module.exports = Remixd

Loading…
Cancel
Save