Skip to content

Commit

Permalink
fix: polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
doodlewind committed Sep 21, 2024
1 parent b78978c commit 300fe2c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 28 deletions.
3 changes: 1 addition & 2 deletions packages/framework/global/src/env/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,4 @@ export const IS_IPAD =

export const IS_WINDOWS = /Win/.test(platform);

export const REQUEST_IDLE_CALLBACK_ENABLED =
'requestIdleCallback' in globalThis;
export const RIC_ENABLED = 'requestIdleCallback' in globalThis;
36 changes: 36 additions & 0 deletions packages/framework/global/src/utils/function.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { IS_WEB, RIC_ENABLED } from '../env/index.js';

export async function sleep(ms: number, signal?: AbortSignal): Promise<void> {
return new Promise(resolve => {
if (signal?.aborted) {
Expand Down Expand Up @@ -112,3 +114,37 @@ export const debounce = <T extends (...args: any[]) => void>(
timer = setTimeout(setTimer, limit);
} as T;
};

interface IdleDeadline {
timeRemaining: () => number;
didTimeout: boolean;
}

type IdleRequestCallback = (deadline: IdleDeadline) => void;

interface IdleRequestOptions {
timeout?: number;
}

function requestIdleCallbackPolyfill(
callback: IdleRequestCallback,
options?: IdleRequestOptions
): number {
const start = Date.now();
return setTimeout(() => {
callback({
didTimeout: false,
timeRemaining: () => Math.max(0, 50 - (Date.now() - start)),
});
}, options?.timeout ?? 1) as unknown as number;
}

export const requestIdleCallback =
IS_WEB && RIC_ENABLED
? window.requestIdleCallback
: requestIdleCallbackPolyfill;

export const cancelIdleCallback =
IS_WEB && RIC_ENABLED
? window.cancelIdleCallback
: (id: number) => clearTimeout(id);
24 changes: 1 addition & 23 deletions packages/framework/store/src/__tests__/collection.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import type { Slot } from '@blocksuite/global/utils';

import { assert, beforeEach, describe, expect, it, vi } from 'vitest';
import { assert, describe, expect, it, vi } from 'vitest';
import { applyUpdate, encodeStateAsUpdate } from 'yjs';

import type { BlockModel, BlockSchemaType, Doc } from '../index.js';
Expand Down Expand Up @@ -70,28 +70,6 @@ function createTestDoc(docId = defaultDocId) {
return doc;
}

function requestIdleCallbackPolyfill(
callback: IdleRequestCallback,
options?: IdleRequestOptions
) {
const timeout = options?.timeout ?? 1000;
const start = Date.now();
return setTimeout(function () {
callback({
didTimeout: false,
timeRemaining: function () {
return Math.max(0, timeout - (Date.now() - start));
},
});
}, timeout) as unknown as number;
}

beforeEach(() => {
if (globalThis.requestIdleCallback === undefined) {
globalThis.requestIdleCallback = requestIdleCallbackPolyfill;
}
});

describe('basic', () => {
it('can init collection', () => {
const options = createTestOptions();
Expand Down
6 changes: 3 additions & 3 deletions packages/framework/store/src/transformer/job.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { IS_WEB } from '@blocksuite/global/env';
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
import { Slot } from '@blocksuite/global/utils';
import { requestIdleCallback, Slot } from '@blocksuite/global/utils';

import type { BlockModel, BlockSchemaType } from '../schema/index.js';
import type { Doc, DocCollection, DocMeta } from '../store/index.js';
Expand Down Expand Up @@ -486,8 +487,7 @@ export class Job {
children,
});

const nextTick =
typeof window !== 'undefined' ? window.requestIdleCallback : setImmediate;
const nextTick = IS_WEB ? requestIdleCallback : setImmediate;
await new Promise(resolve => nextTick(() => resolve(undefined)));
doc.addBlock(
modelData.flavour as BlockSuite.Flavour,
Expand Down

0 comments on commit 300fe2c

Please sign in to comment.