File size: 1,029 Bytes
970fc3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function createAuthoringAccess(makeId) {
  const sessions = /* @__PURE__ */ new Map();
  function check(input, write = false) {
    const ref = input?.authoringSession;
    if (!ref) return null;
    const session = sessions.get(ref.id);
    if (!session || session.epoch !== ref.epoch) throw Error("Operation expired. Please retry.");
    if (write && session.mode !== "edit") throw Error("Enter Edit mode to change the scene");
    return session;
  }
  return { check, transition(input) {
    if (!["edit", "explore"].includes(input.mode)) throw Error("Invalid mode");
    let session = input.id ? sessions.get(input.id) : null;
    if (input.id && (!session || session.epoch !== input.epoch)) throw Error("Mode changed. Please refresh.");
    session = { id: session?.id || makeId(), mode: input.mode, epoch: (session?.epoch || 0) + 1 };
    sessions.set(session.id, session);
    if (sessions.size > 100) sessions.delete(sessions.keys().next().value);
    return { ...session };
  } };
}
export {
  createAuthoringAccess
};