Skip to content

Commit

Permalink
feat: Changed initializing event timing
Browse files Browse the repository at this point in the history
  • Loading branch information
kyasbal committed Aug 14, 2017
1 parent 38c49ad commit fbaf9fb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
12 changes: 8 additions & 4 deletions src/Interface/GrimoireInterface.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
import GrimoireInterfaceImpl from "./GrimoireInterfaceImpl";
import GomlInterfaceImpl from "./GomlInterfaceImpl";
import GomlNode from "../Node/GomlNode";
import {GomlInterface, GrimoireInterface} from "../Base/Types";
import { GomlInterface, GrimoireInterface } from "../Base/Types";


const context = new GrimoireInterfaceImpl();

function obtainGomlInterface(query: string): GomlInterface;
function obtainGomlInterface(query: GomlNode[]): GomlInterface;
function obtainGomlInterface(callback: (scriptTags: HTMLScriptElement[]) => void): void;
function obtainGomlInterface(query: string | GomlNode[] | ((scriptTags: HTMLScriptElement[]) => void)): void | GomlInterface {
function obtainGomlInterface(callback: () => void): void;
function obtainGomlInterface(query: string | GomlNode[] | (() => void)): void | GomlInterface {
if (typeof query === "string") {
const gomlContext = new GomlInterfaceImpl(context.queryRootNodes(query));
const queryFunc = gomlContext.queryFunc.bind(gomlContext);
Object.setPrototypeOf(queryFunc, gomlContext);
return queryFunc;
} else if (typeof query === "function") {
context.initializedEventHandler.push(query);
if (context.callInitializedAlready) {
query();
} else {
context.initializedEventHandler.push(query);
}
} else {
const gomlContext = new GomlInterfaceImpl(query);
const queryFunc = gomlContext.queryFunc.bind(gomlContext);
Expand Down
14 changes: 9 additions & 5 deletions src/Interface/GrimoireInterfaceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import NSIdentity from "../Base/NSIdentity";
import Namespace from "../Base/Namespace";
import NSDictionary from "../Base/NSDictionary";
import Ensure from "../Base/Ensure";
import {Name, Nullable, Ctor, ComponentRegistering} from "../Base/Types";
import { Name, Nullable, Ctor, ComponentRegistering } from "../Base/Types";

export default class GrimoireInterfaceImpl extends EEObject {

Expand Down Expand Up @@ -64,6 +64,10 @@ export default class GrimoireInterfaceImpl extends EEObject {
return GomlLoader.initializedEventHandlers;
}

public get callInitializedAlready(): boolean {
return GomlLoader.callInitializedAlready;
}

/**
* [obsolete] use `Namespace.define` instead of.
* @param {string} ns namespace URI to be used
Expand Down Expand Up @@ -216,9 +220,9 @@ export default class GrimoireInterfaceImpl extends EEObject {
id: rootNode.id
});
// send events to catch root node appended
this.emit("root-node-added",{
ownerScriptTag:tag,
rootNode:rootNode
this.emit("root-node-added", {
ownerScriptTag: tag,
rootNode: rootNode
});
return rootNode.id;
}
Expand Down Expand Up @@ -369,7 +373,7 @@ export default class GrimoireInterfaceImpl extends EEObject {

private _ensureTobeNSIdentityOnRegister(name: Name): NSIdentity;
private _ensureTobeNSIdentityOnRegister(name: null | undefined): null;
private _ensureTobeNSIdentityOnRegister(name: Name |null | undefined): Nullable<NSIdentity> {
private _ensureTobeNSIdentityOnRegister(name: Name | null | undefined): Nullable<NSIdentity> {
if (!name) {
return null;
}
Expand Down
7 changes: 5 additions & 2 deletions src/Node/GomlLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import XMLHttpRequestAsync from "../Base/XMLHttpRequestAsync";
* Provides the features to fetch Goml source.
*/
class GomlLoader {
public static initializedEventHandlers: ((scriptTags: HTMLScriptElement[]) => void)[] = [];
public static callInitializedAlready = false;

public static initializedEventHandlers: (() => void)[] = [];

/**
* Obtain the Goml source from specified tag.
Expand Down Expand Up @@ -48,8 +50,9 @@ class GomlLoader {
}
await Promise.all<void>(pArray);
GomlLoader.initializedEventHandlers.forEach(handler => {
handler(elements);
handler();
});
this.callInitializedAlready = true;
}

/**
Expand Down

0 comments on commit fbaf9fb

Please sign in to comment.