107 lines
5.3 KiB
JavaScript
107 lines
5.3 KiB
JavaScript
// Several people on one jumphp.net board: the pure logic of the Board window (board.js), tested with node --test
|
|
// (board-merge.test.mjs). The site has the same file (jumphp-playground assets/board-merge.js): keep them alike.
|
|
|
|
/** the fields of a card a person edits (the rest: its place, its issue, its pictures, follows the server) */
|
|
export const CARD_FIELDS = ['title', 'body', 'labels', 'assignee', 'due', 'priority', 'checklist', 'milestoneId'];
|
|
|
|
/** equal as data (arrays and objects compared by value, null and undefined and '' alike for scalars) */
|
|
export function same(a, b) {
|
|
const norm = (v) => (v === undefined || v === '' ? null : v);
|
|
a = norm(a); b = norm(b);
|
|
if (a === b) return true;
|
|
if (typeof a !== 'object' || typeof b !== 'object' || a === null || b === null) return false;
|
|
return JSON.stringify(a) === JSON.stringify(b);
|
|
}
|
|
|
|
/**
|
|
* A three-way merge of one card, field by field: base = the card when the edit started, mine = the edited values, theirs = the card
|
|
* as the server has it now. A field only I changed is kept, a field only they changed is theirs, a field we both changed the same way
|
|
* is fine; a field we both changed differently is a conflict (the person chooses).
|
|
* @returns {{fields: object, conflicts: Array<{field: string, base: any, mine: any, theirs: any}>}} fields: what to send again
|
|
*/
|
|
export function mergeCard(base, mine, theirs, fields = CARD_FIELDS) {
|
|
const out = {};
|
|
const conflicts = [];
|
|
for (const field of fields) {
|
|
if (!(field in mine)) continue;
|
|
if (same(mine[field], base[field])) continue; // I did not change it: theirs stays
|
|
if (same(theirs[field], base[field]) || same(theirs[field], mine[field])) { out[field] = mine[field]; continue; }
|
|
conflicts.push({ field, base: base[field], mine: mine[field], theirs: theirs[field] });
|
|
}
|
|
return { fields: out, conflicts };
|
|
}
|
|
|
|
/** the fields to send once the person chose, per conflicting field, 'mine' or 'theirs' */
|
|
export function resolve(merge, choices) {
|
|
const out = { ...merge.fields };
|
|
for (const c of merge.conflicts) if ((choices[c.field] || 'mine') === 'mine') out[c.field] = c.mine;
|
|
return out;
|
|
}
|
|
|
|
/**
|
|
* The events of the live channel applied to a board {cards, columns, milestones}: an item saved replaces the one it has (never by an
|
|
* older version), an item deleted goes. Returns what changed: {cards: Set of ids, columns: bool, milestones: bool, notes: [events], http: [events]}.
|
|
*/
|
|
export function applyEvents(board, events, key = (x) => String(x)) {
|
|
const changed = { cards: new Set(), columns: false, milestones: false, notes: [], http: [], members: false };
|
|
const upsert = (list, item, isCard) => {
|
|
const i = list.findIndex((x) => key(x.id) === key(item.id));
|
|
if (i < 0) { list.push(item); return true; }
|
|
if (isCard && (item.version ?? 0) < (list[i].version ?? 0)) return false; // an older message arrives late: keep the newer
|
|
list[i] = { ...list[i], ...item };
|
|
return true;
|
|
};
|
|
const drop = (list, id) => {
|
|
const i = list.findIndex((x) => key(x.id) === key(id));
|
|
if (i >= 0) list.splice(i, 1);
|
|
return i >= 0;
|
|
};
|
|
for (const e of events || []) {
|
|
switch (e.type) {
|
|
case 'card.saved': if (upsert(board.cards, e.card, true)) changed.cards.add(key(e.card.id)); break;
|
|
case 'card.deleted': if (drop(board.cards, e.id)) changed.cards.add(key(e.id)); break;
|
|
case 'column.saved': upsert(board.columns, e.column); changed.columns = true; break;
|
|
case 'column.deleted': drop(board.columns, e.id); changed.columns = true; break;
|
|
case 'milestone.saved': if (board.milestones) { upsert(board.milestones, e.milestone); changed.milestones = true; } break;
|
|
case 'milestone.deleted': if (board.milestones) { drop(board.milestones, e.id); changed.milestones = true; } break;
|
|
case 'note.saved': case 'note.deleted': changed.notes.push(e); break;
|
|
case 'http.saved': case 'http.deleted': changed.http.push(e); break;
|
|
default: break;
|
|
}
|
|
}
|
|
return changed;
|
|
}
|
|
|
|
/**
|
|
* Who is on the board, one entry per person (several windows of one person: one avatar; "ide" when one of them is the IDE), and
|
|
* who edits what: {people: [{login, name, avatar, via: Set}], editing: Map("card:12" -> [{login, name}])}. My own window is left out of
|
|
* `editing` (my own edits need no badge).
|
|
*/
|
|
export function presence(list, myClient) {
|
|
const people = new Map();
|
|
const editing = new Map();
|
|
for (const p of list || []) {
|
|
const person = people.get(p.login) || { login: p.login, name: p.name || p.login, avatar: p.avatar || null, via: new Set() };
|
|
person.via.add(p.via || 'web');
|
|
people.set(p.login, person);
|
|
if (p.editing && p.client !== myClient) {
|
|
const who = editing.get(p.editing) || [];
|
|
if (!who.some((w) => w.login === p.login)) who.push({ login: p.login, name: p.name || p.login });
|
|
editing.set(p.editing, who);
|
|
}
|
|
}
|
|
return { people: [...people.values()], editing };
|
|
}
|
|
|
|
/** a page's / window's random id (X-Board-Client) */
|
|
export function clientId() {
|
|
const bytes = new Uint8Array(12);
|
|
globalThis.crypto.getRandomValues(bytes);
|
|
return [...bytes].map((b) => b.toString(16).padStart(2, '0')).join('');
|
|
}
|
|
|
|
/** the delay before the n-th reconnection (seconds): 1, 2, 4, 8, 15, 30, 30... with a little jitter */
|
|
export function backoff(attempt, random = Math.random) {
|
|
const base = [1, 2, 4, 8, 15, 30][Math.min(attempt, 5)];
|
|
return base * (0.8 + random() * 0.4);
|
|
}
|