From 17dadf7efb96a595ed6b2737aff2c720b7d87d1f Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sun, 5 Jul 2020 23:04:34 +0100 Subject: [PATCH] working on log updates etc. --- src/bin/server.rs | 10 ++++++---- src/global.rs | 21 +++++++++++++++++---- src/sse.rs | 35 ++++++++++++++++++++--------------- templates/script.js | 21 +++++++++++++++++++++ templates/session.tera | 15 ++++++--------- 5 files changed, 70 insertions(+), 32 deletions(-) diff --git a/src/bin/server.rs b/src/bin/server.rs index 5cb031c1..0da139ff 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -156,10 +156,12 @@ fn api_grab(form : Json) -> impl response::Responder<'static> { let json = serde_json::to_string(&json).expect("convert to json"); let update = PreparedUpdate { gen, - client, - piece : vpiece, - cseq : form.cseq, - json, + u : PreparedPieceUpdate { + client, + sameclient_cseq : form.cseq, + piece : vpiece, + json, + }, }; let update = Arc::new(update); // split vie wthing would go here, see also update.piece diff --git a/src/global.rs b/src/global.rs index 6c5603b9..b90c371f 100644 --- a/src/global.rs +++ b/src/global.rs @@ -27,10 +27,23 @@ impl Client { #[derive(Debug)] pub struct PreparedUpdate { pub gen : Generation, - pub client : ClientId, - pub piece : VisiblePieceId, - pub cseq : ClientSequence, - pub json : String, + pub u : PreparedUpdatePayload, +} +#[derive(Debug)] +pub enum PreparedUpdatePayload { + PreparedPieceUpdate { + client : ClientId, + sameclient_cseq : ClientSequence, + piece : VisiblePieceId, + json : String, + }, +} +pub use PreparedUpdatePayload::*; + +impl PreparedUpdatePayload { + pub fn json_len(&self) -> usize { match self { + &PreparedPieceUpdate { ref json, .. } => json.len(), + } } } #[derive(Debug,Default)] diff --git a/src/sse.rs b/src/sse.rs index 7e83ca5a..b360c481 100644 --- a/src/sse.rs +++ b/src/sse.rs @@ -81,23 +81,28 @@ impl Read for UpdateReader { let next = match pu.log.get(self.to_send) { Some(next) => next, None => { break } }; - let next_len = UPDATE_MAX_FRAMING_SIZE + next.json.len(); + let next_len = UPDATE_MAX_FRAMING_SIZE + next.u.json_len(); if next_len > buf.len() { break } - if next.client == self.client { - write!(buf, "event: recorded\n\ - data: ")?; - serde_json::to_writer(&mut buf, &RecordedConfirmation { - gen : next.gen, - piece : next.piece, - cseq : next.cseq, - })?; - write!(buf, "\n\n")?; - } else { - write!(buf, "id: {}\n\ - data: {}\n\n", - &self.to_send, - &next.json)?; + match &next.u { + &PreparedPieceUpdate { + piece, client : uclient, sameclient_cseq : cseq, .. + } if uclient== self.client => { + write!(buf, "event: recorded\n\ + data: ")?; + serde_json::to_writer(&mut buf, &RecordedConfirmation { + gen : next.gen, + piece : piece, + cseq : cseq, + })?; + write!(buf, "\n\n")?; + }, + PreparedPieceUpdate { json, .. } => { + write!(buf, "id: {}\n\ + data: {}\n\n", + &self.to_send, + json)?; + }, } self.to_send.try_increment().unwrap(); } diff --git a/templates/script.js b/templates/script.js index 872ee124..85a45a62 100644 --- a/templates/script.js +++ b/templates/script.js @@ -210,6 +210,26 @@ function drag_cancel() { drag_uelem = null; } +// ----- logs ----- + +messages.LogUpdate = function(data) { + lastent = logdiv.lastElementChild; + in_scrollback = + // inspired by + // https://stackoverflow.com/questions/487073/how-to-check-if-element-is-visible-after-scrolling/21627295#21627295 + // rejected + // https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API + lastent.getBoundingClientRect().bottom > + logdiv.getBoundingClientRect().bottom; + + console.log('LOG UPDATE ',in_scrollback,data); + + if (!in_scrollback) { + lastent = logdiv.lastElementChild; + lastent.scrollIntoView(); + } +} + // ----- test counter, startup ----- messages.PieceUpdate = function(data) { @@ -238,6 +258,7 @@ function startup() { status_node.innerHTML = 'js-done' dragthresh = 5; space = document.getElementById('space'); + logdiv = document.getElementById("log"); svg_ns = space.getAttribute('xmlns'); es = new EventSource("/_/updates/"+ctoken+'/'+gen); diff --git a/templates/session.tera b/templates/session.tera index 04443673..f4effe7d 100644 --- a/templates/session.tera +++ b/templates/session.tera @@ -21,12 +21,10 @@ {{ piece.1 }} {%- endfor %} -

-

- Log item 1 -
- Log item 2 -
+ +
+
Log item 1
+
Log item 2
Log item 3
Log item 4 @@ -44,8 +42,7 @@ Log item 10
Log item 11 -
- Log item 12 -
+
Log item 12 +

-- 2.30.2