Connecting to the shared job sheet...
The form will appear when the shared answers have loaded.
JSON definition · shared/definitions/fill-together.json
View on GitHub
{
"title": "Job sheet",
"completeText": "Finish job sheet",
"description": "Filled together by the dispatcher and the technician",
"elements": [
{
"type": "panel",
"name": "dispatch",
"title": "Dispatcher",
"elements": [
{
"type": "text",
"name": "customer",
"title": "Customer",
"isRequired": true
},
{
"type": "text",
"name": "address",
"title": "Site address"
},
{
"type": "dropdown",
"name": "job_type",
"title": "Job type",
"choices": [
"Installation",
"Repair",
"Inspection"
]
},
{
"type": "comment",
"name": "dispatch_notes",
"title": "Notes for the technician"
}
]
},
{
"type": "panel",
"name": "onsite",
"title": "Technician",
"elements": [
{
"type": "comment",
"name": "findings",
"title": "What did you find?"
},
{
"type": "checkbox",
"name": "parts",
"title": "Parts used",
"choices": [
"Filter",
"Valve",
"Pump",
"Sensor"
],
"showOtherItem": true
},
{
"type": "text",
"name": "hours",
"title": "Hours on site",
"inputType": "number",
"min": 0,
"step": 0.25
},
{
"type": "boolean",
"name": "resolved",
"title": "Issue resolved?"
}
]
}
]
}
Client · shared/client/fill-together.js · sjs:fill-together.save
View on GitHub
// Ask the relay to save once and confirm the result to every participant
function completeJobSheet(sender, options) {
completionOptions = options;
if (responseId !== null) {
sender.completedHtml = `<h3>The job sheet has been saved.</h3><p>Response ID: ${responseId}</p>`;
return options.showSaveSuccess();
}
if (!ready || ws.readyState !== WebSocket.OPEN) {
sender.completedHtml = "<h3>The job sheet hasn't been saved.</h3>";
return options.showSaveError("Reconnect to the shared session, then try again.");
}
sender.completedHtml = "<h3>Saving the job sheet...</h3>";
options.showSaveInProgress();
saveOptions = options;
setLocked(true);
ws.send(JSON.stringify({ type: "complete", data: sender.data }));
}
Client · shared/client/fill-together.js · sjs:fill-together.client
View on GitHub
// Transport the plugin's answer and presence messages over a WebSocket
function applySharedState(message) {
const draft = survey?.data;
const previousSession = sessionId;
if (!collab) {
survey = new Model(message.seed);
survey.completeText = "Finish job sheet";
collab = new CollaborationPlugin(survey, { getInviteLink: partnerLink });
collab.onEvent.add((_, { message }) => {
if (ready && responseId === null && ws.readyState === WebSocket.OPEN) ws.send(JSON.stringify(message));
});
survey.onComplete.add(completeJobSheet);
mountSurvey(survey);
}
sessionId = message.sessionId;
ready = true;
collab.apply(message);
// Restore the local draft if an empty session replaces the disconnected one
if (previousSession && previousSession !== sessionId && !message.responseId && !message.peers.length) survey.data = draft;
form.hidden = false;
document.getElementById("collaboration-loading").hidden = true;
setLocked(false);
if (message.responseId) confirmCompletion(message.responseId, message.values);
}
Server · relay/fill-relay.js · sjs:fill-together.server
View on GitHub
// The relay runs in this app's process, on its port, and relays JSON: it never parses the definition
const rooms = new Map(); // room key => FillRoom { seed, values, clients }
function connect(ws, { recordId, roomKey, user, database, name }) { // relay/index.js checked who may open it
let room = rooms.get(roomKey);
if (!room) rooms.set(roomKey, (room = new FillRoom(...seed(database, recordId)))); // the definition + saved answers
room.cancelDrop(); // back within 2 s: keep the room
const client = { id: randomBytes(8).toString("hex"), name: displayName(name, user.name), slot: room.freeSlot() };
ws.client = { ...client, state: null, roomKey, database };
room.clients.add(ws);
send(ws, {
type: "init", clientId: client.id, name: client.name, colorIndex: FillRoom.colorIndex(client.slot), seed: room.seed,
values: room.values, peers: room.roster(ws), sessionId: room.sessionId, responseId: room.responseId,
});
}
function message(ws, raw) {
const msg = parse(raw);
const room = rooms.get(ws.client.roomKey);
if (!room || !isObject(msg)) return;
if (msg.type === "complete" && isObject(msg.data)) {
complete(ws, room, msg.data);
} else if (msg.type === "value" && typeof msg.key === "string" && room.responseId === null) { // last write wins
room.values[msg.key] = msg.value ?? null;
room.broadcast(ws, { type: "value", from: ws.client.id, key: msg.key, value: msg.value ?? null });
} else if (msg.type === "presence" && msg.state != null && raw.length <= 4096) { // stamp identity, relay
const retain = msg.retain !== false;
if (retain) ws.client.state = msg.state;
room.broadcast(ws, { type: "peer", retain, peer: FillRoom.peer(ws, msg.state) }, { droppable: !retain });
} // unknown types: ignore
}
function close(ws) {
const room = rooms.get(ws.client?.roomKey);
if (!room) return;
room.clients.delete(ws);
room.broadcast(ws, { type: "peer-left", clientId: ws.client.id });
if (room.clients.size === 0) room.dropAfter(2000, () => rooms.delete(ws.client.roomKey)); // 2 s after the last leaves
}
Server · relay/fill-relay.js · sjs:fill-together.complete
View on GitHub
// Save once per room session and send the same confirmation to every participant
function complete(ws, room, data) {
try {
if (room.responseId === null) {
room.responseId = insertResponse(ws.client.database, "job-sheet", data); // as save-response stores it
room.values = toValues(data);
}
for (const client of room.clients) send(client, { type: "completed", id: room.responseId, data: room.values });
} catch (error) {
console.error("fill-together: couldn't save the job sheet", error);
send(ws, { type: "save-error", error: "Couldn't save the job sheet. Please try again." });
}
}