From 2ac7be6218ea470607a5a7fbb3b90e48c04e5d38 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Sat, 12 Feb 2022 10:21:11 +0100 Subject: [PATCH] state: add isReceiptBefore --- components/app.js | 13 +++++-------- state.js | 13 +++++++++++++ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/components/app.js b/components/app.js index 320fff1..2ef8cb9 100644 --- a/components/app.js +++ b/components/app.js @@ -16,7 +16,7 @@ import ScrollManager from "./scroll-manager.js"; import Dialog from "./dialog.js"; import { html, Component, createRef } from "../lib/index.js"; import { strip as stripANSI } from "../lib/ansi.js"; -import { SERVER_BUFFER, BufferType, ReceiptType, ServerStatus, Unread, State, getServerName, receiptFromMessage, isMessageBeforeReceipt } from "../state.js"; +import { SERVER_BUFFER, BufferType, ReceiptType, ServerStatus, Unread, State, getServerName, receiptFromMessage, isReceiptBefore, isMessageBeforeReceipt } from "../state.js"; import commands from "../commands.js"; import { setup as setupKeybindings } from "../keybindings.js"; import * as store from "../store.js"; @@ -144,14 +144,11 @@ function getLatestReceipt(bufferStore, server, type) { let buffers = bufferStore.list(server); let last = null; for (let buf of buffers) { - if (!buf.receipts || buf.name === "*") { + if (buf.name === "*") { continue; } - let receipt = buf.receipts[type]; - if (!receipt) { - continue; - } - if (!last || receipt.time > last.time) { + let receipt = getReceipt(buf, type); + if (isReceiptBefore(last, receipt)) { last = receipt; } } @@ -878,7 +875,7 @@ export default class App extends Component { let from = lastReceipt; let stored = this.bufferStore.get({ name: target.name, server: client.params }); let readReceipt = getReceipt(stored, ReceiptType.READ); - if (readReceipt && readReceipt.time > from.time) { + if (isReceiptBefore(from, readReceipt)) { from = readReceipt; } diff --git a/state.js b/state.js index b2e4aeb..8b9f9fe 100644 --- a/state.js +++ b/state.js @@ -95,6 +95,19 @@ export function receiptFromMessage(msg) { return { time: msg.tags.time }; } +export function isReceiptBefore(a, b) { + if (!b) { + return false; + } + if (!a) { + return true; + } + if (!a.time || !b.time) { + throw new Error("Missing receipt time"); + } + return a.time <= b.time; +} + export function isMessageBeforeReceipt(msg, receipt) { if (!receipt) { return false;