Skip to content

Commit

Permalink
feat: Accepting library preference as a object on global that is init…
Browse files Browse the repository at this point in the history
…ialized before loading.

GrimoireJS.postponeLoading
  • Loading branch information
kyasbal committed Oct 18, 2017
1 parent 125e618 commit 6fc0163
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/Interface/GrimoireInterfaceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export default class GrimoireInterfaceImpl extends EEObject {

public nodeDictionary: { [nodeId: string]: GomlNode } = {};
public componentDictionary: { [componentId: string]: Component } = {};
public libraryPreference?: { [preference: string]: any };
public debug = true;

/**
Expand Down
34 changes: 27 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import GrimoireInterface from "./Interface/GrimoireInterface";
import GomlLoader from "./Node/GomlLoader";

interface IGrimoireWindow extends Window {
GrimoireJS: typeof GrimoireInterface;
gr?: typeof GrimoireInterface;
}

/**
* Provides procedures for initializing.
*/
Expand All @@ -15,9 +20,8 @@ class GrimoireInitializer {
GrimoireInitializer._notifyLibraryLoadingToWindow();
GrimoireInitializer._copyGLConstants();
GrimoireInterface.initialize();
if (document.readyState === "loading") {
await GrimoireInitializer._waitForDOMLoading();
}
await GrimoireInitializer._waitForDOMLoading();
await GrimoireInitializer._waitForPluginLoadingSuspendPromise();
GrimoireInitializer._logVersions();
await GrimoireInterface.resolvePlugins();
await GomlLoader.loadForPage();
Expand Down Expand Up @@ -51,9 +55,13 @@ class GrimoireInitializer {
*/
private static _waitForDOMLoading(): Promise<void> {
return new Promise<void>((resolve) => {
window.addEventListener("DOMContentLoaded", () => {
if (document.readyState === "loading") {
window.addEventListener("DOMContentLoaded", () => {
resolve();
});
} else {
resolve();
});
}
});
}

Expand All @@ -79,13 +87,25 @@ class GrimoireInitializer {
$messageType: "library-loading"
}, "*");
}

private static async _waitForPluginLoadingSuspendPromise(): Promise<void> {
if (!GrimoireInterface.libraryPreference) {
return;
}
await (GrimoireInterface.libraryPreference["suspendForLoading"] as Promise<void>);
}
}

/**
* Just start the process.
*/
export default function (): typeof GrimoireInterface {
const gwin = window as IGrimoireWindow;
if (gwin.GrimoireJS) {
GrimoireInterface.libraryPreference = gwin.GrimoireJS;
}
gwin.gr = gwin.GrimoireJS = GrimoireInterface;
GrimoireInitializer.initialize();
GrimoireInterface.noConflictPreserve = (window as any)["gr"];
return (window as any)["gr"] = (window as any)["GrimoireJS"] = GrimoireInterface;
GrimoireInterface.noConflictPreserve = gwin.gr;
return GrimoireInterface;
}

0 comments on commit 6fc0163

Please sign in to comment.