Skip to content

armgit / plane

Public Imported from GitHub
0

πŸ”₯πŸ”₯πŸ”₯ Open-source Jira, Linear, Monday, and ClickUp alternative. Plane is a modern project management platform to manage tasks, sprints, docs, and triage.

fix(web): add Safari fallback for requestIdleCallback (#9137)

KanteshMuradef14451a5de3b
diff --git a/apps/web/app/entry.client.tsx b/apps/web/app/entry.client.tsx
index 9c665ede07..8dc2a58771 100644
--- a/apps/web/app/entry.client.tsx
+++ b/apps/web/app/entry.client.tsx
@@ -8,6 +8,10 @@ import { startTransition, StrictMode } from "react";
import { hydrateRoot } from "react-dom/client";
import { HydratedRouter } from "react-router/dom";
+import polyfills from "@/lib/polyfills";
+
+void polyfills;
+
startTransition(() => {
hydrateRoot(
document,
diff --git a/apps/web/core/lib/idle-task.ts b/apps/web/core/lib/idle-task.ts
index 34d070bfe4..3d6db72b8f 100644
--- a/apps/web/core/lib/idle-task.ts
+++ b/apps/web/core/lib/idle-task.ts
@@ -8,20 +8,47 @@ export type IdleTaskHandle = {
cancel: () => void;
};
+const requestIdleFallback = (callback: IdleRequestCallback, options?: IdleRequestOptions): number => {
+ const start = Date.now();
+
+ return globalThis.setTimeout(() => {
+ callback({
+ didTimeout: false,
+ timeRemaining: () => Math.max(0, 50 - (Date.now() - start)),
+ });
+ }, options?.timeout ?? 1) as unknown as number;
+};
+
+const cancelIdleFallback = (id: number) => {
+ globalThis.clearTimeout(id);
+};
+
+export const requestIdle = (callback: IdleRequestCallback, options?: IdleRequestOptions): number => {
+ if (typeof globalThis.requestIdleCallback === "function") return globalThis.requestIdleCallback(callback, options);
+
+ return requestIdleFallback(callback, options);
+};
+
+export const cancelIdle = (id: number) => {
+ if (typeof globalThis.cancelIdleCallback === "function") return globalThis.cancelIdleCallback(id);
+
+ return cancelIdleFallback(id);
+};
+
+export const installIdleCallbackPolyfill = () => {
+ if (typeof globalThis === "undefined") return;
+
+ globalThis.requestIdleCallback = globalThis.requestIdleCallback ?? requestIdleFallback;
+ globalThis.cancelIdleCallback = globalThis.cancelIdleCallback ?? cancelIdleFallback;
+};
+
/**
* Schedule lightweight work for idle time and return a cancel handle.
* Falls back to setTimeout when requestIdleCallback is unavailable.
*/
-export const runIdleTask = (callback: () => void): IdleTaskHandle => {
- if (typeof window !== "undefined" && typeof window.requestIdleCallback === "function") {
- const idleId = window.requestIdleCallback(callback, { timeout: 300 });
- return {
- cancel: () => window.cancelIdleCallback(idleId),
- };
- }
-
- const timeoutId = window.setTimeout(callback, 0);
+export const runIdleTask = (callback: IdleRequestCallback): IdleTaskHandle => {
+ const idleId = requestIdle(callback, { timeout: 300 });
return {
- cancel: () => window.clearTimeout(timeoutId),
+ cancel: () => cancelIdle(idleId),
};
};
diff --git a/apps/web/core/lib/polyfills/index.ts b/apps/web/core/lib/polyfills/index.ts
index 2243dc620c..fae765ebf8 100644
--- a/apps/web/core/lib/polyfills/index.ts
+++ b/apps/web/core/lib/polyfills/index.ts
@@ -4,27 +4,8 @@
* See the LICENSE file for details.
*/
-if (typeof window !== "undefined" && window) {
- // Add request callback polyfill to browser in case it does not exist
- window.requestIdleCallback =
- window.requestIdleCallback ??
- function (cb) {
- const start = Date.now();
- return setTimeout(function () {
- cb({
- didTimeout: false,
- timeRemaining: function () {
- return Math.max(0, 50 - (Date.now() - start));
- },
- });
- }, 1);
- };
+import { installIdleCallbackPolyfill } from "@/lib/idle-task";
- window.cancelIdleCallback =
- window.cancelIdleCallback ??
- function (id) {
- clearTimeout(id);
- };
-}
+installIdleCallbackPolyfill();
-export {};
+export default true;