Skip to content

Commit

Permalink
Fix "memory access out of bounds" error
Browse files Browse the repository at this point in the history
  • Loading branch information
ivnsch committed Nov 17, 2022
1 parent 7ba73a9 commit e3dc010
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/context/WASM.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import { ReactNode, useEffect, useState } from 'react'
import { createContext } from 'react'

// initialize at module scope, to prevent executing twice on strict mode,
// which can cause issues when calling async functions on wasm
const wasmPromise = import('wasm').then(wasm => {
if (typeof window !== "undefined") {
// client side: initialize
return wasm.default().then(() => wasm)
} else {
// server side: do nothing
return wasm
}
})

const initial: IWASMContext = {}

export const WASMContext = createContext(initial)
Expand All @@ -12,8 +24,7 @@ export const WASMContextProvider: React.FC<WASMContextProviderProps> = ({

useEffect(() => {
(async() => {
const wasm = await import('wasm')
await wasm.default()
const wasm = await wasmPromise
setState({ wasm })
})()
}, [])
Expand Down

0 comments on commit e3dc010

Please sign in to comment.