Skip to content

Commit 7c298f6

Browse files
committed
feat: keep the AudioPlayer descriptor data updated when its retrieval from the pool
1 parent 8f8ca7e commit 7c298f6

File tree

3 files changed

+43
-24
lines changed

3 files changed

+43
-24
lines changed

src/components/AudioPlayback/AudioPlayer.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -272,19 +272,10 @@ export class AudioPlayer {
272272
}
273273
};
274274

275-
private setDescriptor({ durationSeconds, mimeType, src }: AudioDescriptor) {
276-
if (mimeType !== this.mimeType) {
277-
this._data.mimeType = mimeType;
278-
}
279-
280-
if (durationSeconds !== this.durationSeconds) {
281-
this._data.durationSeconds = durationSeconds;
282-
}
283-
if (src !== this.src) {
284-
this._data.src = src;
285-
if (this.elementRef) {
286-
this.elementRef.src = src;
287-
}
275+
setDescriptor(descriptor: AudioDescriptor) {
276+
this._data = { ...this._data, ...descriptor };
277+
if (descriptor.src !== this.src && this.elementRef) {
278+
this.elementRef.src = descriptor.src;
288279
}
289280
}
290281

src/components/AudioPlayback/AudioPlayerPool.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,19 @@ export class AudioPlayerPool {
2828
}
2929

3030
getOrAdd = (params: Omit<AudioPlayerOptions, 'pool'>) => {
31+
const { playbackRates, plugins, ...descriptor } = params;
3132
let player = this.pool.get(params.id);
3233
if (player) {
33-
if (!player.disposed) return player;
34+
if (!player.disposed) {
35+
player.setDescriptor(descriptor);
36+
return player;
37+
}
3438
this.deregister(params.id);
3539
}
3640
player = new AudioPlayer({
37-
...params,
41+
playbackRates,
42+
plugins,
43+
...descriptor,
3844
pool: this,
3945
});
4046
this.pool.set(params.id, player);

src/components/AudioPlayback/__tests__/AudioPlayerPool.test.js

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,44 @@ describe('AudioPlayerPool', () => {
2929
jest.restoreAllMocks();
3030
createdAudios.length = 0;
3131
});
32-
33-
const makePlayer = (pool, { id, mimeType = 'audio/mpeg', src }) =>
32+
const defaultDescriptor = { durationSeconds: 100, mimeType: 'audio/mpeg' };
33+
const makePlayer = (pool, descriptor) =>
3434
pool.getOrAdd({
35-
durationSeconds: 100,
36-
id,
37-
mimeType,
38-
src,
35+
...defaultDescriptor,
36+
...descriptor,
3937
});
4038

41-
it('getOrAdd returns same instance for same id and does not auto-register listeners', () => {
39+
it('getOrAdd returns same instance for same id and does not auto-register listeners, updates descriptor fields', () => {
4240
const pool = new AudioPlayerPool();
43-
const p1 = makePlayer(pool, { id: 'a', src: 'https://example.com/a.mp3' });
41+
const p1 = makePlayer(pool, {
42+
durationSeconds: 3,
43+
fileSize: 35,
44+
id: 'a',
45+
mimeType: 'audio/abc',
46+
src: 'https://example.com/a.mp3',
47+
title: 'Title A',
48+
waveformData: [1],
49+
});
4450
const regSpy = jest.spyOn(p1, 'registerSubscriptions');
45-
const p1Again = makePlayer(pool, { id: 'a', src: 'https://example.com/a.mp3' });
51+
const p1Again = makePlayer(pool, {
52+
durationSeconds: 10,
53+
id: 'a',
54+
mimeType: 'audio/mpeg',
55+
src: 'https://example.com/b.mp3',
56+
waveformData: [2],
57+
});
4658
expect(p1Again).toBe(p1);
4759
expect(regSpy).not.toHaveBeenCalled();
60+
// eslint-disable-next-line no-underscore-dangle
61+
expect(p1._data).toStrictEqual({
62+
durationSeconds: 10,
63+
fileSize: 35,
64+
id: 'a',
65+
mimeType: 'audio/mpeg',
66+
src: 'https://example.com/b.mp3',
67+
title: 'Title A',
68+
waveformData: [2],
69+
});
4870
});
4971

5072
it('concurrent mode: per-owner elements are created lazily; src set without explicit load()', () => {

0 commit comments

Comments
 (0)