Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

fix: add requestIdleCallback shim for safari

Wenzhao Hueeee425eddac
diff --git a/packages/core/src/common/shims.ts b/packages/core/src/common/shims.ts
new file mode 100644
index 0000000000..ae75fdb6de
--- /dev/null
+++ b/packages/core/src/common/shims.ts
@@ -0,0 +1,44 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+export function installShims() {
+ installRequestIdleCallback();
+}
+
+const TIME_WINDOW = 50;
+
+function installRequestIdleCallback() {
+ if (typeof window.requestIdleCallback !== 'function') {
+ window.requestIdleCallback = function shimRIC(callback: Function) {
+ const start = Date.now();
+ return setTimeout(function rICCallback() {
+ const remaining = Math.max(0, TIME_WINDOW - (Date.now() - start));
+ callback({
+ didTimeout: remaining === 0,
+ timeRemaining() {
+ return remaining;
+ },
+ });
+ }, 1) as unknown as number;
+ };
+ }
+
+ if (typeof window.cancelIdleCallback !== 'function') {
+ window.cancelIdleCallback = function shimCancelRIC(id: number) {
+ clearTimeout(id);
+ };
+ }
+}
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index 3a1896426a..86a7c65762 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -14,6 +14,8 @@
* limitations under the License.
*/
+import { installShims } from './common/shims';
+
export * from './basics';
export { dedupe, remove } from './common/array';
export {
@@ -112,3 +114,5 @@ export * from './slides/domain';
export * from './types/const';
export * from './types/enum';
export * from './types/interfaces';
+
+installShims();