Skip to content

Commit

Permalink
EDITOR: fix disposing models (bitburner-official#1026)
Browse files Browse the repository at this point in the history
  • Loading branch information
Caldwell-74 authored and G4mingJon4s committed Mar 23, 2024
1 parent b0960f3 commit 860b35c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/ScriptEditor/ui/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function Editor({ onMount, onChange }: EditorProps) {
// Unmounting
return () => {
subscription.current?.dispose();
editorRef.current?.getModel()?.dispose();
monaco.editor.getModels().forEach((model) => model.dispose());
editorRef.current?.dispose();
};
// this eslint ignore instruction can potentially cause unobvious bugs
Expand Down
12 changes: 3 additions & 9 deletions src/ScriptEditor/ui/OpenScript.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ContentFilePath } from "../../Paths/ContentFile";
import monaco, { editor, Position } from "monaco-editor";
import { editor, Position } from "monaco-editor";
import { makeModel } from "./utils";

type ITextModel = editor.ITextModel;

Expand All @@ -24,13 +25,6 @@ export class OpenScript {
}

regenerateModel(): void {
this.model = editor.createModel(
this.code,
this.isTxt ? "plaintext" : "javascript",
monaco.Uri.from({
scheme: "file",
path: `${this.hostname}/${this.path}`,
}),
);
this.model = makeModel(this.hostname, this.path, this.code);
}
}
15 changes: 4 additions & 11 deletions src/ScriptEditor/ui/ScriptEditorRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { PromptEvent } from "../../ui/React/PromptManager";

import { useRerender } from "../../ui/React/hooks";

import { dirty, getServerCode } from "./utils";
import { dirty, getServerCode, makeModel } from "./utils";
import { OpenScript } from "./OpenScript";
import { Tabs } from "./Tabs";
import { Toolbar } from "./Toolbar";
Expand Down Expand Up @@ -191,14 +191,7 @@ function Root(props: IProps): React.ReactElement {
code,
props.hostname,
new monaco.Position(0, 0),
monaco.editor.createModel(
code,
filename.endsWith(".txt") ? "plaintext" : "javascript",
monaco.Uri.from({
scheme: "file",
path: `${props.hostname}/${filename}`,
}),
),
makeModel(props.hostname, filename, code),
);
openScripts.push(newScript);
currentScript = newScript;
Expand Down Expand Up @@ -274,12 +267,12 @@ function Root(props: IProps): React.ReactElement {
// Save changes
closingScript.code = savedScriptCode;
saveScript(closingScript);
Router.toPage(Page.Terminal);
}
},
});
}

//unmounting the editor will dispose all, doesnt hurt to dispose on close aswell
closingScript.model.dispose();
openScripts.splice(index, 1);
if (openScripts.length === 0) {
currentScript = null;
Expand Down
13 changes: 11 additions & 2 deletions src/ScriptEditor/ui/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GetServer } from "../../Server/AllServers";

import { editor, Uri } from "monaco-editor";
import { OpenScript } from "./OpenScript";

function getServerCode(scripts: OpenScript[], index: number): string | null {
Expand All @@ -21,5 +21,14 @@ function reorder(list: unknown[], startIndex: number, endIndex: number): void {
const [removed] = list.splice(startIndex, 1);
list.splice(endIndex, 0, removed);
}
function makeModel(hostname: string, filename: string, code: string) {
const uri = Uri.from({
scheme: "file",
path: `${hostname}/${filename}`,
});
const language = filename.endsWith(".txt") ? "plaintext" : "javascript";
//if somehow a model already exist return it
return editor.getModel(uri) ?? editor.createModel(code, language, uri);
}

export { getServerCode, dirty, reorder };
export { getServerCode, dirty, reorder, makeModel };

0 comments on commit 860b35c

Please sign in to comment.