Merge pull request #4026 from Snarling/debounceUpdateRam

SCRIPT EDITOR: Debounce updateRAM calls.
This commit is contained in:
hydroflame 2022-08-23 17:43:17 -03:00 committed by GitHub
commit 821253363f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 31 additions and 36 deletions

@ -1278,8 +1278,7 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
const callbackScript = helpers.string(ctx, "callbackScript", _callbackScript);
const wd = GetServer(SpecialServers.WorldDaemon);
if (!(wd instanceof Server))
throw new Error("WorldDaemon was not a normal server. This is a bug contact dev.");
if (!(wd instanceof Server)) throw new Error("WorldDaemon was not a normal server. This is a bug contact dev.");
const hackingRequirements = (): boolean => {
if (player.skills.hacking < wd.requiredHackingSkill) return false;
if (!wd.hasAdminRights) return false;

@ -36,7 +36,6 @@ import { Faction } from "./Faction/Faction";
import { safetlyCreateUniqueServer } from "./Server/ServerHelpers";
import { SpecialServers } from "./Server/data/SpecialServers";
import { v2APIBreak } from "./utils/v2APIBreak";
import { Script } from "./Script/Script";
/* SaveObject.js
* Defines the object used to save/load games

@ -1,5 +1,4 @@
export enum RamCalculationErrorCode {
SyntaxError = -1,
ImportError = -2,
URLImportError = -3,
}

@ -1,4 +1,4 @@
import React, { useState, useEffect, useRef, useMemo } from "react";
import React, { useState, useEffect, useRef } from "react";
import Editor, { Monaco } from "@monaco-editor/react";
import * as monaco from "monaco-editor";
@ -240,22 +240,17 @@ export function Root(props: IProps): React.ReactElement {
}
}
const debouncedSetRAM = useMemo(
() =>
debounce((s, e) => {
setRAM(s);
setRamEntries(e);
const debouncedUpdateRAM = debounce((newCode: string) => {
updateRAM(newCode);
setUpdatingRam(false);
}, 300),
[],
);
}, 300);
async function updateRAM(newCode: string): Promise<void> {
function updateRAM(newCode: string): void {
if (currentScript != null && currentScript.isTxt) {
debouncedSetRAM("N/A", [["N/A", ""]]);
setRAM("N/A");
setRamEntries([["N/A", ""]]);
return;
}
setUpdatingRam(true);
const codeCopy = newCode + "";
const ramUsage = calculateRamUsage(props.player, codeCopy, props.player.getCurrentServer().scripts);
if (ramUsage.cost > 0) {
@ -265,25 +260,28 @@ export function Root(props: IProps): React.ReactElement {
entriesDisp.push([`${entry.name} (${entry.type})`, numeralWrapper.formatRAM(entry.cost)]);
}
debouncedSetRAM("RAM: " + numeralWrapper.formatRAM(ramUsage.cost), entriesDisp);
setRAM("RAM: " + numeralWrapper.formatRAM(ramUsage.cost));
setRamEntries(entriesDisp);
return;
}
let RAM = "";
const entriesDisp = [];
switch (ramUsage.cost) {
case RamCalculationErrorCode.ImportError: {
debouncedSetRAM("RAM: Import Error", [["Import Error", ""]]);
break;
}
case RamCalculationErrorCode.URLImportError: {
debouncedSetRAM("RAM: HTTP Import Error", [["HTTP Import Error", ""]]);
RAM = "RAM: Import Error";
entriesDisp.push(["Import Error", ""]);
break;
}
case RamCalculationErrorCode.SyntaxError:
default: {
debouncedSetRAM("RAM: Syntax Error", [["Syntax Error", ""]]);
RAM = "RAM: Syntax Error";
entriesDisp.push(["Syntax Error", ""]);
break;
}
}
return new Promise<void>(() => undefined);
setRAM(RAM);
setRamEntries(entriesDisp);
return;
}
// Formats the code
@ -446,7 +444,8 @@ export function Root(props: IProps): React.ReactElement {
// When the code is updated within the editor
function updateCode(newCode?: string): void {
if (newCode === undefined) return;
updateRAM(newCode);
setUpdatingRam(true);
debouncedUpdateRAM(newCode);
if (editorRef.current === null) return;
const newPos = editorRef.current.getPosition();
if (newPos === null) return;

@ -7,8 +7,7 @@ import { IPlayer } from "../PersonObjects/IPlayer";
import { dialogBoxCreate } from "../ui/React/DialogBox";
import { CrimeType } from "../utils/WorkType";
import { Work, WorkType } from "./Work";
import { newWorkStats, scaleWorkStats, WorkStats } from "./WorkStats";
import { BitNodeMultipliers } from "../BitNode/BitNodeMultipliers";
import { scaleWorkStats, WorkStats } from "./WorkStats";
import { calculateCrimeWorkStats } from "./formulas/Crime";
enum newCrimeType {