From d16197340dff8c86e1548b648b708dc43545f89a Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Fri, 19 Apr 2019 22:50:52 +0900 Subject: [PATCH] Added null checks --- scripts/AnimeNotifier.ts | 15 ++++++++++----- scripts/Application.ts | 2 +- scripts/AudioPlayer.ts | 4 ++-- scripts/DateView.ts | 2 +- scripts/NotificationManager.ts | 6 +++--- scripts/ServerEvents.ts | 4 ++++ scripts/ServiceWorkerManager.ts | 5 ++++- scripts/Utils/swapElements.ts | 11 ++++++----- 8 files changed, 31 insertions(+), 18 deletions(-) diff --git a/scripts/AnimeNotifier.ts b/scripts/AnimeNotifier.ts index 75356998..eed08b26 100644 --- a/scripts/AnimeNotifier.ts +++ b/scripts/AnimeNotifier.ts @@ -27,7 +27,7 @@ export default class AnimeNotifier { visibilityObserver: IntersectionObserver pushManager: PushManager serviceWorkerManager: ServiceWorkerManager - notificationManager: NotificationManager + notificationManager: NotificationManager | undefined touchController: TouchController audioPlayer: AudioPlayer videoPlayer: VideoPlayer @@ -126,8 +126,8 @@ export default class AnimeNotifier { // Status message this.statusMessage = new StatusMessage( - document.getElementById("status-message"), - document.getElementById("status-message-text") + document.getElementById("status-message") as HTMLElement, + document.getElementById("status-message-text") as HTMLElement ) this.app.onError = (error: Error) => { @@ -138,7 +138,12 @@ export default class AnimeNotifier { this.pushManager = new PushManager() // Notification manager - this.notificationManager = new NotificationManager() + if(this.user) { + this.notificationManager = new NotificationManager( + document.getElementById("notification-icon") as HTMLElement, + document.getElementById("notification-count") as HTMLElement + ) + } // Audio player this.audioPlayer = new AudioPlayer(this) @@ -248,7 +253,7 @@ export default class AnimeNotifier { } // Notification manager - if(this.user) { + if(this.notificationManager) { this.notificationManager.update() } diff --git a/scripts/Application.ts b/scripts/Application.ts index ef037b1a..19f6a127 100644 --- a/scripts/Application.ts +++ b/scripts/Application.ts @@ -257,7 +257,7 @@ export default class Application { } scrollToTop() { - let parent : HTMLElement | null = this.content + let parent: any = this.content Diff.mutations.queue(() => { while(parent = parent.parentElement) { diff --git a/scripts/AudioPlayer.ts b/scripts/AudioPlayer.ts index 26c285b4..0f8df755 100644 --- a/scripts/AudioPlayer.ts +++ b/scripts/AudioPlayer.ts @@ -6,7 +6,7 @@ export default class AudioPlayer { // Web audio audioContext: AudioContext - audioNode: AudioBufferSourceNode + audioNode: AudioBufferSourceNode | null gainNode: GainNode // Parameters @@ -30,7 +30,7 @@ export default class AudioPlayer { constructor(arn: AnimeNotifier) { this.arn = arn - this.audioPlayer = document.getElementById("audio-player") + this.audioPlayer = document.getElementById("audio-player") as HTMLElement this.audioPlayerPlay = document.getElementById("audio-player-play") as HTMLButtonElement this.audioPlayerPause = document.getElementById("audio-player-pause") as HTMLButtonElement this.trackLink = document.getElementById("audio-player-track-title") as HTMLLinkElement diff --git a/scripts/DateView.ts b/scripts/DateView.ts index 0959cc1f..d769b4d4 100644 --- a/scripts/DateView.ts +++ b/scripts/DateView.ts @@ -103,7 +103,7 @@ export function displayAiringDate(element: HTMLElement, now: Date) { } export function displayDate(element: HTMLElement, now: Date) { - if(element.dataset.date === "") { + if(!element.dataset.date) { element.textContent = "" return } diff --git a/scripts/NotificationManager.ts b/scripts/NotificationManager.ts index 86d4e248..5cae3f99 100644 --- a/scripts/NotificationManager.ts +++ b/scripts/NotificationManager.ts @@ -5,9 +5,9 @@ export default class NotificationManager { icon: HTMLElement counter: HTMLElement - constructor() { - this.icon = document.getElementById("notification-icon") - this.counter = document.getElementById("notification-count") + constructor(icon: HTMLElement, counter: HTMLElement) { + this.icon = icon + this.counter = counter } async update() { diff --git a/scripts/ServerEvents.ts b/scripts/ServerEvents.ts index 43a3e8db..4b56a055 100644 --- a/scripts/ServerEvents.ts +++ b/scripts/ServerEvents.ts @@ -91,6 +91,10 @@ export default class ServerEvents { } notificationCount(e: ServerEvent) { + if(!this.arn.notificationManager) { + return + } + this.arn.notificationManager.setCounter(parseInt(e.data)) } } \ No newline at end of file diff --git a/scripts/ServiceWorkerManager.ts b/scripts/ServiceWorkerManager.ts index 8cd7a5ff..b4f4157b 100644 --- a/scripts/ServiceWorkerManager.ts +++ b/scripts/ServiceWorkerManager.ts @@ -77,7 +77,10 @@ export default class ServiceWorkerManager { switch(message.type) { case "new notification": - this.arn.notificationManager.update() + if(this.arn.notificationManager) { + this.arn.notificationManager.update() + } + break // case "new content": diff --git a/scripts/Utils/swapElements.ts b/scripts/Utils/swapElements.ts index 255d8d37..dcfc8113 100644 --- a/scripts/Utils/swapElements.ts +++ b/scripts/Utils/swapElements.ts @@ -1,22 +1,23 @@ +// swapElements assumes that both elements have valid parent nodes. export function swapElements(a: Node, b: Node) { - let parent = b.parentNode + let bParent = b.parentNode as Node let bNext = b.nextSibling // Special case for when a is the next sibling of b if(bNext === a) { // Just put a before b - parent.insertBefore(a, b) + bParent.insertBefore(a, b) } else { // Insert b right before a - a.parentNode.insertBefore(b, a) + (a.parentNode as Node).insertBefore(b, a) // Now insert a where b was if(bNext) { // If there was an element after b, then insert a right before that - parent.insertBefore(a, bNext) + bParent.insertBefore(a, bNext) } else { // Otherwise just append it as the last child - parent.appendChild(a) + bParent.appendChild(a) } } } \ No newline at end of file