expo-popcore-old/components/sker/video-player/player-pool.ts

56 lines
1.2 KiB
TypeScript

const MAX_PLAYERS = 6;
class PlayerPool {
private activeCount = 0;
private waitingQueue: Array<() => void> = [];
canCreate(): boolean {
return this.activeCount < MAX_PLAYERS;
}
async acquire(): Promise<void> {
if (this.canCreate()) {
this.activeCount++;
if (__DEV__ && this.activeCount >= MAX_PLAYERS * 0.8) {
console.log(`[PlayerPool] High usage: ${this.activeCount}/${MAX_PLAYERS} active`);
}
return Promise.resolve();
}
if (__DEV__ && this.waitingQueue.length > 10) {
console.warn(`[PlayerPool] Long queue: ${this.waitingQueue.length} waiting`);
}
return new Promise((resolve) => {
this.waitingQueue.push(() => {
this.activeCount++;
resolve();
});
});
}
release(): void {
this.activeCount--;
const next = this.waitingQueue.shift();
if (next) {
next();
}
}
getActiveCount(): number {
return this.activeCount;
}
getStats() {
return {
active: this.activeCount,
waiting: this.waitingQueue.length,
capacity: MAX_PLAYERS,
utilization: this.activeCount / MAX_PLAYERS,
};
}
}
export const playerPool = new PlayerPool();