Merge final group into main #3

Merged
quic merged 10 commits from group-26d3b827-6587-46c2-a46e-001281299174 into main 2026-05-10 20:10:02 +00:00
2 changed files with 37 additions and 37 deletions
Showing only changes of commit 5ecb5f1076 - Show all commits

View File

@@ -11,6 +11,10 @@ export function PollList(
onVote: (optionId: string) => void, onVote: (optionId: string) => void,
onDelete: (optionId: string) => void, onDelete: (optionId: string) => void,
): HTMLElement { ): HTMLElement {
var currentOptions : { [x: string]: any; } | undefined = undefined
var currentVotes : { [x: string]: any; } | undefined = undefined
const wrapper = document.createElement("div"); const wrapper = document.createElement("div");
wrapper.className = "poll-list-wrapper"; wrapper.className = "poll-list-wrapper";
@@ -42,25 +46,29 @@ export function PollList(
votes: number; votes: number;
voted: boolean; voted: boolean;
}> = []; }> = [];
if (currentOptions && currentVotes){
// Tally votes per option // Tally votes per option
const tally = new Map<string, number>(); const tally = new Map<string, number>();
for (const optionId of yVotes.values()) { for (const optionId of Object.values(currentVotes)) {
tally.set(optionId, (tally.get(optionId) ?? 0) + 1); tally.set(optionId, (tally.get(optionId) ?? 0) + 1);
}
const myVote = currentVotes[userId] ?? null;
Object.entries(currentOptions).forEach(([id,record]) => {
console.log(`${record}: ${id}`)
entries.push({
id,
name: record.label,
votes: tally.get(id) ?? 0,
voted: myVote === id,
});
});
entries.sort((a, b) => b.votes - a.votes || a.name.localeCompare(b.name));
} }
const myVote = yVotes.get(userId) ?? null;
yOptions.forEach((record, id) => {
entries.push({
id,
name: record.label,
votes: tally.get(id) ?? 0,
voted: myVote === id,
});
});
entries.sort((a, b) => b.votes - a.votes || a.name.localeCompare(b.name));
return entries; return entries;
} }
@@ -119,9 +127,10 @@ export function PollList(
} }
}); });
} }
yOptions.observe(enforceAppendOnly(yOptions,(update : { [x: string]: any; }) => {currentOptions = update}, render));
yOptions.observe(enforceAppendOnly(yOptions,render)); yVotes.observe(enforceAppendOnly(yVotes,(update : { [x: string]: any; }) => {currentVotes = update},render));
yVotes.observe(enforceAppendOnly(yVotes,render)); currentOptions=yOptions.toJSON()
currentVotes=yVotes.toJSON()
render(); render();
return wrapper; return wrapper;

View File

@@ -4,30 +4,21 @@ import * as Y from "yjs";
* Enforces append-only logic on a Y.Map. * Enforces append-only logic on a Y.Map.
* Reverts any 'update' or 'delete' actions detected in the observer. * Reverts any 'update' or 'delete' actions detected in the observer.
*/ */
export function enforceAppendOnly(yMap: Y.Map<any>,render: () => void) { export function enforceAppendOnly(yMap: Y.Map<any>,update: (update : { [x: string]: any; }) => void, render: () => void) {
return (event: Y.YMapEvent<any>, transaction: Y.Transaction) => { return (event: Y.YMapEvent<any>, transaction: Y.Transaction) => {
// Avoid infinite loops: check if this change was var isOperationIllegal = false
// triggered by our own 'undo' logic.
if (transaction.origin === 'revert-logic') return;
event.keys.forEach((change, key) => { event.keys.forEach((change, key) => {
const { action, oldValue } = change; const { action, oldValue } = change;
if (action === 'update' || action === 'delete') { if (action === 'update' || action === 'delete') {
// Use the transaction to undo the illegal operation isOperationIllegal = true
yMap.doc?.transact(() => { console.log("Illegal Operation: "+action)
if (action === 'update' && oldValue !== undefined) {
// Revert to previous value
yMap.set(key, oldValue);
} else if (action === 'delete' && oldValue !== undefined) {
// Restore the deleted key
yMap.set(key, oldValue);
}
console.warn(`Illegal ${action} attempt on key: "${key}". Reverted.`);
}, 'revert-logic');
} }
}); });
if(!isOperationIllegal) {
render(); console.log("Updating Map!")
update(yMap.toJSON())
render();
}
}; };
} }