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(svelte-vega): fix infinite update depth issues #978

Merged
merged 1 commit into from
Nov 18, 2024
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 packages/svelte-vega/src/lib/Vega.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
options?: EmbedOptions;
data: Record<string, unknown>;
signalListeners?: SignalListeners;
view: View | undefined;
view?: View;
onNewView?: (view: View) => void;
onError?: (error: Error) => void;
} = $props();
Expand Down
105 changes: 58 additions & 47 deletions packages/svelte-vega/src/lib/VegaEmbed.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import type { EmbedOptions, Result } from 'vega-embed';
import type { SignalListeners, View, VisualizationSpec } from './types';

import { onDestroy } from 'svelte';
import { onDestroy, untrack } from 'svelte';
import vegaEmbed from 'vega-embed';

import { WIDTH_HEIGHT } from './constants';
Expand All @@ -26,7 +26,7 @@
}: {
options: EmbedOptions;
spec: VisualizationSpec;
view: View | undefined;
view?: View;
signalListeners?: SignalListeners;
data: Record<string, unknown>;
onError?: (error: Error) => void;
Expand All @@ -37,66 +37,77 @@
let prevOptions: EmbedOptions = $state({});
let prevSignalListeners: SignalListeners | undefined = $state(undefined);
let prevSpec: VisualizationSpec = $state({});
let prevData: Record<string, unknown> = $state({});
let chartContainer: HTMLElement | undefined = $state(undefined);

Check warning on line 40 in packages/svelte-vega/src/lib/VegaEmbed.svelte

View workflow job for this annotation

GitHub Actions / Node

'HTMLElement' is not defined

$effect(() => {
if (!shallowEqual(data, prevData)) {
data;

Check warning on line 43 in packages/svelte-vega/src/lib/VegaEmbed.svelte

View workflow job for this annotation

GitHub Actions / Node

Expected an assignment or function call and instead saw an expression
untrack(() => {
update();
}
prevData = data;
});
});

$effect(() => {
if (chartContainer !== undefined) {
// only create a new view if neccessary
if (!shallowEqual(options, prevOptions, WIDTH_HEIGHT)) {
createView();
} else {
const specChanges = computeSpecChanges(
combineSpecWithDimension(spec, options),
combineSpecWithDimension(prevSpec, prevOptions)
);
const newSignalListeners = signalListeners;
const oldSignalListeners = prevSignalListeners;
chartContainer;

Check warning on line 50 in packages/svelte-vega/src/lib/VegaEmbed.svelte

View workflow job for this annotation

GitHub Actions / Node

Expected an assignment or function call and instead saw an expression
options;

Check warning on line 51 in packages/svelte-vega/src/lib/VegaEmbed.svelte

View workflow job for this annotation

GitHub Actions / Node

Expected an assignment or function call and instead saw an expression
spec;

Check warning on line 52 in packages/svelte-vega/src/lib/VegaEmbed.svelte

View workflow job for this annotation

GitHub Actions / Node

Expected an assignment or function call and instead saw an expression
signalListeners;

Check warning on line 53 in packages/svelte-vega/src/lib/VegaEmbed.svelte

View workflow job for this annotation

GitHub Actions / Node

Expected an assignment or function call and instead saw an expression
untrack(() => {
if (chartContainer !== undefined) {
// only create a new view if neccessary
if (!shallowEqual(options, prevOptions, WIDTH_HEIGHT)) {
createView();
} else {
const specChanges = computeSpecChanges(
combineSpecWithDimension(spec, options),
combineSpecWithDimension(prevSpec, prevOptions)
);
const newSignalListeners = signalListeners;
const oldSignalListeners = prevSignalListeners;

if (specChanges) {
if (specChanges.isExpensive) {
createView();
} else if (result !== undefined) {
const areSignalListenersChanged = !shallowEqual(newSignalListeners, oldSignalListeners);
view = result.view;
if (specChanges.width !== false) {
view.width(specChanges.width);
}
if (specChanges.height !== false) {
view.height(specChanges.height);
}
if (areSignalListenersChanged) {
if (oldSignalListeners) {
removeSignalListenersFromView(view, oldSignalListeners);
if (specChanges) {
if (specChanges.isExpensive) {
createView();
} else if (result !== undefined) {
const areSignalListenersChanged = !shallowEqual(
newSignalListeners,
oldSignalListeners
);
view = result.view;
if (specChanges.width !== false) {
view.width(specChanges.width);
}
if (specChanges.height !== false) {
view.height(specChanges.height);
}
if (newSignalListeners) {
addSignalListenersToView(view, newSignalListeners);
if (areSignalListenersChanged) {
if (oldSignalListeners) {
removeSignalListenersFromView(view, oldSignalListeners);
}
if (newSignalListeners) {
addSignalListenersToView(view, newSignalListeners);
}
}
view.runAsync();
}
} else if (
!shallowEqual(newSignalListeners, oldSignalListeners) &&
result !== undefined
) {
view = result.view;
if (oldSignalListeners) {
removeSignalListenersFromView(view, oldSignalListeners);
}
if (newSignalListeners) {
addSignalListenersToView(view, newSignalListeners);
}
view.runAsync();
}
} else if (!shallowEqual(newSignalListeners, oldSignalListeners) && result !== undefined) {
view = result.view;
if (oldSignalListeners) {
removeSignalListenersFromView(view, oldSignalListeners);
}
if (newSignalListeners) {
addSignalListenersToView(view, newSignalListeners);
}
view.runAsync();
}
prevOptions = options;
prevSignalListeners = signalListeners;
prevSpec = spec;
}
prevOptions = options;
prevSignalListeners = signalListeners;
prevSpec = spec;
}
});
});

onDestroy(() => {
Expand Down Expand Up @@ -130,7 +141,7 @@

function error(error: Error) {
onError?.(error);
console.warn(error);

Check warning on line 144 in packages/svelte-vega/src/lib/VegaEmbed.svelte

View workflow job for this annotation

GitHub Actions / Node

'console' is not defined
}

function newView(view: View) {
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte-vega/src/lib/VegaLite.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
options?: EmbedOptions;
data: Record<string, unknown>;
signalListeners?: SignalListeners;
view: View | undefined;
view?: View;
onNewView?: (view: View) => void;
onError?: (error: Error) => void;
} = $props();
Expand Down
Loading