diff --git a/mod/ovis-composer/main.js b/mod/ovis-composer/main.js index 6828e83..5b95a51 100644 --- a/mod/ovis-composer/main.js +++ b/mod/ovis-composer/main.js @@ -81,6 +81,7 @@ import { parseJson, queueLabel, shortPath, + stillSameAgent, statusText, targetAgent, } from "./view.js"; @@ -294,6 +295,25 @@ export default class Composer extends View { cx.notify(); } + /** + * The composer is brought up to date with the focus - and told whether an + * event that began before may go on. + * + * The focus is polled every 500 ms, so for a moment the field can show the + * draft of agent A while the focus is already on B. An event that began in + * that window (a key, Enter, Stop, a drop) belongs to A: the sync saves A's + * text under A, and the event is dropped - it must not write A's text into + * B's draft or touch B's queue. + * + * @returns {boolean} true when the focus and the field are on the same agent + */ + syncOk(cx) { + const before = this.currentKey; + this.syncAgent(cx); + const target = this.target(); + return stillSameAgent({ before, after: this.currentKey, target: target ? agentKey(target) : "" }); + } + /** The draft of the agent (and its caret) goes into the field. */ showDraft(key) { const draft = this.state.drafts[key] ?? ""; @@ -461,6 +481,12 @@ export default class Composer extends View { /** The field changed: the draft follows and the menu looks at the new text. */ changed(cx) { + // The text that was just typed belongs to the agent the field showed; the + // sync keeps it there and the rest of this handler is not for another one. + if (!this.syncOk(cx)) { + cx.notify(); + return; + } const next = this.textarea.value(); const previous = this.value; this.value = next; @@ -944,6 +970,10 @@ export default class Composer extends View { /** The message goes out, or it waits in the queue. */ submit(text, cx, mode = "auto", force = false) { + if (!this.syncOk(cx)) { + this.notice("Agent się zmienił — wiadomość nie została wysłana. Zostaje w szkicu poprzedniego agenta.", "error", cx); + return; + } const agent = this.target(); if (!agent) { this.notice("Composer nie ma do kogo pisać — brak agenta.", "error", cx); @@ -1513,6 +1543,9 @@ export default class Composer extends View { * start it again. */ interrupt(agent, cx) { + // Stop is for the agent the person was looking at - not for one the focus + // moved to a moment ago. + if (!this.syncOk(cx) || agentKey(agent) !== this.currentKey) return; const key = agentKey(agent); const status = this.statusOf(agent); const flagged = status === "working" || status === "blocked"; @@ -1725,6 +1758,7 @@ export default class Composer extends View { * @param {"drop"|"paste"|"picker"} origin */ addFiles(entries, origin, cx) { + if (!this.syncOk(cx)) return; const agent = this.target(); if (!agent) { this.notice("Wybierz agenta — nie ma do kogo dodać pliku.", "error", cx); diff --git a/mod/ovis-composer/view.js b/mod/ovis-composer/view.js index 2276287..3d7df56 100644 --- a/mod/ovis-composer/view.js +++ b/mod/ovis-composer/view.js @@ -478,3 +478,20 @@ export function overLimit(text) { export function canAutoSend(ready, key) { return ready instanceof Set && ready.has(key); } + +/** + * Is the composer still talking to the agent whose draft the field shows? + * + * The field shows the draft of `before` (the agent the composer was synced to + * when an event began). After a sync the focus may have moved: then the event + * belongs to an agent that is not the target any more, and it must not touch + * the draft or the queue of the new one. + * + * @param {{before:string, after:string, target:string}} keys - `before`: the + * key the composer had before the sync; `after`: the key it has now; + * `target`: the key of the agent under the focus ("" for none) + * @returns {boolean} + */ +export function stillSameAgent({ before, after, target }) { + return before !== "" && before === after && before === target; +} diff --git a/tests/view.test.mjs b/tests/view.test.mjs index 71c3384..549483e 100644 --- a/tests/view.test.mjs +++ b/tests/view.test.mjs @@ -69,3 +69,15 @@ test("queueLabel for every state", () => { assert.equal(queueLabel({ state: "uncertain" }), "Nie potwierdzono"); assert.equal(queueLabel({ state: "countdown", left: 2 }), "Wysyłam za 2 s"); }); + +import { stillSameAgent } from "../mod/ovis-composer/view.js"; + +test("stillSameAgent: only when the field and the focus are on one agent", () => { + assert.equal(stillSameAgent({ before: "t2", after: "t2", target: "t2" }), true); + // the focus moved to t3 while the field still showed t2: the sync switched the field + assert.equal(stillSameAgent({ before: "t2", after: "t3", target: "t3" }), false); + // the focus is on something that is not an agent + assert.equal(stillSameAgent({ before: "t2", after: "", target: "" }), false); + assert.equal(stillSameAgent({ before: "", after: "", target: "" }), false); + assert.equal(stillSameAgent({ before: "", after: "t2", target: "t2" }), false); +});