Skip to content

Commit

Permalink
signal
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-the-crayon committed May 6, 2024
1 parent 33eabe1 commit 8645b6c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
18 changes: 11 additions & 7 deletions src/FileData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Signal, type SignalSetter, type SimpleLogger } from "@lmstudio/lms-common";
import { Signal, type Setter, type SimpleLogger } from "@lmstudio/lms-common";
import { isAvailable, type StripNotAvailable } from "@lmstudio/lms-common/dist/LazySignal";
import { existsSync, writeFileSync } from "fs";
import { mkdir, readFile, watch } from "fs/promises";
import path from "path";
Expand Down Expand Up @@ -28,7 +29,7 @@ export class FileData<TData, TSerialized> {
return this.internalDataSignal;
}
private readonly internalDataSignal!: Signal<TData>;
private readonly setData!: SignalSetter<TData>;
private readonly setData!: Setter<TData>;
private lastWroteString: string | null = null;
private initializationState: InitializationState = { type: "notStarted" };
public constructor(
Expand Down Expand Up @@ -75,7 +76,7 @@ export class FileData<TData, TSerialized> {
if (data === null) {
data = this.defaultData;
}
this.setData(data);
this.setData(data as StripNotAvailable<TData>);
this.startWatcher().catch(e => {
this.logger?.error(`Watcher failed: ${e}`);
});
Expand All @@ -89,8 +90,8 @@ export class FileData<TData, TSerialized> {
if (event.eventType === "change") {
this.logger?.debug("File changed, reading data");
const data: TData | null = await this.readData();
if (data !== null) {
this.setData(data);
if (data !== null && isAvailable(data)) {
this.setData(data as any);
}
}
}
Expand Down Expand Up @@ -128,12 +129,15 @@ export class FileData<TData, TSerialized> {
}

public set(data: TData) {
if (!isAvailable(data)) {
throw new Error("Cannot set data to NOT_AVAILABLE");
}
this.setData(data);
this.writeData(this.dataSignal.get());
}

public setWithImmer(producer: (draft: TData) => void) {
this.setData.withImmer(producer);
public setWithProducer(producer: (draft: TData) => void) {
this.setData.withProducer(producer);
this.writeData(this.dataSignal.get());
}

Expand Down
2 changes: 1 addition & 1 deletion src/createClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ async function maybeTryStartServer(logger: SimpleLogger, startServerOpts: StartS
${chalk.greenBright("~/.cache/lm-studio/.internal/cli-pref.json")}
`);
}
pref.setWithImmer(draft => {
pref.setWithProducer(draft => {
draft.autoStartServer = cont;
});
if (!cont) {
Expand Down
2 changes: 1 addition & 1 deletion src/subcommands/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ export const load = command({
}
lastLoadedModels.unshift(model.path);
logger.debug("Updating cliPref");
cliPref.setWithImmer(draft => {
cliPref.setWithProducer(draft => {
// Keep only the last 20 loaded models
draft.lastLoadedModels = lastLoadedModels.slice(0, 20);
});
Expand Down
2 changes: 1 addition & 1 deletion src/subcommands/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ export async function startServer(
},
},
]);
cliPref.setWithImmer(pref => {
cliPref.setWithProducer(pref => {
pref.autoLaunchMinimizedWarned = true;
});
}
Expand Down

0 comments on commit 8645b6c

Please sign in to comment.