Skip to content

Commit

Permalink
bug: race condition caused icons not to render #89 (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
regevbr authored Jun 9, 2022
1 parent 81405fb commit 3aac74a
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 46 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ v3 is only compatible from version 2022.3 onwards

```yaml
resources:
- url: /local/mini-humidifier-bundle.js?v=3.0.3
- url: /local/mini-humidifier-bundle.js?v=3.0.4
type: module
```
Expand All @@ -40,14 +40,14 @@ v3 is only compatible from version 2022.3 onwards
2. Grab `mini-humidifier-bundle.js`

```console
$ wget https://github.com/artem-sedykh/mini-humidifier/releases/download/v3.0.3/mini-humidifier-bundle.js
$ wget https://github.com/artem-sedykh/mini-humidifier/releases/download/v3.0.4/mini-humidifier-bundle.js
```

3. Add a reference to `mini-humidifier-bundle.js` inside your `configuration.yaml`.

```yaml
resources:
- url: /local/mini-humidifier-bundle.js?v=3.0.3
- url: /local/mini-humidifier-bundle.js?v=3.0.4
type: module
```

Expand All @@ -60,7 +60,7 @@ v3 is only compatible from version 2022.3 onwards

```yaml
resources:
- url: /local/mini-humidifier-bundle.js?v=3.0.3
- url: /local/mini-humidifier-bundle.js?v=3.0.4
type: module
```

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mini-humidifier",
"version": "v3.0.3",
"version": "v3.0.4",
"description": "humidifier card for Home Assistant Lovelace UI",
"keywords": [
"home-assistant",
Expand Down
5 changes: 5 additions & 0 deletions release_notes/v3.0.4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## v3.0.4
[![Downloads](https://img.shields.io/github/downloads/artem-sedykh/mini-humidifier/v3.0.4/total.svg)](https://github.com/artem-sedykh/mini-humidifier/releases/tag/v3.0.4)

### Fixed
- bug: race condition caused icons not to render by @regevbr
9 changes: 6 additions & 3 deletions src/components/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import { styleMap } from 'lit/directives/style-map';
import { ScopedRegistryHost } from '@lit-labs/scoped-registry-mixin';
import sharedStyle from '../sharedStyle';
import buildElementDefinitions from '../utils/buildElementDefinitions';
import globalElementLoader from '../utils/globalElementLoader';

export default class HumidifierButton extends ScopedRegistryHost(LitElement) {
static get defineId() { return 'mh-button'; }

static get elementDefinitions() {
return buildElementDefinitions([
globalElementLoader('ha-icon'),
globalElementLoader('ha-icon-button'),
'ha-icon',
'ha-icon-button',
], HumidifierButton);
}

Expand Down Expand Up @@ -53,6 +52,10 @@ export default class HumidifierButton extends ScopedRegistryHost(LitElement) {
}

render() {
if (!HumidifierButton.elementDefinitionsLoaded) {
return html``;
}

clearTimeout(this.timer);

return html`
Expand Down
4 changes: 4 additions & 0 deletions src/components/buttons.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export default class HumidifierButtons extends ScopedRegistryHost(LitElement) {
}

render() {
if (!HumidifierButtons.elementDefinitionsLoaded) {
return html``;
}

const context = this;
return html`${Object.entries(this.buttons)
.map(b => b[1])
Expand Down
9 changes: 6 additions & 3 deletions src/components/dropdown-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import sharedStyle from '../sharedStyle';
import HumidifierMenu from './mwc/menu';
import HumidifierListItem from './mwc/list-item';
import buildElementDefinitions from '../utils/buildElementDefinitions';
import globalElementLoader from '../utils/globalElementLoader';

export default class HumidifierDropdownBase extends ScopedRegistryHost(LitElement) {
static get defineId() { return 'mh-dropdown-base'; }

static get elementDefinitions() {
return buildElementDefinitions([
globalElementLoader('ha-icon'),
globalElementLoader('ha-icon-button'),
'ha-icon',
'ha-icon-button',
HumidifierMenu,
HumidifierListItem,
], HumidifierDropdownBase);
Expand Down Expand Up @@ -51,6 +50,10 @@ export default class HumidifierDropdownBase extends ScopedRegistryHost(LitElemen
}

render() {
if (!HumidifierDropdownBase.elementDefinitionsLoaded) {
return html``;
}

return html`
<div class='mh-dropdown'>
<ha-icon-button class='mh-dropdown__button icon'
Expand Down
4 changes: 4 additions & 0 deletions src/components/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export default class HumidifierDropDown extends ScopedRegistryHost(LitElement) {
}

render() {
if (!HumidifierDropDown.elementDefinitionsLoaded) {
return html``;
}

clearTimeout(this.timer);

return html`
Expand Down
8 changes: 5 additions & 3 deletions src/components/indicators.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import { styleMap } from 'lit/directives/style-map';
import handleClick from '../utils/handleClick';
import { TAP_ACTIONS } from '../const';
import buildElementDefinitions from '../utils/buildElementDefinitions';
import globalElementLoader from '../utils/globalElementLoader';


export default class HumidifierIndicators extends ScopedRegistryHost(LitElement) {
static get defineId() { return 'mh-indicators'; }

static get elementDefinitions() {
return buildElementDefinitions([globalElementLoader('ha-icon')], HumidifierIndicators);
return buildElementDefinitions(['ha-icon'], HumidifierIndicators);
}

static get properties() {
Expand Down Expand Up @@ -59,6 +57,10 @@ export default class HumidifierIndicators extends ScopedRegistryHost(LitElement)
}

render() {
if (!HumidifierIndicators.elementDefinitionsLoaded) {
return html``;
}

const context = this;
// console.log('render Indicators');

Expand Down
9 changes: 6 additions & 3 deletions src/components/power.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import { ScopedRegistryHost } from '@lit-labs/scoped-registry-mixin';
import sharedStyle from '../sharedStyle';
import buildElementDefinitions from '../utils/buildElementDefinitions';
import HumidifierButton from './button';
import globalElementLoader from '../utils/globalElementLoader';

export default class HumidifierPower extends ScopedRegistryHost(LitElement) {
static get defineId() { return 'mh-power'; }

static get elementDefinitions() {
return buildElementDefinitions([
HumidifierButton,
globalElementLoader('ha-entity-toggle'),
'ha-entity-toggle',
], HumidifierPower);
}

Expand All @@ -27,8 +26,12 @@ export default class HumidifierPower extends ScopedRegistryHost(LitElement) {
}

render() {
if (!HumidifierPower.elementDefinitionsLoaded) {
return html``;
}

if (this.power.hide)
return '';
return html``;

if (this.power.type === 'toggle') {
return html`
Expand Down
9 changes: 6 additions & 3 deletions src/components/targetHumidity.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import { css, html, LitElement } from 'lit';
import { styleMap } from 'lit/directives/style-map';
import { ScopedRegistryHost } from '@lit-labs/scoped-registry-mixin';
import buildElementDefinitions from '../utils/buildElementDefinitions';
import globalElementLoader from '../utils/globalElementLoader';

export default class HumidifierTargetHumidity extends ScopedRegistryHost(LitElement) {
static get defineId() { return 'mh-target-humidity'; }

static get elementDefinitions() {
return buildElementDefinitions([
globalElementLoader('ha-slider'),
globalElementLoader('ha-icon'),
'ha-slider',
'ha-icon',
], HumidifierTargetHumidity);
}

Expand Down Expand Up @@ -64,6 +63,10 @@ export default class HumidifierTargetHumidity extends ScopedRegistryHost(LitElem
}

render() {
if (!HumidifierTargetHumidity.elementDefinitionsLoaded) {
return html``;
}

return html`
<div class='mh-target_humidifier --slider flex'>
<ha-slider
Expand Down
13 changes: 8 additions & 5 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,17 @@ import HumidifierPower from './components/power';
import HumidifierIndicators from './components/indicators';
import HumidifierButtons from './components/buttons';
import buildElementDefinitions from './utils/buildElementDefinitions';
import globalElementLoader from './utils/globalElementLoader';

class MiniHumidifier extends ScopedRegistryHost(LitElement) {
static get elementDefinitions() {
return buildElementDefinitions([
globalElementLoader('ha-card'),
globalElementLoader('ha-icon'),
globalElementLoader('ha-relative-time'),
'ha-card',
'ha-icon',
'ha-relative-time',
HumidifierTargetHumidity,
HumidifierPower,
HumidifierIndicators,
globalElementLoader('ha-icon-button'),
'ha-icon-button',
HumidifierButtons,
], MiniHumidifier);
}
Expand Down Expand Up @@ -439,6 +438,10 @@ class MiniHumidifier extends ScopedRegistryHost(LitElement) {
}

render() {
if (!MiniHumidifier.elementDefinitionsLoaded) {
return html``;
}

const cls = this.config.target_humidity.hide ? 'full' : '';
return html`
<ha-card
Expand Down
45 changes: 32 additions & 13 deletions src/utils/buildElementDefinitions.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
const buildElementDefinitions = (elements, constructor) => elements.reduce(
(aggregate, element) => {
if (element.defineId) {
// eslint-disable-next-line no-param-reassign
aggregate[element.defineId] = element;
} else {
element.promise.then((clazz) => {
if (constructor.registry.get(element.name) === undefined) {
constructor.registry.define(element.name, clazz);
const buildElementDefinitions = (elements, constructor) => {
const promises = [];
const definitions = elements.reduce(
(aggregate, element) => {
if (typeof element === 'string') {
const clazz = customElements.get(element);
if (clazz) {
// eslint-disable-next-line no-param-reassign
aggregate[element] = clazz;
} else {
promises.push(customElements.whenDefined(element).then(() => {
if (constructor.registry.get(element) === undefined) {
constructor.registry.define(element, customElements.get(element));
}
}));
}
} else {
// eslint-disable-next-line no-param-reassign
aggregate[element.defineId] = element;
}
return aggregate;
}, {},
);
// eslint-disable-next-line no-param-reassign
constructor.elementDefinitionsLoaded = promises.length === 0;
if (!constructor.elementDefinitionsLoaded) {
Promise.all(promises)
.then(() => {
// eslint-disable-next-line no-param-reassign
constructor.elementDefinitionsLoaded = true;
});
}
return aggregate;
}, {},
);
}
return definitions;
};

export default buildElementDefinitions;
6 changes: 0 additions & 6 deletions src/utils/globalElementLoader.js

This file was deleted.

0 comments on commit 3aac74a

Please sign in to comment.