Skip to content

Commit

Permalink
perf(edgeless): optimize selection frame rate (#8751)
Browse files Browse the repository at this point in the history
  • Loading branch information
doodlewind authored Nov 18, 2024
1 parent 03fd5bb commit 9e2af17
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
12 changes: 9 additions & 3 deletions packages/blocks/src/root-block/widgets/format-bar/format-bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import {
} from '@blocksuite/affine-components/toolbar';
import { matchFlavours } from '@blocksuite/affine-shared/utils';
import { WidgetComponent } from '@blocksuite/block-std';
import { assertExists, DisposableGroup } from '@blocksuite/global/utils';
import {
assertExists,
DisposableGroup,
nextTick,
} from '@blocksuite/global/utils';
import {
autoUpdate,
computePosition,
Expand Down Expand Up @@ -146,7 +150,9 @@ export class AffineFormatBarWidget extends WidgetComponent {
}
}

await this.host.getUpdateComplete();
// We cannot use `host.getUpdateComplete()` here
// because it would cause excessive DOM queries, leading to UI jamming.
await nextTick();

if (textSelection) {
const block = this.host.view.getBlock(textSelection.blockId);
Expand Down Expand Up @@ -178,7 +184,7 @@ export class AffineFormatBarWidget extends WidgetComponent {
return;
}

if (blockSelections.length > 0) {
if (this.block && blockSelections.length > 0) {
this._displayType = 'block';
const selectedBlocks = blockSelections
.map(selection => {
Expand Down
12 changes: 12 additions & 0 deletions packages/framework/global/src/utils/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,15 @@ export const debounce = <T extends (...args: any[]) => void>(
timer = setTimeout(setTimer, limit);
} as T;
};

export async function nextTick() {
// @ts-ignore
if ('scheduler' in window && 'yield' in window.scheduler) {
// @ts-ignore
return window.scheduler.yield();
} else if (typeof requestIdleCallback !== 'undefined') {
return new Promise(resolve => requestIdleCallback(resolve));
} else {
return new Promise(resolve => setTimeout(resolve, 0));
}
}
14 changes: 1 addition & 13 deletions packages/framework/store/src/transformer/job.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
import { Slot } from '@blocksuite/global/utils';
import { nextTick, 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 @@ -45,18 +45,6 @@ interface DraftBlockTreeNode {
children: Array<DraftBlockTreeNode>;
}

async function nextTick() {
// @ts-ignore
if ('scheduler' in window && 'yield' in window.scheduler) {
// @ts-ignore
return window.scheduler.yield();
} else if (typeof requestIdleCallback !== 'undefined') {
return new Promise(resolve => requestIdleCallback(resolve));
} else {
return new Promise(resolve => setTimeout(resolve, 0));
}
}

// The number of blocks to insert in one batch
const BATCH_SIZE = 100;

Expand Down

0 comments on commit 9e2af17

Please sign in to comment.