-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from /issues/33
Issues/33
- Loading branch information
Showing
9 changed files
with
128 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ on: | |
branches: [master] | ||
|
||
jobs: | ||
test: | ||
e2e-test: | ||
timeout-minutes: 60 | ||
runs-on: ubuntu-latest | ||
steps: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
const PAGE_ELEMENT_CLASS_NAME = 'page'; | ||
const APP_LOAD_TIMEOUT = 30; // seconds | ||
|
||
export class AppLoader { | ||
private resolveLoadingPromise: (() => void) | null = null; | ||
private observer: MutationObserver; | ||
|
||
constructor(private logger: ILogger) { | ||
this.observer = new MutationObserver(this.mutationCallback); | ||
this.observer.observe(document.body, { childList: true, subtree: true }); | ||
} | ||
|
||
public wait(): Promise<void> { | ||
return new Promise((res) => { | ||
if (document.querySelector(`.${PAGE_ELEMENT_CLASS_NAME}`)) { | ||
this.observer.disconnect(); | ||
res(); | ||
} else { | ||
this.startAppLoadWaiting(); | ||
this.resolveLoadingPromise = res; | ||
} | ||
}); | ||
} | ||
|
||
private mutationCallback: MutationCallback = ( | ||
mutations: MutationRecord[], | ||
observer: MutationObserver, | ||
): void => { | ||
for (const mutation of mutations) { | ||
for (const node of mutation.addedNodes) { | ||
if (!(node instanceof HTMLElement)) { | ||
return; | ||
} | ||
|
||
// Detect app load | ||
if ( | ||
this.resolveLoadingPromise !== null && | ||
(node.matches(`.${PAGE_ELEMENT_CLASS_NAME}`) || | ||
node.querySelector(`.${PAGE_ELEMENT_CLASS_NAME}`)) | ||
) { | ||
this.resolveLoadingPromise(); | ||
this.resolveLoadingPromise = null; | ||
this.observer.disconnect(); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
/** | ||
* Start a timer waiting for the app to load. | ||
*/ | ||
private startAppLoadWaiting() { | ||
let seconds = 0; | ||
|
||
const interval = setInterval(() => { | ||
seconds += 1; | ||
|
||
if (this.resolveLoadingPromise === null) { | ||
return clearInterval(interval); | ||
} | ||
|
||
if (document.querySelector(`.${PAGE_ELEMENT_CLASS_NAME}`)) { | ||
clearInterval(interval); | ||
this.resolveLoadingPromise(); | ||
this.resolveLoadingPromise = null; | ||
} | ||
|
||
if (seconds > APP_LOAD_TIMEOUT) { | ||
clearInterval(interval); | ||
this.logger.error( | ||
`Failed to wait for the app to load within ${APP_LOAD_TIMEOUT} seconds.`, | ||
); | ||
} | ||
}, 1 * 1000); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
interface IDomManager { | ||
loadingApp(): Promise<void>; | ||
subscribe(callback: (node: HTMLElement) => void): void; | ||
subscribeToContentChanges(callback: (node: HTMLElement) => void): void; | ||
createHashtag(text: string): HTMLSpanElement; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters