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

feat: add iframe heartbeat implementation. #816

Merged
merged 20 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
48a7289
feat: add iframe heartbeat
khamdam-magic Oct 9, 2024
edf7596
fix: install and fix test issues
khamdam-magic Oct 10, 2024
c8fb4cc
fix: fix deep source error
khamdam-magic Oct 10, 2024
f0dec86
fix: make reloadIframe optionalfor mobile
khamdam-magic Oct 10, 2024
d42ed2b
fix: yarn install
khamdam-magic Oct 10, 2024
c8846a8
fix: yarn install lock
khamdam-magic Oct 10, 2024
b6d1148
Merge branch 'master' of https://github.com/magiclabs/magic-js into k…
khamdam-magic Oct 10, 2024
f0105fe
fix: add a new line
khamdam-magic Oct 10, 2024
d4c6164
test: add new tests
khamdam-magic Oct 11, 2024
912b3f7
fix: remove heartbeat from viewcontroller
khamdam-magic Oct 15, 2024
4d14b66
fix: add heartbeat to iframecontroller
khamdam-magic Oct 15, 2024
d163d10
fix: use Magic message enums
khamdam-magic Oct 15, 2024
99f6bc8
Merge branch 'master' of https://github.com/magiclabs/magic-js into k…
khamdam-magic Oct 15, 2024
efab991
fix: update iframe controller
khamdam-magic Oct 15, 2024
642e0b3
fix: fix test issues
khamdam-magic Oct 15, 2024
1b4acd5
chore: update test coverage for magic-sdk
khamdam-magic Oct 16, 2024
95cf8d4
Merge branch 'master' into khamdam-sc-PDEEXP-1908-Iframe-heartbeat
khamdam-magic Oct 21, 2024
3d7b3e6
Merge branch 'master' of https://github.com/magiclabs/magic-js into k…
khamdam-magic Oct 21, 2024
ee07988
Merge branch 'khamdam-sc-PDEEXP-1908-Iframe-heartbeat' of https://git…
khamdam-magic Oct 21, 2024
7c91deb
fix: update yarn.lock
khamdam-magic Oct 21, 2024
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
41 changes: 41 additions & 0 deletions packages/@magic-sdk/provider/src/core/view-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ async function persistMagicEventRefreshToken(event: MagicMessageEvent) {
await setItem('rt', event.data.rt);
}

const PING_INTERVAL = 5000; // 5 seconds
const RELOAD_THRESHOLD = 10000; // 10 seconds
const INITIAL_HEARTBEAT_DELAY = 3600000; // 1 hour

export abstract class ViewController {
public checkIsReadyForRequest: Promise<void>;
public isReadyForRequest: boolean;
Expand All @@ -136,12 +140,17 @@ export abstract class ViewController {
this.checkIsReadyForRequest = this.waitForReady();
this.isReadyForRequest = false;
this.listen();
this.startHeartBeat();
}

protected abstract init(): void;
protected abstract _post(data: MagicMessageRequest): Promise<void>;
protected abstract hideOverlay(): void;
protected abstract showOverlay(): void;
protected abstract reloadIframe(): void;

private lastPingTime = Date.now();
private pingTimer: ReturnType<typeof setInterval> | null = null;

/**
* Send a payload to the Magic `<iframe>` for processing and automatically
Expand Down Expand Up @@ -259,6 +268,34 @@ export abstract class ViewController {
});
}

private ping = () => {
this._post({ msgType: MagicOutgoingWindowMessage.MAGIC_PING, payload: [] });
this.lastPingTime = Date.now();
};

private startHeartBeat = () => {
const heartBeatCheck = () => {
this.pingTimer = setInterval(() => {
this.ping();
const timeSinceLastPing = Date.now() - this.lastPingTime;

if (timeSinceLastPing > RELOAD_THRESHOLD) {
this.reloadIframe();
}
}, PING_INTERVAL);
};

setTimeout(heartBeatCheck, INITIAL_HEARTBEAT_DELAY);
};

protected stopHeartBeat = () => {
if (this.pingTimer) {
clearInterval(this.pingTimer);

this.pingTimer = null;
}
};

/**
* Listen for messages sent from the underlying Magic `<WebView>`.
*/
Expand All @@ -276,5 +313,9 @@ export abstract class ViewController {
new MagicSDKWarning(SDKWarningCode.ProductAnnouncement, event.data.response.result.product_announcement).log();
}
});

this.on(MagicIncomingWindowMessage.MAGIC_PONG, () => {
this.lastPingTime = Date.now();
});
}
}
2 changes: 2 additions & 0 deletions packages/@magic-sdk/types/src/core/message-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ export enum MagicIncomingWindowMessage {
MAGIC_HANDLE_EVENT = 'MAGIC_HANDLE_EVENT',
MAGIC_MG_BOX_SEND_RECEIPT = 'MAGIC_MG_BOX_SEND_RECEIPT',
MAGIC_SEND_PRODUCT_ANNOUNCEMENT = 'MAGIC_SEND_PRODUCT_ANNOUNCEMENT',
MAGIC_PONG = 'MAGIC_PONG',
}

export enum MagicOutgoingWindowMessage {
MAGIC_HANDLE_REQUEST = 'MAGIC_HANDLE_REQUEST',
MAGIC_PING = 'MAGIC_PING',
}

/** The shape of responding window message datas from the Magic iframe context. */
Expand Down
13 changes: 13 additions & 0 deletions packages/magic-sdk/src/iframe-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ export class IframeController extends ViewController {
}
}
});

window.addEventListener('beforeunload', () => {
this.stopHeartBeat();
});
}

protected async showOverlay() {
Expand All @@ -123,4 +127,13 @@ export class IframeController extends ViewController {
throw createModalNotReadyError();
}
}

protected async reloadIframe() {
const iframe = await this.iframe;
if (iframe && iframe.contentWindow) {
iframe.contentWindow.location.reload();
} else {
throw createModalNotReadyError();
}
}
}
Loading