diff --git a/examples/web/assets/js/app.js b/examples/web/assets/js/app.js index 5118f5e4..540b035f 100644 --- a/examples/web/assets/js/app.js +++ b/examples/web/assets/js/app.js @@ -1,4 +1,4 @@ -var container = desktopJS.resolveContainer(); +var container; var snapAssist; var hostName = document.getElementById('hostName'); @@ -59,38 +59,42 @@ desktopJS.Electron.ElectronContainer.prototype.showNotification = function (titl document.addEventListener("DOMContentLoaded", function (event) { updatefps(); - hostName.innerHTML = container.hostType + " • " + container.uuid + " • "+ desktopJS.version; + container = desktopJS.resolveContainer(); - $("#button-joingroup").prop("disabled", !container.getCurrentWindow().allowGrouping); - $("#button-leavegroup").prop("disabled", !container.getCurrentWindow().allowGrouping); + container.ready().then(() => { + hostName.innerHTML = container.hostType + " • " + container.uuid + " • " + desktopJS.version; - container.addListener("window-created", (e) => container.log("info", "Window created: " + e.window + ", " + e.windowId + ", " + e.windowName)); - container.addListener("layout-loaded", (e) => container.log("info", "Layout loaded")); - container.addListener("layout-saved", (e) => container.log("info", "Layout saved")); + $("#button-joingroup").prop("disabled", !container.getCurrentWindow().allowGrouping); + $("#button-leavegroup").prop("disabled", !container.getCurrentWindow().allowGrouping); - desktopJS.Container.addListener("window-created", (e) => container.log("info", "Window created - static (Container): " + e.windowId + ", " + e.windowName)); - desktopJS.ContainerWindow.addListener("window-created", (e) => container.log("info", "Window created - static (ContainerWindow): " + e.windowId + ", " + e.windowName)); - desktopJS.Container.addListener("layout-saved", (e) => container.log("info", "Layout saved - static: " + e.layoutName)); - desktopJS.Container.addListener("layout-loaded", (e) => container.log("info", "Layout loaded - static: " + e.layoutName)); + container.addListener("window-created", (e) => container.log("info", "Window created: " + e.window + ", " + e.windowId + ", " + e.windowName)); + container.addListener("layout-loaded", (e) => container.log("info", "Layout loaded")); + container.addListener("layout-saved", (e) => container.log("info", "Layout saved")); - desktopJS.ContainerWindow.addListener("window-joinGroup", (e) => container.log("info", "grouped " + JSON.stringify(e))); - desktopJS.ContainerWindow.addListener("window-leaveGroup", (e) => container.log("info", "ungrouped" + JSON.stringify(e))); + desktopJS.Container.addListener("window-created", (e) => container.log("info", "Window created - static (Container): " + e.windowId + ", " + e.windowName)); + desktopJS.ContainerWindow.addListener("window-created", (e) => container.log("info", "Window created - static (ContainerWindow): " + e.windowId + ", " + e.windowName)); + desktopJS.Container.addListener("layout-saved", (e) => container.log("info", "Layout saved - static: " + e.layoutName)); + desktopJS.Container.addListener("layout-loaded", (e) => container.log("info", "Layout loaded - static: " + e.layoutName)); - subscribe(); + desktopJS.ContainerWindow.addListener("window-joinGroup", (e) => container.log("info", "grouped " + JSON.stringify(e))); + desktopJS.ContainerWindow.addListener("window-leaveGroup", (e) => container.log("info", "ungrouped" + JSON.stringify(e))); - // If url is provided a hash, try to navigate to bootstrap tab if exists - var activeTab = $('[href="' + location.hash + '"]'); - activeTab && activeTab.tab('show'); + subscribe(); - // Enable popovers - $('[data-toggle="popover"]').popover(); + // If url is provided a hash, try to navigate to bootstrap tab if exists + var activeTab = $('[href="' + location.hash + '"]'); + activeTab && activeTab.tab('show'); - if (container.getCurrentWindow().id === "desktopJS") { - snapAssist = new desktopJS.SnapAssistWindowManager(container, - { - windowStateTracking: desktopJS.WindowStateTracking.Main | desktopJS.WindowStateTracking.Group - }); - } + // Enable popovers + $('[data-toggle="popover"]').popover(); + + if (container.getCurrentWindow().id === "desktopJS") { + snapAssist = new desktopJS.SnapAssistWindowManager(container, + { + windowStateTracking: desktopJS.WindowStateTracking.Main | desktopJS.WindowStateTracking.Group + }); + } + }); }); openWindowButton.onclick = function () { diff --git a/packages/desktopjs-openfin/src/openfin.ts b/packages/desktopjs-openfin/src/openfin.ts index 9cc87ce3..3e533d02 100644 --- a/packages/desktopjs-openfin/src/openfin.ts +++ b/packages/desktopjs-openfin/src/openfin.ts @@ -456,6 +456,10 @@ export class OpenFinContainer extends WebContainerBase { }); } + public ready(): Promise { + return new Promise(resolve => this.desktop.main(resolve)); + } + public getMainWindow(): ContainerWindow { if (!this.mainWindow) { this.mainWindow = this.wrapWindow(this.desktop.Application.getCurrent().getWindow()); diff --git a/packages/desktopjs-openfin/tests/openfin.spec.ts b/packages/desktopjs-openfin/tests/openfin.spec.ts index 95d8b02b..119209d1 100644 --- a/packages/desktopjs-openfin/tests/openfin.spec.ts +++ b/packages/desktopjs-openfin/tests/openfin.spec.ts @@ -21,6 +21,7 @@ class MockDesktop { } } } + main(callback): any { callback() }; GlobalHotkey: any = { }; Window: any = MockWindow; Notification(): any { return {}; } @@ -502,6 +503,13 @@ describe("OpenFinContainer", () => { expect(container.hostType).toEqual("OpenFin"); }); + it("ready invokes underlying main", (done) => { + spyOn(desktop, "main").and.callThrough(); + container.ready().then(() => { + expect(desktop.main).toHaveBeenCalled(); + }).then(done); + }); + describe("ctor options", () => { describe("registerUser", () => { let desktop; diff --git a/packages/desktopjs/src/container.ts b/packages/desktopjs/src/container.ts index cbff62a1..8680e5fe 100644 --- a/packages/desktopjs/src/container.ts +++ b/packages/desktopjs/src/container.ts @@ -48,6 +48,10 @@ export abstract class Container extends EventEmitter implements ContainerWindowM */ public abstract uuid: string; + public ready(): Promise { + return Promise.resolve(); + } + public abstract getMainWindow(): ContainerWindow; public abstract getCurrentWindow(): ContainerWindow; diff --git a/packages/desktopjs/tests/unit/container.spec.ts b/packages/desktopjs/tests/unit/container.spec.ts index 2a721bae..e7e6bc4c 100644 --- a/packages/desktopjs/tests/unit/container.spec.ts +++ b/packages/desktopjs/tests/unit/container.spec.ts @@ -97,6 +97,10 @@ describe("container", () => { expect(container.ipc).toBeDefined(); }); + it("ready resolves", (done) => { + container.ready().then(done); + }); + describe("Static events", () => { it("addListener adds callback to listeners", () => { expect(Container.listeners("TestEvent").length).toEqual(0);