Skip to content

Commit dbc8cd5

Browse files
authored
Merge pull request #589 from adobe/cloneBuiltSpec
Clone built spec
2 parents 68eab9e + 5d35fb3 commit dbc8cd5

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

packages/react-spectrum-charts/src/hooks/useSpec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export default function useSpec({
7373
});
7474

7575
// stringify-parse so that all immer stuff gets cleared out
76-
return JSON.parse(JSON.stringify(buildSpec(chartOptions)));
76+
return buildSpec(chartOptions);
7777
}, [
7878
UNSAFE_vegaSpec,
7979
backgroundColor,

packages/vega-spec-builder/src/chartSpecBuilder.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ describe('Chart spec builder', () => {
391391
highlightedSeries: 'Chrome',
392392
});
393393

394-
expect(spec.signals?.find((signal) => signal.name === HIGHLIGHTED_SERIES)).toStrictEqual({
394+
expect(spec.signals?.find((signal) => signal.name === HIGHLIGHTED_SERIES)).toEqual({
395395
name: HIGHLIGHTED_SERIES,
396396
value: 'Chrome',
397397
});
@@ -405,7 +405,7 @@ describe('Chart spec builder', () => {
405405
highlightedSeries: 'Chrome',
406406
});
407407

408-
expect(spec.signals?.find((signal) => signal.name === HIGHLIGHTED_SERIES)).toStrictEqual({
408+
expect(spec.signals?.find((signal) => signal.name === HIGHLIGHTED_SERIES)).toEqual({
409409
name: HIGHLIGHTED_SERIES,
410410
value: 'Chrome',
411411
});
@@ -432,7 +432,7 @@ describe('Chart spec builder', () => {
432432
],
433433
};
434434

435-
expect(spec.signals?.find((signal) => signal.name === 'highlightedSeries')).toStrictEqual(
435+
expect(spec.signals?.find((signal) => signal.name === 'highlightedSeries')).toEqual(
436436
uncontrolledHighlightSignal
437437
);
438438
});

packages/vega-spec-builder/src/chartSpecBuilder.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212
import { produce } from 'immer';
13-
import { Data, LinearScale, OrdinalScale, PointScale, Scale, Signal, Spec } from 'vega';
13+
import { Data, LinearScale, OrdinalScale, PointScale, Scale, Signal } from 'vega';
1414

1515
import {
1616
BACKGROUND_COLOR,
@@ -77,10 +77,6 @@ import {
7777
SymbolSize,
7878
} from './types';
7979

80-
export interface ChartSpecBuilder {
81-
buildSpec: (options: ChartOptions) => Spec;
82-
}
83-
8480
export function buildSpec({
8581
axes = [],
8682
backgroundColor = DEFAULT_BACKGROUND_COLOR,
@@ -194,7 +190,7 @@ export function buildSpec({
194190
// clear out all scales that don't have any fields on the domain
195191
spec = removeUnusedScales(spec);
196192

197-
return spec;
193+
return safeClone(spec);
198194
}
199195

200196
export const removeUnusedScales = produce<ScSpec>((spec) => {
@@ -422,3 +418,10 @@ export const isNumberArray = (opacities: Opacities): opacities is number[] => {
422418
export const isSymbolShapeArray = (symbolShapes: SymbolShapes): symbolShapes is ChartSymbolShape[] => {
423419
return !symbolShapes.some((symbolShape) => Array.isArray(symbolShape));
424420
};
421+
422+
const safeClone = <T>(obj: T): T => {
423+
if (typeof structuredClone === 'function') {
424+
return structuredClone(obj);
425+
}
426+
return JSON.parse(JSON.stringify(obj));
427+
};

0 commit comments

Comments
 (0)