debounce updateRAM

This commit is contained in:
Snarling 2022-08-23 16:38:30 -04:00
parent efeb37fa52
commit 445e365959
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 callbackScript = helpers.string(ctx, "callbackScript", _callbackScript);
const wd = GetServer(SpecialServers.WorldDaemon); const wd = GetServer(SpecialServers.WorldDaemon);
if (!(wd instanceof Server)) if (!(wd instanceof Server)) throw new Error("WorldDaemon was not a normal server. This is a bug contact dev.");
throw new Error("WorldDaemon was not a normal server. This is a bug contact dev.");
const hackingRequirements = (): boolean => { const hackingRequirements = (): boolean => {
if (player.skills.hacking < wd.requiredHackingSkill) return false; if (player.skills.hacking < wd.requiredHackingSkill) return false;
if (!wd.hasAdminRights) return false; if (!wd.hasAdminRights) return false;

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

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

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