-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
36 lines (30 loc) · 859 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import {Main} from './Main.elm';
import BubbleChart from './BubbleChart';
const app = Main.embed(document.getElementById('root'), {
apiUrl: process.env.ELM_APP_API_URL,
});
let chartExistenceIntervalId = null;
let chart = null;
const renderChart = data => {
if (!chart) {
chart = new BubbleChart(app, '#d3-simulation');
}
chart.setNodes(data.voteEvents, data.restartSimulation);
};
const chartElementExists = () => !!document.getElementById('d3-simulation');
const createChartIfElementExists = data => () => {
if (chartElementExists()) {
renderChart(data);
window.clearTimeout(chartExistenceIntervalId);
}
};
app.ports.chartData.subscribe(data => {
if (chartElementExists()) {
renderChart(data);
} else {
chartExistenceIntervalId = window.setInterval(
createChartIfElementExists(data),
10,
);
}
});