-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: avoid creating new view when only width or height in spec chang…
…es (#95) * feat: add library * feat: add isSpecChanged * feat: detect background change * refactor: rename updateVegaViewData to updateVegaViewDataset * feat: avoid creating new view if not necessary * docs: add storybook demo * fix: disable padding * docs: add more data to demo * build: coverage threshold
- Loading branch information
Showing
12 changed files
with
270 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
packages/react-vega-demo/stories/ChangingDimensionDemo.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import React from "react"; | ||
import { VegaLite } from "../../react-vega/src"; | ||
|
||
const values = []; | ||
|
||
for (let i = 0; i< 100 ;i++ ) { | ||
values.push({ | ||
a: `X${i}`, | ||
b: Math.round(Math.random() * 1000) | ||
}) | ||
} | ||
|
||
export default class ChangingDimensionDemo extends React.Component<{}, { | ||
width: number; | ||
height: number; | ||
padding: number | { | ||
top?: number; | ||
bottom?: number; | ||
left?: number; | ||
right?: number; | ||
}; | ||
grow: boolean; | ||
}> { | ||
interval: NodeJS.Timeout; | ||
|
||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
width: 100, | ||
height: 100, | ||
padding: 20, | ||
grow: true, | ||
}; | ||
} | ||
|
||
componentDidMount() { | ||
this.interval = setInterval(() => { | ||
this.setState(({width, height, padding, grow}) => ({ | ||
width: width + (grow ? 1 : -1), | ||
height: height + (grow ? 1 : -1), | ||
grow: grow && width < 400 || !grow && width === 100 | ||
})); | ||
}, 10); | ||
} | ||
|
||
componentWillUnmount() { | ||
clearInterval(this.interval); | ||
} | ||
|
||
render() { | ||
const { width, height, padding } = this.state; | ||
|
||
const SPEC = { | ||
$schema: "https://vega.github.io/schema/vega-lite/v4.json", | ||
width, | ||
height, | ||
padding, | ||
data: { | ||
values, | ||
name: "source" | ||
}, | ||
selection: { | ||
a: { type: "single" } | ||
}, | ||
mark: "bar", | ||
encoding: { | ||
x: { field: "a", type: "ordinal" }, | ||
y: { field: "b", type: "quantitative" }, | ||
tooltip: { field: "b", type: "quantitative" }, | ||
color: { | ||
condition: { selection: "a", value: "steelblue" }, | ||
value: "grey" | ||
} | ||
} | ||
} as const; | ||
|
||
return <VegaLite spec={SPEC} />; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { View, SignalListeners } from '../types'; | ||
|
||
export default function addSignalListenersToView(view: View, signalListeners: SignalListeners) { | ||
const signalNames = Object.keys(signalListeners); | ||
signalNames.forEach(signalName => { | ||
try { | ||
view.addSignalListener(signalName, signalListeners[signalName]); | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.warn('Cannot add invalid signal listener.', error); | ||
} | ||
}); | ||
|
||
return signalNames.length > 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { VisualizationSpec } from 'vega-embed'; | ||
import equal from 'fast-deep-equal'; | ||
import getUniqueFieldNames from './getUniqueFieldNames'; | ||
|
||
interface SpecChanges { | ||
width: false | number; | ||
height: false | number; | ||
isExpensive: boolean; | ||
} | ||
|
||
export default function computeSpecChanges(newSpec: VisualizationSpec, oldSpec: VisualizationSpec) { | ||
if (newSpec === oldSpec) return false; | ||
|
||
const changes: SpecChanges = { | ||
width: false, | ||
height: false, | ||
isExpensive: false, | ||
}; | ||
|
||
const fieldNames = getUniqueFieldNames([newSpec, oldSpec]); | ||
|
||
if ( | ||
fieldNames.has('width') && | ||
(!('width' in newSpec) || !('width' in oldSpec) || newSpec.width !== oldSpec.width) | ||
) { | ||
if ('width' in newSpec && typeof newSpec.width === 'number') { | ||
changes.width = newSpec.width; | ||
} else { | ||
changes.isExpensive = true; | ||
} | ||
} | ||
|
||
if ( | ||
fieldNames.has('height') && | ||
(!('height' in newSpec) || !('height' in oldSpec) || newSpec.height !== oldSpec.height) | ||
) { | ||
if ('height' in newSpec && typeof newSpec.height === 'number') { | ||
changes.height = newSpec.height; | ||
} else { | ||
changes.isExpensive = true; | ||
} | ||
} | ||
|
||
// Delete cheap fields | ||
fieldNames.delete('width'); | ||
fieldNames.delete('height'); | ||
|
||
if ( | ||
[...fieldNames].some( | ||
field => | ||
!(field in newSpec) || | ||
!(field in oldSpec) || | ||
!equal(newSpec[field as keyof typeof newSpec], oldSpec[field as keyof typeof oldSpec]), | ||
) | ||
) { | ||
changes.isExpensive = true; | ||
} | ||
|
||
return changes.width !== false || changes.height !== false || changes.isExpensive | ||
? changes | ||
: false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
packages/react-vega/src/utils/removeSignalListenersFromView.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { View, SignalListeners } from '../types'; | ||
|
||
export default function removeSignalListenersFromView( | ||
view: View, | ||
signalListeners: SignalListeners, | ||
) { | ||
const signalNames = Object.keys(signalListeners); | ||
signalNames.forEach(signalName => { | ||
try { | ||
view.removeSignalListener(signalName, signalListeners[signalName]); | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.warn('Cannot remove invalid signal listener.', error); | ||
} | ||
}); | ||
|
||
return signalNames.length > 0; | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/react-vega/src/utils/updateMultipleDatasetsInView.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import updateSingleDatasetInView from './updateSingleDatasetInView'; | ||
import { PlainObject, View } from '../types'; | ||
|
||
export default function updateMultipleDatasetsInView(view: View, data: PlainObject) { | ||
Object.keys(data).forEach(name => { | ||
updateSingleDatasetInView(view, name, data[name]); | ||
}); | ||
} |
Oops, something went wrong.