Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait for link requests #8

Merged
merged 1 commit into from
Aug 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ if (!Object.prototype.hasOwnProperty.call(window.EventTarget, 'caller')) {
class HomebridgePluginUi extends EventTargetConstructor {
private origin = '';
private lastBodyHeight = 0;
private linkRequests: Promise<unknown>[] = [];

public toast = new HomebridgeUiToastHelper();
public plugin = window['_homebridge'].plugin;
Expand All @@ -62,9 +63,10 @@ class HomebridgePluginUi extends EventTargetConstructor {
window.addEventListener('message', this._handleIncomingMessage.bind(this), false);
}

private _handleIncomingMessage(e) {
private async _handleIncomingMessage(e) {
switch (e.data.action) {
case 'ready': {
await Promise.all(this.linkRequests);
this.origin = e.origin;
document.body.style.display = 'block';
this.dispatchEvent(new Event('ready'));
Expand Down Expand Up @@ -116,10 +118,14 @@ class HomebridgePluginUi extends EventTargetConstructor {
}

private _setLinkElement(e) {
const linkElement = document.createElement('link');
linkElement.setAttribute('href', e.data.href);
linkElement.setAttribute('rel', e.data.rel);
document.head.appendChild(linkElement);
const request = new Promise(resolve => {
const linkElement = document.createElement('link');
linkElement.setAttribute('href', e.data.href);
linkElement.setAttribute('rel', e.data.rel);
linkElement.onload = resolve
document.head.appendChild(linkElement);
})
this.linkRequests.push(request);
}

private _monitorFrameHeight() {
Expand Down