EDITOR: fix disposing models (#1026)

This commit is contained in:
Caldwell 2024-01-10 00:45:31 +01:00 committed by GitHub
parent 82511e5030
commit 6f8a59593a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 23 deletions

@ -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

@ -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;
@ -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);
}
}

@ -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";
@ -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;
@ -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;

@ -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 {
@ -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 };