Skip to content

Commit acc127c

Browse files
authored
feat(netwatch): Add browser support for rough network changes (online/offline) (#14)
## Description Uses `window.navigator.onLine` and [`window.addEventListener("online", ...)` and friends](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/onLine#listening_for_changes_in_network_status) to listen for network changes. ## Notes & open questions I tested this manually in firefox, chromium and gnome's WebKit-based browser. ~~I'm not sure how easy it would be to set up automated testing for this :S~~ Pretty hard. I've at least made sure there's a node.js smoke test for this, so that *using* the API doesn't fail, even if the underlying `navigator.onLine` or "online"/"offline" event listener APIs don't exist. Even if this were run in a browser, it'd be hard to trigger online/offline. The code generally falls back to not doing monitoring when the platform doesn't support it (e.g. node.js). In that case, you just won't get any events. ## Change checklist - [x] Self-review. - [x] Test
1 parent 58ca29f commit acc127c

17 files changed

+1073
-48
lines changed

.cargo/config.toml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[target.wasm32-unknown-unknown]
2+
runner = "wasm-bindgen-test-runner"
3+
rustflags = ['--cfg', 'getrandom_backend="wasm_js"']

.github/workflows/ci.yaml

+34
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,40 @@ jobs:
147147
env:
148148
RUST_LOG: ${{ runner.debug && 'TRACE' || 'DEBUG' }}
149149

150+
wasm_test:
151+
name: Build & test wasm32 for browsers
152+
runs-on: ubuntu-latest
153+
steps:
154+
- name: Checkout sources
155+
uses: actions/checkout@v4
156+
157+
- name: Install stable toolchain
158+
uses: dtolnay/rust-toolchain@stable
159+
160+
- name: Add wasm target
161+
run: rustup target add wasm32-unknown-unknown
162+
163+
- name: Install cargo-binstall
164+
uses: cargo-bins/cargo-binstall@main
165+
166+
- name: Install wasm-bindgen-test-runner
167+
run: cargo binstall wasm-bindgen-cli --locked --no-confirm
168+
169+
- name: Install wasm-tools
170+
uses: bytecodealliance/actions/wasm-tools/setup@v1
171+
172+
- name: wasm32 build (netwatch)
173+
run: cargo build --target wasm32-unknown-unknown -p netwatch
174+
175+
# If the Wasm file contains any 'import "env"' declarations, then
176+
# some non-Wasm-compatible code made it into the final code.
177+
- name: Ensure no 'import "env"' in netwatch Wasm
178+
run: |
179+
! wasm-tools print --skeleton target/wasm32-unknown-unknown/debug/netwatch.wasm | grep 'import "env"'
180+
181+
- name: Run smoke test in wasm
182+
run: cargo test -p netwatch --test smoke --target=wasm32-unknown-unknown
183+
150184
check_semver:
151185
runs-on: ubuntu-latest
152186
env:

0 commit comments

Comments
 (0)