Skip to content

Commit

Permalink
feat: delay disposal to disconnection if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Justineo committed Dec 29, 2022
1 parent 3b2f2b5 commit 0a4601b
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 6.4.0

* Delay the disposal of the ECharts instance to the moment the element is disconnected from the DOM if possible (#433).

## 6.3.3

* Make autoresize work for grid layout by default (#675).
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ Drop `<script>` inside your HTML file and access the component via `window.VueEC
```html
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-echarts@6.3.3"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-echarts@6.4.0"></script>
```
<!-- vue3Scripts:end -->

Expand All @@ -247,7 +247,7 @@ app.component('v-chart', VueECharts)
```html
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-echarts@6.3.3"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-echarts@6.4.0"></script>
```
<!-- vue2Scripts:end -->

Expand Down
4 changes: 2 additions & 2 deletions README.zh-Hans.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ import "echarts";
```html
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-echarts@6.3.3"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-echarts@6.4.0"></script>
```
<!-- vue3Scripts:end -->

Expand All @@ -245,7 +245,7 @@ app.component('v-chart', VueECharts)
```html
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-echarts@6.3.3"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-echarts@6.4.0"></script>
```
<!-- vue2Scripts:end -->

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-echarts",
"version": "6.3.3",
"version": "6.4.0",
"description": "Vue.js component for Apache ECharts.",
"author": "GU Yiling <[email protected]>",
"scripts": {
Expand Down
19 changes: 15 additions & 4 deletions src/ECharts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
computed,
inject,
onMounted,
onUnmounted,
onBeforeUnmount,
h,
nextTick,
watchEffect,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down
51 changes: 51 additions & 0 deletions src/wc.ts
Original file line number Diff line number Diff line change
@@ -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);
}

1 comment on commit 0a4601b

@vercel
Copy link

@vercel vercel bot commented on 0a4601b Dec 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.