-
-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(collaboration): fix debouncing of
onSendableReceived
handler
fix(collaboration): fix the debounce of onSendableReceived handler so it works as intended feat(collaboration): add the ability to cancel and flush debounced sendable steps test(collaboration): add tests for debounced onSendableReceived handler
- Loading branch information
Showing
5 changed files
with
181 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@remirror/core-helpers': patch | ||
--- | ||
|
||
Expose the return type of the throttle and debounce helpers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@remirror/extension-collaboration': minor | ||
--- | ||
|
||
Fix `onSendableReceived` handler so it is actually debounced as intended. | ||
|
||
Add two new commands `cancelSendableSteps` and `flushSendableSteps` which more control over the debounced functionality |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 131 additions & 2 deletions
133
packages/remirror__extension-collaboration/__tests__/collaboration-extension.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,134 @@ | ||
import { extensionValidityTest } from 'jest-remirror'; | ||
import { extensionValidityTest, renderEditor } from 'jest-remirror'; | ||
|
||
import { CollaborationExtension } from '../'; | ||
import { CollaborationExtension, CollaborationOptions } from '../'; | ||
|
||
extensionValidityTest(CollaborationExtension, { clientID: 'abc' }); | ||
|
||
jest.useFakeTimers('modern'); | ||
|
||
function create(options: Partial<CollaborationOptions> = {}) { | ||
const collabExtension = new CollaborationExtension({ | ||
clientID: 1, | ||
...options, | ||
}); | ||
|
||
if (options.onSendableReceived) { | ||
collabExtension.addHandler('onSendableReceived', options.onSendableReceived); | ||
} | ||
|
||
return renderEditor([collabExtension]); | ||
} | ||
|
||
describe('getSendableSteps', () => { | ||
afterAll(() => { | ||
jest.useRealTimers(); | ||
}); | ||
|
||
const expectedSteps = [ | ||
{ | ||
from: 0, | ||
slice: { | ||
content: [ | ||
{ | ||
content: [ | ||
{ | ||
text: 'Initial state', | ||
type: 'text', | ||
}, | ||
], | ||
type: 'paragraph', | ||
}, | ||
], | ||
}, | ||
stepType: 'replace', | ||
to: 2, | ||
}, | ||
{ | ||
from: 14, | ||
slice: { | ||
content: [ | ||
{ | ||
text: ' update 1', | ||
type: 'text', | ||
}, | ||
], | ||
}, | ||
stepType: 'replace', | ||
to: 14, | ||
}, | ||
{ | ||
from: 23, | ||
slice: { | ||
content: [ | ||
{ | ||
text: ' update 2', | ||
type: 'text', | ||
}, | ||
], | ||
}, | ||
stepType: 'replace', | ||
to: 23, | ||
}, | ||
]; | ||
|
||
it('should debounce calls to the onSendableReceived handler', () => { | ||
const handleSendableReceived = jest.fn(); | ||
|
||
const { | ||
nodes: { doc, p }, | ||
add, | ||
commands, | ||
} = create({ onSendableReceived: handleSendableReceived }); | ||
|
||
add(doc(p('Initial state<cursor>'))); | ||
|
||
jest.advanceTimersByTime(100); | ||
commands.insertText(' update 1'); | ||
|
||
jest.advanceTimersByTime(100); | ||
commands.insertText(' update 2'); | ||
|
||
jest.advanceTimersByTime(1000); | ||
|
||
expect(handleSendableReceived).toHaveBeenCalledOnce(); | ||
expect(handleSendableReceived).toHaveBeenCalledWith({ | ||
sendable: expect.any(Object), | ||
jsonSendable: { | ||
clientID: 1, | ||
version: 0, | ||
steps: expectedSteps, | ||
}, | ||
}); | ||
}); | ||
|
||
it('should allow me to flush the steps on demand', () => { | ||
const handleSendableReceived = jest.fn(); | ||
|
||
const { | ||
nodes: { doc, p }, | ||
add, | ||
commands, | ||
} = create({ onSendableReceived: handleSendableReceived }); | ||
|
||
add(doc(p('Initial state<cursor>'))); | ||
|
||
jest.advanceTimersByTime(100); | ||
commands.insertText(' update 1'); | ||
|
||
jest.advanceTimersByTime(100); | ||
commands.insertText(' update 2'); | ||
|
||
expect(handleSendableReceived).not.toHaveBeenCalled(); | ||
|
||
commands.flushSendableSteps(); | ||
expect(handleSendableReceived).toHaveBeenCalledOnce(); | ||
expect(handleSendableReceived).toHaveBeenCalledWith({ | ||
sendable: expect.any(Object), | ||
jsonSendable: { | ||
clientID: 1, | ||
version: 0, | ||
steps: expectedSteps, | ||
}, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7722f97
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉 Published on https://remirror.io as production
🚀 Deployed on https://62ac7dd91515fc5bd72b1980--remirror.netlify.app