Compare commits
3 Commits
group-af5e
...
5ecb5f1076
| Author | SHA1 | Date | |
|---|---|---|---|
| 5ecb5f1076 | |||
| 043e813864 | |||
| 2abc0f8930 |
@@ -4,7 +4,7 @@
|
|||||||
"private": true,
|
"private": true,
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite",
|
"dev": "PORT=4444 npx y-webrtc & vite",
|
||||||
"build": "tsc --noEmit && vite build",
|
"build": "tsc --noEmit && vite build",
|
||||||
"preview": "vite preview"
|
"preview": "vite preview"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import * as Y from "yjs";
|
import * as Y from "yjs";
|
||||||
import { getDeadline } from "../state";
|
import { getDeadline } from "../state";
|
||||||
|
import { enforceAppendOnly } from "../yDocUtil";
|
||||||
|
|
||||||
const DEADLINE_DURATION_MS = 2 * 60 * 1000; // 2 minutes
|
const DEADLINE_DURATION_MS = 2 * 60 * 1000; // 2 minutes
|
||||||
|
|
||||||
@@ -79,7 +80,7 @@ export function DeadlineTimer(
|
|||||||
onClearDeadline();
|
onClearDeadline();
|
||||||
});
|
});
|
||||||
|
|
||||||
deadlineMap.observe(() => render());
|
deadlineMap.observe(enforceAppendOnly(deadlineMap,render));
|
||||||
render();
|
render();
|
||||||
|
|
||||||
return wrapper;
|
return wrapper;
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import * as Y from "yjs";
|
import * as Y from "yjs";
|
||||||
import type { OptionRecord } from "../state";
|
import type { OptionRecord } from "../state";
|
||||||
import { PollOption } from "./PollOption";
|
import { PollOption } from "./PollOption";
|
||||||
|
import { enforceAppendOnly } from "../yDocUtil";
|
||||||
|
|
||||||
export function PollList(
|
export function PollList(
|
||||||
yOptions: Y.Map<OptionRecord>,
|
yOptions: Y.Map<OptionRecord>,
|
||||||
@@ -10,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";
|
||||||
|
|
||||||
@@ -41,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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,9 +127,10 @@ export function PollList(
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
yOptions.observe(enforceAppendOnly(yOptions,(update : { [x: string]: any; }) => {currentOptions = update}, render));
|
||||||
yOptions.observeDeep(() => render());
|
yVotes.observe(enforceAppendOnly(yVotes,(update : { [x: string]: any; }) => {currentVotes = update},render));
|
||||||
yVotes.observe(() => render());
|
currentOptions=yOptions.toJSON()
|
||||||
|
currentVotes=yVotes.toJSON()
|
||||||
render();
|
render();
|
||||||
|
|
||||||
return wrapper;
|
return wrapper;
|
||||||
|
|||||||
@@ -28,7 +28,9 @@ export function initSync(roomId: string): AppSync {
|
|||||||
? "connecting"
|
? "connecting"
|
||||||
: "offline";
|
: "offline";
|
||||||
|
|
||||||
const provider = new WebrtcProvider(roomId, doc);
|
const provider = new WebrtcProvider(roomId, doc,{
|
||||||
|
signaling: ["ws://localhost:4444", "ws://lynxpi.ddns.net:4444"]
|
||||||
|
});
|
||||||
const persistence = new IndexeddbPersistence(roomId, doc);
|
const persistence = new IndexeddbPersistence(roomId, doc);
|
||||||
|
|
||||||
const syncConnectionStatus = (status: ConnectionStatus) => {
|
const syncConnectionStatus = (status: ConnectionStatus) => {
|
||||||
|
|||||||
24
src/yDocUtil.ts
Normal file
24
src/yDocUtil.ts
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
import * as Y from "yjs";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enforces append-only logic on a Y.Map.
|
||||||
|
* Reverts any 'update' or 'delete' actions detected in the observer.
|
||||||
|
*/
|
||||||
|
export function enforceAppendOnly(yMap: Y.Map<any>,update: (update : { [x: string]: any; }) => void, render: () => void) {
|
||||||
|
return (event: Y.YMapEvent<any>, transaction: Y.Transaction) => {
|
||||||
|
var isOperationIllegal = false
|
||||||
|
event.keys.forEach((change, key) => {
|
||||||
|
const { action, oldValue } = change;
|
||||||
|
|
||||||
|
if (action === 'update' || action === 'delete') {
|
||||||
|
isOperationIllegal = true
|
||||||
|
console.log("Illegal Operation: "+action)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if(!isOperationIllegal) {
|
||||||
|
console.log("Updating Map!")
|
||||||
|
update(yMap.toJSON())
|
||||||
|
render();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user