From 063837350b31db98c1d12bf85b5f73974648af0f Mon Sep 17 00:00:00 2001 From: xion Date: Sat, 22 Mar 2025 14:36:33 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E4=BB=A5=E5=89=8D?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitmodules | 2 +- .submodules | 0 package.json | 2 + pnpm-lock.yaml | 24 +++++----- src/modules/query.ts | 67 +++++++++------------------ src/pages/container/preview/index.tsx | 17 +++++-- submodules/query-login | 2 +- 7 files changed, 50 insertions(+), 64 deletions(-) delete mode 100644 .submodules diff --git a/.gitmodules b/.gitmodules index a8e9f11..76846be 100644 --- a/.gitmodules +++ b/.gitmodules @@ -3,4 +3,4 @@ url = git@git.xiongxiao.me:kevisual/kevsiual-query-login.git [submodule "submodules/query-config"] path = submodules/query-config - url = git@git.xiongxiao.me:kevisual/kevsiual-query-config.git + url = git@git.xiongxiao.me:kevisual/kevsiual-query-config.git \ No newline at end of file diff --git a/.submodules b/.submodules deleted file mode 100644 index e69de29..0000000 diff --git a/package.json b/package.json index 6622ffa..04cb8cd 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,8 @@ "prepub": "envision switch root", "pub": "envision deploy ./dist -k center -v 0.0.9 -u -o root", "turbo:dev": "turbo dev:lib", + "dev:lib": "turbo dev:lib", + "dev:query": "turbo dev:lib --filter=@kevisual/query", "turbo:build": "turbo build --filter=submodules/**" }, "dependencies": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 779fa69..834598f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -452,8 +452,11 @@ importers: specifier: ^0.0.1 version: 0.0.1(rollup@4.36.0)(tslib@2.8.1)(typescript@5.8.2) '@kevisual/query': - specifier: ^0.0.12 - version: 0.0.12(ws@8.18.1) + specifier: ^0.0.14 + version: 0.0.14(ws@8.18.1) + dotenv: + specifier: ^16.4.7 + version: 16.4.7 devDependencies: '@types/node': specifier: ^22.13.11 @@ -969,9 +972,6 @@ packages: crypto-js: ^4.2.0 eventemitter3: ^5.0.1 - '@kevisual/query@0.0.12': - resolution: {integrity: sha512-/1lZJhNCdcNG4a2Nncl55XC/nha5+GZiJPQdP77lufv2dnZfcssA7NHSZ2Ubh0Pm0HWV22IiGfQMiaYl2pkK1Q==} - '@kevisual/query@0.0.13': resolution: {integrity: sha512-gSEIDiCvwSaLLAFZv4vam4wSrMsaCuQ3VGjE3kwRwZ8urlVH1TOA+NUO908A22p9m1Iij7Y1Q/JlfSJi2QzuKQ==} @@ -1937,6 +1937,10 @@ packages: dom-helpers@5.2.1: resolution: {integrity: sha512-nRCa7CK3VTrM2NmGkIy4cbK7IZlgBE/PYMn55rrXefr5xXDP0LdtfPnblFDoVdcAfslJ7or6iqAUnx0CCGIWQA==} + dotenv@16.4.7: + resolution: {integrity: sha512-47qPchRCykZC03FhkYAhrvwU4xDBFIj1QPqaarj6mdM/hgUzfPHcpkHJOn3mJAufFeeAxAzeGsr5X0M4k6fLZQ==} + engines: {node: '>=12'} + dunder-proto@1.0.1: resolution: {integrity: sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A==} engines: {node: '>= 0.4'} @@ -4230,14 +4234,6 @@ snapshots: - rollup - typescript - '@kevisual/query@0.0.12(ws@8.18.1)': - dependencies: - openai: 4.89.0(ws@8.18.1) - transitivePeerDependencies: - - encoding - - ws - - zod - '@kevisual/query@0.0.13(ws@8.18.1)': dependencies: openai: 4.89.0(ws@8.18.1) @@ -5261,6 +5257,8 @@ snapshots: '@babel/runtime': 7.26.10 csstype: 3.1.3 + dotenv@16.4.7: {} + dunder-proto@1.0.1: dependencies: call-bind-apply-helpers: 1.0.2 diff --git a/src/modules/query.ts b/src/modules/query.ts index fb74bd8..f7c295b 100644 --- a/src/modules/query.ts +++ b/src/modules/query.ts @@ -1,14 +1,14 @@ import { QueryClient } from '@kevisual/query'; -import { create } from 'zustand'; -import { QueryLogin } from '@kevisual/query-login'; +import { QueryLoginBrowser } from '@kevisual/query-login'; import { toastLogin } from '@kevisual/resources/pages/message/ToastLogin.tsx'; -export const query = new QueryClient(); - +export const query = new QueryClient({ + io: true, +}); /** * 登录查询 */ -export const queryLogin = new QueryLogin({ - query, +export const queryLogin = new QueryLoginBrowser({ + query: query as any, }); // @ts-ignore if (window.context) { @@ -20,51 +20,28 @@ if (window.context) { queryLogin, }; } -// 在请求之前,如果stop了 -query.beforeRequest = async (config) => { - if (config.headers) { - const token = localStorage.getItem('token'); - if (token) { - config.headers['Authorization'] = 'Bearer ' + token; - } - } - return config; -}; query.afterResponse = async (res, ctx) => { - if (res?.code === 401) { - if (query?.stop) { - return { code: 500, success: false, message: '登录已过期' }; - } - query.stop = true; - const queryLoginRes = await queryLogin.afterCheck401ToRefreshToken(res, ctx); - query.stop = false; - if (queryLoginRes.code === 401) { + const newRes = await queryLogin.run401Action(res, ctx, { + afterAlso401: () => { toastLogin(); - } - return queryLoginRes; - } + }, + }); - return res as any; + return newRes as any; }; export const request = query.post; -export const ws = query.qws?.ws || new WebSocket(query.qws?.url); -type Store = { - connected: boolean; - setConnected: (connected: boolean) => void; -}; -export const useStore = create((set) => ({ - connected: false, - setConnected: (connected) => set({ connected }), -})); +export const ws = query.qws?.ws; // 当连接成功时 -ws.onopen = () => { - console.log('Connected to WebSocket server'); - useStore.getState().setConnected(true); -}; +// query.qws.ws.onopen = () => { +// console.log('Connected to WebSocket server'); +// }; -// 处理 WebSocket 关闭 -ws.onclose = () => { - console.log('Disconnected from WebSocket server'); -}; +// // 处理 WebSocket 关闭 +// query.qws.ws.onclose = () => { +// console.log('Disconnected from WebSocket server'); +// }; +query.qws.listenConnect(() => { + console.log('Connected to WebSocket server'); +}); diff --git a/src/pages/container/preview/index.tsx b/src/pages/container/preview/index.tsx index 525eb99..57c3420 100644 --- a/src/pages/container/preview/index.tsx +++ b/src/pages/container/preview/index.tsx @@ -1,16 +1,17 @@ -import { Container, RenderData, ContainerOne } from '@kevisual/container'; +import { Container } from '@kevisual/container'; import { useEffect, useRef, useState } from 'react'; -import { replace, useParams } from 'react-router'; -import { query, ws, useStore } from '@/modules'; +import { useParams } from 'react-router'; +import { query } from '@/modules'; import { message } from '@/modules/message'; export const useListener = (id?: string, opts?: any) => { const { refresh } = opts || {}; - const connected = useStore((state) => state.connected); + const [connected, setConnected] = useState(false); // 监听服务器的消息 useEffect(() => { if (!id) return; if (!connected) return; + const ws = query.qws.ws; ws.send( JSON.stringify({ type: 'subscribe', @@ -31,6 +32,14 @@ export const useListener = (id?: string, opts?: any) => { ws.removeEventListener('message', listener); }; }, [id, connected]); + const init = async () => { + query.qws.listenConnect(() => { + setConnected(true); + }); + }; + useEffect(() => { + init(); + }, []); const listener = (event) => { const parseIfJson = (data: string) => { try { diff --git a/submodules/query-login b/submodules/query-login index a457dba..24f091a 160000 --- a/submodules/query-login +++ b/submodules/query-login @@ -1 +1 @@ -Subproject commit a457dbabe931327b3b644d88e07c41ce82af4e76 +Subproject commit 24f091ac792342ea295496cf8ff49f6c79ab47a6