Skip to content

Commit

Permalink
nats: one subject for all patches in a project
Browse files Browse the repository at this point in the history
  • Loading branch information
williamstein committed Jan 25, 2025
1 parent f03c3b0 commit 2fa9fd2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
6 changes: 5 additions & 1 deletion docs/nats/devlog.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,11 @@ Plan.
- [x] Use a kv store hosted on nats to trac syncstring objects as before. This means anybody can participate \(browser, compute server, project\) without any need to contact the database, hence eliminating all proxying!
#now Next Goal \- collaborative file editing! This requires implementing the "ordered patches list" but on jetstream. Similar to the nats SyncTable I wrote yesterday, except will use jetstream directly, since it is an event stream, after all.
[x] Next Goal \- collaborative file editing \-\- some sort of "proof of concept"! This requires implementing the "ordered patches list" but on jetstream. Similar to the nats SyncTable I wrote yesterday, except will use jetstream directly, since it is an event stream, after all.
- [ ] synctable\-stream: change to one big stream for the whole project but **consume** a specific subject in that stream?
[ ] cursors \- an ephemeral table
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ export class Terminal<T extends CodeEditorState = CodeEditorState> {
};

async connect(): Promise<void> {
if (this.path == "nats.term") {
if (this.path.startsWith("nats/")) {
return await this.connectNats();
}
this.assert_not_closed();
Expand Down
11 changes: 7 additions & 4 deletions src/packages/util/nats/synctable-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export class SyncTableStream extends EventEmitter {
private primaryKeys: string[];
private project_id: string;
private streamName: string;
private streamSubject: string;
private path: string;
private subject: string;
private string_id: string;
Expand Down Expand Up @@ -72,7 +73,8 @@ export class SyncTableStream extends EventEmitter {
query[table][0].string_id = this.string_id = this.sha1(
`${this.project_id}${this.path}`,
);
this.streamName = `${this.table}-${query[table][0].string_id}`;
this.streamName = `project-${this.project_id}-${this.table}`;
this.streamSubject = `project.${this.project_id}.${this.table}.>`;
this.subject = `project.${this.project_id}.${this.table}.${query[table][0].string_id}`;
this.primaryKeys = client_db.primary_keys(table);
}
Expand All @@ -82,13 +84,14 @@ export class SyncTableStream extends EventEmitter {
try {
await jsm.streams.add({
name: this.streamName,
subjects: [this.subject],
subjects: [this.streamSubject],
compression: "s2",
});
} catch (_err) {
} catch (err) {
console.log("createStream", err);
// probably already exists
await jsm.streams.update(this.streamName, {
subjects: [this.subject],
subjects: [this.streamSubject],
compression: "s2" as any,
});
}
Expand Down

0 comments on commit 2fa9fd2

Please sign in to comment.