Skip to content

Commit

Permalink
fix: cannot find document if group destroyed (#5870)
Browse files Browse the repository at this point in the history
* fix: cannot find document if group destroyed

* fix: fallback
  • Loading branch information
lijinke666 committed Dec 5, 2023
1 parent 5422bbb commit c78da52
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ site/.dumi/tmp-production

# Editor
.vscode

.history

# Other
!/src/lib
Expand Down
20 changes: 15 additions & 5 deletions src/runtime/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,33 @@ export function useLibrary<
>(
namespace: G2ComponentNamespaces,
publicLibrary: G2Library,
): [(options: O, context?) => V, (type: O['type']) => C] {
): [(options: O, context?: G2Context) => V, (type: O['type']) => C] {
const library = { ...builtinlib(), ...publicLibrary };

const create = (type: O['type']) => {
if (typeof type !== 'string') return type;
const key = `${namespace}.${type}`;
return library[key] || error(`Unknown Component: ${key}`);
};
const use = (options: O, context?) => {

const use = (options: O, context?: G2Context) => {
const { type, ...rest } = options;
return create(type)(rest, context);
if (!type) {
error(`Plot type is required!`);
}

const currentLibrary = create(type);
return currentLibrary?.(rest, context);
};

return [use, create];
}

export function documentOf(library: G2Context): IDocument {
const { canvas, group } = library;
if (group) return group.ownerDocument;
return canvas.document;
return (
canvas?.document ||
group?.ownerDocument ||
error(`Cannot find library document`)
);
}
5 changes: 3 additions & 2 deletions src/runtime/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,15 @@ export function renderToMountedElement<T extends G2ViewTree = G2ViewTree>(
const selection = select(group);
context.group = group;
context.emitter = emitter;
context.canvas =
context.canvas || (group?.ownerDocument?.defaultView as GCanvas);

emitter.emit(ChartEvent.BEFORE_RENDER);
// Plot the chart and mutate context.
// Make sure that plot chart after container is ready for every time.
plot<T>({ ...keyed, width, height }, selection, library, context)
.then(() => {
const canvas = group.ownerDocument.defaultView;
canvas.requestAnimationFrame(() => {
context.canvas?.requestAnimationFrame(() => {
emitter.emit(ChartEvent.AFTER_RENDER);
resolve?.();
});
Expand Down

0 comments on commit c78da52

Please sign in to comment.