@hippy/debug-server-next
Version:
Debug server for hippy.
78 lines (77 loc) • 3.18 kB
JavaScript
;
/*
* Tencent is pleased to support the open source community by making
* Hippy available.
*
* Copyright (C) 2017-2019 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.onReactClientConnection = void 0;
const enum_1 = require("@debug-server-next/@types/enum");
const log_1 = require("@debug-server-next/utils/log");
const pub_sub_channel_1 = require("@debug-server-next/utils/pub-sub-channel");
const db_1 = require("@debug-server-next/db");
const report_1 = require("@debug-server-next/utils/report");
const log = new log_1.Logger('react-devtools', enum_1.WinstonColor.Yellow);
/**
* Pub/Sub react devtools msg
*/
const onReactClientConnection = async (ws, wsUrlParams) => {
const { contextName, clientRole, clientId } = wsUrlParams;
log.info('%s connected', clientRole);
const { Subscriber, Publisher } = (0, db_1.getDBOperator)();
report_1.report.event({
name: enum_1.ReportEvent.ReactDevtools,
ext1: clientId,
ext2: contextName,
});
const downChannel = (0, pub_sub_channel_1.createReactDevtoolsChannel)(clientId, 'down');
const upChannel = (0, pub_sub_channel_1.createReactDevtoolsChannel)(clientId, 'up');
let publisher;
let subscriber;
if (clientRole === enum_1.ClientRole.ReactJSRuntime) {
publisher = new Publisher(downChannel);
subscriber = new Subscriber(upChannel);
}
else {
publisher = new Publisher(upChannel);
subscriber = new Subscriber(downChannel);
}
const handler = (msg) => {
ws.send(msg.toString());
};
subscriber.subscribe(handler);
/**
* dispatch event when connection
* 1. backend connect: reload frontend UI, ignore previous devtools instance
* 2. frontend connect: activate backend, start dispatch debug protocol
*/
publisher.publish({
event: clientRole === enum_1.ClientRole.ReactJSRuntime ? "react-devtools-connect-backend" /* ReactDevtoolsEvent.BackendConnect */ : "react-devtools-connect-frontend" /* ReactDevtoolsEvent.FrontendConnect */,
});
ws.on('message', async (msg) => {
const msgStr = msg.toString();
if (msgStr)
publisher.publish(msgStr);
});
ws.on('close', (code, reason) => {
log.info('%s closed. code: %s, reason: %s', clientRole, code, reason);
subscriber.disconnect();
publisher.disconnect();
});
ws.on('error', (e) => log.error('JSRuntime ws error: %s', e.stack || e));
};
exports.onReactClientConnection = onReactClientConnection;