From dba7c3464937fd4d1943137aebddd41789798c60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fernando=20Fern=C3=A1ndez?= Date: Tue, 29 Oct 2024 10:25:49 +0000 Subject: [PATCH] perf&fix: some arrays to sets, defaulting state --- frontend/src/plugins/remote/auth.ts | 4 +-- frontend/src/store/client-settings/index.ts | 4 +-- .../client-settings/subtitle-settings.ts | 6 ++-- frontend/src/store/playback-manager.ts | 4 +-- frontend/src/store/player-element.ts | 4 +-- frontend/src/store/super/common-store.ts | 6 ++-- frontend/src/store/super/synced-store.ts | 28 +++++++------------ frontend/src/store/task-manager.ts | 4 +-- 8 files changed, 26 insertions(+), 34 deletions(-) diff --git a/frontend/src/plugins/remote/auth.ts b/frontend/src/plugins/remote/auth.ts index e90a5a068f5..03fad8cfc12 100644 --- a/frontend/src/plugins/remote/auth.ts +++ b/frontend/src/plugins/remote/auth.ts @@ -333,14 +333,14 @@ class RemotePluginAuth extends CommonStore { }; public constructor() { - super('auth', { + super('auth', () => ({ servers: [], currentServerIndex: -1, currentUserIndex: -1, users: [], rememberMe: true, accessTokens: {} - }, 'localStorage'); + }), 'localStorage'); void this.refreshCurrentUserInfo(); void this._refreshServers(); } diff --git a/frontend/src/store/client-settings/index.ts b/frontend/src/store/client-settings/index.ts index dd7a23d8d2d..a5c1c765588 100644 --- a/frontend/src/store/client-settings/index.ts +++ b/frontend/src/store/client-settings/index.ts @@ -81,11 +81,11 @@ class ClientSettingsStore extends SyncedStore { }; public constructor() { - super('clientSettings', { + super('clientSettings', () => ({ typography: 'default', darkMode: 'auto', locale: 'auto' - }, 'localStorage'); + }), 'localStorage'); /** * == WATCHERS == */ diff --git a/frontend/src/store/client-settings/subtitle-settings.ts b/frontend/src/store/client-settings/subtitle-settings.ts index 5ccfe4b79fe..6393a66bfa2 100644 --- a/frontend/src/store/client-settings/subtitle-settings.ts +++ b/frontend/src/store/client-settings/subtitle-settings.ts @@ -34,20 +34,20 @@ class SubtitleSettingsStore extends SyncedStore { public state = this._state; public constructor() { - super('subtitleSettings', { + super('subtitleSettings', () => ({ enabled: false, fontFamily: 'auto', fontSize: 1.5, positionFromBottom: 10, backdrop: true, stroke: false - }, 'localStorage', [ + }), 'localStorage', new Set([ 'enabled', 'fontSize', 'positionFromBottom', 'backdrop', 'stroke' - ]); + ])); /** * == WATCHERS == diff --git a/frontend/src/store/playback-manager.ts b/frontend/src/store/playback-manager.ts index 99fb3c44091..bf24026d5c0 100644 --- a/frontend/src/store/playback-manager.ts +++ b/frontend/src/store/playback-manager.ts @@ -1013,7 +1013,7 @@ class PlaybackManagerStore extends CommonStore { }; public constructor() { - super('playbackManager', { + super('playbackManager', () => ({ status: PlaybackStatus.Stopped, currentSourceUrl: undefined, currentItemIndex: undefined, @@ -1034,7 +1034,7 @@ class PlaybackManagerStore extends CommonStore { playbackInitiator: undefined, playbackInitMode: InitMode.Unknown, playbackSpeed: 1 - }); + })); /** * Logic is divided by concerns and scope. Watchers for callbacks * that rely on the same variables might not be together. Categories: diff --git a/frontend/src/store/player-element.ts b/frontend/src/store/player-element.ts index aded6611888..553ae9eda2d 100644 --- a/frontend/src/store/player-element.ts +++ b/frontend/src/store/player-element.ts @@ -334,10 +334,10 @@ class PlayerElementStore extends CommonStore { }; public constructor() { - super('playerElement', { + super('playerElement', () => ({ isStretched: false, currentExternalSubtitleTrack: undefined - }); + })); /** * * Move user to the fullscreen page when starting video playback by default diff --git a/frontend/src/store/super/common-store.ts b/frontend/src/store/super/common-store.ts index 91c512665f9..c6b9a4b290f 100644 --- a/frontend/src/store/super/common-store.ts +++ b/frontend/src/store/super/common-store.ts @@ -15,12 +15,12 @@ export abstract class CommonStore { } protected readonly _reset = (): void => { - Object.assign(this._state, this._defaultState); + Object.assign(this._state, this._defaultState()); }; - protected constructor(storeKey: string, defaultState: T, persistence?: Persistence) { + protected constructor(storeKey: string, defaultState: () => T, persistence?: Persistence) { this._storeKey = storeKey; - this._defaultState = () => defaultState; + this._defaultState = defaultState; let storage; diff --git a/frontend/src/store/super/synced-store.ts b/frontend/src/store/super/synced-store.ts index eb7cb34ad7d..31aa3472a83 100644 --- a/frontend/src/store/super/synced-store.ts +++ b/frontend/src/store/super/synced-store.ts @@ -12,7 +12,7 @@ import { i18n } from '@/plugins/i18n'; export abstract class SyncedStore extends CommonStore { private readonly _clientSyncName = 'vue'; - private readonly _syncedKeys: (keyof T)[] = []; + private readonly _syncedKeys: Set<(keyof T)>; private readonly _effectScope = new EffectScope(); /** * Serializes custom pref values for storage as string @@ -130,27 +130,19 @@ export abstract class SyncedStore extends CommonStore { * * @param keys - The keys to be synced with the server. If not provided, all keys will be synced */ - protected constructor(storeKey: string, defaultState: T, persistence?: Persistence, keys?: (keyof T)[]) { + protected constructor(storeKey: string, defaultState: () => T, persistence?: Persistence, keys?: Set<(keyof T)>) { super(storeKey, defaultState, persistence); - this._syncedKeys = keys ?? []; + this._syncedKeys = keys ?? new Set(Object.keys(defaultState()) as (keyof T)[]); - if (!this._syncedKeys.length) { - for (const key in defaultState) { - this._syncedKeys.push(key); - } - } - - if (keys?.length) { - for (const key of keys) { - this._effectScope.run(() => { + this._effectScope.run(() => { + if (keys?.size) { + for (const key of keys) { watchDeep(() => this._state[key], this._updateState); - }); - } - } else { - this._effectScope.run(() => { + } + } else { watchDeep(() => this._state, this._updateState); - }); - } + } + }); /** * Trigger sync when the user logs in diff --git a/frontend/src/store/task-manager.ts b/frontend/src/store/task-manager.ts index 8ad365a5ad8..c6a5acb6a24 100644 --- a/frontend/src/store/task-manager.ts +++ b/frontend/src/store/task-manager.ts @@ -106,10 +106,10 @@ class TaskManagerStore extends CommonStore { }; public constructor() { - super('taskManager', { + super('taskManager', () => ({ tasks: [], finishedTasksTimeout: 5000 - }, 'sessionStorage'); + }), 'sessionStorage'); /** * Handle refresh progress update for library items