Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add store action stats to repluggableAppDebug #157

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/repluggableAppDebug/debug.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface RepluggableAppDebugInfo {
shellInstallers: WeakMap<PrivateShell, string[]>
utils: RepluggableDebugUtils
hmr: RepluggableHMR
store: StoreDebugUtility
}

export interface RepluggableDebugUtils {
Expand All @@ -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
}
30 changes: 29 additions & 1 deletion src/repluggableAppDebug/repluggableAppDebug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -70,6 +72,31 @@ export function setupDebugInfo({
performance: getPerformanceDebug(options, trace, memoizedArr)
}

const actionCountByType = new Map<string, number>()
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,
Expand All @@ -81,6 +108,7 @@ export function setupDebugInfo({
utils,
hmr: {
hot
}
},
store
}
}
9 changes: 8 additions & 1 deletion src/throttledStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ export interface ThrottledStore<T = any> extends Store<T> {
flush(): void
}

export type ActionMonitorCallback = (action: AnyAction) => void
export interface PrivateThrottledStore<T = any> extends ThrottledStore<T> {
broadcastNotify(): void
observableNotify(observer: AnyPrivateObservableState): void
resetPendingNotifications(): void
setActionMonitor(callback: ActionMonitorCallback | null): void
}

export interface PrivateObservableState<TState, TSelector> extends ObservableState<TSelector> {
Expand Down Expand Up @@ -137,6 +139,7 @@ export const createThrottledStore = (
): PrivateThrottledStore => {
let pendingBroadcastNotification = false
let pendingObservableNotifications: Set<AnyPrivateObservableState> | undefined
let actionMonitor: ActionMonitorCallback | null = null

const onBroadcastNotify = () => {
pendingBroadcastNotification = true
Expand Down Expand Up @@ -207,6 +210,7 @@ export const createThrottledStore = (

const dispatch: Dispatch<AnyAction> = action => {
resetPendingNotifications()
actionMonitor && actionMonitor(action)
const dispatchResult = store.dispatch(action)
return dispatchResult
}
Expand All @@ -218,7 +222,10 @@ export const createThrottledStore = (
flush,
broadcastNotify: onBroadcastNotify,
observableNotify: onObservableNotify,
resetPendingNotifications
resetPendingNotifications,
setActionMonitor(callback: ActionMonitorCallback | null) {
actionMonitor = callback
}
}

resetPendingNotifications()
Expand Down