Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Commit

Permalink
refactor(plugins): add destroy method and binary websocket (#1500)
Browse files Browse the repository at this point in the history
  • Loading branch information
alessiodf authored and luciorubeens committed Oct 30, 2019
1 parent ab7ce69 commit 90f09ad
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ describe('PluginWebsocket', () => {
socket.send('ping')
})

it('should change the binary type to arraybuffer', () => {
const socket = walletApi.websocket.connect(host)

socket.binaryType = 'arraybuffer'
expect(socket.binaryType).toBe('arraybuffer')
})

it('should close the websocket', (done) => {
const socket = walletApi.websocket.connect(host)

Expand Down Expand Up @@ -152,6 +159,12 @@ describe('PluginWebsocket', () => {
}).toThrow('URL "ws://my.test.com:8081" not allowed')
})

it('should destroy the socket', () => {
const socket = walletApi.websocket.connect(host)
socket.destroy()
expect(socket.isDestroyed()).toBeTrue()
})

it('should ignore an invalid whitelist', () => {
expect(() => {
const api = {}
Expand Down
37 changes: 29 additions & 8 deletions src/renderer/services/plugin-manager/sandbox/websocket-sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class PluginWebsocket {
connect (url) {
this.validateUrl(url)

const websocket = new WebSocket(url)
let websocket = new WebSocket(url)

const websocketEvents = {
events: [],
Expand All @@ -42,7 +42,7 @@ class PluginWebsocket {
on (action, eventCallback) {
const eventTrigger = event => {
const result = {}
if (event.data && event.data !== Object(event.data)) {
if (event.data) {
result.data = event.data
}

Expand All @@ -63,34 +63,55 @@ class PluginWebsocket {
this.events[action] = eventTrigger
},

get binaryType () {
return websocket && websocket.binaryType
},

set binaryType (type) {
websocket.binaryType = type
},

close () {
websocket.close()
},

destroy () {
if (websocket) {
if (!websocketEvents.isClosing() && !websocketEvents.isClosed()) {
websocket.close()
}
websocketEvents.clear()
}
websocket = null
},

send (data) {
websocket.send(data)
},

isConnecting () {
return websocket.readyState === WebSocket.CONNECTING
return websocket && websocket.readyState === WebSocket.CONNECTING
},

isDestroyed () {
return websocket === null
},

isOpen () {
return websocket.readyState === WebSocket.OPEN
return websocket && websocket.readyState === WebSocket.OPEN
},

isClosing () {
return websocket.readyState === WebSocket.CLOSING
return websocket && websocket.readyState === WebSocket.CLOSING
},

isClosed () {
return websocket.readyState === WebSocket.CLOSED
return websocket && websocket.readyState === WebSocket.CLOSED
}
}

this.router.beforeEach((_, __, next) => {
websocketEvents.clear()
websocket.close()
websocketEvents.destroy()
next()
})

Expand Down

0 comments on commit 90f09ad

Please sign in to comment.