mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2024-12-18 12:15:44 +01:00
BUGFIX: Fix unresolved promise in ns.prompt API (#1257)
This commit is contained in:
parent
e55387df4d
commit
35c32e2871
@ -32,7 +32,9 @@ True if the player clicks “Yes”; false if the player clicks “No”; or the
|
||||
|
||||
RAM cost: 0 GB
|
||||
|
||||
Prompts the player with a dialog box. Here is an explanation of the various options.
|
||||
Prompts the player with a dialog box and returns a promise. If the player cancels this dialog box (press X button or click outside the dialog box), the promise is resolved with a default value (empty string or "false"). If this API is called again while the old dialog box still exists, the old dialog box will be replaced with a new one, and the old promise will be resolved with the default value.
|
||||
|
||||
Here is an explanation of the various options.
|
||||
|
||||
- `options.type` is not provided to the function. If `options.type` is left out and only a string is passed to the function, then the default behavior is to create a boolean dialog box.
|
||||
|
||||
|
7
src/ScriptEditor/NetscriptDefinitions.d.ts
vendored
7
src/ScriptEditor/NetscriptDefinitions.d.ts
vendored
@ -7158,7 +7158,12 @@ export interface NS {
|
||||
* @remarks
|
||||
* RAM cost: 0 GB
|
||||
*
|
||||
* Prompts the player with a dialog box. Here is an explanation of the various options.
|
||||
* Prompts the player with a dialog box and returns a promise. If the player cancels this dialog box (press X button
|
||||
* or click outside the dialog box), the promise is resolved with a default value (empty string or "false"). If this
|
||||
* API is called again while the old dialog box still exists, the old dialog box will be replaced with a new one, and
|
||||
* the old promise will be resolved with the default value.
|
||||
*
|
||||
* Here is an explanation of the various options.
|
||||
*
|
||||
* - `options.type` is not provided to the function. If `options.type` is left out and
|
||||
* only a string is passed to the function, then the default behavior is to create a
|
||||
|
@ -18,19 +18,31 @@ interface Prompt {
|
||||
|
||||
export function PromptManager({ hidden }: { hidden: boolean }): React.ReactElement {
|
||||
const [prompt, setPrompt] = useState<Prompt | null>(null);
|
||||
|
||||
const resolveCurrentPromptWithDefaultValue = (currentPrompt: Prompt) => {
|
||||
if (["text", "select"].includes(currentPrompt.options?.type ?? "")) {
|
||||
currentPrompt.resolve("");
|
||||
} else {
|
||||
currentPrompt.resolve(false);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
return PromptEvent.subscribe((p: Prompt) => {
|
||||
setPrompt(p);
|
||||
return PromptEvent.subscribe((newPrompt: Prompt) => {
|
||||
setPrompt((currentPrompt) => {
|
||||
if (currentPrompt) {
|
||||
resolveCurrentPromptWithDefaultValue(currentPrompt);
|
||||
}
|
||||
return newPrompt;
|
||||
});
|
||||
});
|
||||
}, []);
|
||||
|
||||
function close(): void {
|
||||
if (prompt === null) return;
|
||||
if (["text", "select"].includes(prompt.options?.type ?? "")) {
|
||||
prompt.resolve("");
|
||||
} else {
|
||||
prompt.resolve(false);
|
||||
if (prompt === null) {
|
||||
return;
|
||||
}
|
||||
resolveCurrentPromptWithDefaultValue(prompt);
|
||||
setPrompt(null);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user