Skip to content

Commit

Permalink
Added better logging for our event system
Browse files Browse the repository at this point in the history
  • Loading branch information
underfisk committed Mar 23, 2021
1 parent 8c3d51b commit f37dc92
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/api/useApiEventHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect } from 'react'
import { ApiRef } from './types/apiRef'
import { ApiRef } from './types'
import { useLogger } from '../logger'

export function useApiEventHandler(
Expand All @@ -14,6 +14,9 @@ export function useApiEventHandler(
return apiRef.current.subscribeEvent(eventName, handler)
}

logger.warn(
`Event name or handler is missing therefore it isn't possible to subscribe. EventName: ${eventName}, Handler: ${handler}`,
)
return undefined
}, [apiRef, eventName, handler, logger])
}
11 changes: 7 additions & 4 deletions src/api/useApiFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { useLogger } from '../logger'
* Initializes a new api instance
* @param gridRootRef
* @param apiRef
* @param theme
* @param selectionKey
*/
export function useApiFactory(
gridRootRef: React.RefObject<HTMLDivElement>,
Expand All @@ -21,18 +23,19 @@ export function useApiFactory(

const publishEvent = useCallback(
(name: string, ...args: any[]) => {
logger.debug(`Publishing event ${name}`)
apiRef.current.emit(name, ...args)
},
[apiRef],
[logger, apiRef],
)

const subscribeEvent = useCallback(
(event: string, handler: (param: any) => void): (() => void) => {
logger.debug(`Binding ${event} event`)
logger.debug(`Subscribing to event: ${event}`)
apiRef.current.on(event, handler)
const api = apiRef.current
return () => {
logger.debug(`Clearing ${event} event`)
logger.debug(`Removing event listener: ${event}`)
api.removeListener(event, handler)
}
},
Expand All @@ -50,7 +53,7 @@ export function useApiFactory(
const api = apiRef.current

return () => {
logger.debug('Clearing all events listeners')
logger.debug('[Unmount] Clearing all events listeners')
api.removeAllListeners()
}
}, [selectionKey, gridRootRef, apiRef, theme, logger])
Expand Down

0 comments on commit f37dc92

Please sign in to comment.