From f2ba7bddf5ed2f928b9f1b77a07c2851b4caeca5 Mon Sep 17 00:00:00 2001 From: beefchimi Date: Thu, 21 Dec 2023 13:36:20 -0500 Subject: [PATCH] :bug: [Earwurm] Do not reinitialize an existing Stack on .add() --- src/Earwurm.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Earwurm.ts b/src/Earwurm.ts index a6d3144..d3fd0c2 100644 --- a/src/Earwurm.ts +++ b/src/Earwurm.ts @@ -133,7 +133,12 @@ export class Earwurm extends EmittenCommon { add(...entries: LibraryEntry[]) { const newKeys: LibraryKeys = []; - const newStacks = entries.map(({id, path}) => { + const newStacks = entries.reduce((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, { @@ -143,8 +148,8 @@ export class Earwurm extends EmittenCommon { newStack.on('statechange', this.#handleStackStateChange); - return newStack; - }); + return [...collection, newStack]; + }, []); const replacedKeys = this.#library.reduce( (collection, {id}) => @@ -152,7 +157,7 @@ export class Earwurm extends EmittenCommon { [], ); - this.remove(...replacedKeys); + if (replacedKeys.length) this.remove(...replacedKeys); this.#setLibrary([...this.#library, ...newStacks]); return newKeys;