diff --git a/frontend/src/store/playback-manager.ts b/frontend/src/store/playback-manager.ts index ba417a79..696f67ee 100644 --- a/frontend/src/store/playback-manager.ts +++ b/frontend/src/store/playback-manager.ts @@ -35,7 +35,7 @@ import playbackProfile from '@/utils/playback-profiles'; import { msToTicks } from '@/utils/time'; import { mediaControls, mediaElementRef } from '@/store'; import { CommonStore } from '@/store/super/common-store'; -import { genericWorker } from '@/plugins/workers'; +import { shuffle } from '@/utils/data-manipulation'; /** * == INTERFACES AND TYPES == @@ -173,7 +173,7 @@ class PlaybackManagerStore extends CommonStore { * Get reactive BaseItemDto's objects of the queue */ public get queue(): BaseItemDto[] { - return apiStore.getItemsById(this._state.queue) as BaseItemDto[] ?? []; + return apiStore.getItemsById(this._state.queue) as BaseItemDto[]; } /** @@ -776,7 +776,7 @@ class PlaybackManagerStore extends CommonStore { this._state.originalQueue = []; this._state.isShuffling = false; } else { - const queue = await genericWorker.shuffle(toRaw(this._state.queue)); + const queue = await shuffle(this._state.queue); this._state.originalQueue = this._state.queue; diff --git a/frontend/src/utils/data-manipulation.ts b/frontend/src/utils/data-manipulation.ts index 121379e3..4fd8670e 100644 --- a/frontend/src/utils/data-manipulation.ts +++ b/frontend/src/utils/data-manipulation.ts @@ -1,4 +1,6 @@ import { defu } from 'defu'; +import { toRaw } from 'vue'; +import { genericWorker } from '@/plugins/workers'; /** * Merge 2 objects, excluding the keys from the destination that are not present in source @@ -68,3 +70,13 @@ export function pick(object: T, keys: (keyof T)[]): Partial return res; } + +/** + * Shuffles an array in a WebWorker using the Durstenfeld shuffle algorithm, an + * optimized version of Fisher-Yates shuffle. + * + * It's also prepared for the case when the array is reactive thorugh Vue's `ref` or `reactive`. + */ +export async function shuffle(array: T[]): Promise { + return await genericWorker.shuffle(toRaw(array)) as T[]; +} diff --git a/frontend/src/utils/validation.ts b/frontend/src/utils/validation.ts index 9f85857c..75f77123 100644 --- a/frontend/src/utils/validation.ts +++ b/frontend/src/utils/validation.ts @@ -31,7 +31,7 @@ export function isStr(value: unknown): value is string { /** * Check if the given value is a funcion */ -export function isFunc unknown>(value: unknown): value is T { +export function isFunc(value: unknown): value is (...args: unknown[]) => unknown { return typeof value === 'function'; } @@ -82,7 +82,7 @@ export function isArray(object: unknown): object is unknown[] { * * @type TypeScript Decorator */ -export function sealed unknown>(constructor: T): void { +export function sealed(constructor: new (...args: never[]) => object): void { Object.seal(constructor); Object.seal(constructor.prototype); }