From 94e347ee90601e140a82363f549b68015e6e387b Mon Sep 17 00:00:00 2001 From: Oliver Salzburg Date: Fri, 17 Mar 2023 17:49:57 +0100 Subject: [PATCH] feat(core): Allow users to override the loader timeout Fixes #177 --- packages/documentation/docs/sections/internals.md | 6 ++++++ packages/userscript/source/UserScript.ts | 12 +++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/documentation/docs/sections/internals.md b/packages/documentation/docs/sections/internals.md index f1699ebc0..8b9cb4a6a 100644 --- a/packages/documentation/docs/sections/internals.md +++ b/packages/documentation/docs/sections/internals.md @@ -16,6 +16,12 @@ Allows you to select a language different from the language used in KG. Initially, KS will just use the language KG is set at, but you can override this language here. +## Other internals + +Some users may experience slow loads of KG itself, causing KS to fail to load in time, as the timeout for the loader is "only" 2 minutes. + +In case you are impacted by this, set `localStorage["ks.timeout"]` to the amount of milliseconds KS should wait for KG to load. The default value is `120000`, which correlates to the amount of milliseconds in 2 minutes. + *[JS]: JavaScript *[KG]: Kittens Game diff --git a/packages/userscript/source/UserScript.ts b/packages/userscript/source/UserScript.ts index d46a84592..cecddeeaf 100644 --- a/packages/userscript/source/UserScript.ts +++ b/packages/userscript/source/UserScript.ts @@ -45,6 +45,16 @@ export const ksVersion = (prefix = "") => { return `${prefix}${KS_VERSION}`; }; +// How long to wait for KG to load, in milliseconds. +const TIMEOUT_DEFAULT = 2 * 60 * 1000; + +// Allows the user to define a timeout override in their browser's web storage. +// This allows users to extend the timeout period, in case their local configuration +// requires it. +const TIMEOUT_OVERRIDE = !isNil(localStorage["ks.timeout"]) + ? Number(localStorage["ks.timeout"]) + : undefined; + export class UserScript { readonly gamePage: GamePage; @@ -254,7 +264,7 @@ export class UserScript { return state[0]; } - static async waitForGame(timeout = 30000): Promise { + static async waitForGame(timeout = TIMEOUT_OVERRIDE ?? TIMEOUT_DEFAULT): Promise { const signals: Array> = [sleep(2000)]; if (isNil(UserScript._gameStartSignal) && typeof UserScript.window.dojo !== "undefined") {