Skip to content

Commit

Permalink
Merge pull request #12 from macabeus/fix/dispose-emulator
Browse files Browse the repository at this point in the history
react-gbajs: fix when the emulator is disposed and mounted again
  • Loading branch information
macabeus authored Jun 18, 2021
2 parents 232439d + adfe69b commit f278f4f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/react/gba-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { Ref } from 'react'

const defaultValue = {
gba: undefined as Gba,
clearGbaInstance: (() => {}) as ClearGbaInstance,
setFpsCallback: (() => {}) as SetFpsCallback,
canvasRef: undefined as unknown as Ref<HTMLCanvasElement>,
play: (() => {}) as unknown as Play,
Expand Down
4 changes: 4 additions & 0 deletions src/react/gba-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,13 @@ const GbaProvider: FunctionComponent = ({ children }) => {
const saveState = () =>
gba.freeze()

const clearGbaInstance = () =>
setGba(undefined)

return (
<GbaContext.Provider value={{
gba,
clearGbaInstance,
canvasRef,
setFpsCallback: (newFpsCallback) => {
setFpsCallback(newFpsCallback)
Expand Down
13 changes: 12 additions & 1 deletion src/react/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ const ReactGbaJs: FunctionComponent<Props> = ({
onLogReceived = () => {},
scale = 1,
}) => {
const { gba, setFpsCallback, canvasRef } = useContext(GbaContext)
const {
gba,
clearGbaInstance,
setFpsCallback,
canvasRef,
} = useContext(GbaContext)

useEffect(() => {
setFpsCallback(onFpsReported)
Expand All @@ -32,6 +37,12 @@ const ReactGbaJs: FunctionComponent<Props> = ({
)
}, [gba, JSON.stringify(watchLogLevels), onLogReceived])

useEffect(
() =>
() => clearGbaInstance(),
[]
)

useEffect(() => {
if (gba === undefined) {
return
Expand Down
2 changes: 2 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
type Gba = any
type State = any

type ClearGbaInstance = () => void

type FpsCallback = (fps: number) => void

type FrozenAddress = { size: 8 | 16 | 32, value: number }
Expand Down

0 comments on commit f278f4f

Please sign in to comment.