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() {
super('auth', {
super('auth', () => ({
servers: [],
currentServerIndex: -1,
currentUserIndex: -1,
users: [],
rememberMe: true,
accessTokens: {}
}, 'localStorage');
}), 'localStorage');
void this.refreshCurrentUserInfo();
void this._refreshServers();
}

View File

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

View File

@ -34,20 +34,20 @@ class SubtitleSettingsStore extends SyncedStore<SubtitleSettingsState> {
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 ==

View File

@ -1013,7 +1013,7 @@ class PlaybackManagerStore extends CommonStore<PlaybackManagerState> {
};
public constructor() {
super('playbackManager', {
super('playbackManager', () => ({
status: PlaybackStatus.Stopped,
currentSourceUrl: undefined,
currentItemIndex: undefined,
@ -1034,7 +1034,7 @@ class PlaybackManagerStore extends CommonStore<PlaybackManagerState> {
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:

View File

@ -334,10 +334,10 @@ class PlayerElementStore extends CommonStore<PlayerElementState> {
};
public constructor() {
super('playerElement', {
super('playerElement', () => ({
isStretched: false,
currentExternalSubtitleTrack: undefined
});
}));
/**
* * 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 => {
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;

View File

@ -12,7 +12,7 @@ import { i18n } from '@/plugins/i18n';
export abstract class SyncedStore<T extends object> extends CommonStore<T> {
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<T extends object> extends CommonStore<T> {
*
* @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

View File

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