Skip to content

Commit

Permalink
feat: adapter init
Browse files Browse the repository at this point in the history
  • Loading branch information
twlite committed Jun 17, 2024
1 parent 544a586 commit 92644be
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { unsafe } from './common/types';
import { getPlayerAdapterContext } from './context';
import { Player } from './player';
import { Player } from './Player';

export type OnGatewayPacket = (packet: unsafe) => void;

Expand Down Expand Up @@ -38,9 +37,11 @@ export interface AdapterImpl {
}

export class Adapter<T> implements AdapterImpl {
public readonly player: Player<T>;
public constructor(private readonly config: IAdapter<T>) {
this.player = getPlayerAdapterContext<T>();
private player!: Player<T>;
public constructor(private readonly config: IAdapter<T>) {}

public setPlayer(player: Player<T>): void {
this.player = player;
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down
13 changes: 5 additions & 8 deletions packages/discord-player/src/Player.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import type { IAdapter } from './adapter';
import { setPlayerAdapterContext } from './context';
import type { Adapter } from './Adapter';

export class Player<T> {
public readonly adapter: IAdapter<T>;
public constructor(adapter: PlayerAdapterInterface<T>) {
this.adapter = setPlayerAdapterContext(this, adapter);
public constructor(public readonly adapter: Adapter<T>) {
this.adapter = adapter;
this.adapter.setPlayer(this);
}
}

export type PlayerAdapterInterface<T> = () => IAdapter<T>;

export function createPlayer<T>(adapter: PlayerAdapterInterface<IAdapter<T>>) {
export function createPlayer<T>(adapter: Adapter<T>) {
return new Player(adapter);
}
17 changes: 0 additions & 17 deletions packages/discord-player/src/context.ts

This file was deleted.

Empty file.
48 changes: 48 additions & 0 deletions packages/discord-player/src/structures/ArrayList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { randomInt } from 'crypto';

export class ArrayList<T> extends Array<T> {
private _prev: T[] = [];

public random() {
return this[randomInt(this.length)];
}

public flush() {
this._prev = [];
}

public canUnshuffle() {
return this._prev.length > 0;
}

public shuffle(inPlace = true) {
const len = this.length;

if (inPlace) {
this._prev = this.slice();
} else {
this._prev = [];
}

const list = inPlace ? this : this.slice();

for (let i = 0; i < len; i++) {
const j = Math.floor(Math.random() * len);
[list[i], list[j]] = [list[j], list[i]];
}
}

public unshuffle() {
if (!this._prev.length) return false;

this.splice(0, this.length, ...this._prev);
this._prev = [];

return true;
}

public clear(): void {
this.flush();
this.length = 0;
}
}
15 changes: 15 additions & 0 deletions packages/discord-player/src/structures/Queue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ArrayList } from './ArrayList';

export class Queue<T> extends ArrayList<T> {
public add(...items: T[]): void {
this.push(...items);
}

public next(): T | undefined {
return this.shift();
}

public peek(): T | undefined {
return this[0];
}
}
15 changes: 15 additions & 0 deletions packages/discord-player/src/structures/Stack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ArrayList } from './ArrayList';

export class Stack<T> extends ArrayList<T> {
public add(...items: T[]): void {
this.unshift(...items);
}

public next(): T | undefined {
return this.shift();
}

public peek(): T | undefined {
return this[0];
}
}

0 comments on commit 92644be

Please sign in to comment.