refactor(utils.ts): add parameter arg0 to addWatcher callback for improved readability

This commit is contained in:
Sped0n
2023-11-02 12:27:58 +08:00
parent 8b48cceb8f
commit dd01dd8bec
5 changed files with 14 additions and 14 deletions

View File

@@ -4,8 +4,8 @@ export let container: HTMLDivElement
export function initContainer(): void { export function initContainer(): void {
container = document.getElementsByClassName('container').item(0) as HTMLDivElement container = document.getElementsByClassName('container').item(0) as HTMLDivElement
scrollable.addWatcher(() => { scrollable.addWatcher((o) => {
if (scrollable.get()) { if (o) {
container.classList.remove('disableScroll') container.classList.remove('disableScroll')
} else { } else {
container.classList.add('disableScroll') container.classList.add('disableScroll')

View File

@@ -38,8 +38,8 @@ export function initCustomCursor(): void {
// bind mousemove event to window // bind mousemove event to window
window.addEventListener('mousemove', onMouse, { passive: true }) window.addEventListener('mousemove', onMouse, { passive: true })
// add active callback // add active callback
active.addWatcher(() => { active.addWatcher((o) => {
if (active.get()) { if (o) {
cursor.classList.add('active') cursor.classList.add('active')
} else { } else {
cursor.classList.remove('active') cursor.classList.remove('active')

View File

@@ -211,13 +211,13 @@ export function initStage(ijs: ImageJSON[]): void {
}) })
window.addEventListener('mousemove', onMouse, { passive: true }) window.addEventListener('mousemove', onMouse, { passive: true })
// watchers // watchers
isOpen.addWatcher(() => { isOpen.addWatcher((o) => {
active.set(isOpen.get() && !isAnimating.get()) active.set(o && !isAnimating.get())
}) })
isAnimating.addWatcher(() => { isAnimating.addWatcher((o) => {
active.set(isOpen.get() && !isAnimating.get()) active.set(isOpen.get() && !o)
}) })
cordHist.addWatcher(() => { cordHist.addWatcher((_) => {
setPositions() setPositions()
}) })
// preload // preload

View File

@@ -93,8 +93,8 @@ export function initGallery(ijs: ImageJSON[]): void {
lastIndex = s.index lastIndex = s.index
}) })
// mounted watcher // mounted watcher
mounted.addWatcher(() => { mounted.addWatcher((o) => {
if (!mounted.get()) return if (!o) return
scrollable.set(true) scrollable.set(true)
swiper = new Swiper(swiperNode, { spaceBetween: 20 }) swiper = new Swiper(swiperNode, { spaceBetween: 20 })
swiper.on('slideChange', ({ realIndex }) => { swiper.on('slideChange', ({ realIndex }) => {

View File

@@ -42,7 +42,7 @@ export function onVisible<T extends Element>(
export class Watchable<T> { export class Watchable<T> {
constructor(private obj: T) {} constructor(private obj: T) {}
private readonly watchers: Array<() => void> = [] private readonly watchers: Array<(arg0: T) => void> = []
get(): T { get(): T {
return this.obj return this.obj
@@ -51,11 +51,11 @@ export class Watchable<T> {
set(e: T): void { set(e: T): void {
this.obj = e this.obj = e
this.watchers.forEach((watcher) => { this.watchers.forEach((watcher) => {
watcher() watcher(this.obj)
}) })
} }
addWatcher(watcher: () => void): void { addWatcher(watcher: (arg0: T) => void): void {
this.watchers.push(watcher) this.watchers.push(watcher)
} }
} }