From 9004d4b339b7ef0bfafb17cc7f074836568fa4a0 Mon Sep 17 00:00:00 2001 From: Felix Berman Date: Tue, 26 Jan 2021 10:07:29 +0200 Subject: [PATCH] Add store action stats to repluggableAppDebug --- src/repluggableAppDebug/debug.d.ts | 12 ++++++++ .../repluggableAppDebug.ts | 30 ++++++++++++++++++- src/throttledStore.tsx | 9 +++++- 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/repluggableAppDebug/debug.d.ts b/src/repluggableAppDebug/debug.d.ts index cf26a846..f0531dea 100644 --- a/src/repluggableAppDebug/debug.d.ts +++ b/src/repluggableAppDebug/debug.d.ts @@ -17,6 +17,7 @@ export interface RepluggableAppDebugInfo { shellInstallers: WeakMap utils: RepluggableDebugUtils hmr: RepluggableHMR + store: StoreDebugUtility } export interface RepluggableDebugUtils { @@ -34,3 +35,14 @@ export interface APIDebugInfo { key: AnySlotKey impl(): any } + +export interface StoreDebugUtility { + startActionStats(): void + stopActionStats(): void + getActionStats(): StoreActionDebugInfo[] + resetActionStats(): void +} +export interface StoreActionDebugInfo { + type: string + count: number +} diff --git a/src/repluggableAppDebug/repluggableAppDebug.ts b/src/repluggableAppDebug/repluggableAppDebug.ts index f895730c..fb401c67 100644 --- a/src/repluggableAppDebug/repluggableAppDebug.ts +++ b/src/repluggableAppDebug/repluggableAppDebug.ts @@ -4,6 +4,8 @@ import _ from 'lodash' import { hot } from '../hot' import { AppHostServicesProvider } from '../appHostServices' import { AnyExtensionSlot } from '../extensionSlot' +import { StoreDebugUtility } from './debug' +import { PrivateThrottledStore } from '../throttledStore' interface PerformanceDebugParams { options: AppHost['options'] @@ -70,6 +72,31 @@ export function setupDebugInfo({ performance: getPerformanceDebug(options, trace, memoizedArr) } + const actionCountByType = new Map() + const getPrivateStore = () => host.getStore() as PrivateThrottledStore + + const store: StoreDebugUtility = { + startActionStats() { + getPrivateStore().setActionMonitor(action => { + const currentCount = actionCountByType.get(action.type) + actionCountByType.set(action.type, (currentCount || 0) + 1) + }) + }, + stopActionStats() { + getPrivateStore().setActionMonitor(null) + }, + getActionStats() { + return ( + [...actionCountByType.entries()] + .map(([type, count]) => ({ type, count })) + .sort((a, b) => b.count - a.count) + ) + }, + resetActionStats() { + actionCountByType.clear() + } + } + window.repluggableAppDebug = { host, uniqueShellNames, @@ -81,6 +108,7 @@ export function setupDebugInfo({ utils, hmr: { hot - } + }, + store } } diff --git a/src/throttledStore.tsx b/src/throttledStore.tsx index 2db27cf6..b25757b9 100644 --- a/src/throttledStore.tsx +++ b/src/throttledStore.tsx @@ -44,10 +44,12 @@ export interface ThrottledStore extends Store { flush(): void } +export type ActionMonitorCallback = (action: AnyAction) => void export interface PrivateThrottledStore extends ThrottledStore { broadcastNotify(): void observableNotify(observer: AnyPrivateObservableState): void resetPendingNotifications(): void + setActionMonitor(callback: ActionMonitorCallback | null): void } export interface PrivateObservableState extends ObservableState { @@ -137,6 +139,7 @@ export const createThrottledStore = ( ): PrivateThrottledStore => { let pendingBroadcastNotification = false let pendingObservableNotifications: Set | undefined + let actionMonitor: ActionMonitorCallback | null = null const onBroadcastNotify = () => { pendingBroadcastNotification = true @@ -207,6 +210,7 @@ export const createThrottledStore = ( const dispatch: Dispatch = action => { resetPendingNotifications() + actionMonitor && actionMonitor(action) const dispatchResult = store.dispatch(action) return dispatchResult } @@ -218,7 +222,10 @@ export const createThrottledStore = ( flush, broadcastNotify: onBroadcastNotify, observableNotify: onObservableNotify, - resetPendingNotifications + resetPendingNotifications, + setActionMonitor(callback: ActionMonitorCallback | null) { + actionMonitor = callback + } } resetPendingNotifications()