Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复最顶层frame下存在不止一个wujie依赖包时的报错 #956

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
16 changes: 13 additions & 3 deletions packages/wujie-core/src/common.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import Wujie from "./sandbox";
import { cacheOptions } from "./index";
import { createWujieApp } from "./shadow";
export interface SandboxCache {
wujie?: Wujie;
options?: cacheOptions;
}

export type appAddEventListenerOptions = AddEventListenerOptions & { targetWindow?: Window };

let getIdToSandboxCacheMap: () => Map<String, SandboxCache> = () => {
let registeredWujie = window.customElements.get("wujie-app") as unknown as ReturnType<typeof createWujieApp>;
if (registeredWujie) {
return registeredWujie.idToSandboxCacheMap;
} else if (window.__POWERED_BY_WUJIE__) {
return window.__WUJIE.inject.idToSandboxMap;
} else {
return new Map<String, SandboxCache>();
}
};

// 全部无界实例和配置存储map
export const idToSandboxCacheMap = window.__POWERED_BY_WUJIE__
? window.__WUJIE.inject.idToSandboxMap
: new Map<String, SandboxCache>();
export const idToSandboxCacheMap = getIdToSandboxCacheMap();

export function getWujieById(id: String): Wujie | null {
return idToSandboxCacheMap.get(id)?.wujie || null;
Expand Down
1 change: 1 addition & 0 deletions packages/wujie-core/src/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type lifecycles = {
* 基于 Proxy和iframe 实现的沙箱
*/
export default class Wujie {
static idToSandboxCacheMap = idToSandboxCacheMap;
public id: string;
/** 激活时路由地址 */
public url: string;
Expand Down
34 changes: 19 additions & 15 deletions packages/wujie-core/src/shadow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from "./constant";
import {
getWujieById,
idToSandboxCacheMap,
rawAppendChild,
rawElementAppendChild,
rawElementRemoveChild,
Expand All @@ -32,28 +33,31 @@ declare global {
body: HTMLBodyElement;
}
}
export let createWujieApp = () => {
return class WujieApp extends HTMLElement {
static idToSandboxCacheMap = idToSandboxCacheMap;
connectedCallback(): void {
if (this.shadowRoot) return;
const shadowRoot = this.attachShadow({ mode: "open" });
const sandbox = getWujieById(this.getAttribute(WUJIE_APP_ID));
patchElementEffect(shadowRoot, sandbox.iframe.contentWindow);
sandbox.shadowRoot = shadowRoot;
}

disconnectedCallback(): void {
const sandbox = getWujieById(this.getAttribute(WUJIE_APP_ID));
sandbox?.unmount();
}
};
};

/**
* 定义 wujie webComponent,将shadow包裹并获得dom装载和卸载的生命周期
*/
export function defineWujieWebComponent() {
const customElements = window.customElements;
if (customElements && !customElements?.get("wujie-app")) {
class WujieApp extends HTMLElement {
connectedCallback(): void {
if (this.shadowRoot) return;
const shadowRoot = this.attachShadow({ mode: "open" });
const sandbox = getWujieById(this.getAttribute(WUJIE_APP_ID));
patchElementEffect(shadowRoot, sandbox.iframe.contentWindow);
sandbox.shadowRoot = shadowRoot;
}

disconnectedCallback(): void {
const sandbox = getWujieById(this.getAttribute(WUJIE_APP_ID));
sandbox?.unmount();
}
}
customElements?.define("wujie-app", WujieApp);
customElements?.define("wujie-app", createWujieApp());
}
}

Expand Down