Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: faster tick for snapshot #8427

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 3 additions & 5 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,10 +487,7 @@ export class Job {
children,
});

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