diff --git a/package-lock.json b/package-lock.json index c7d7a2d..42f59be 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2327,8 +2327,7 @@ "optional": true, "os": [ "android" - ], - "peer": true + ] }, "node_modules/@rollup/rollup-android-arm64": { "version": "4.59.0", @@ -2341,8 +2340,7 @@ "optional": true, "os": [ "android" - ], - "peer": true + ] }, "node_modules/@rollup/rollup-darwin-arm64": { "version": "4.59.0", @@ -2355,8 +2353,7 @@ "optional": true, "os": [ "darwin" - ], - "peer": true + ] }, "node_modules/@rollup/rollup-darwin-x64": { "version": "4.59.0", @@ -2369,8 +2366,7 @@ "optional": true, "os": [ "darwin" - ], - "peer": true + ] }, "node_modules/@rollup/rollup-freebsd-arm64": { "version": "4.59.0", @@ -2383,8 +2379,7 @@ "optional": true, "os": [ "freebsd" - ], - "peer": true + ] }, "node_modules/@rollup/rollup-freebsd-x64": { "version": "4.59.0", @@ -2397,8 +2392,7 @@ "optional": true, "os": [ "freebsd" - ], - "peer": true + ] }, "node_modules/@rollup/rollup-linux-arm-gnueabihf": { "version": "4.59.0", @@ -2411,8 +2405,7 @@ "optional": true, "os": [ "linux" - ], - "peer": true + ] }, "node_modules/@rollup/rollup-linux-arm-musleabihf": { "version": "4.59.0", @@ -2425,8 +2418,7 @@ "optional": true, "os": [ "linux" - ], - "peer": true + ] }, "node_modules/@rollup/rollup-linux-arm64-gnu": { "version": "4.59.0", @@ -2439,8 +2431,7 @@ "optional": true, "os": [ "linux" - ], - "peer": true + ] }, "node_modules/@rollup/rollup-linux-arm64-musl": { "version": "4.59.0", @@ -2453,8 +2444,7 @@ "optional": true, "os": [ "linux" - ], - "peer": true + ] }, "node_modules/@rollup/rollup-linux-loong64-gnu": { "version": "4.59.0", @@ -2467,8 +2457,7 @@ "optional": true, "os": [ "linux" - ], - "peer": true + ] }, "node_modules/@rollup/rollup-linux-loong64-musl": { "version": "4.59.0", @@ -2481,8 +2470,7 @@ "optional": true, "os": [ "linux" - ], - "peer": true + ] }, "node_modules/@rollup/rollup-linux-ppc64-gnu": { "version": "4.59.0", @@ -2495,8 +2483,7 @@ "optional": true, "os": [ "linux" - ], - "peer": true + ] }, "node_modules/@rollup/rollup-linux-ppc64-musl": { "version": "4.59.0", @@ -2509,8 +2496,7 @@ "optional": true, "os": [ "linux" - ], - "peer": true + ] }, "node_modules/@rollup/rollup-linux-riscv64-gnu": { "version": "4.59.0", @@ -2523,8 +2509,7 @@ "optional": true, "os": [ "linux" - ], - "peer": true + ] }, "node_modules/@rollup/rollup-linux-riscv64-musl": { "version": "4.59.0", @@ -2537,8 +2522,7 @@ "optional": true, "os": [ "linux" - ], - "peer": true + ] }, "node_modules/@rollup/rollup-linux-s390x-gnu": { "version": "4.59.0", @@ -2551,8 +2535,7 @@ "optional": true, "os": [ "linux" - ], - "peer": true + ] }, "node_modules/@rollup/rollup-linux-x64-gnu": { "version": "4.59.0", @@ -2565,8 +2548,7 @@ "optional": true, "os": [ "linux" - ], - "peer": true + ] }, "node_modules/@rollup/rollup-linux-x64-musl": { "version": "4.59.0", @@ -2579,8 +2561,7 @@ "optional": true, "os": [ "linux" - ], - "peer": true + ] }, "node_modules/@rollup/rollup-openbsd-x64": { "version": "4.59.0", @@ -2593,8 +2574,7 @@ "optional": true, "os": [ "openbsd" - ], - "peer": true + ] }, "node_modules/@rollup/rollup-openharmony-arm64": { "version": "4.59.0", @@ -2607,8 +2587,7 @@ "optional": true, "os": [ "openharmony" - ], - "peer": true + ] }, "node_modules/@rollup/rollup-win32-arm64-msvc": { "version": "4.59.0", @@ -2621,8 +2600,7 @@ "optional": true, "os": [ "win32" - ], - "peer": true + ] }, "node_modules/@rollup/rollup-win32-ia32-msvc": { "version": "4.59.0", @@ -2635,8 +2613,7 @@ "optional": true, "os": [ "win32" - ], - "peer": true + ] }, "node_modules/@rollup/rollup-win32-x64-gnu": { "version": "4.59.0", @@ -2649,8 +2626,7 @@ "optional": true, "os": [ "win32" - ], - "peer": true + ] }, "node_modules/@rollup/rollup-win32-x64-msvc": { "version": "4.59.0", @@ -2663,8 +2639,7 @@ "optional": true, "os": [ "win32" - ], - "peer": true + ] }, "node_modules/@shikijs/primitive": { "version": "4.0.1", @@ -8450,7 +8425,6 @@ "os": [ "android" ], - "peer": true, "engines": { "node": ">= 12.0.0" }, @@ -8471,7 +8445,6 @@ "os": [ "darwin" ], - "peer": true, "engines": { "node": ">= 12.0.0" }, @@ -8492,7 +8465,6 @@ "os": [ "darwin" ], - "peer": true, "engines": { "node": ">= 12.0.0" }, @@ -8513,7 +8485,6 @@ "os": [ "freebsd" ], - "peer": true, "engines": { "node": ">= 12.0.0" }, @@ -8534,7 +8505,6 @@ "os": [ "linux" ], - "peer": true, "engines": { "node": ">= 12.0.0" }, @@ -8555,7 +8525,6 @@ "os": [ "linux" ], - "peer": true, "engines": { "node": ">= 12.0.0" }, @@ -8576,7 +8545,6 @@ "os": [ "linux" ], - "peer": true, "engines": { "node": ">= 12.0.0" }, @@ -8597,7 +8565,6 @@ "os": [ "linux" ], - "peer": true, "engines": { "node": ">= 12.0.0" }, @@ -8618,7 +8585,6 @@ "os": [ "linux" ], - "peer": true, "engines": { "node": ">= 12.0.0" }, @@ -8639,7 +8605,6 @@ "os": [ "win32" ], - "peer": true, "engines": { "node": ">= 12.0.0" }, @@ -8660,7 +8625,6 @@ "os": [ "win32" ], - "peer": true, "engines": { "node": ">= 12.0.0" }, @@ -13522,7 +13486,6 @@ "os": [ "darwin" ], - "peer": true, "engines": { "node": "^8.16.0 || ^10.6.0 || >=11.0.0" } diff --git a/packages/storybook-addon-performance-panel/__tests__/performance-decorator-universal.browser.test.ts b/packages/storybook-addon-performance-panel/__tests__/performance-decorator-universal.browser.test.ts index d970d98..6e9f86c 100644 --- a/packages/storybook-addon-performance-panel/__tests__/performance-decorator-universal.browser.test.ts +++ b/packages/storybook-addon-performance-panel/__tests__/performance-decorator-universal.browser.test.ts @@ -225,6 +225,37 @@ describe('withPerformanceMonitor (universal / web-component usage)', () => { expect(metrics.reactMountCount).toBe(0) }) + it('creates a new core when force remount is triggered (same story ID)', () => { + const ctx = makeCtx({id: 'story-a'} as Partial) + + withPerformanceMonitor(vi.fn(() => ''), ctx) + const first = getActiveCore() + + // Simulate force remount — same ID but forceRemount flag + const remountCtx = makeCtx({id: 'story-a', forceRemount: true} as Partial) + withPerformanceMonitor(vi.fn(() => ''), remountCtx) + const second = getActiveCore() + + expect(second).not.toBe(first) + }) + + it('cleans up channel listeners when core is replaced by force remount', () => { + withPerformanceMonitor( + vi.fn(() => ''), + makeCtx({id: 'story-a'} as Partial), + ) + + // Force remount replaces the core, which should stop the old one and call channel.off + withPerformanceMonitor( + vi.fn(() => ''), + makeCtx({id: 'story-a', forceRemount: true} as Partial), + ) + + expect(mockChannel.off).toHaveBeenCalledWith(PERF_EVENTS.REQUEST_METRICS, expect.any(Function)) + expect(mockChannel.off).toHaveBeenCalledWith(PERF_EVENTS.RESET, expect.any(Function)) + expect(mockChannel.off).toHaveBeenCalledWith(PERF_EVENTS.INSPECT_ELEMENT, expect.any(Function)) + }) + it('cleans up channel listeners when core is replaced', () => { withPerformanceMonitor( vi.fn(() => ''), diff --git a/packages/storybook-addon-performance-panel/decorators/universal.ts b/packages/storybook-addon-performance-panel/decorators/universal.ts index a8a2696..ea38712 100644 --- a/packages/storybook-addon-performance-panel/decorators/universal.ts +++ b/packages/storybook-addon-performance-panel/decorators/universal.ts @@ -19,7 +19,7 @@ * @see {@link ./performance-decorator.tsx} - React-specific decorator with Profiler support */ -import type {DecoratorFunction, Renderer} from 'storybook/internal/types' +import type {DecoratorFunction, Renderer, StoryContext} from 'storybook/internal/types' import {getActiveCore, PerformanceMonitorCore, setActiveCore} from '../core/preview-core' @@ -102,7 +102,11 @@ export const withPerformanceMonitor: DecoratorFunction = (storyFn, ctx): Rendere // The decorator runs on every render — creating a new core each time // would stop/restart metrics collection and lose all accumulated data. let core = getActiveCore() - if (core?.storyId !== ctx.id) { + const isForceRemount = (ctx as StoryContext & {forceRemount?: boolean}).forceRemount === true + if (core?.storyId !== ctx.id || isForceRemount) { + if (core) { + core.stop() + } core = new PerformanceMonitorCore(ctx.id) setActiveCore(core) core.start() diff --git a/packages/storybook-addon-performance-panel/performance-panel.tsx b/packages/storybook-addon-performance-panel/performance-panel.tsx index 9ec3526..e4ad3c8 100644 --- a/packages/storybook-addon-performance-panel/performance-panel.tsx +++ b/packages/storybook-addon-performance-panel/performance-panel.tsx @@ -1521,6 +1521,11 @@ function ConnectedPanelContent({storyId}: {storyId: string}) { dispatch({type: 'RESET_METRICS'}) } }, + + forceRemount: () => { + emit(PERF_EVENTS.RESET) + dispatch({type: 'RESET_METRICS'}) + }, }) // Request metrics on mount and when preview initializes