Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/services/miniapp-runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,56 @@ function sendKeyAppContext(iframe: HTMLIFrameElement): void {
iframe.contentWindow?.postMessage(context, '*');
}

type KeyAppContextRequestMessage = {
type: 'keyapp:context-request';
payload?: {
appId?: string;
};
};

function getIframeByMessageSource(source: MessageEvent['source']): HTMLIFrameElement | null {
if (!source) return null;
for (const app of miniappRuntimeStore.state.apps.values()) {
const iframe = app.containerHandle?.getIframe() ?? app.iframeRef;
if (iframe?.contentWindow === source) {
return iframe;
}
}
return null;
}

function handleKeyAppContextRequest(event: MessageEvent): void {
const data = event.data as KeyAppContextRequestMessage | undefined;
if (!data || data.type !== 'keyapp:context-request') return;

const targetIframe = getIframeByMessageSource(event.source);
if (targetIframe) {
sendKeyAppContext(targetIframe);
return;
}

const appId = data.payload?.appId;
if (!appId) return;
const app = miniappRuntimeStore.state.apps.get(appId);
const iframe = app?.containerHandle?.getIframe() ?? app?.iframeRef;
if (iframe) {
sendKeyAppContext(iframe);
}
}

let keyAppContextRequestListenerReady = false;
function ensureKeyAppContextRequestListener(): void {
if (keyAppContextRequestListenerReady) return;
keyAppContextRequestListenerReady = true;
window.addEventListener('message', handleKeyAppContextRequest);
}

/** Store 实例 */
export const miniappRuntimeStore = new Store<MiniappRuntimeState>(initialState);

// 注册 context-request 监听器(必须在 store 定义之后)
ensureKeyAppContextRequestListener();

export function setMiniappVisualConfig(update: MiniappVisualConfigUpdate): void {
miniappRuntimeStore.setState((s) => ({
...s,
Expand Down