UNPKG

@hippy/debug-server-next

Version:
96 lines (95 loc) 4.26 kB
"use strict"; /* * 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.onVueClientConnection = void 0; const enum_tdf_mapping_1 = require("@hippy/devtools-protocol/dist/types/enum-tdf-mapping"); 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 reload_adapter_1 = require("@debug-server-next/utils/reload-adapter"); const debug_targets_1 = require("@debug-server-next/controller/debug-targets"); const log = new log_1.Logger('vue-devtools', enum_1.WinstonColor.Yellow); /** * Pub/Sub vue devtools msg */ const onVueClientConnection = 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.VueDevtools, ext1: clientId, ext2: contextName, }); let internalHandler; let internalSubscriber; if (clientRole === enum_1.ClientRole.JSRuntime) { const internalChannelId = (0, pub_sub_channel_1.createInternalChannel)(clientId, ''); internalSubscriber = new Subscriber(internalChannelId); internalHandler = (msg) => { if (msg === enum_1.InternalChannelEvent.DevtoolsConnected) { // pub enable after devtools connected pubEnableVueDevtools(); } }; internalSubscriber.subscribe(internalHandler); // pub enable immediately, support for reload scene pubEnableVueDevtools(); async function pubEnableVueDevtools() { const debugTarget = await debug_targets_1.DebugTargetManager.findDebugTarget(clientId, undefined, true); (0, reload_adapter_1.publishRes)(clientId, { method: enum_tdf_mapping_1.TdfEvent.TDFRuntimeEnableVueDevtools, params: { contextName: debugTarget === null || debugTarget === void 0 ? void 0 : debugTarget.title, }, }); } } const channel = (0, pub_sub_channel_1.createVueDevtoolsChannel)(clientId); const publisher = new Publisher(channel); const subscriber = new Subscriber(channel); const handler = (msg) => { ws.send(msg.toString()); }; subscriber.subscribe(handler); if (clientRole === enum_1.ClientRole.JSRuntime) publisher.publish(JSON.stringify(["vue-devtools-disconnect-backend" /* VueDevtoolsEvent.BackendDisconnect */])); ws.on('message', async (msg) => { const msgStr = msg.toString(); if (msgStr) publisher.publish(msgStr); }); ws.on('close', async (code, reason) => { log.info('%s closed. code: %s, reason: %s', clientRole, code, reason); await subscriber.disconnect(); if (reason.indexOf('client') !== -1) { await publisher.publish(JSON.stringify(["vue-devtools-disconnect-devtools" /* VueDevtoolsEvent.DevtoolsDisconnect */])); } if (clientRole === enum_1.ClientRole.JSRuntime) { await internalSubscriber.disconnect(); } await publisher.disconnect(); }); ws.on('error', (e) => log.error('JSRuntime ws error: %s', e.stack || e)); }; exports.onVueClientConnection = onVueClientConnection;