update everything, add CI

This commit is contained in:
andy 2021-06-21 22:41:48 +01:00
parent 817610127d
commit 3829f119e6
9 changed files with 8535 additions and 2457 deletions

50
.github/workflows/test.yml vendored Normal file
View File

@ -0,0 +1,50 @@
name: test and deploy
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
env:
CARGO_TERM_COLOR: always
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install wasm-pack
uses: jetli/wasm-pack-action@v0.3.0
- name: Build Rust for WASM
run: wasm-pack build
- name: Test WASM on Firefox
run: wasm-pack test --firefox --headless
- name: Test WASM on Chrome
run: wasm-pack test --chrome --headless
- name: Install Node
uses: actions/setup-node@v2
with:
node-version: 16
- name: Change Working Directory to Js
run: cd www
- name: Install Node Modules
run: npm ci
- name: Build Js
run: npm run build --if-present
- name: Deploy To Pages
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./dist

View File

@ -16,9 +16,9 @@ default = ["console_error_panic_hook",
random_init = ["rand", "rand_pcg"] random_init = ["rand", "rand_pcg"]
[dependencies] [dependencies]
wasm-bindgen = "0.2.63" wasm-bindgen = "0.2.74"
rand = {version = "0.7.3", optional = true } rand = {version = "0.8.4", optional = true }
rand_pcg = {version = "0.2.1", optional = true } rand_pcg = {version = "0.3.1", optional = true }
# The `console_error_panic_hook` crate provides better debugging of panics by # The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires # logging them with `console.error`. This is great for development, but requires
@ -39,8 +39,11 @@ features = [
"console", "console",
] ]
[dependencies.getrandom]
features = ["js"]
[dev-dependencies] [dev-dependencies]
wasm-bindgen-test = "0.3.13" wasm-bindgen-test = "0.3.24"
[profile.release] [profile.release]
# Tell `rustc` to optimize for small code size. # Tell `rustc` to optimize for small code size.

View File

@ -8,6 +8,8 @@ use rand::prelude::*;
use rand_pcg::Pcg64Mcg; use rand_pcg::Pcg64Mcg;
mod time;
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global // When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
// allocator. // allocator.
// #[cfg(feature = "wee_alloc")] // #[cfg(feature = "wee_alloc")]
@ -80,7 +82,7 @@ impl Universe {
#[cfg(feature = "random_init")] #[cfg(feature = "random_init")]
fn populate_cell(i: u32, rng: &mut Pcg64Mcg, threshold: u32) -> Cell { fn populate_cell(i: u32, rng: &mut Pcg64Mcg, threshold: u32) -> Cell {
match rng.gen_range(0, 101).cmp(&threshold) { match rng.gen_range(0..101).cmp(&threshold) {
Ordering::Less => Cell::Alive, Ordering::Less => Cell::Alive,
Ordering::Greater => Cell::Dead, Ordering::Greater => Cell::Dead,
Ordering::Equal => Cell::Dead, Ordering::Equal => Cell::Dead,
@ -88,10 +90,11 @@ impl Universe {
} }
} }
#[wasm_bindgen] #[wasm_bindgen] // public methods exported to js
impl Universe { impl Universe {
pub fn tick(&mut self) { pub fn tick(&mut self) {
// log!("ticking"); // log!("ticking");
let _timer = time::Timer::new("Universe::tick"); // will stop when dropped
let mut next = self.cells.clone(); let mut next = self.cells.clone();
for row in 0..self.height { for row in 0..self.height {

19
src/time.rs Normal file
View File

@ -0,0 +1,19 @@
extern crate web_sys;
use web_sys::console;
pub struct Timer<'a> {
name: &'a str,
}
impl<'a> Timer<'a> {
pub fn new(name: &'a str) -> Timer<'a> {
console::time_with_label(name);
Timer { name }
}
}
impl<'a> Drop for Timer<'a> {
fn drop(&mut self) {
console::time_end_with_label(self.name);
}
}

View File

@ -32,6 +32,7 @@
<label for="play-check">Play</label> <label for="play-check">Play</label>
<button id="step">Step</button> <button id="step">Step</button>
<button id="reset">Reset</button> <button id="reset">Reset</button>
<div id="fps"></div>
<script src="./bootstrap.js"></script> <script src="./bootstrap.js"></script>
</body> </body>
</html> </html>

View File

@ -1,5 +1,5 @@
import { Universe, Cell, init } from "game-of-life"; import { Universe, Cell, init } from "game-of-life";
import { memory } from "game-of-life/game_of_life_bg"; import { memory } from "game-of-life/game_of_life_bg.wasm";
// let PLAY = true; // let PLAY = true;
// let PLAY = false; // let PLAY = false;
@ -72,6 +72,7 @@ const drawCells = () => {
}; };
const renderSingle = () => { const renderSingle = () => {
fps.render(); //new
universe.tick(); universe.tick();
drawGrid(); drawGrid();
@ -181,4 +182,47 @@ const onReset = () => {
document.getElementById("reset").onclick = onReset; document.getElementById("reset").onclick = onReset;
drawGrid(); drawGrid();
drawCells(); drawCells();
const fps = new class {
constructor() {
this.fps = document.getElementById("fps");
this.frames = [];
this.lastFrameTimeStamp = performance.now();
}
render() {
// Convert the delta time since the last frame render into a measure
// of frames per second.
const now = performance.now();
const delta = now - this.lastFrameTimeStamp;
this.lastFrameTimeStamp = now;
const fps = 1 / delta * 1000;
// Save only the latest 100 timings.
this.frames.push(fps);
if (this.frames.length > 100) {
this.frames.shift();
}
// Find the max, min, and mean of our 100 latest timings.
let min = Infinity;
let max = -Infinity;
let sum = 0;
for (let i = 0; i < this.frames.length; i++) {
sum += this.frames[i];
min = Math.min(this.frames[i], min);
max = Math.max(this.frames[i], max);
}
let mean = sum / this.frames.length;
// Render the statistics.
this.fps.textContent = `
Frames per Second:
latest = ${Math.round(fps)} //
avg of last 100 = ${Math.round(mean)} //
min of last 100 = ${Math.round(min)} //
max of last 100 = ${Math.round(max)}
`.trim();
}
};

10835
www/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -5,7 +5,7 @@
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"build": "webpack --config webpack.config.js", "build": "webpack --config webpack.config.js",
"start": "webpack-dev-server" "start": "webpack serve --config webpack.config.js --progress"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@ -26,9 +26,9 @@
"game-of-life": "file:../pkg" "game-of-life": "file:../pkg"
}, },
"devDependencies": { "devDependencies": {
"copy-webpack-plugin": "^5.1.2", "copy-webpack-plugin": "^9.0.0",
"webpack": "^4.44.2", "webpack": "^5.40.0",
"webpack-cli": "^3.1.0", "webpack-cli": "^4.7.2",
"webpack-dev-server": "^3.1.5" "webpack-dev-server": "^3.11.2"
} }
} }

View File

@ -8,7 +8,16 @@ module.exports = {
filename: "bootstrap.js", filename: "bootstrap.js",
}, },
mode: "development", mode: "development",
experiments: {
asyncWebAssembly: true
},
plugins: [ plugins: [
new CopyWebpackPlugin(['index.html']) new CopyWebpackPlugin({
patterns: [
{
from: path.resolve(__dirname, "index.html")
}
]
})
], ],
}; };