{"version":3,"file":"whenIdleOrHidden.js","sources":["../../../../../src/metrics/web-vitals/lib/whenIdleOrHidden.ts"],"sourcesContent":["/*\n * Copyright 2024 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { WINDOW } from '../../../types.js';\nimport { addPageListener, removePageListener } from './globalListeners.js';\nimport { runOnce } from './runOnce.js';\n\n/**\n * Runs the passed callback during the next idle period, or immediately\n * if the browser's visibility state is (or becomes) hidden.\n */\nexport const whenIdleOrHidden = (cb: () => void) => {\n const rIC = WINDOW.requestIdleCallback || WINDOW.setTimeout;\n\n // If the document is hidden, run the callback immediately, otherwise\n // race an idle callback with the next `visibilitychange` event.\n if (WINDOW.document?.visibilityState === 'hidden') {\n cb();\n } else {\n // eslint-disable-next-line no-param-reassign\n cb = runOnce(cb);\n addPageListener('visibilitychange', cb, { once: true, capture: true });\n // sentry: we use pagehide instead of directly listening to visibilitychange\n // because some browsers we still support (Safari <14.4) don't fully support\n // `visibilitychange` or have known bugs w.r.t the `visibilitychange` event.\n // TODO(v11): remove this once we drop support for Safari <14.4\n addPageListener('pagehide', cb, { once: true, capture: true });\n rIC(() => {\n cb();\n // Remove the above event listener since no longer required.\n // See: https://github.com/GoogleChrome/web-vitals/issues/622\n removePageListener('visibilitychange', cb, { capture: true });\n // TODO(v11): remove this once we drop support for Safari <14.4\n removePageListener('pagehide', cb, { capture: true });\n });\n }\n};\n"],"names":[],"mappings":";;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AAMA;AACA;AACA;AACA;MACa,gBAAA,GAAmB,CAAC,EAAE,KAAiB;AACpD,EAAE,MAAM,MAAM,MAAM,CAAC,mBAAA,IAAuB,MAAM,CAAC,UAAU;;AAE7D;AACA;AACA,EAAE,IAAI,MAAM,CAAC,QAAQ,EAAE,eAAA,KAAoB,QAAQ,EAAE;AACrD,IAAI,EAAE,EAAE;AACR,EAAE,OAAO;AACT;AACA,IAAI,EAAA,GAAK,OAAO,CAAC,EAAE,CAAC;AACpB,IAAI,eAAe,CAAC,kBAAkB,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,IAAA,EAAM,CAAC;AAC1E;AACA;AACA;AACA;AACA,IAAI,eAAe,CAAC,UAAU,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,IAAA,EAAM,CAAC;AAClE,IAAI,GAAG,CAAC,MAAM;AACd,MAAM,EAAE,EAAE;AACV;AACA;AACA,MAAM,kBAAkB,CAAC,kBAAkB,EAAE,EAAE,EAAE,EAAE,OAAO,EAAE,IAAA,EAAM,CAAC;AACnE;AACA,MAAM,kBAAkB,CAAC,UAAU,EAAE,EAAE,EAAE,EAAE,OAAO,EAAE,IAAA,EAAM,CAAC;AAC3D,IAAI,CAAC,CAAC;AACN,EAAE;AACF;;;;"}