Skip to content

Commit

Permalink
🐛 [Earwurm] Do not reinitialize an existing Stack on .add()
Browse files Browse the repository at this point in the history
  • Loading branch information
beefchimi committed Dec 22, 2023
1 parent 58e135a commit c4a8386
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/Earwurm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,12 @@ export class Earwurm extends EmittenCommon<ManagerEventMap> {
add(...entries: LibraryEntry[]) {
const newKeys: LibraryKeys = [];

const newStacks = entries.map(({id, path}) => {
const newStacks = entries.reduce<Stack[]>((collection, {id, path}) => {
const existingStack = this.get(id);
const identicalStack = existingStack?.path === path;

if (identicalStack) return collection;

newKeys.push(id);

const newStack = new Stack(id, path, this.#context, this.#gainNode, {
Expand All @@ -143,16 +148,16 @@ export class Earwurm extends EmittenCommon<ManagerEventMap> {

newStack.on('statechange', this.#handleStackStateChange);

return newStack;
});
return [...collection, newStack];
}, []);

const replacedKeys = this.#library.reduce<LibraryKeys>(
(collection, {id}) =>
newKeys.includes(id) ? [...collection, id] : collection,
[],
);

this.remove(...replacedKeys);
if (replacedKeys.length) this.remove(...replacedKeys);
this.#setLibrary([...this.#library, ...newStacks]);

return newKeys;
Expand Down

0 comments on commit c4a8386

Please sign in to comment.