























@@ -21,6 +21,8 @@ const runtimeModuleId = new URL("../src/runtime.js", import.meta.url).pathname;
2121type UnknownMock = Mock<(...args: unknown[]) => unknown>;
2222type AsyncUnknownMock = Mock<(...args: unknown[]) => Promise<unknown>>;
2323const loadedMonitorModules = new Set<MonitorModule>();
24+const cachedMonitorModules = new Map<string, Promise<MonitorModule>>();
25+let cachedWebhookModule: Promise<WebhookModule> | undefined;
24262527type ZaloLifecycleMocks = {
2628setWebhookMock: AsyncUnknownMock;
@@ -102,17 +104,17 @@ async function importSecretInputModule(cacheBust: string): Promise<SecretInputMo
102104)) as SecretInputModule;
103105}
104106105-async function importWebhookModule(cacheBust: string): Promise<WebhookModule> {
106-return (await import(`${webhookModuleUrl}?t=${cacheBust}-${Date.now()}`)) as WebhookModule;
107+async function importCachedWebhookModule(): Promise<WebhookModule> {
108+cachedWebhookModule ??= import(webhookModuleUrl) as Promise<WebhookModule>;
109+return await cachedWebhookModule;
107110}
108111109112export async function resetLifecycleTestState() {
110113vi.clearAllMocks();
111-(await importWebhookModule("reset-webhook")).clearZaloWebhookSecurityStateForTest();
114+(await importCachedWebhookModule()).clearZaloWebhookSecurityStateForTest();
112115for (const module of loadedMonitorModules) {
113116module.__testing.clearHostedMediaRouteRefsForTest();
114117}
115-loadedMonitorModules.clear();
116118setActivePluginRegistry(createEmptyPluginRegistry());
117119}
118120@@ -130,12 +132,30 @@ export async function loadLifecycleMonitorModule(): Promise<MonitorModule> {
130132return await importMonitorModule({ cacheBust: "monitor", mocked: true });
131133}
132134135+export async function loadCachedLifecycleMonitorModule(cacheKey: string): Promise<MonitorModule> {
136+const key = cacheKey.trim();
137+if (!key) {
138+throw new Error("cacheKey is required");
139+}
140+const cached =
141+cachedMonitorModules.get(key) ??
142+(async () => {
143+installLifecycleModuleMocks();
144+const module = (await import(`${monitorModuleUrl}?t=${key}`)) as MonitorModule;
145+loadedMonitorModules.add(module);
146+return module;
147+})();
148+cachedMonitorModules.set(key, cached);
149+return await cached;
150+}
151+133152export async function startWebhookLifecycleMonitor(params: {
134153account: ResolvedZaloAccount;
135154config: OpenClawConfig;
136155token?: string;
137156webhookUrl?: string;
138157webhookSecret?: string;
158+cacheKey?: string;
139159}) {
140160const registry = createEmptyPluginRegistry();
141161setActivePluginRegistry(registry);
@@ -149,7 +169,9 @@ export async function startWebhookLifecycleMonitor(params: {
149169const { normalizeSecretInputString } = await importSecretInputModule("secret-input");
150170const webhookSecret =
151171params.webhookSecret ?? normalizeSecretInputString(params.account.config?.webhookSecret);
152-const { monitorZaloProvider } = await loadLifecycleMonitorModule();
172+const { monitorZaloProvider } = params.cacheKey
173+ ? await loadCachedLifecycleMonitorModule(params.cacheKey)
174+ : await loadLifecycleMonitorModule();
153175const run = monitorZaloProvider({
154176token: params.token ?? "zalo-token",
155177account: params.account,
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。