group code popup
This commit is contained in:
@@ -5,6 +5,9 @@ globalThis.Wegwichtel ??= {};
|
||||
let database;
|
||||
let current = null;
|
||||
let generation = 0;
|
||||
let activity = 0;
|
||||
let sentActivity = 0;
|
||||
let touching = false;
|
||||
const key = (routeId, poiId) => `${routeId}:${poiId}`;
|
||||
async function store(mode, operation) {
|
||||
if (!database) database = new Promise((resolve, reject) => {
|
||||
@@ -45,6 +48,7 @@ globalThis.Wegwichtel ??= {};
|
||||
method: 'POST', credentials: 'same-origin', cache: 'no-store',
|
||||
headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body)
|
||||
});
|
||||
if (response.status === 204) return {};
|
||||
const payload = await response.json();
|
||||
if (!response.ok) {
|
||||
const error = new Error(t(response.status === 423 ? 'lost' : 'unavailable'));
|
||||
@@ -158,6 +162,23 @@ globalThis.Wegwichtel ??= {};
|
||||
if (savingGeneration !== generation) return false;
|
||||
return remote;
|
||||
}
|
||||
function noteActivity() { activity += 1; }
|
||||
async function keepActive() {
|
||||
if (!current?.token || activity === sentActivity || touching) return;
|
||||
const editing = current;
|
||||
const observed = activity;
|
||||
touching = true;
|
||||
try {
|
||||
await request(editing.routeId, editing.poiId, { heartbeat: true, token: editing.token });
|
||||
if (editing === current) sentActivity = observed;
|
||||
} catch (error) {
|
||||
if (error.status === 423) {
|
||||
sentActivity = observed;
|
||||
document.dispatchEvent(new Event('wegwichtel:point-lock-lost'));
|
||||
}
|
||||
} finally { touching = false; }
|
||||
}
|
||||
globalThis.setInterval?.(() => { void keepActive(); }, 30_000);
|
||||
function headersFor(path) {
|
||||
const match = /^\/routes\/(\d+)\/pois\/(\d+)(?:\/|$)/.exec(path);
|
||||
if (!match || !current || String(current.routeId) !== match[1] || String(current.poiId) !== match[2]) return {};
|
||||
@@ -167,7 +188,7 @@ globalThis.Wegwichtel ??= {};
|
||||
const version = response.headers?.get('X-Poi-Version');
|
||||
if (current && String(current.poiId) === response.headers?.get('X-Poi-Id') && version) current.version = version;
|
||||
}
|
||||
ns.GroupPoints = Object.freeze({ begin, release, remember, read, list, remove, prepare, headersFor, observe,
|
||||
ns.GroupPoints = Object.freeze({ begin, release, remember, read, list, remove, prepare, headersFor, observe, noteActivity, keepActive,
|
||||
acceptNew: async (routeId, poi) => { current = { ...await acquire(routeId, poi.id), routeId, poiId: poi.id }; },
|
||||
changed: () => document.dispatchEvent(new Event('wegwichtel:point-drafts-changed')) });
|
||||
}(globalThis.Wegwichtel));
|
||||
|
||||
Reference in New Issue
Block a user