Skip to content

Commit

Permalink
feat: divide stage1 and stage2 render
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentdchan committed Nov 13, 2023
1 parent f17ad79 commit db7ebb2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/blocky-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "blocky-core",
"version": "3.3.1",
"version": "3.3.2",
"description": "The core of the blocky editor",
"main": "dist/index.js",
"module": "dist/index.js",
Expand Down
1 change: 1 addition & 0 deletions packages/blocky-core/src/data/change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ export interface FinalizedChangeset {
afterCursor?: CursorState | null;
forceUpdate: boolean;
options: ChangesetApplyOptions;
needsRender?: boolean;
}

/**
Expand Down
24 changes: 22 additions & 2 deletions packages/blocky-core/src/data/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,24 @@ export interface CursorStateUpdateEvent {

export class State implements ChangesetStateLogger {
readonly beforeChangesetApply: Subject<FinalizedChangeset> = new Subject();
readonly changesetApplied: Subject<FinalizedChangeset> = new Subject();

get changesetApplied() {
return this.changesetApplied$;
}

/**
* Important jobs need to put in the first stage
* - Update the editor
* - Data sync
*/
readonly changesetApplied$: Subject<FinalizedChangeset> = new Subject();

/**
* Secondary jobs:
* - Update toolbar
* - Plugins
*/
readonly changesetApplied2$: Subject<FinalizedChangeset> = new Subject();
readonly versionHistory = new VersionHistory();
readonly cursorStateChanged: Subject<CursorStateUpdateEvent> = new Subject();
#appliedVersion: number;
Expand Down Expand Up @@ -140,8 +157,11 @@ export class State implements ChangesetStateLogger {
}

this.#appliedVersion = changeset.version;
this.changesetApplied.next(changeset);
this.changesetApplied$.next(changeset);
this.versionHistory.insert(changeset);

this.changesetApplied2$.next(changeset);

return true;
}

Expand Down
10 changes: 9 additions & 1 deletion packages/blocky-core/src/view/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ export class Editor {
this.state.changesetApplied
.pipe(takeUntil(this.dispose$))
.subscribe(this.#handleChangesetApplied);
this.state.changesetApplied2$
.pipe(takeUntil(this.dispose$))
.subscribe(this.#handleChangesetApplied2);
this.state.blockWillDelete
.pipe(takeUntil(this.dispose$))
.subscribe((blockElement: BlockDataElement) => {
Expand Down Expand Up @@ -1083,6 +1086,7 @@ export class Editor {
const { options } = changeset;
const isThisUser = changeset.userId === this.controller.userId;
const needsRender = options.updateView || changeset.forceUpdate;
changeset.needsRender = needsRender;
if (needsRender) {
this.render(() => {
if (!isThisUser) {
Expand All @@ -1101,11 +1105,15 @@ export class Editor {
}
});
}
};

#handleChangesetApplied2 = (changeset: FinalizedChangeset) => {
// we need to make the state.changesetApplied finished
// there should be some data sync jobs
this.#emitStagedInput();
this.#refreshSearch();

if (!needsRender) {
if (!changeset.needsRender) {
this.controller.__emitNextTicks();
}
};
Expand Down

1 comment on commit db7ebb2

@vercel
Copy link

@vercel vercel bot commented on db7ebb2 Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.