Added null checks

This commit is contained in:
Eduard Urbach 2019-04-19 22:50:52 +09:00
parent 707233a422
commit d16197340d
8 changed files with 31 additions and 18 deletions

View File

@ -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()
}

View File

@ -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) {

View File

@ -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

View File

@ -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
}

View File

@ -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() {

View File

@ -91,6 +91,10 @@ export default class ServerEvents {
}
notificationCount(e: ServerEvent) {
if(!this.arn.notificationManager) {
return
}
this.arn.notificationManager.setCounter(parseInt(e.data))
}
}

View File

@ -77,7 +77,10 @@ export default class ServiceWorkerManager {
switch(message.type) {
case "new notification":
if(this.arn.notificationManager) {
this.arn.notificationManager.update()
}
break
// case "new content":

View File

@ -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)
}
}
}