-
Notifications
You must be signed in to change notification settings - Fork 3
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
[WIP] fix: remove event listeners on unmount #8
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import type { InitConfig, RunOptions, ZoomOptions } from '../types'; | ||
import { useEffect, useState, useCallback } from 'react'; | ||
import { useEffect, useState, useCallback, useRef } from 'react'; | ||
|
||
export type RuntimeOptions = { | ||
onError?: (error: any) => void | ||
|
@@ -31,24 +31,25 @@ function omit<O extends Record<string, any>, P extends string, R extends { | |
|
||
export function MermaidRuntime(props: InitConfig & RunOptions & RuntimeOptions) { | ||
const renderMermaid = useMermaid(); | ||
const config = omit(props, [ 'querySelector', 'nodes', 'onError' ]); | ||
const options = pick(props, [ 'querySelector', 'nodes' ]); | ||
const config = omit(props, ['querySelector', 'nodes', 'onError']); | ||
const options = pick(props, ['querySelector', 'nodes']); | ||
|
||
useEffect(() => { | ||
renderMermaid(config, options).catch(props.onError || (() => {})); | ||
renderMermaid(config, options).catch(props.onError || (() => { })); | ||
}); | ||
|
||
return null; | ||
} | ||
|
||
export function useMermaid() { | ||
const [ mermaid, setMermaid ] = useState<Parameters<Callback>[0] | null>(null); | ||
const onUnmountRef = useRef<any>() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You forgot to add ";" at the end of the operation. What about linters in the current project? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have the same question |
||
const [mermaid, setMermaid] = useState<Parameters<Callback>[0] | null>(null); | ||
const render = useCallback(async (config: InitConfig, options?: RunOptions) => { | ||
if (mermaid) { | ||
mermaid.initialize(config); | ||
return mermaid.run(options); | ||
onUnmountRef.current = mermaid.initialize(config); | ||
mermaid.run(options); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please return promise from mermaid.run |
||
} | ||
}, [ mermaid ]); | ||
}, [mermaid]); | ||
|
||
useEffect(() => { | ||
(window.mermaidJsonp = window.mermaidJsonp || []).push(setMermaid); | ||
|
@@ -58,6 +59,8 @@ export function useMermaid() { | |
if (index > -1) { | ||
window.mermaidJsonp.splice(index, 1); | ||
} | ||
|
||
onUnmountRef.current?.() | ||
}; | ||
}, []); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,9 +64,22 @@ async function next(): Promise<void> { | |
|
||
const { zoom } = mermaid.mermaidAPI.getConfig() as InitConfig; | ||
|
||
let onUnmount = [() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This variable assignrd only once, why |
||
document.removeEventListener('click', zoomBehavior) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This string doesn't make sense because nobody calls |
||
}] | ||
|
||
document.removeEventListener('click', zoomBehavior); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This string too |
||
if (zoom) { | ||
document.addEventListener('click', zoomBehavior); | ||
document.addEventListener('click', (e) => { | ||
const cb = zoomBehavior(e) | ||
if(cb){ | ||
onUnmount.push(cb) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand what is happening here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
}); | ||
} | ||
|
||
return () => { | ||
onUnmount.map(cb => cb()) | ||
} | ||
}, | ||
render: mermaid.render, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unexpected diff