Skip to content

Combine viewing and editing sessionIds into 1 session #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 25, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ app.use(async (ctx) => {

// Creates various IDs
const docId = uuid();
const sessionEditingId = generateShortId();
documents.set(sessionEditingId, [docId, false]);
const sessionViewingId = generateShortId();
documents.set(sessionViewingId, [docId, true]);
const sessionId = generateShortId();
documents.set(sessionId, [docId, false]);

const connection = db.connect(undefined, { docId, readOnly: false });
const doc = connection.get(COLLECTION_NAME, docId);
Expand All @@ -57,18 +55,26 @@ app.use(async (ctx) => {
}
});
});
ctx.body = { docId, sessionEditingId, sessionViewingId };
ctx.body = { docId, sessionId };
return;
}

const sessionId = ctx.path.substr(1);
const [docId, readOnly] = getSessionDetails(sessionId);
const [docId, defaultReadOnly] = getSessionDetails(sessionId);

if (docId === null) {
ctx.status = 404;
return;
}

if (ctx.method === 'PATCH') {
const { defaultReadOnly } = ctx.request.body;

documents.set(sessionId, [docId, defaultReadOnly]);
ctx.status = 200;
return;
}

if (ctx.method !== 'GET') {
ctx.status = 405;
return;
Expand All @@ -87,9 +93,9 @@ app.use(async (ctx) => {
break;
}
});
db.listen(ws, { docId, readOnly }); // docId and readOnly is passed to 'connect' middleware as ctx.req
db.listen(ws, { docId, defaultReadOnly }); // docId and defaultReadOnly is passed to 'connect' middleware as ctx.req
} else {
ctx.body = { docId, readOnly };
ctx.body = { docId, defaultReadOnly };
}
});

Expand Down