Skip to content

Commit 918be77

Browse files
author
NellowTCS
committed
make-pretty
1 parent 5f488ed commit 918be77

File tree

100 files changed

+1698
-723
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+1698
-723
lines changed

Build/src/core/engine/engine.ts

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import type {
99
} from "./types";
1010
import { KomorebiEvents } from "./events";
1111
import { StateMachine } from "./state";
12-
import { QueueManager, type WeightedRandomizer, type ShuffleMode } from "./queue";
12+
import {
13+
QueueManager,
14+
type WeightedRandomizer,
15+
type ShuffleMode,
16+
} from "./queue";
1317
import { Scheduler } from "./scheduler";
1418
import type { IAudioBackend } from "../../platform/audio";
1519

@@ -125,7 +129,7 @@ export class KomorebiEngine {
125129
}
126130

127131
this.queue.setCurrentIndex(
128-
this.queue.getTracks().findIndex((t) => t.id === track.id)
132+
this.queue.getTracks().findIndex((t) => t.id === track.id),
129133
);
130134

131135
this.stateMachine.transition("loading");
@@ -209,7 +213,9 @@ export class KomorebiEngine {
209213
const nextTrack = this.queue.getNextTrack(this.settings.smartShuffle);
210214
if (nextTrack) {
211215
const currentTrack = this.queue.getCurrentTrack();
212-
this.queue.setCurrentIndex(this.queue.getNextIndex(this.settings.smartShuffle));
216+
this.queue.setCurrentIndex(
217+
this.queue.getNextIndex(this.settings.smartShuffle),
218+
);
213219

214220
if (this.stateMachine.isPlaying()) {
215221
this.loadAndPlay(nextTrack);
@@ -239,7 +245,9 @@ export class KomorebiEngine {
239245
const prevTrack = this.queue.getPreviousTrack(this.settings.smartShuffle);
240246
if (prevTrack) {
241247
const currentTrack = this.queue.getCurrentTrack();
242-
this.queue.setCurrentIndex(this.queue.getPreviousIndex(this.settings.smartShuffle));
248+
this.queue.setCurrentIndex(
249+
this.queue.getPreviousIndex(this.settings.smartShuffle),
250+
);
243251

244252
if (this.stateMachine.isPlaying()) {
245253
this.loadAndPlay(prevTrack);
@@ -300,8 +308,11 @@ export class KomorebiEngine {
300308

301309
toggleRepeat(): void {
302310
const current = this.settings.repeat;
303-
this.settings.repeat = current === "off" ? "all" : current === "all" ? "one" : "off";
304-
this.events.emit("settingschange", { settings: { repeat: this.settings.repeat } });
311+
this.settings.repeat =
312+
current === "off" ? "all" : current === "all" ? "one" : "off";
313+
this.events.emit("settingschange", {
314+
settings: { repeat: this.settings.repeat },
315+
});
305316
}
306317

307318
setCrossfade(duration: number): void {
@@ -313,7 +324,9 @@ export class KomorebiEngine {
313324
setGapless(enabled: boolean): void {
314325
this.settings.gaplessPlayback = enabled;
315326
this.scheduler.setGaplessConfig({ enabled });
316-
this.events.emit("settingschange", { settings: { gaplessPlayback: enabled } });
327+
this.events.emit("settingschange", {
328+
settings: { gaplessPlayback: enabled },
329+
});
317330
}
318331

319332
updateSettings(newSettings: Partial<EngineSettings>): void {
@@ -372,14 +385,14 @@ export class KomorebiEngine {
372385

373386
on<E extends keyof EngineEventMap>(
374387
event: E,
375-
callback: (data: EngineEventMap[E]) => void
388+
callback: (data: EngineEventMap[E]) => void,
376389
): void {
377390
this.events.on(event, callback);
378391
}
379392

380393
off<E extends keyof EngineEventMap>(
381394
event: E,
382-
callback: (data: EngineEventMap[E]) => void
395+
callback: (data: EngineEventMap[E]) => void,
383396
): void {
384397
this.events.off(event, callback);
385398
}
@@ -396,17 +409,18 @@ export class KomorebiEngine {
396409
return new Promise((resolve, reject) => {
397410
const onEndedHandler = () => {
398411
this.backend?.offEnded(onEndedHandler);
399-
this.play()
400-
.then(resolve)
401-
.catch(reject);
412+
this.play().then(resolve).catch(reject);
402413
};
403414

404415
this.backend?.onEnded(onEndedHandler);
405416
this.load(track);
406417
});
407418
}
408419

409-
private async waitForState(targetState: PlayerState, timeout = 5000): Promise<void> {
420+
private async waitForState(
421+
targetState: PlayerState,
422+
timeout = 5000,
423+
): Promise<void> {
410424
return new Promise((resolve, reject) => {
411425
const checkState = () => {
412426
if (this.stateMachine.getState() === targetState) {
@@ -465,7 +479,8 @@ export class KomorebiEngine {
465479
const nextTrack = this.queue.getNextTrack(this.settings.smartShuffle);
466480
if (!nextTrack) return;
467481

468-
const delay = this.scheduler.getMode() === "gapless" ? 100 : this.settings.crossfade;
482+
const delay =
483+
this.scheduler.getMode() === "gapless" ? 100 : this.settings.crossfade;
469484

470485
this.scheduledTransitionId = window.setTimeout(() => {
471486
this.scheduledTransitionId = null;
@@ -475,7 +490,9 @@ export class KomorebiEngine {
475490

476491
private executeTransition(nextTrack: Track): void {
477492
const currentTrack = this.queue.getCurrentTrack();
478-
this.queue.setCurrentIndex(this.queue.getNextIndex(this.settings.smartShuffle));
493+
this.queue.setCurrentIndex(
494+
this.queue.getNextIndex(this.settings.smartShuffle),
495+
);
479496

480497
this.load(nextTrack);
481498
this.play();
@@ -544,15 +561,21 @@ export class KomorebiEngine {
544561
private emitStateChange(): void {
545562
const prevState = this.stateMachine.getPreviousState();
546563
const newState = this.stateMachine.getState();
547-
this.events.emit("statechange", { oldState: prevState ?? "idle", newState });
564+
this.events.emit("statechange", {
565+
oldState: prevState ?? "idle",
566+
newState,
567+
});
548568
}
549569

550570
private emitTrackChange(from: Track | null, to: Track | null): void {
551571
this.events.emit("trackchange", { from, to });
552572
}
553573

554574
private emitTimeUpdate(): void {
555-
this.events.emit("timeupdate", { currentTime: this.currentTime, duration: this.duration });
575+
this.events.emit("timeupdate", {
576+
currentTime: this.currentTime,
577+
duration: this.duration,
578+
});
556579
}
557580

558581
private emitError(code: string, message: string, track?: Track): void {
@@ -561,4 +584,4 @@ export class KomorebiEngine {
561584
this.events.emit("error", { error: this.currentError });
562585
this.emitStateChange();
563586
}
564-
}
587+
}

Build/src/core/engine/events.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,20 @@ type EventCallback<T> = (data: T) => void;
55
export class KomorebiEvents {
66
private listeners: Map<EngineEvent, Set<EventCallback<unknown>>> = new Map();
77

8-
on<E extends EngineEvent>(event: E, callback: EventCallback<EngineEventMap[E]>): void {
8+
on<E extends EngineEvent>(
9+
event: E,
10+
callback: EventCallback<EngineEventMap[E]>,
11+
): void {
912
if (!this.listeners.has(event)) {
1013
this.listeners.set(event, new Set());
1114
}
1215
this.listeners.get(event)!.add(callback as EventCallback<unknown>);
1316
}
1417

15-
off<E extends EngineEvent>(event: E, callback: EventCallback<EngineEventMap[E]>): void {
18+
off<E extends EngineEvent>(
19+
event: E,
20+
callback: EventCallback<EngineEventMap[E]>,
21+
): void {
1622
const callbacks = this.listeners.get(event);
1723
if (callbacks) {
1824
callbacks.delete(callback as EventCallback<unknown>);
@@ -26,7 +32,10 @@ export class KomorebiEvents {
2632
}
2733
}
2834

29-
once<E extends EngineEvent>(event: E, callback: EventCallback<EngineEventMap[E]>): void {
35+
once<E extends EngineEvent>(
36+
event: E,
37+
callback: EventCallback<EngineEventMap[E]>,
38+
): void {
3039
const wrapper: EventCallback<EngineEventMap[E]> = (data) => {
3140
this.off(event, wrapper);
3241
callback(data);
@@ -45,4 +54,4 @@ export class KomorebiEvents {
4554
listenerCount(event: EngineEvent): number {
4655
return this.listeners.get(event)?.size ?? 0;
4756
}
48-
}
57+
}

Build/src/core/engine/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,4 @@ export { StateMachine } from "./state";
55
export { QueueManager } from "./queue";
66
export { Scheduler, CrossfadeScheduler, GaplessScheduler } from "./scheduler";
77
export type { IAudioBackend } from "../../platform/audio";
8-
export type {
9-
ScheduledTransition,
10-
TransitionCurve,
11-
} from "./scheduler";
8+
export type { ScheduledTransition, TransitionCurve } from "./scheduler";

Build/src/core/engine/queue/index.ts

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,23 @@ export class QueueManager {
4444

4545
const tracks = [...playlist.songs];
4646
const currentTrack = preserveCurrent ? this.getCurrentTrack() : null;
47-
const newIndex = currentTrack ? tracks.findIndex((t) => t.id === currentTrack.id) : -1;
47+
const newIndex = currentTrack
48+
? tracks.findIndex((t) => t.id === currentTrack.id)
49+
: -1;
4850

4951
this.state.tracks = tracks;
50-
this.state.shuffleOrder = this.generateShuffleOrder(tracks.length, newIndex);
52+
this.state.shuffleOrder = this.generateShuffleOrder(
53+
tracks.length,
54+
newIndex,
55+
);
5156
this.state.currentIndex = newIndex >= 0 ? newIndex : -1;
5257
}
5358

5459
getCurrentTrack(): Track | null {
55-
if (this.state.currentIndex < 0 || this.state.currentIndex >= this.state.tracks.length) {
60+
if (
61+
this.state.currentIndex < 0 ||
62+
this.state.currentIndex >= this.state.tracks.length
63+
) {
5664
return null;
5765
}
5866
return this.state.tracks[this.state.currentIndex];
@@ -129,21 +137,24 @@ export class QueueManager {
129137

130138
shuffle(preserveCurrent = true): void {
131139
this.state.shuffled = true;
132-
140+
133141
if (this.shuffleMode === "smart" && this.weightedRandomizer) {
134142
this.state.shuffleOrder = this.generateSmartShuffleOrder(
135143
this.state.tracks.length,
136-
preserveCurrent ? this.state.currentIndex : -1
144+
preserveCurrent ? this.state.currentIndex : -1,
137145
);
138146
} else {
139147
this.state.shuffleOrder = this.generateShuffleOrder(
140148
this.state.tracks.length,
141-
preserveCurrent ? this.state.currentIndex : -1
149+
preserveCurrent ? this.state.currentIndex : -1,
142150
);
143151
}
144152
}
145153

146-
private generateSmartShuffleOrder(length: number, preserveIndex: number): number[] {
154+
private generateSmartShuffleOrder(
155+
length: number,
156+
preserveIndex: number,
157+
): number[] {
147158
if (length === 0) return [];
148159

149160
const trackIds = this.state.tracks.map((t) => t.id);
@@ -152,16 +163,17 @@ export class QueueManager {
152163

153164
while (availableIndices.length > 0) {
154165
const availableTrackIds = availableIndices.map((i) => trackIds[i]);
155-
const selectedId = this.weightedRandomizer!.getWeightedRandomTrack(availableTrackIds);
156-
166+
const selectedId =
167+
this.weightedRandomizer!.getWeightedRandomTrack(availableTrackIds);
168+
157169
if (!selectedId) break;
158-
170+
159171
const selectedIndex = trackIds.indexOf(selectedId);
160172
if (selectedIndex === -1) break;
161-
173+
162174
const actualIndex = availableIndices.indexOf(selectedIndex);
163175
if (actualIndex === -1) break;
164-
176+
165177
selectedIds.push(selectedId);
166178
availableIndices.splice(actualIndex, 1);
167179
}
@@ -187,7 +199,10 @@ export class QueueManager {
187199
return this.state.shuffled;
188200
}
189201

190-
private generateShuffleOrder(length: number, preserveIndex: number): number[] {
202+
private generateShuffleOrder(
203+
length: number,
204+
preserveIndex: number,
205+
): number[] {
191206
if (length === 0) return [];
192207

193208
const indices = Array.from({ length }, (_, i) => i);
@@ -200,7 +215,10 @@ export class QueueManager {
200215
if (preserveIndex >= 0 && preserveIndex < length) {
201216
const currentInShuffle = indices.indexOf(preserveIndex);
202217
if (currentInShuffle > 0) {
203-
[indices[0], indices[currentInShuffle]] = [indices[currentInShuffle], indices[0]];
218+
[indices[0], indices[currentInShuffle]] = [
219+
indices[currentInShuffle],
220+
indices[0],
221+
];
204222
}
205223
}
206224

@@ -221,8 +239,9 @@ export class QueueManager {
221239
}
222240

223241
if (this.history.size > this.maxHistorySize) {
224-
const sorted = Array.from(this.history.values())
225-
.sort((a, b) => a.lastPlayed - b.lastPlayed);
242+
const sorted = Array.from(this.history.values()).sort(
243+
(a, b) => a.lastPlayed - b.lastPlayed,
244+
);
226245
const toRemove = sorted.slice(0, this.history.size - this.maxHistorySize);
227246
toRemove.forEach((h) => this.history.delete(h.trackId));
228247
}
@@ -249,7 +268,8 @@ export class QueueManager {
249268
if (this.state.shuffled) {
250269
const newIndex = this.state.tracks.length - 1;
251270
this.state.shuffleOrder.push(newIndex);
252-
const insertPos = Math.floor(Math.random() * (this.state.shuffleOrder.length - 1)) + 1;
271+
const insertPos =
272+
Math.floor(Math.random() * (this.state.shuffleOrder.length - 1)) + 1;
253273
this.state.shuffleOrder.splice(insertPos, 0, newIndex);
254274
}
255275
}
@@ -276,4 +296,4 @@ export class QueueManager {
276296
this.state.currentIndex = -1;
277297
this.state.shuffleOrder = [];
278298
}
279-
}
299+
}

0 commit comments

Comments
 (0)