perf&fix: some arrays to sets, defaulting state

This commit is contained in:
Fernando Fernández 2024-10-29 10:25:49 +00:00 committed by GitHub
parent 54f0f5b11f
commit dba7c34649
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 26 additions and 34 deletions

View File

@ -333,14 +333,14 @@ class RemotePluginAuth extends CommonStore<AuthState> {
}; };
public constructor() { public constructor() {
super('auth', { super('auth', () => ({
servers: [], servers: [],
currentServerIndex: -1, currentServerIndex: -1,
currentUserIndex: -1, currentUserIndex: -1,
users: [], users: [],
rememberMe: true, rememberMe: true,
accessTokens: {} accessTokens: {}
}, 'localStorage'); }), 'localStorage');
void this.refreshCurrentUserInfo(); void this.refreshCurrentUserInfo();
void this._refreshServers(); void this._refreshServers();
} }

View File

@ -81,11 +81,11 @@ class ClientSettingsStore extends SyncedStore<ClientSettingsState> {
}; };
public constructor() { public constructor() {
super('clientSettings', { super('clientSettings', () => ({
typography: 'default', typography: 'default',
darkMode: 'auto', darkMode: 'auto',
locale: 'auto' locale: 'auto'
}, 'localStorage'); }), 'localStorage');
/** /**
* == WATCHERS == * == WATCHERS ==
*/ */

View File

@ -34,20 +34,20 @@ class SubtitleSettingsStore extends SyncedStore<SubtitleSettingsState> {
public state = this._state; public state = this._state;
public constructor() { public constructor() {
super('subtitleSettings', { super('subtitleSettings', () => ({
enabled: false, enabled: false,
fontFamily: 'auto', fontFamily: 'auto',
fontSize: 1.5, fontSize: 1.5,
positionFromBottom: 10, positionFromBottom: 10,
backdrop: true, backdrop: true,
stroke: false stroke: false
}, 'localStorage', [ }), 'localStorage', new Set([
'enabled', 'enabled',
'fontSize', 'fontSize',
'positionFromBottom', 'positionFromBottom',
'backdrop', 'backdrop',
'stroke' 'stroke'
]); ]));
/** /**
* == WATCHERS == * == WATCHERS ==

View File

@ -1013,7 +1013,7 @@ class PlaybackManagerStore extends CommonStore<PlaybackManagerState> {
}; };
public constructor() { public constructor() {
super('playbackManager', { super('playbackManager', () => ({
status: PlaybackStatus.Stopped, status: PlaybackStatus.Stopped,
currentSourceUrl: undefined, currentSourceUrl: undefined,
currentItemIndex: undefined, currentItemIndex: undefined,
@ -1034,7 +1034,7 @@ class PlaybackManagerStore extends CommonStore<PlaybackManagerState> {
playbackInitiator: undefined, playbackInitiator: undefined,
playbackInitMode: InitMode.Unknown, playbackInitMode: InitMode.Unknown,
playbackSpeed: 1 playbackSpeed: 1
}); }));
/** /**
* Logic is divided by concerns and scope. Watchers for callbacks * Logic is divided by concerns and scope. Watchers for callbacks
* that rely on the same variables might not be together. Categories: * that rely on the same variables might not be together. Categories:

View File

@ -334,10 +334,10 @@ class PlayerElementStore extends CommonStore<PlayerElementState> {
}; };
public constructor() { public constructor() {
super('playerElement', { super('playerElement', () => ({
isStretched: false, isStretched: false,
currentExternalSubtitleTrack: undefined currentExternalSubtitleTrack: undefined
}); }));
/** /**
* * Move user to the fullscreen page when starting video playback by default * * Move user to the fullscreen page when starting video playback by default

View File

@ -15,12 +15,12 @@ export abstract class CommonStore<T extends object> {
} }
protected readonly _reset = (): void => { 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._storeKey = storeKey;
this._defaultState = () => defaultState; this._defaultState = defaultState;
let storage; let storage;

View File

@ -12,7 +12,7 @@ import { i18n } from '@/plugins/i18n';
export abstract class SyncedStore<T extends object> extends CommonStore<T> { export abstract class SyncedStore<T extends object> extends CommonStore<T> {
private readonly _clientSyncName = 'vue'; private readonly _clientSyncName = 'vue';
private readonly _syncedKeys: (keyof T)[] = []; private readonly _syncedKeys: Set<(keyof T)>;
private readonly _effectScope = new EffectScope(); private readonly _effectScope = new EffectScope();
/** /**
* Serializes custom pref values for storage as string * Serializes custom pref values for storage as string
@ -130,27 +130,19 @@ export abstract class SyncedStore<T extends object> extends CommonStore<T> {
* *
* @param keys - The keys to be synced with the server. If not provided, all keys will be synced * @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); super(storeKey, defaultState, persistence);
this._syncedKeys = keys ?? []; this._syncedKeys = keys ?? new Set(Object.keys(defaultState()) as (keyof T)[]);
if (!this._syncedKeys.length) { this._effectScope.run(() => {
for (const key in defaultState) { if (keys?.size) {
this._syncedKeys.push(key); for (const key of keys) {
}
}
if (keys?.length) {
for (const key of keys) {
this._effectScope.run(() => {
watchDeep(() => this._state[key], this._updateState); watchDeep(() => this._state[key], this._updateState);
}); }
} } else {
} else {
this._effectScope.run(() => {
watchDeep(() => this._state, this._updateState); watchDeep(() => this._state, this._updateState);
}); }
} });
/** /**
* Trigger sync when the user logs in * Trigger sync when the user logs in

View File

@ -106,10 +106,10 @@ class TaskManagerStore extends CommonStore<TaskManagerState> {
}; };
public constructor() { public constructor() {
super('taskManager', { super('taskManager', () => ({
tasks: [], tasks: [],
finishedTasksTimeout: 5000 finishedTasksTimeout: 5000
}, 'sessionStorage'); }), 'sessionStorage');
/** /**
* Handle refresh progress update for library items * Handle refresh progress update for library items