Remove unnecessary assignments & spreads

This commit is contained in:
Snarling 2022-08-16 12:24:50 -04:00
parent fbee07ffd7
commit 8cbb8aac2e

@ -451,18 +451,8 @@ export function Root(props: IProps): React.ReactElement {
const newPos = editorRef.current.getPosition(); const newPos = editorRef.current.getPosition();
if (newPos === null) return; if (newPos === null) return;
if (currentScript !== null) { if (currentScript !== null) {
currentScript = { ...currentScript, code: newCode, lastPosition: newPos }; currentScript.code = newCode;
const curIndex = openScripts.findIndex( currentScript.lastPosition = newPos;
(script) =>
currentScript !== null &&
script.fileName === currentScript.fileName &&
script.hostname === currentScript.hostname,
);
const newArr = [...openScripts];
const tempScript = currentScript;
tempScript.code = newCode;
newArr[curIndex] = tempScript;
openScripts = [...newArr];
} }
try { try {
infLoop(newCode); infLoop(newCode);
@ -608,24 +598,15 @@ export function Root(props: IProps): React.ReactElement {
rerender(); rerender();
} }
function reorder(list: Array<OpenScript>, startIndex: number, endIndex: number): OpenScript[] { function reorder(list: OpenScript[], startIndex: number, endIndex: number): void {
const result = Array.from(list); const [removed] = list.splice(startIndex, 1);
const [removed] = result.splice(startIndex, 1); list.splice(endIndex, 0, removed);
result.splice(endIndex, 0, removed);
return result;
} }
function onDragEnd(result: any): void { function onDragEnd(result: any): void {
// Dropped outside of the list // Dropped outside of the list
if (!result.destination) { if (!result.destination) return;
result; reorder(openScripts, result.source.index, result.destination.index);
return;
}
const items = reorder(openScripts, result.source.index, result.destination.index);
openScripts = items;
} }
function currentTabIndex(): number | undefined { function currentTabIndex(): number | undefined {
@ -650,17 +631,17 @@ export function Root(props: IProps): React.ReactElement {
} }
} }
currentScript = { ...openScripts[index] }; currentScript = openScripts[index];
if (editorRef.current !== null && openScripts[index] !== null) { if (editorRef.current !== null && openScripts[index] !== null) {
if (openScripts[index].model === undefined || openScripts[index].model.isDisposed()) { if (currentScript.model === undefined || currentScript.model.isDisposed()) {
regenerateModel(openScripts[index]); regenerateModel(currentScript);
} }
editorRef.current.setModel(openScripts[index].model); editorRef.current.setModel(currentScript.model);
editorRef.current.setPosition(openScripts[index].lastPosition); editorRef.current.setPosition(currentScript.lastPosition);
editorRef.current.revealLineInCenter(openScripts[index].lastPosition.lineNumber); editorRef.current.revealLineInCenter(currentScript.lastPosition.lineNumber);
updateRAM(openScripts[index].code); updateRAM(currentScript.code);
editorRef.current.focus(); editorRef.current.focus();
} }
} }
@ -668,18 +649,10 @@ export function Root(props: IProps): React.ReactElement {
function onTabClose(index: number): void { function onTabClose(index: number): void {
// See if the script on the server is up to date // See if the script on the server is up to date
const closingScript = openScripts[index]; const closingScript = openScripts[index];
const savedScriptIndex = openScripts.findIndex( const savedScriptCode = closingScript.code;
(script) => script.fileName === closingScript.fileName && script.hostname === closingScript.hostname, const wasCurrentScript = openScripts[index] === currentScript;
);
let savedScriptCode = "";
if (savedScriptIndex !== -1) {
savedScriptCode = openScripts[savedScriptIndex].code;
}
const server = GetServer(closingScript.hostname);
if (server === null) throw new Error(`Server '${closingScript.hostname}' should not be null, but it is.`);
const serverScriptIndex = server.scripts.findIndex((script) => script.filename === closingScript.fileName); if (dirty(index)) {
if (serverScriptIndex === -1 || savedScriptCode !== server.scripts[serverScriptIndex].code) {
PromptEvent.emit({ PromptEvent.emit({
txt: `Do you want to save changes to ${closingScript.fileName} on ${closingScript.hostname}?`, txt: `Do you want to save changes to ${closingScript.fileName} on ${closingScript.hostname}?`,
resolve: (result: boolean | string) => { resolve: (result: boolean | string) => {
@ -693,30 +666,19 @@ export function Root(props: IProps): React.ReactElement {
} }
if (openScripts.length > 1) { if (openScripts.length > 1) {
openScripts = openScripts.filter((value, i) => i !== index); openScripts.splice(index, 1);
const indexOffset = openScripts.length === index ? -1 : 0;
let indexOffset = -1;
if (openScripts[index + indexOffset] === undefined) {
indexOffset = 1;
if (openScripts[index + indexOffset] === undefined) {
indexOffset = 0;
}
}
// Change current script if we closed it // Change current script if we closed it
currentScript = openScripts[index + indexOffset]; currentScript = wasCurrentScript ? openScripts[index + indexOffset] : (currentScript as OpenScript);
if (editorRef.current !== null) { if (editorRef.current !== null) {
if ( if (currentScript.model.isDisposed() || !currentScript.model) {
openScripts[index + indexOffset].model === undefined || regenerateModel(currentScript);
openScripts[index + indexOffset].model === null ||
openScripts[index + indexOffset].model.isDisposed()
) {
regenerateModel(openScripts[index + indexOffset]);
} }
editorRef.current.setModel(openScripts[index + indexOffset].model); editorRef.current.setModel(currentScript.model);
editorRef.current.setPosition(openScripts[index + indexOffset].lastPosition); editorRef.current.setPosition(currentScript.lastPosition);
editorRef.current.revealLineInCenter(openScripts[index + indexOffset].lastPosition.lineNumber); editorRef.current.revealLineInCenter(currentScript.lastPosition.lineNumber);
editorRef.current.focus(); editorRef.current.focus();
} }
rerender(); rerender();