Skip to content

Commit f278f4f

Browse files
authored
Merge pull request #12 from macabeus/fix/dispose-emulator
react-gbajs: fix when the emulator is disposed and mounted again
2 parents 232439d + adfe69b commit f278f4f

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

src/react/gba-context.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { Ref } from 'react'
22

33
const defaultValue = {
44
gba: undefined as Gba,
5+
clearGbaInstance: (() => {}) as ClearGbaInstance,
56
setFpsCallback: (() => {}) as SetFpsCallback,
67
canvasRef: undefined as unknown as Ref<HTMLCanvasElement>,
78
play: (() => {}) as unknown as Play,

src/react/gba-provider.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,13 @@ const GbaProvider: FunctionComponent = ({ children }) => {
5252
const saveState = () =>
5353
gba.freeze()
5454

55+
const clearGbaInstance = () =>
56+
setGba(undefined)
57+
5558
return (
5659
<GbaContext.Provider value={{
5760
gba,
61+
clearGbaInstance,
5862
canvasRef,
5963
setFpsCallback: (newFpsCallback) => {
6064
setFpsCallback(newFpsCallback)

src/react/index.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ const ReactGbaJs: FunctionComponent<Props> = ({
2020
onLogReceived = () => {},
2121
scale = 1,
2222
}) => {
23-
const { gba, setFpsCallback, canvasRef } = useContext(GbaContext)
23+
const {
24+
gba,
25+
clearGbaInstance,
26+
setFpsCallback,
27+
canvasRef,
28+
} = useContext(GbaContext)
2429

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

40+
useEffect(
41+
() =>
42+
() => clearGbaInstance(),
43+
[]
44+
)
45+
3546
useEffect(() => {
3647
if (gba === undefined) {
3748
return

src/types.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
type Gba = any
22
type State = any
33

4+
type ClearGbaInstance = () => void
5+
46
type FpsCallback = (fps: number) => void
57

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

0 commit comments

Comments
 (0)