Skip to content

Conversation

@benniekiss
Copy link
Contributor

Description

This PR implements a debounced word counting plugin to improve typing performance.

Changes

The word count computation was moved to a ViewPlugin so that it can be debounced using setTimeout(). It uses a StateEffect to update the existing countField value.

Event listeners were added for the new updateWordCountEffect so that the UI shows updated counts when they arrive rather than waiting for cursor activity.

Additional information

Currently, word counts are recomputed every keypress. In larger documents (>50,000 words) this starts to add a small but noticeable, and increasing, delay. By debouncing the counting, typing performance is dramatically improved in large documents.

Word counts are no longer live, however, they are updated the moment the transaction is dispatched.

Tested on:

@nathanlesage
Copy link
Member

You guys are all writing too large documents.

(/s)

Will have a look later!

@benniekiss
Copy link
Contributor Author

Trust me, I wish I wasnt!

Copy link
Member

@nathanlesage nathanlesage left a comment

Choose a reason for hiding this comment

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

I'm sorry, I didn't submit my review last week, but somehow GitHub seems to have remembered all my comments…?!


if (update.selectionSet) {
this.emit('cursorActivity')
this.emit('docUpdate')
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't this also now need to be emitted from the update branch a few lines above?

for (const effect of transaction.effects) {
// Listen for word count updates
if (effect.is(updateWordCountEffect)) {
this.emit('docUpdate')
Copy link
Member

Choose a reason for hiding this comment

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

Mh, I'm wondering if docUpdate semantically correctly describes when and how this event is being emitted ...?

// return true.
if (!transaction.docChanged) {
return value
for (let e of transaction.effects) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
for (let e of transaction.effects) {
for (const e of transaction.effects) {


export const countPlugin = ViewPlugin.fromClass(class {
private timeout: number | null = null
private delay = 750
Copy link
Member

Choose a reason for hiding this comment

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

Please move this as a constant to the top of the file so that we can adjust this if necessary (e.g., something like const WORD_COUNT_DELAY = 750)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants