fix(core): improve performance (#6345)

This commit is contained in:
EYHN
2024-03-27 14:01:54 +00:00
parent 710edd28db
commit ba9dad95b4
6 changed files with 66 additions and 40 deletions

View File

@@ -5,20 +5,22 @@ import type {
OperatorFunction,
Subscription,
TeardownLogic,
ThrottleConfig,
} from 'rxjs';
import {
BehaviorSubject,
combineLatest,
distinctUntilChanged,
EMPTY,
filter,
map,
mergeMap,
Observable,
of,
scan,
skip,
Subject,
switchMap,
throttleTime,
} from 'rxjs';
const logger = new DebugLogger('livedata');
@@ -97,11 +99,10 @@ export class LiveData<T = unknown>
? upstream$
: stream$ =>
stream$.pipe(
filter(
(op): op is Exclude<LiveDataOperation, 'set'> => op !== 'set'
),
switchMap(v => {
if (v === 'get') {
mergeMap(v => {
if (v === 'set') {
return EMPTY;
} else if (v === 'get') {
return of('watch' as const, 'unwatch' as const);
} else {
return of(v);
@@ -333,6 +334,23 @@ export class LiveData<T = unknown>
return sub$;
}
distinctUntilChanged(comparator?: (previous: T, current: T) => boolean) {
return LiveData.from(
this.pipe(distinctUntilChanged(comparator)),
null as any
);
}
throttleTime(
duration: number,
{ trailing = true, leading = true }: ThrottleConfig = {}
) {
return LiveData.from(
this.pipe(throttleTime(duration, undefined, { trailing, leading })),
null as any
);
}
// eslint-disable-next-line rxjs/finnish
asObservable(): Observable<T> {
return new Observable<T>(subscriber => {

View File

@@ -8,6 +8,7 @@ import type { Workspace, WorkspaceLocalState } from '../workspace';
export type PageMode = 'edgeless' | 'page';
export class PageRecord {
meta: Partial<DocMeta> | null = null;
constructor(
public readonly id: string,
private readonly workspace: Workspace,
@@ -15,15 +16,14 @@ export class PageRecord {
) {}
meta$ = LiveData.from<Partial<DocMeta>>(
new Observable<DocMeta>(subscriber => {
new Observable<Partial<DocMeta>>(subscriber => {
const emit = () => {
const meta = this.workspace.docCollection.meta.docMetas.find(
page => page.id === this.id
);
if (meta === undefined) {
return;
if (this.meta === null) {
// getDocMeta is heavy, so we cache the doc meta reference
this.meta =
this.workspace.docCollection.meta.getDocMeta(this.id) || null;
}
subscriber.next(meta);
subscriber.next({ ...this.meta });
};
emit();