diff --git a/src/engine/runtime.js b/src/engine/runtime.js index 35156b93c..50e4c43e4 100644 --- a/src/engine/runtime.js +++ b/src/engine/runtime.js @@ -457,11 +457,19 @@ class Runtime extends EventEmitter { } /** - * Event name for reporting that a peripheral has encountered an error. + * Event name for reporting that a peripheral has encountered a request error. * @const {string} */ - static get PERIPHERAL_ERROR () { - return 'PERIPHERAL_ERROR'; + static get PERIPHERAL_REQUEST_ERROR () { + return 'PERIPHERAL_REQUEST_ERROR'; + } + + /** + * Event name for reporting that a peripheral has encountered a disconnect error. + * @const {string} + */ + static get PERIPHERAL_DISCONNECT_ERROR () { + return 'PERIPHERAL_DISCONNECT_ERROR'; } /** diff --git a/src/io/ble.js b/src/io/ble.js index cf6530f8d..e407a9856 100644 --- a/src/io/ble.js +++ b/src/io/ble.js @@ -18,8 +18,8 @@ class BLE extends JSONRPCWebSocket { this._ws = ws; this._ws.onopen = this.requestPeripheral.bind(this); // only call request peripheral after socket opens - this._ws.onerror = this._sendError.bind(this, 'ws onerror'); - this._ws.onclose = this._sendError.bind(this, 'ws onclose'); + this._ws.onerror = this._sendRequestError.bind(this, 'ws onerror'); + this._ws.onclose = this._sendDisconnectError.bind(this, 'ws onclose'); this._availablePeripherals = {}; this._connectCallback = connectCallback; @@ -41,7 +41,7 @@ class BLE extends JSONRPCWebSocket { this._discoverTimeoutID = window.setTimeout(this._sendDiscoverTimeout.bind(this), 15000); this.sendRemoteRequest('discover', this._peripheralOptions) .catch(e => { - this._sendError(e); + this._sendRequestError(e); }); // never reached? } // TODO: else? @@ -60,7 +60,7 @@ class BLE extends JSONRPCWebSocket { this._connectCallback(); }) .catch(e => { - this._sendError(e); + this._sendRequestError(e); }); } @@ -69,7 +69,6 @@ class BLE extends JSONRPCWebSocket { */ disconnect () { this._ws.close(); - this._connected = false; } /** @@ -94,7 +93,7 @@ class BLE extends JSONRPCWebSocket { this._characteristicDidChangeCallback = onCharacteristicChanged; return this.sendRemoteRequest('startNotifications', params) .catch(e => { - this._sendError(e); + this._sendDisconnectError(e); }); } @@ -117,7 +116,7 @@ class BLE extends JSONRPCWebSocket { this._characteristicDidChangeCallback = onCharacteristicChanged; return this.sendRemoteRequest('read', params) .catch(e => { - this._sendError(e); + this._sendDisconnectError(e); }); } @@ -140,7 +139,7 @@ class BLE extends JSONRPCWebSocket { } return this.sendRemoteRequest('write', params) .catch(e => { - this._sendError(e); + this._sendDisconnectError(e); }); } @@ -171,10 +170,23 @@ class BLE extends JSONRPCWebSocket { } } - _sendError (/* e */) { - if (this._connected) this.disconnect(); + _sendRequestError (/* e */) { // log.error(`BLE error: ${JSON.stringify(e)}`); - this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR, { + + this._runtime.emit(this._runtime.constructor.PERIPHERAL_REQUEST_ERROR, { + message: `Scratch lost connection to`, + extensionId: this._extensionId + }); + } + + _sendDisconnectError (/* e */) { + // log.error(`BLE error: ${JSON.stringify(e)}`); + + if (!this._connected) return; + + this._connected = false; + + this._runtime.emit(this._runtime.constructor.PERIPHERAL_DISCONNECT_ERROR, { message: `Scratch lost connection to`, extensionId: this._extensionId }); diff --git a/src/io/bt.js b/src/io/bt.js index bf492f84a..a2b27f4f9 100644 --- a/src/io/bt.js +++ b/src/io/bt.js @@ -19,8 +19,8 @@ class BT extends JSONRPCWebSocket { this._ws = ws; this._ws.onopen = this.requestPeripheral.bind(this); // only call request peripheral after socket opens - this._ws.onerror = this._sendError.bind(this, 'ws onerror'); - this._ws.onclose = this._sendError.bind(this, 'ws onclose'); + this._ws.onerror = this._sendDisconnectError.bind(this, 'ws onerror'); + this._ws.onclose = this._sendDisconnectError.bind(this, 'ws onclose'); this._availablePeripherals = {}; this._connectCallback = connectCallback; @@ -42,7 +42,7 @@ class BT extends JSONRPCWebSocket { this._availablePeripherals = {}; this._discoverTimeoutID = window.setTimeout(this._sendDiscoverTimeout.bind(this), 15000); this.sendRemoteRequest('discover', this._peripheralOptions) - .catch(e => this._sendError(e)); // never reached? + .catch(e => this._sendRequestError(e)); // never reached? } // TODO: else? } @@ -60,7 +60,7 @@ class BT extends JSONRPCWebSocket { this._connectCallback(); }) .catch(e => { - this._sendError(e); + this._sendRequestError(e); }); } @@ -69,7 +69,6 @@ class BT extends JSONRPCWebSocket { */ disconnect () { this._ws.close(); - this._connected = false; } /** @@ -82,7 +81,7 @@ class BT extends JSONRPCWebSocket { sendMessage (options) { return this.sendRemoteRequest('send', options) .catch(e => { - this._sendError(e); + this._sendDisconnectError(e); }); } @@ -114,10 +113,23 @@ class BT extends JSONRPCWebSocket { } } - _sendError (/* e */) { - if (this._connected) this.disconnect(); + _sendRequestError (/* e */) { // log.error(`BT error: ${JSON.stringify(e)}`); - this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR, { + + this._runtime.emit(this._runtime.constructor.PERIPHERAL_REQUEST_ERROR, { + message: `Scratch lost connection to`, + extensionId: this._extensionId + }); + } + + _sendDisconnectError (/* e */) { + // log.error(`BT error: ${JSON.stringify(e)}`); + + if (!this._connected) return; + + this._connected = false; + + this._runtime.emit(this._runtime.constructor.PERIPHERAL_DISCONNECT_ERROR, { message: `Scratch lost connection to`, extensionId: this._extensionId }); diff --git a/src/virtual-machine.js b/src/virtual-machine.js index 5be8a553c..43a5e34a9 100644 --- a/src/virtual-machine.js +++ b/src/virtual-machine.js @@ -111,8 +111,11 @@ class VirtualMachine extends EventEmitter { this.runtime.on(Runtime.PERIPHERAL_CONNECTED, () => this.emit(Runtime.PERIPHERAL_CONNECTED) ); - this.runtime.on(Runtime.PERIPHERAL_ERROR, data => - this.emit(Runtime.PERIPHERAL_ERROR, data) + this.runtime.on(Runtime.PERIPHERAL_REQUEST_ERROR, () => + this.emit(Runtime.PERIPHERAL_REQUEST_ERROR) + ); + this.runtime.on(Runtime.PERIPHERAL_DISCONNECT_ERROR, data => + this.emit(Runtime.PERIPHERAL_DISCONNECT_ERROR, data) ); this.runtime.on(Runtime.PERIPHERAL_SCAN_TIMEOUT, () => this.emit(Runtime.PERIPHERAL_SCAN_TIMEOUT)