chiark / gitweb /
working on log updates etc.
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 5 Jul 2020 22:04:34 +0000 (23:04 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 5 Jul 2020 22:04:34 +0000 (23:04 +0100)
src/bin/server.rs
src/global.rs
src/sse.rs
templates/script.js
templates/session.tera

index 5cb031c1d3de9ffc237e3882fff5963d58047ccb..0da139fffb646b8d194cbad63bc882afaa906497 100644 (file)
@@ -156,10 +156,12 @@ fn api_grab(form : Json<ApiGrab>) -> 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
index 6c5603b905f0a2b1e76a93a4810b7f75be23d324..b90c371f3318bd1b39496480a1344b3c784683a0 100644 (file)
@@ -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)]
index 7e83ca5aa549cea38cfd62127881266d577b77bf..b360c4815eb547b9bfa70026da161248c5d48a13 100644 (file)
@@ -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();
     }
index 872ee1242265b28c807f45f40d5af335e3c17c80..85a45a622a8b03d474425c6f744a2804cf5664d3 100644 (file)
@@ -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);
index 044436736d941a2551c80a7ba7683a9d257df653..f4effe7defe15d379bfeea17c90e36dcf62f7402 100644 (file)
       <defs id="defs{{ piece.0 }}">{{ piece.1 }}</defs>
 {%- endfor %}
     </svg>
-<p>
-  <div style="overflow-y: scroll; height: 200px;">
-    Log item 1
-    <br>
-    Log item 2
-    <br>
+
+  <div id="log" style="overflow-y: scroll; height: 200px;">
+    <div id="log1">Log item 1</div>
+    <div id="log2">Log item 2</div>
     Log item 3
     <br>
     Log item 4
@@ -44,8 +42,7 @@
     Log item 10
     <br>
     Log item 11
-    <br>
-    Log item 12
-  </div>
+    <div id="log12">Log item 12</id>
+  </p>
 
 </body>