Extract onceDocumentLoaded to function

This commit is contained in:
Matt Bierner
2018-03-05 11:19:21 -08:00
parent 5ef492b88d
commit 08baa73de8
2 changed files with 15 additions and 10 deletions

View File

@@ -0,0 +1,12 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
export function onceDocumentLoaded(f: () => void) {
if (document.readyState === 'loading' || document.readyState === 'uninitialized') {
document.addEventListener('DOMContentLoaded', f);
} else {
f();
}
}

View File

@@ -5,6 +5,7 @@
import { getSettings } from './settings';
import { postCommand, postMessage } from './messaging';
import { onceDocumentLoaded } from './events';
// From https://remysharp.com/2010/07/21/throttling-function-calls
function throttle(fn: (x: any) => any, threshhold: any, scope?: any) {
@@ -182,7 +183,7 @@ var scrollDisabled = true;
const marker = new ActiveLineMarker();
const settings = getSettings();
function onLoad() {
onceDocumentLoaded(() => {
if (settings.scrollPreviewWithEditor) {
setTimeout(() => {
const initialLine = +settings.line;
@@ -192,7 +193,7 @@ function onLoad() {
}
}, 0);
}
}
});
const onUpdateView = (() => {
const doScroll = throttle((line: number) => {
@@ -208,14 +209,6 @@ const onUpdateView = (() => {
};
})();
if (document.readyState === 'loading' || document.readyState === 'uninitialized') {
document.addEventListener('DOMContentLoaded', onLoad);
} else {
onLoad();
}
window.addEventListener('resize', () => {
scrollDisabled = true;
}, true);