From 626b551ff2e09f2770bac71a5d14d139f1b09226 Mon Sep 17 00:00:00 2001 From: leeyeh Date: Thu, 1 Oct 2015 17:38:40 -0700 Subject: [PATCH] Implement EventTarget interface for WebSocket. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: close #2583Closes https://github.com/facebook/react-native/pull/2599 Reviewed By: @​svcscm Differential Revision: D2498641 Pulled By: @vjeux --- Libraries/WebSocket/WebSocket.ios.js | 30 ++++++++++++++++++++++++---- Libraries/WebSocket/WebSocketBase.js | 5 ++++- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/Libraries/WebSocket/WebSocket.ios.js b/Libraries/WebSocket/WebSocket.ios.js index e48d7882bf9e07..c2318b4976ec60 100644 --- a/Libraries/WebSocket/WebSocket.ios.js +++ b/Libraries/WebSocket/WebSocket.ios.js @@ -16,6 +16,19 @@ var RCTWebSocketManager = require('NativeModules').WebSocketManager; var WebSocketBase = require('WebSocketBase'); +class Event { + constructor(type) { + this.type = type.toString(); + } +} + +class MessageEvent extends Event { + constructor(type, eventInitDict) { + super(type); + Object.assign(this, eventInitDict); + } +} + var WebSocketId = 0; class WebSocket extends WebSocketBase { @@ -58,9 +71,11 @@ class WebSocket extends WebSocketBase { if (ev.id !== id) { return; } - this.onmessage && this.onmessage({ + var event = new MessageEvent('message', { data: ev.data }); + this.onmessage && this.onmessage(event); + this.dispatchEvent(event); }.bind(this) ), RCTDeviceEventEmitter.addListener( @@ -70,7 +85,9 @@ class WebSocket extends WebSocketBase { return; } this.readyState = this.OPEN; - this.onopen && this.onopen(); + var event = new Event('open'); + this.onopen && this.onopen(event); + this.dispatchEvent(event); }.bind(this) ), RCTDeviceEventEmitter.addListener( @@ -80,7 +97,9 @@ class WebSocket extends WebSocketBase { return; } this.readyState = this.CLOSED; - this.onclose && this.onclose(ev); + var event = new Event('close'); + this.onclose && this.onclose(event); + this.dispatchEvent(event); this._unregisterEvents(); RCTWebSocketManager.close(id); }.bind(this) @@ -91,7 +110,10 @@ class WebSocket extends WebSocketBase { if (ev.id !== id) { return; } - this.onerror && this.onerror(new Error(ev.message)); + var event = new Event('error'); + event.message = ev.message; + this.onerror && this.onerror(event); + this.dispatchEvent(event); this._unregisterEvents(); RCTWebSocketManager.close(id); }.bind(this) diff --git a/Libraries/WebSocket/WebSocketBase.js b/Libraries/WebSocket/WebSocketBase.js index 73cc62cc91d0fb..a4cca418401e94 100644 --- a/Libraries/WebSocket/WebSocketBase.js +++ b/Libraries/WebSocket/WebSocketBase.js @@ -11,10 +11,12 @@ */ 'use strict'; +var EventTarget = require('event-target-shim'); + /** * Shared base for platform-specific WebSocket implementations. */ -class WebSocketBase { +class WebSocketBase extends EventTarget { CONNECTING: number; OPEN: number; CLOSING: number; @@ -33,6 +35,7 @@ class WebSocketBase { url: ?string; constructor(url: string, protocols: ?any) { + super(); this.CONNECTING = 0; this.OPEN = 1; this.CLOSING = 2;