From 7e49bfa984dc995285d1a677afc6eabe92d98b14 Mon Sep 17 00:00:00 2001 From: "Wesley F. Young" Date: Fri, 9 Aug 2024 11:03:25 +0800 Subject: [PATCH] refactor: make selfInfo a 'runtime info' --- src/core/core.ts | 43 +++++++++++++++++-------------------- src/core/wrapper/context.ts | 5 ++--- src/liteloader/napcat.ts | 4 +--- src/shell/napcat.ts | 3 +-- 4 files changed, 24 insertions(+), 31 deletions(-) diff --git a/src/core/core.ts b/src/core/core.ts index 40cdd204..c54ab644 100644 --- a/src/core/core.ts +++ b/src/core/core.ts @@ -1,4 +1,4 @@ -import { WrapperNodeApi } from "./wrapper/wrapper"; +import { WrapperNodeApi } from "@/core/wrapper"; import path from "node:path"; import fs from "node:fs"; import { InstanceContext } from "./wrapper"; @@ -6,7 +6,7 @@ import { NTEventChannel } from "@/common/framework/event"; import { proxiedListenerOf } from "@/common/utils/proxy-handler"; import { MsgListener } from "./listeners"; import { sleep } from "@/common/utils/helper"; -import { CoreCache, LineDevice } from "./entities"; +import { SelfInfo, LineDevice } from "./entities"; export enum NapCatCoreWorkingEnv { Unknown = 0, @@ -27,20 +27,21 @@ export function loadQQWrapper(QQVersion: string): WrapperNodeApi { export class NapCatCore { readonly context: InstanceContext; readonly eventChannel: NTEventChannel; - readonly cache: CoreCache; - constructor(context: InstanceContext) { + + // runtime info, not readonly + selfInfo: SelfInfo; + deviceList: { + app_id: string, + device_name: string, + device_kind: string + }[] = []; + + // 通过构造器递过去的 runtime info 应该尽量少 + constructor(context: InstanceContext, selfInfo: SelfInfo) { + this.selfInfo = selfInfo; this.context = context; this.eventChannel = new NTEventChannel(context.wrapper, context.session); - this.cache = { - selfInfo: { - uid: "", - uin: "", - nick: "" - }, - DeviceList: [] - } this.initNapCatCoreListeners().then().catch(console.error); - } // Renamed from 'InitDataListener' @@ -49,16 +50,12 @@ export class NapCatCore { msgListener.onRecvMsg = (msg) => { console.log("RecvMsg", msg); } - msgListener.onLineDev = (Devices: LineDevice[]) => { - this.cache.DeviceList.splice(0, this.cache.DeviceList.length); - Devices.map((Device: LineDevice) => { - let DeviceData = { - app_id: Device.devUid, - device_name: Device.clientType.toString(), - device_kind: Device.clientType.toString(), - }; - this.cache.DeviceList.push(DeviceData); - }); + msgListener.onLineDev = (devices: LineDevice[]) => { + this.deviceList = devices.map((device: LineDevice) => ({ + app_id: device.devUid, + device_name: device.clientType.toString(), + device_kind: device.clientType.toString(), + })); }; //await sleep(2500); this.context.session.getMsgService().addKernelMsgListener( diff --git a/src/core/wrapper/context.ts b/src/core/wrapper/context.ts index 9c6812f0..2074245c 100644 --- a/src/core/wrapper/context.ts +++ b/src/core/wrapper/context.ts @@ -1,9 +1,9 @@ import { LogWrapper } from "@/common/utils/log"; import { QQBasicInfoWrapper } from "@/common/utils/QQBasicInfo"; -import { NapCatCoreWorkingEnv } from "../core"; +import { NapCatCoreWorkingEnv } from "@/core"; import { SelfInfo } from "../entities"; import { NodeIKernelLoginService } from "../services"; -import { WrapperNodeApi, NodeIQQNTWrapperSession } from "./wrapper"; +import { WrapperNodeApi, NodeIQQNTWrapperSession } from "@/core"; export interface InstanceContext { readonly workingEnv: NapCatCoreWorkingEnv; @@ -11,6 +11,5 @@ export interface InstanceContext { readonly session: NodeIQQNTWrapperSession; readonly logger: LogWrapper; readonly loginService: NodeIKernelLoginService; - readonly selfInfo: SelfInfo; readonly basicInfoWrapper: QQBasicInfoWrapper; } diff --git a/src/liteloader/napcat.ts b/src/liteloader/napcat.ts index 92ebd840..98e86593 100644 --- a/src/liteloader/napcat.ts +++ b/src/liteloader/napcat.ts @@ -1,4 +1,3 @@ -import { NTEventChannel } from "@/common/framework/event"; import { NapCatPathWrapper } from "@/common/framework/napcat"; import { LogWrapper } from "@/common/utils/log"; import { proxiedListenerOf } from "@/common/utils/proxy-handler"; @@ -61,9 +60,8 @@ export class NapCatLiteLoader { session, logger, loginService, - selfInfo, basicInfoWrapper }; - this.core = new NapCatCore(this.context); + this.core = new NapCatCore(this.context, selfInfo); } } diff --git a/src/shell/napcat.ts b/src/shell/napcat.ts index 906d7b76..4acb44a7 100644 --- a/src/shell/napcat.ts +++ b/src/shell/napcat.ts @@ -149,10 +149,9 @@ export class NapCatShell { session, logger, loginService, - selfInfo, basicInfoWrapper }; - this.core = new NapCatCore(this.context); + this.core = new NapCatCore(this.context, selfInfo); new NapCatOneBot11Adapter(this.core, this.context); }