@aws-amplify/core
Version:
Core category of aws-amplify
1 lines • 4.09 kB
Source Map (JSON)
{"version":3,"file":"detectFramework.mjs","sources":["../../../src/Platform/detectFramework.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { Framework } from './types';\nimport { detect } from './detection';\n// We want to cache detection since the framework won't change\nlet frameworkCache;\nexport const frameworkChangeObservers = [];\n// Setup the detection reset tracking / timeout delays\nlet resetTriggered = false;\nconst SSR_RESET_TIMEOUT = 10; // ms\nconst WEB_RESET_TIMEOUT = 10; // ms\nconst PRIME_FRAMEWORK_DELAY = 1000; // ms\nexport const detectFramework = () => {\n if (!frameworkCache) {\n frameworkCache = detect();\n if (resetTriggered) {\n // The final run of detectFramework:\n // Starting from this point, the `frameworkCache` becomes \"final\".\n // So we don't need to notify the observers again so the observer\n // can be removed after the final notice.\n while (frameworkChangeObservers.length) {\n frameworkChangeObservers.pop()?.();\n }\n }\n else {\n // The first run of detectFramework:\n // Every time we update the cache, call each observer function\n frameworkChangeObservers.forEach(fcn => {\n fcn();\n });\n }\n // Retry once for either Unknown type after a delay (explained below)\n resetTimeout(Framework.ServerSideUnknown, SSR_RESET_TIMEOUT);\n resetTimeout(Framework.WebUnknown, WEB_RESET_TIMEOUT);\n }\n return frameworkCache;\n};\n/**\n * @internal Setup observer callback that will be called everytime the framework changes\n */\nexport const observeFrameworkChanges = (fcn) => {\n // When the `frameworkCache` won't be updated again, we ignore all incoming\n // observers.\n if (resetTriggered) {\n return;\n }\n frameworkChangeObservers.push(fcn);\n};\nexport function clearCache() {\n frameworkCache = undefined;\n}\n// For a framework type and a delay amount, setup the event to re-detect\n// During the runtime boot, it is possible that framework detection will\n// be triggered before the framework has made modifications to the\n// global/window/etc needed for detection. When no framework is detected\n// we will reset and try again to ensure we don't use a cached\n// non-framework detection result for all requests.\nfunction resetTimeout(framework, delay) {\n if (frameworkCache === framework && !resetTriggered) {\n setTimeout(() => {\n clear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