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.

feat(core): add queueMicrotask polyfill (#7131)

白熱e955a29c7cc2
diff --git a/packages/core/src/common/__tests__/shims.spec.ts b/packages/core/src/common/__tests__/shims.spec.ts
index 5989a3df52..7e87c131b4 100644
--- a/packages/core/src/common/__tests__/shims.spec.ts
+++ b/packages/core/src/common/__tests__/shims.spec.ts
@@ -30,6 +30,7 @@ function restoreProperty(target: object, key: PropertyKey, descriptor: Restorabl
describe('installShims', () => {
let requestIdleDescriptor: RestorableDescriptor;
let cancelIdleDescriptor: RestorableDescriptor;
+ let queueMicrotaskDescriptor: RestorableDescriptor;
let findLastDescriptor: RestorableDescriptor;
let findLastIndexDescriptor: RestorableDescriptor;
let stringAtDescriptor: RestorableDescriptor;
@@ -38,6 +39,7 @@ describe('installShims', () => {
vi.useFakeTimers();
requestIdleDescriptor = Object.getOwnPropertyDescriptor(globalThis, 'requestIdleCallback');
cancelIdleDescriptor = Object.getOwnPropertyDescriptor(globalThis, 'cancelIdleCallback');
+ queueMicrotaskDescriptor = Object.getOwnPropertyDescriptor(globalThis, 'queueMicrotask');
findLastDescriptor = Object.getOwnPropertyDescriptor(Array.prototype, 'findLast');
findLastIndexDescriptor = Object.getOwnPropertyDescriptor(Array.prototype, 'findLastIndex');
stringAtDescriptor = Object.getOwnPropertyDescriptor(String.prototype, 'at');
@@ -52,6 +54,11 @@ describe('installShims', () => {
writable: true,
value: undefined,
});
+ Object.defineProperty(globalThis, 'queueMicrotask', {
+ configurable: true,
+ writable: true,
+ value: undefined,
+ });
// eslint-disable-next-line no-extend-native
Object.defineProperty(Array.prototype, 'findLast', {
configurable: true,
@@ -75,6 +82,7 @@ describe('installShims', () => {
afterEach(() => {
restoreProperty(globalThis, 'requestIdleCallback', requestIdleDescriptor);
restoreProperty(globalThis, 'cancelIdleCallback', cancelIdleDescriptor);
+ restoreProperty(globalThis, 'queueMicrotask', queueMicrotaskDescriptor);
restoreProperty(Array.prototype, 'findLast', findLastDescriptor);
restoreProperty(Array.prototype, 'findLastIndex', findLastIndexDescriptor);
restoreProperty(String.prototype, 'at', stringAtDescriptor);
@@ -101,6 +109,19 @@ describe('installShims', () => {
expect(idleDeadline.timeRemaining()).toBeGreaterThanOrEqual(0);
});
+ it('should install queueMicrotask polyfill', async () => {
+ const callback = vi.fn();
+
+ installShims();
+ globalThis.queueMicrotask?.(callback);
+
+ expect(callback).not.toHaveBeenCalled();
+
+ await Promise.resolve();
+
+ expect(callback).toHaveBeenCalledTimes(1);
+ });
+
it('should install array findLastIndex and findLast polyfills', () => {
installShims();
diff --git a/packages/core/src/common/shims.ts b/packages/core/src/common/shims.ts
index 8de6de5ebd..73adeb7da1 100644
--- a/packages/core/src/common/shims.ts
+++ b/packages/core/src/common/shims.ts
@@ -57,6 +57,23 @@ function installRequestIdleCallback() {
}
}
+/**
+ * Polyfill for queueMicrotask
+ */
+function installQueueMicrotask() {
+ if (typeof glob.queueMicrotask !== 'function') {
+ glob.queueMicrotask = function shimQueueMicrotask(callback: VoidFunction) {
+ Promise.resolve()
+ .then(callback)
+ .catch((error) => {
+ setTimeout(() => {
+ throw error;
+ }, 0);
+ });
+ };
+ }
+}
+
/**
* Polyfill for Array.prototype.findLastIndex and Array.prototype.findLast
*/
@@ -110,6 +127,7 @@ function installStringAt() {
export function installShims() {
installRequestIdleCallback();
+ installQueueMicrotask();
installArrayFindLastIndex();
installStringAt();
}