Skip to content
This repository has been archived by the owner on Jul 10, 2023. It is now read-only.

Commit

Permalink
RC: 1.3.8 (#78)
Browse files Browse the repository at this point in the history
* Fix "memory access out of bounds" error (#77)

* Fix "memory access out of bounds" error

See rustwasm/wasm-bindgen#3153 for context

* Use useEffect

* hook: `useMountEffectOnce`

* version: 1.3.8

Co-authored-by: ivanschuetz <ivanschuetz@gmail.com>
  • Loading branch information
satelllte and ivnsch committed Nov 19, 2022
1 parent 7ba73a9 commit df4999e
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 12 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nextjs-wasm",
"version": "1.3.7",
"version": "1.3.8",
"scripts": {
"dev": "next dev",
"build:wasm": "wasm-pack build wasm --release --target web",
Expand Down
18 changes: 11 additions & 7 deletions src/context/WASM.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ReactNode, useEffect, useState } from 'react'
import { createContext } from 'react'
import { useState, createContext } from 'react'
import type { ReactNode } from 'react'
import { useMountEffectOnce } from '../hooks/useMountEffectOnce'

const initial: IWASMContext = {}

Expand All @@ -10,13 +11,16 @@ export const WASMContextProvider: React.FC<WASMContextProviderProps> = ({
}) => {
const [state, setState] = useState<IWASMContext>(initial)

useEffect(() => {
// This has to run only once: https://github.com/rustwasm/wasm-bindgen/issues/3153
// Though, in development React renders twice when Strict Mode is enabled: https://reactjs.org/docs/strict-mode.html
// That's why it must be limited to a single mount run
useMountEffectOnce(() => {
(async() => {
const wasm = await import('wasm')
await wasm.default()
setState({ wasm })
const wasm = await import("wasm");
await wasm.default();
setState({ wasm });
})()
}, [])
})

return (
<WASMContext.Provider value={state}>
Expand Down
11 changes: 11 additions & 0 deletions src/hooks/useMountEffectOnce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { useEffect, useRef } from 'react'

export const useMountEffectOnce = (fn: () => void) => {
const wasExecutedRef = useRef(false)
useEffect(() => {
if (!wasExecutedRef.current) {
fn()
}
wasExecutedRef.current = true
}, [fn])
}
2 changes: 1 addition & 1 deletion wasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wasm"
version = "1.3.7"
version = "1.3.8"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down

0 comments on commit df4999e

Please sign in to comment.