2020-05-25 15:57:47 +00:00
|
|
|
import * as plugins from './domtools.plugins';
|
|
|
|
import { Stringmap, FastMap } from '@pushrocks/lik';
|
|
|
|
|
|
|
|
export class DomTools {
|
|
|
|
public static async setupDomTools() {
|
|
|
|
let domToolsInstance: DomTools;
|
|
|
|
if (!globalThis.deesDomTools) {
|
|
|
|
globalThis.deesDomTools = new DomTools();
|
|
|
|
domToolsInstance = globalThis.deesDomTools;
|
|
|
|
// lets make sure the dom is ready
|
|
|
|
const readyStateChangedFunc = () => {
|
|
|
|
if (document.readyState === 'interactive' || document.readyState === 'complete') {
|
|
|
|
domToolsInstance.headElement = document.querySelector('head');
|
|
|
|
domToolsInstance.bodyElement = document.querySelector('body');
|
|
|
|
domToolsInstance.domReady.resolve();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
document.addEventListener('readystatechange', readyStateChangedFunc);
|
|
|
|
domToolsInstance.domToolsReady.resolve();
|
|
|
|
} else {
|
|
|
|
domToolsInstance = globalThis.deesDomTools;
|
|
|
|
}
|
|
|
|
await domToolsInstance.domToolsReady.promise;
|
|
|
|
return domToolsInstance;
|
2020-05-23 16:55:36 +00:00
|
|
|
}
|
2020-05-25 15:57:47 +00:00
|
|
|
|
|
|
|
public domToolsReady = plugins.smartpromise.defer();
|
|
|
|
public domReady = plugins.smartpromise.defer();
|
|
|
|
public globalStylesReady = plugins.smartpromise.defer();
|
|
|
|
|
|
|
|
// elements
|
|
|
|
public headElement: HTMLElement;
|
|
|
|
public bodyElement: HTMLElement;
|
|
|
|
|
|
|
|
public async setGlobalStyles(stylesText: string) {
|
|
|
|
await this.domReady.promise;
|
|
|
|
const styleElement = document.createElement('style');
|
|
|
|
styleElement.type = 'text/css';
|
|
|
|
styleElement.appendChild(document.createTextNode(stylesText));
|
|
|
|
this.headElement.appendChild(styleElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
private runOnceTrackerStringMap = new Stringmap();
|
|
|
|
private runOnceResultMap = new FastMap();
|
|
|
|
public async runOnce<T>(identifierArg: string, funcArg: () => Promise<T>) {
|
|
|
|
const runningId = `${identifierArg}+runningCheck`;
|
|
|
|
if(!this.runOnceTrackerStringMap.checkString(identifierArg)) {
|
|
|
|
this.runOnceTrackerStringMap.addString(identifierArg);
|
|
|
|
this.runOnceTrackerStringMap.addString(runningId);
|
|
|
|
const result = await funcArg();
|
|
|
|
this.runOnceResultMap.addToMap(identifierArg, result);
|
|
|
|
this.runOnceTrackerStringMap.removeString(runningId);
|
|
|
|
}
|
|
|
|
return await this.runOnceTrackerStringMap.registerUntilTrue(stringMap => {
|
|
|
|
return !stringMap.includes(runningId);
|
|
|
|
}, () => {
|
|
|
|
return this.runOnceResultMap.getByKey(identifierArg);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-23 16:55:36 +00:00
|
|
|
}
|