Skip to content

Commit

Permalink
perf&fix: some arrays to sets, defaulting state
Browse files Browse the repository at this point in the history
  • Loading branch information
ferferga authored Oct 29, 2024
1 parent 54f0f5b commit dba7c34
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 34 deletions.
4 changes: 2 additions & 2 deletions frontend/src/plugins/remote/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/store/client-settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ class ClientSettingsStore extends SyncedStore<ClientSettingsState> {
};

public constructor() {
super('clientSettings', {
super('clientSettings', () => ({
typography: 'default',
darkMode: 'auto',
locale: 'auto'
}, 'localStorage');
}), 'localStorage');
/**
* == WATCHERS ==
*/
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/store/client-settings/subtitle-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ==
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/store/playback-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,7 @@ class PlaybackManagerStore extends CommonStore<PlaybackManagerState> {
};

public constructor() {
super('playbackManager', {
super('playbackManager', () => ({
status: PlaybackStatus.Stopped,
currentSourceUrl: undefined,
currentItemIndex: undefined,
Expand All @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/store/player-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/store/super/common-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
28 changes: 10 additions & 18 deletions frontend/src/store/super/synced-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/store/task-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit dba7c34

Please sign in to comment.