├── .gitignore ├── src ├── lib.rs ├── background_timer.rs ├── background_worker.rs └── background_listener.rs ├── Cargo.toml ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Generated by Cargo 2 | # will have compiled files and executables 3 | debug/ 4 | target/ 5 | 6 | # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries 7 | # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html 8 | Cargo.lock 9 | 10 | # These are backup files generated by rustfmt 11 | **/*.rs.bk 12 | 13 | # MSVC Windows builds of rustc generate these, which store debugging information 14 | *.pdb 15 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | #![cfg_attr(docsrs, feature(doc_cfg))] 2 | 3 | #[cfg(feature = "listener")] 4 | mod background_listener; 5 | #[cfg(feature = "listener")] 6 | #[cfg_attr(docsrs, doc(cfg(feature = "listener")))] 7 | pub use background_listener::{VisibilityChangeListenerPlugin, WindowVisibility}; 8 | 9 | #[cfg(feature = "timer")] 10 | mod background_timer; 11 | #[cfg(feature = "timer")] 12 | #[cfg_attr(docsrs, doc(cfg(feature = "timer")))] 13 | pub use background_timer::{BackgroundTimer, BackgroundTimerPlugin}; 14 | 15 | mod background_worker; 16 | pub use background_worker::{KeepaliveSettings, WebKeepalivePlugin}; 17 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "bevy_web_keepalive" 3 | version = "0.4.0" 4 | authors = ["Nulled"] 5 | edition = "2021" 6 | rust-version = "1.76" 7 | description = "Bevy plugins to keep a bevy app running in the browser despite not being visible" 8 | readme = "README.md" 9 | repository = "https://github.com/Nul-led/bevy_web_keepalive" 10 | keywords = ["bevy", "wasm", "web", "gamedev"] 11 | categories = ["game-development", "game-engines"] 12 | license = "MIT OR Apache-2.0" 13 | 14 | [package.metadata.docs.rs] 15 | all-features = true 16 | rustdoc-args = ["--cfg", "docsrs"] 17 | default-target = "wasm32-unknown-unknown" 18 | 19 | [features] 20 | listener = [] 21 | timer = [] 22 | 23 | [dependencies] 24 | bevy_winit = "0.17" 25 | bevy_app = "0.17" 26 | bevy_ecs = "0.17" 27 | bevy_time = "0.17" 28 | wasm-bindgen = "0.2.104" 29 | web-sys = { version = "0.3.81", features = [ 30 | "Window", 31 | "Document", 32 | "Worker", 33 | "Blob", 34 | "Url", 35 | ] } 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Nulled 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/background_timer.rs: -------------------------------------------------------------------------------- 1 | use bevy_app::{App, Plugin, Update}; 2 | use bevy_ecs::{ 3 | resource::Resource, 4 | system::{Res, ResMut}, 5 | }; 6 | use bevy_time::{Stopwatch, Time}; 7 | use web_sys::window; 8 | 9 | /// The `BackgroundTimerPlugin` plugin creates a timer that keeps track of the time the app isn't in focus (aka in background). 10 | /// 11 | /// To function properly running a background worker is REQUIRED. 12 | /// 13 | /// It may prove to be useful to establish timeouts for inactive users in multiplayer games. 14 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] 15 | pub struct BackgroundTimerPlugin; 16 | 17 | impl Plugin for BackgroundTimerPlugin { 18 | fn build(&self, app: &mut App) { 19 | app.insert_resource(BackgroundTimer::default()); 20 | 21 | app.add_systems(Update, system_background_timer); 22 | } 23 | } 24 | 25 | /// The `BackgroundTimer` contains a `Stopwatch` that keeps track of the time bevy ran in the background 26 | #[derive(Clone, Debug, PartialEq, Default, Resource)] 27 | pub struct BackgroundTimer(pub Stopwatch); 28 | 29 | /// The `system_background_timer` system updates the `Stopwatch` based on the documents visibility 30 | fn system_background_timer(mut timer: ResMut, time: Res