UNPKG

@tarojs/plugin-react-devtools

Version:

Taro 小程序端支持使用 React DevTools 的插件

81 lines (78 loc) 2.52 kB
import Taro from '@tarojs/taro'; const ALIPAY = 'alipay'; class WebSocket { constructor(url) { this.url = url; this.readyState = this.CONNECTING; Taro.connectSocket({ url }) .then(ws => { this._ws = ws; ws.onClose(res => { this.readyState = this.CLOSED; this.onclose(res); }); ws.onError(res => { this.readyState = this.CLOSED; this.onerror(res); }); ws.onMessage(res => { if (res.data.includes('"event":"highlightNativeElement"')) { // 元素高亮暂时不实现,需要魔改 backend 的代码 return; } this.onmessage(res); }); if (this.readyState !== this.OPEN) { ws.onOpen(res => { this.readyState = this.OPEN; this.onopen(res); }); } else { // 支付宝全局的 onSocketOpen 已触发过了,直接调用 onopen this.onopen({}); } }); // 支付宝只支持一个 socket 连接,且 onSocketOpen 的触发时机比 connectSocket 回调的时机早 if (process.env.TARO_ENV === ALIPAY) { Taro.onSocketOpen(() => { this.readyState = this.OPEN; }); } } send(data) { this._ws.send({ data }); } close(code, reason) { this.readyState = this.CLOSING; this._ws.close({ code: code || 1000, reason: reason || '' }); } get CONNECTING() { return 0; } get OPEN() { return 1; } get CLOSING() { return 2; } get CLOSED() { return 3; } } const { connectToDevTools } = require('./backend'); const ws = new WebSocket(`ws://${__REACT_DEVTOOLS_HOSTNAME__}:${__REACT_DEVTOOLS_PORT__}`); connectToDevTools({ host: __REACT_DEVTOOLS_HOSTNAME__, // string (defaults to "localhost") - Websocket will connect to this host. port: __REACT_DEVTOOLS_PORT__, // number (defaults to 8097) - Websocket will connect to this port. // useHttps: boolean (defaults to false) - Websocket should use a secure protocol (wss). websocket: ws // Custom websocket to use. Overrides host and port settings if provided. }); //# sourceMappingURL=runtime.js.map