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: cannot find document if group destroyed #5870

Merged
merged 2 commits into from
Dec 5, 2023
Merged
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
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] {
pearmini marked this conversation as resolved.
Show resolved Hide resolved
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
Loading