From 5ef492b88dd8755fcfaa37427eab72e75486532b Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Mon, 5 Mar 2018 11:15:02 -0800 Subject: [PATCH] Move style loading monitor to class --- extensions/markdown/preview-src/loading.ts | 38 ++++++++++++---------- extensions/markdown/preview-src/pre.ts | 7 ++-- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/extensions/markdown/preview-src/loading.ts b/extensions/markdown/preview-src/loading.ts index f60cdf226d8..34692558774 100644 --- a/extensions/markdown/preview-src/loading.ts +++ b/extensions/markdown/preview-src/loading.ts @@ -4,24 +4,28 @@ *--------------------------------------------------------------------------------------------*/ import { postCommand } from './messaging'; -const unloadedStyles: string[] = []; +export class StyleLoadingMonitor { + private unloadedStyles: string[] = []; -const onStyleLoadError = (event: any) => { - const source = event.target.dataset.source; - unloadedStyles.push(source); -}; + constructor() { + const onStyleLoadError = (event: any) => { + const source = event.target.dataset.source; + this.unloadedStyles.push(source); + }; -window.addEventListener('DOMContentLoaded', () => { - for (const link of document.getElementsByClassName('code-user-style') as HTMLCollectionOf) { - if (link.dataset.source) { - link.onerror = onStyleLoadError; - } - } -}); + window.addEventListener('DOMContentLoaded', () => { + for (const link of document.getElementsByClassName('code-user-style') as HTMLCollectionOf) { + if (link.dataset.source) { + link.onerror = onStyleLoadError; + } + } + }); -window.addEventListener('load', () => { - if (!unloadedStyles.length) { - return; + window.addEventListener('load', () => { + if (!this.unloadedStyles.length) { + return; + } + postCommand('_markdown.onPreviewStyleLoadError', [this.unloadedStyles]); + }); } - postCommand('_markdown.onPreviewStyleLoadError', [unloadedStyles]); -}); \ No newline at end of file +} \ No newline at end of file diff --git a/extensions/markdown/preview-src/pre.ts b/extensions/markdown/preview-src/pre.ts index bf5ba852591..c9fd0ec5b35 100644 --- a/extensions/markdown/preview-src/pre.ts +++ b/extensions/markdown/preview-src/pre.ts @@ -3,8 +3,11 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import './loading'; import { CspAlerter } from './csp'; +import { StyleLoadingMonitor } from './loading'; // tslint:disable-next-line:no-unused-expression -new CspAlerter(); \ No newline at end of file +new CspAlerter(); + +// tslint:disable-next-line:no-unused-expression +new StyleLoadingMonitor(); \ No newline at end of file -- GitLab