Skip to content

Commit

Permalink
nats: rewrite open files tracker to use new dkv
Browse files Browse the repository at this point in the history
  • Loading branch information
williamstein committed Feb 9, 2025
1 parent df98481 commit 0a7eb65
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 260 deletions.
4 changes: 2 additions & 2 deletions src/packages/frontend/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,14 +348,14 @@ class Client extends EventEmitter implements WebappClient {
touchOpenFile = async ({
project_id,
path,
id,
// id,
}: {
project_id: string;
path: string;
id?: number;
}) => {
const x = await this.nats_client.openFiles(project_id);
await x.touch({ path, id });
await x.touch({ path });
};
}

Expand Down
7 changes: 5 additions & 2 deletions src/packages/frontend/nats/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { type ProjectApi, initProjectApi } from "@cocalc/nats/project-api";
import { type BrowserApi, initBrowserApi } from "@cocalc/nats/browser-api";
import { getPrimusConnection } from "@cocalc/nats/primus";
import { isValidUUID } from "@cocalc/util/misc";
import { OpenFiles } from "@cocalc/nats/sync/open-files";
import { createOpenFiles, OpenFiles } from "@cocalc/nats/sync/open-files";
import { PubSub } from "@cocalc/nats/sync/pubsub";
import type { ChatOptions } from "@cocalc/util/types/llm";
import { kv, type KVOptions } from "@cocalc/nats/sync/kv";
Expand Down Expand Up @@ -333,10 +333,13 @@ export class NatsClient {

openFiles = reuseInFlight(async (project_id: string) => {
if (this.openFilesCache[project_id] == null) {
this.openFilesCache[project_id] = new OpenFiles({
this.openFilesCache[project_id] = await createOpenFiles({
project_id,
env: await this.getEnv(),
});
this.openFilesCache[project_id].on("closed", () => {
delete this.openFilesCache[project_id];
});
}
return this.openFilesCache[project_id]!;
});
Expand Down
12 changes: 11 additions & 1 deletion src/packages/nats/sync/dkv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,19 @@ export class DKV extends EventEmitter {
if (this.generalDKV == null) {
throw Error("closed");
}
return this.generalDKV.time(
const times = this.generalDKV.time(
key ? `${this.prefix}.${this.sha1(key)}` : undefined,
);
if (key != null || times == null) {
return times;
}
const obj = this.generalDKV.get();
const x: any = {};
for (const k in obj) {
const { key } = obj[k];
x[key] = times[k];
}
return x;
};

get = (key?) => {
Expand Down
2 changes: 1 addition & 1 deletion src/packages/nats/sync/general-dkv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export class GeneralDKV extends EventEmitter {
delete this.merge;
};

private handleRemoteChange = ({ key, remote, prev }) => {
private handleRemoteChange = ({ key, value: remote, prev }) => {
const local = this.local[key];
let value: any = remote;
if (local !== undefined) {
Expand Down
Loading

0 comments on commit 0a7eb65

Please sign in to comment.