Skip to content

Commit

Permalink
feat: Improve prediction integration
Browse files Browse the repository at this point in the history
  • Loading branch information
jcs090218 committed Jul 16, 2024
1 parent a37cc81 commit c50ed3f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
18 changes: 16 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,22 @@ impl Client {
self.entered = false;
}

pub fn move_by_delta(
pub fn move_self(&mut self, _point: isize) -> Option<String> {
self.move_by_delta(true, _point, 0, None)
}

pub fn move_other(
&mut self,
_point: isize,
_delta: isize,
_filename: Option<String>,
) -> Option<String> {
self.move_by_delta(false, _point, _delta, _filename)
}

fn move_by_delta(
&mut self,
_sender_p: bool,
_point: isize,
_delta: isize,
_filename: Option<String>,
Expand Down Expand Up @@ -119,7 +133,7 @@ impl Client {
}
}

user.move_by_delta(_point, _delta);
user.move_by_delta(_sender_p, _point, _delta);

Some(filename)
}
Expand Down
18 changes: 10 additions & 8 deletions src/handler/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub mod update {
fn predict_movement(addr: &SocketAddr, room: &mut Room, point: isize, delta: isize) {
let client = room.get_client_mut(addr).unwrap();

let filename = client.move_by_delta(point, delta, None);
let filename = client.move_self(point);

let clients = room.get_clients_mut();

Expand All @@ -34,7 +34,7 @@ pub mod update {
continue;
}

_client.move_by_delta(point, delta, filename.clone());
_client.move_other(point, delta, filename.clone());
}
}

Expand All @@ -50,18 +50,20 @@ pub mod update {

// Predict movement.
{
let delta = if add_or_delete == "delete" {
beg - end
} else {
end - beg
};
let is_delete = add_or_delete == "delete";

// Predict the movement shift.
let delta = if is_delete { beg - end } else { end - beg };

// Predict the cursor final position.
let start = if is_delete { beg } else { end };

// Nothing has changed; return it.
if delta == 0 {
return;
}

predict_movement(&addr, &mut room, beg, delta);
predict_movement(&addr, &mut room, start, delta);
}

// Update the buffer view.
Expand Down
11 changes: 7 additions & 4 deletions src/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ impl User {
///
/// # Arguments
///
/// * `_sender` - Boolean return true if the user is the sender.
/// * `_point` - Only effect point after the origin.
/// * `_delta` - Movement delta.
pub fn move_by_delta(&mut self, _point: isize, _delta: isize) {
pub fn move_by_delta(&mut self, _sender: bool, _point: isize, _delta: isize) {
// Point must exists.
if self.point.is_none() {
return;
Expand All @@ -73,16 +74,18 @@ impl User {

// Shift the point.
if _point <= point {
self.point = Some(point + _delta);
self.point = Some(if _sender { point } else { point + _delta });

// `region_beg`. and `region_end` must exists at the same time.
if !self.region_beg.is_none() {
let region_beg = self.region_beg.unwrap();
let region_end = self.region_end.unwrap();

// Shift the region.
self.region_beg = Some(region_beg + _delta);
self.region_end = Some(region_end + _delta);
//
// If the sender, just set to the point.
self.region_beg = Some(if _sender { point } else { region_beg + _delta });
self.region_end = Some(if _sender { point } else { region_end + _delta });
}
}
}
Expand Down

0 comments on commit c50ed3f

Please sign in to comment.