mirror of
https://github.com/ecomfe/vue-echarts.git
synced 2025-10-28 11:33:57 +08:00
feat: delay disposal to disconnection if possible
This commit is contained in:
@ -8,7 +8,7 @@ import {
|
||||
computed,
|
||||
inject,
|
||||
onMounted,
|
||||
onUnmounted,
|
||||
onBeforeUnmount,
|
||||
h,
|
||||
nextTick,
|
||||
watchEffect,
|
||||
@ -38,9 +38,10 @@ import {
|
||||
loadingProps
|
||||
} from "./composables";
|
||||
import { omitOn, unwrapInjected } from "./utils";
|
||||
import { register, TAG_NAME, type EChartsElement } from "./wc";
|
||||
import "./style.css";
|
||||
|
||||
const TAG_NAME = "x-vue-echarts";
|
||||
const wcRegistered = register();
|
||||
|
||||
if (Vue2) {
|
||||
Vue2.config.ignoredElements.push(TAG_NAME);
|
||||
@ -70,7 +71,7 @@ export default defineComponent({
|
||||
emits: [] as unknown as Emits,
|
||||
inheritAttrs: false,
|
||||
setup(props, { attrs }) {
|
||||
const root = shallowRef<HTMLElement>();
|
||||
const root = shallowRef<EChartsElement>();
|
||||
const chart = shallowRef<EChartsType>();
|
||||
const manualOption = shallowRef<Option>();
|
||||
const defaultTheme = inject(THEME_KEY, null);
|
||||
@ -273,7 +274,17 @@ export default defineComponent({
|
||||
init();
|
||||
});
|
||||
|
||||
onUnmounted(cleanup);
|
||||
onBeforeUnmount(() => {
|
||||
if (wcRegistered && root.value) {
|
||||
// For registered web component, we can leverage the
|
||||
// `disconnectedCallback` to dispose the chart instance
|
||||
// so that we can delay the cleanup after exsiting leaving
|
||||
// transition.
|
||||
root.value.__dispose = cleanup;
|
||||
} else {
|
||||
cleanup();
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
chart,
|
||||
|
||||
51
src/wc.ts
Normal file
51
src/wc.ts
Normal file
@ -0,0 +1,51 @@
|
||||
let registered: boolean | null = null;
|
||||
|
||||
export const TAG_NAME = "x-vue-echarts";
|
||||
|
||||
export interface EChartsElement extends HTMLElement {
|
||||
__dispose: (() => void) | null;
|
||||
}
|
||||
|
||||
export function register(): boolean {
|
||||
if (registered != null) {
|
||||
return registered;
|
||||
}
|
||||
|
||||
if (
|
||||
typeof HTMLElement === "undefined" ||
|
||||
typeof customElements === "undefined"
|
||||
) {
|
||||
return (registered = false);
|
||||
}
|
||||
|
||||
try {
|
||||
// Class definitions cannot be transpiled to ES5
|
||||
// so we are doing a little trick here to ensure
|
||||
// we are using native classes. As we use this as
|
||||
// a progressive enhancement, it will be fine even
|
||||
// if the browser doesn't support native classes.
|
||||
const reg = new Function(
|
||||
"tag",
|
||||
`class EChartsElement extends HTMLElement {
|
||||
__dispose = null;
|
||||
|
||||
disconnectedCallback() {
|
||||
if (this.__dispose) {
|
||||
this.__dispose();
|
||||
this.__dispose = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (customElements.get(tag) == null) {
|
||||
customElements.define(tag, EChartsElement);
|
||||
}
|
||||
`
|
||||
);
|
||||
reg(TAG_NAME);
|
||||
} catch (e) {
|
||||
return (registered = false);
|
||||
}
|
||||
|
||||
return (registered = true);
|
||||
}
|
||||
Reference in New Issue
Block a user