refactor: cache repeated lazy imports

This commit is contained in:
Peter Steinberger
2026-04-18 16:31:48 +01:00
parent d13869aab9
commit cdaa70facb
13 changed files with 161 additions and 50 deletions

View File

@@ -13,6 +13,15 @@ import { shouldLogVerbose } from "openclaw/plugin-sdk/runtime-env";
import { WHATSAPP_LEGACY_OUTBOUND_SEND_DEP_KEYS } from "./outbound-send-deps.js";
import { resolveWhatsAppOutboundTarget } from "./resolve-outbound-target.js";
type WhatsAppSendModule = typeof import("./send.js");
let whatsAppSendModulePromise: Promise<WhatsAppSendModule> | undefined;
function loadWhatsAppSendModule(): Promise<WhatsAppSendModule> {
whatsAppSendModulePromise ??= import("./send.js");
return whatsAppSendModulePromise;
}
function trimLeadingWhitespace(text: string | undefined): string {
return text?.trimStart() ?? "";
}
@@ -54,7 +63,7 @@ export const whatsappOutbound: ChannelOutboundAdapter = {
const send =
resolveOutboundSendDep<typeof import("./send.js").sendMessageWhatsApp>(deps, "whatsapp", {
legacyKeys: WHATSAPP_LEGACY_OUTBOUND_SEND_DEP_KEYS,
}) ?? (await import("./send.js")).sendMessageWhatsApp;
}) ?? (await loadWhatsAppSendModule()).sendMessageWhatsApp;
return await send(to, normalizedText, {
verbose: false,
cfg,
@@ -77,7 +86,7 @@ export const whatsappOutbound: ChannelOutboundAdapter = {
const send =
resolveOutboundSendDep<typeof import("./send.js").sendMessageWhatsApp>(deps, "whatsapp", {
legacyKeys: WHATSAPP_LEGACY_OUTBOUND_SEND_DEP_KEYS,
}) ?? (await import("./send.js")).sendMessageWhatsApp;
}) ?? (await loadWhatsAppSendModule()).sendMessageWhatsApp;
return await send(to, normalizedText, {
verbose: false,
cfg,
@@ -90,7 +99,7 @@ export const whatsappOutbound: ChannelOutboundAdapter = {
},
sendPoll: async ({ cfg, to, poll, accountId }) =>
await (
await import("./send.js")
await loadWhatsAppSendModule()
).sendPollWhatsApp(to, poll, {
verbose: shouldLogVerbose(),
accountId: accountId ?? undefined,