From 5192c425485cf028cd4e86f42aa2f619fc38441c Mon Sep 17 00:00:00 2001 From: Aria Minaei Date: Fri, 28 Jul 2023 20:46:32 +0200 Subject: [PATCH] Fix the false-positive error in studio.extend This removes the `Extension id "${extension.id}" is already defined` error when `studio.extend()` is in a hot-reloaded file but the extension hasn't actually changed. --- theatre/studio/src/Studio.ts | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/theatre/studio/src/Studio.ts b/theatre/studio/src/Studio.ts index 394b938..e1bd052 100644 --- a/theatre/studio/src/Studio.ts +++ b/theatre/studio/src/Studio.ts @@ -275,20 +275,23 @@ export class Studio { throw new Error(`extension.id must be a string`) } - this.transaction(({drafts}) => { - if (drafts.ephemeral.extensions.byId[extension.id]) { - const prevExtension = drafts.ephemeral.extensions.byId[extension.id] - if ( - extension === prevExtension || - shallowEqual(extension, prevExtension) - ) { - // probably running studio.extend() several times because of hot reload. - // as long as it's the same extension, we can safely ignore. - return - } - throw new Error(`Extension id "${extension.id}" is already defined`) - } + const extensionId = extension.id + const prevExtension = + this._store.getState().ephemeral.extensions.byId[extensionId] + if (prevExtension) { + if ( + extension === prevExtension || + shallowEqual(extension, prevExtension) + ) { + // probably running studio.extend() several times because of hot reload. + // as long as it's the same extension, we can safely ignore. + return + } + throw new Error(`Extension id "${extension.id}" is already defined`) + } + + this.transaction(({drafts}) => { drafts.ephemeral.extensions.byId[extension.id] = extension const allPaneClasses = drafts.ephemeral.extensions.paneClasses