bevy_sync 0.18.5

Plugin for synchronizing entities and components between server and its clients.
Documentation
use bevy_connect::{ClientId, prelude::Channel};
use bevy_ecs::world::World;
use tracing::{debug, info, warn};

use crate::{full_sync::build_full_sync, proto::Message};

pub(crate) fn send_initial_sync(client_id: ClientId, world: &mut World) {
    info!("Sending initial sync to client id {}", client_id);
    // exclusive access to world while looping through all objects, this can be blocking/freezing for the server
    let mut initial_sync = match build_full_sync(world) {
        Ok(initial_sync) => initial_sync,
        Err(err) => {
            warn!(
                "Failed initial sync to client id {} because {}",
                client_id, err
            );
            return;
        }
    };
    let mut server = world.resource_mut::<Channel<Message>>();
    debug!("Initial sync size: {}", initial_sync.len());
    for msg in initial_sync.drain(..) {
        server.send_to(client_id, msg);
    }
    server.send_to(client_id, Message::FinishedInitialSync);
}