Skip to content

Commit

Permalink
Add option to change start index
Browse files Browse the repository at this point in the history
  • Loading branch information
shimizudev committed Nov 14, 2024
1 parent 3bafffc commit 0eb7456
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 25 deletions.
3 changes: 2 additions & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@
"useBlockStatements": "error",
"useShorthandArrayType": "error",
"useTemplate": "error",
"noShoutyConstants": "error"
"noShoutyConstants": "error",
"noNonNullAssertion": "off"
},
"suspicious": {
"recommended": true,
Expand Down
3 changes: 2 additions & 1 deletion src/models/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface PlayerConfig {
autoPlay?: boolean;
autoLeave?: boolean;
node?: string;
queueStartIndex?: number;
}

export enum PlayerState {
Expand Down Expand Up @@ -67,7 +68,7 @@ export class LilyPlayer {
this.loop = config.loop || PlayerLoop.OFF;
this.autoPlay = config.autoPlay || false;
this.autoLeave = config.autoLeave || false;
this.queue = new LilyQueue();
this.queue = new LilyQueue(config.queueStartIndex ?? 0);
this.node = this.manager.nodes.get(config.node as string) as LilyNode;
}

Expand Down
50 changes: 27 additions & 23 deletions src/models/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,29 @@ export class LilyQueue {
private head: QueueNode | null;
private tail: QueueNode | null;
private nodeCount: number;
private startIndex: number;

constructor() {
constructor(startIndex = 0) {
this.tracks = new Set<LilyTrack>();
this.head = null;
this.tail = null;
this.nodeCount = 0;
this.startIndex = Math.max(0, startIndex);
}

public setStartIndex(index: number): void {
this.startIndex = Math.max(0, index);
}

public getStartIndex(): number {
return this.startIndex;
}

public add(track: LilyTrack): boolean {
try {
const node = new QueueNode(track);
if (this.head) {
node.prev = this.tail;
// biome-ignore lint/style/noNonNullAssertion: <explanation>
this.tail!.next = node;
this.tail = node;
} else {
Expand All @@ -42,16 +51,15 @@ export class LilyQueue {
}

public get(position: number): LilyTrack {
if (position < 0 || position >= this.nodeCount) {
const adjustedPosition = position - this.startIndex;
if (adjustedPosition < 0 || adjustedPosition >= this.nodeCount) {
throw new Error('Position out of bounds');
}

let current = this.head;
for (let i = 0; i < position; i++) {
// biome-ignore lint/style/noNonNullAssertion: <explanation>
for (let i = 0; i < adjustedPosition; i++) {
current = current!.next;
}
// biome-ignore lint/style/noNonNullAssertion: <explanation>
return current!.value;
}

Expand All @@ -60,36 +68,29 @@ export class LilyQueue {
}

public remove(position: number): boolean {
if (position < 0 || position >= this.nodeCount) {
const adjustedPosition = position - this.startIndex;
if (adjustedPosition < 0 || adjustedPosition >= this.nodeCount) {
return false;
}

let current = this.head;
for (let i = 0; i < position; i++) {
// biome-ignore lint/style/noNonNullAssertion: <explanation>
for (let i = 0; i < adjustedPosition; i++) {
current = current!.next;
}

if (current === this.head) {
// biome-ignore lint/style/noNonNullAssertion: <explanation>
this.head = current!.next;
}
if (current === this.tail) {
// biome-ignore lint/style/noNonNullAssertion: <explanation>
this.tail = current!.prev;
}
// biome-ignore lint/style/noNonNullAssertion: <explanation>
if (current!.prev) {
// biome-ignore lint/style/noNonNullAssertion: <explanation>
current!.prev.next = current!.next;
}
// biome-ignore lint/style/noNonNullAssertion: <explanation>
if (current!.next) {
// biome-ignore lint/style/noNonNullAssertion: <explanation>
current!.next.prev = current!.prev;
}

// biome-ignore lint/style/noNonNullAssertion: <explanation>
this.tracks.delete(current!.value);
this.nodeCount--;
return true;
Expand Down Expand Up @@ -167,7 +168,6 @@ export class LilyQueue {
return true;
}

// Fisher-Yates shuffle using linked list
const positions: QueueNode[] = [];
let current = this.head;
while (current) {
Expand All @@ -193,26 +193,30 @@ export class LilyQueue {
public *values(): IterableIterator<LilyTrack> {
let current = this.head;
while (current) {
yield current.value;
yield { ...current.value };
current = current.next;
}
}

public forEach(callbackfn: (value: LilyTrack) => void): void {
public forEach(callbackfn: (value: LilyTrack, index: number) => void): void {
let current = this.head;
let index = this.startIndex;
while (current) {
callbackfn(current.value);
callbackfn(current.value, index);
current = current.next;
index++;
}
}

public map<T>(callbackfn: (value: LilyTrack) => T): T[] {
public map<T>(callbackfn: (value: LilyTrack, index: number) => T): T[] {
const result: T[] = new Array(this.nodeCount);
let current = this.head;
let index = 0;
let arrayIndex = 0;
let queueIndex = this.startIndex;
while (current) {
result[index++] = callbackfn(current.value);
result[arrayIndex++] = callbackfn(current.value, queueIndex);
current = current.next;
queueIndex++;
}
return result;
}
Expand Down
1 change: 1 addition & 0 deletions src/services/base-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ interface ManagerOptions {
readonly noReplace?: boolean;
readonly nodeLinkFeatures?: boolean;
readonly previousInArray?: boolean;
readonly queueStartIndex?: number;
}

interface PlaylistInfo {
Expand Down

0 comments on commit 0eb7456

Please sign in to comment.