What if you remove the `addLegend()` call here, and then inside `addLegend()` (only after you add it, if you add it) do this:

```
componentManager_.applySharedObject('rootId', 'gl-legend');
```

That will prevent the legend from getting added then removed unnecessarily the first time through if showLegend is initially false.