fix(editor): throttle render linked doc card when blockUpdated (#9361)

[BS-2223](https://linear.app/affine-design/issue/BS-2223/性能问题:linked-doc-card-view加载西藏的文档,app-直接挂了)
This commit is contained in:
donteatfriedrice
2024-12-27 07:24:56 +00:00
parent 7dbb85c8c2
commit 835e7c434e
3 changed files with 51 additions and 31 deletions
@@ -23,7 +23,7 @@ import {
matchFlavours,
referenceToNode,
} from '@blocksuite/affine-shared/utils';
import { Bound } from '@blocksuite/global/utils';
import { Bound, throttle } from '@blocksuite/global/utils';
import { DocCollection } from '@blocksuite/store';
import { computed } from '@preact/signals-core';
import { html, nothing } from 'lit';
@@ -33,7 +33,10 @@ import { styleMap } from 'lit/directives/style-map.js';
import { when } from 'lit/directives/when.js';
import { EmbedBlockComponent } from '../common/embed-block-element.js';
import { renderLinkedDocInCard } from '../common/render-linked-doc.js';
import {
RENDER_CARD_THROTTLE_MS,
renderLinkedDocInCard,
} from '../common/render-linked-doc.js';
import { SyncedDocErrorIcon } from '../embed-synced-doc-block/styles.js';
import {
type EmbedLinkedDocBlockConfig,
@@ -301,24 +304,28 @@ export class EmbedLinkedDocBlockComponent extends EmbedBlockComponent<EmbedLinke
});
})
);
// Should throttle the blockUpdated event to avoid too many re-renders
// Because the blockUpdated event is triggered too frequently at some cases
this.disposables.add(
linkedDoc.slots.blockUpdated.on(payload => {
if (
payload.type === 'update' &&
['', 'caption', 'xywh'].includes(payload.props.key)
) {
return;
}
linkedDoc.slots.blockUpdated.on(
throttle(payload => {
if (
payload.type === 'update' &&
['', 'caption', 'xywh'].includes(payload.props.key)
) {
return;
}
if (payload.type === 'add' && payload.init) {
return;
}
if (payload.type === 'add' && payload.init) {
return;
}
this._load().catch(e => {
console.error(e);
this.isError = true;
});
})
this._load().catch(e => {
console.error(e);
this.isError = true;
});
}, RENDER_CARD_THROTTLE_MS)
)
);
this._setDocUpdatedAt();