Skip to content

Commit

Permalink
[dev]: Define explicitly types inside game logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Takuma Kira committed Apr 19, 2024
1 parent 155e184 commit dd0476c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
19 changes: 17 additions & 2 deletions www/src/game-of-life/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
type BGModule = {}
type WASMModule = { memory: WebAssembly.Memory }

declare module 'wasm-game-of-life/wasm_game_of_life_bg.wasm' {
export default function wasm(importObject: { './wasm_game_of_life_bg.js': any }): Promise<any>
}
export default function wasm(importObject: { './wasm_game_of_life_bg.js': BGModule }): Promise<WASMModule>
}

declare module 'wasm-game-of-life/wasm_game_of_life_bg.js' {
import { Universe, Cell } from "wasm-game-of-life"

function __wbg_set_wasm(wasm: WASMModule): void

export {
__wbg_set_wasm,
Universe,
Cell,
}
}
8 changes: 6 additions & 2 deletions www/src/game-of-life/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/// <reference types="./index.d.ts" />
import wasm from "wasm-game-of-life/wasm_game_of_life_bg.wasm";
import * as bg from "wasm-game-of-life/wasm_game_of_life_bg.js"

export default function run(canvas) {
export default function run(canvas: HTMLCanvasElement): void {
wasm({'./wasm_game_of_life_bg.js': bg}).then(wasm => {
bg.__wbg_set_wasm(wasm)
main(bg.Universe, bg.Cell, wasm.memory, canvas)
})
}

function main(Universe, Cell, memory, canvas) {
function main(Universe: typeof bg.Universe, Cell: typeof bg.Cell, memory: WebAssembly.Memory, canvas: HTMLCanvasElement) {
const CELL_SIZE = 5; // px
const GRID_COLOR = "#CCCCCC";
const DEAD_COLOR = "#FFFFFF";
Expand All @@ -25,6 +26,9 @@ function main(Universe, Cell, memory, canvas) {
canvas.width = (CELL_SIZE + 1) * width + 1;

const ctx = canvas.getContext('2d');
if (ctx === null) {
throw new Error("canvas 2d context not found")
}

const fps = new class {
fps: HTMLDivElement;
Expand Down

0 comments on commit dd0476c

Please sign in to comment.