Skip to content
Open
Show file tree
Hide file tree
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
78 changes: 71 additions & 7 deletions how-to/use-platform/apply-snapshot/client/src/provider.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,90 @@
import type OpenFin from "@openfin/core";
document.addEventListener("DOMContentLoaded", async () => {
await fin.Platform.init();
await fin.Platform.init({
overrideCallback: async (Provider) => {
/**
* Override the provider class.
*/
class Override extends Provider {
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type, jsdoc/require-jsdoc
public async createWindow(
options: OpenFin.PlatformWindowCreationOptions,
callerIdentity: { uuid: string; name: string } | undefined
) {
console.log("createWindow called", options, callerIdentity);
// default behavior for all other creation reasons
const win = await super.createWindow(options, callerIdentity);
console.log("createWindow completed", win.identity.name);
return win;
}
}
return new Override();
}
});
const platform = fin.Platform.getCurrentSync();

const launchOneBtn: HTMLButtonElement = document.querySelector("#launch-one") as HTMLButtonElement;
const launchTenBtn: HTMLButtonElement = document.querySelector("#launch-ten") as HTMLButtonElement;
const launchTwentyBtn: HTMLButtonElement = document.querySelector("#launch-twenty") as HTMLButtonElement;
const baseUrl = "http://localhost:5050";
const url: HTMLInputElement = document.querySelector("#url") as HTMLInputElement;
await platform.on("window-created", async (event) => {
console.log("Window created", event);
});
await platform.on("window-did-start-loading", async (event) => {
console.log("Window did start loading", event);
});
await platform.on("window-did-finish-load", async (event) => {
console.log("Window did finish load", event);
});
await platform.on("window-end-load", async (event) => {
console.log("Window end load", event);
});
await platform.on("window-initialized", async (event) => {
console.log("Window initialized", event);
});
await platform.on("window-performance-report", async (event) => {
console.log("Window performance report", event);
});
await platform.on("window-start-load", async (event) => {
console.log("Window start load", event);
});

if (launchOneBtn || launchTenBtn || launchTwentyBtn) {
launchOneBtn.addEventListener("click", async () => {
const snapshot = await fetch(`${baseUrl}/common/snapshots/snapshot-one.json`);
const snapshotJson = await snapshot.json();
await platform.applySnapshot(snapshotJson);
if(url.value.trim() === "") {
const payload = await snapshot.json();
await platform.applySnapshot(payload);
} else {
const snapshotJson = await snapshot.text();
const snapshotString = snapshotJson.replace(/about:blank/g, url.value.trim());
await platform.applySnapshot(JSON.parse(snapshotString));
}
});
launchTenBtn.addEventListener("click", async () => {
const snapshot = await fetch(`${baseUrl}/common/snapshots/snapshot-ten.json`);
const snapshotJson = await snapshot.json();
await platform.applySnapshot(snapshotJson);
if(url.value.trim() === "") {
const payload = await snapshot.json();
await platform.applySnapshot(payload);
} else {
const snapshotJson = await snapshot.text();
const snapshotString = snapshotJson.replace(/about:blank/g, url.value.trim());
await platform.applySnapshot(JSON.parse(snapshotString));
}
});
launchTwentyBtn.addEventListener("click", async () => {
const snapshot = await fetch(`${baseUrl}/common/snapshots/snapshot-twenty.json`);
const snapshotJson = await snapshot.json();
await platform.applySnapshot(snapshotJson);
if(url.value.trim() === "") {
const payload = await snapshot.json();
await platform.applySnapshot(payload);
} else {
const snapshotJson = await snapshot.text();
const snapshotString = snapshotJson.replace(/about:blank/g, url.value.trim());
await platform.applySnapshot(JSON.parse(snapshotString));
}
});
}
});
4 changes: 4 additions & 0 deletions how-to/use-platform/apply-snapshot/public/html/provider.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ <h1>How to use the applySnapshot API</h1>
<h1 class="tag">Click on the button below to launch 1, 10, or 20 Platform Windows.</h1>
</div>
</header>
<div class="row spread">
<label for="url">Url to launch (defaults to about:blank)</label>
<input type="url" id="url"></input>
</div>
<div class="row spread middle">
<button id="launch-one">Launch 1 window</button>
<button id="launch-ten">Launch 10 windows</button>
Expand Down
Loading