Skip to content

Commit

Permalink
Prevent remote changes from triggering a selection change message
Browse files Browse the repository at this point in the history
  • Loading branch information
mirka committed Feb 16, 2022
1 parent 2c51948 commit 7e403ed
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/components/collaborative-editing/use-yjs/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* External dependencies
*/
import { createMutex } from 'lib0/mutex';
import { v4 as uuidv4 } from 'uuid';
import { noop, once, sample } from 'lodash';

Expand Down Expand Up @@ -37,6 +38,7 @@ export const defaultColors = [ '#4676C0', '#6F6EBE', '#9063B6', '#C3498D', '#9E6
async function initYDoc( { settings, registry } ) {
const { channelId, transport } = settings;
const { dispatch, select } = registry;
const mutex = createMutex();

/** @type {string} */
const identity = uuidv4();
Expand Down Expand Up @@ -90,7 +92,10 @@ async function initYDoc( { settings, registry } ) {

switch ( data.type ) {
case 'doc': {
doc.receiveMessage( data.message );
// The mutex wrapper prevents a remote change from triggering a selection change message
mutex( () => {
doc.receiveMessage( data.message );
} );
break;
}
case 'selection': {
Expand Down Expand Up @@ -128,14 +133,17 @@ async function initYDoc( { settings, registry } ) {

return {
sendSelection: ( start, end ) => {
debug( 'sendSelection', start, end );
transport.sendMessage( {
type: 'selection',
identity,
selection: {
start,
end,
},
// The mutex wrapper prevents a remote change from triggering a selection change message
mutex( () => {
debug( 'sendSelection', start, end );
transport.sendMessage( {
type: 'selection',
identity,
selection: {
start,
end,
},
} );
} );
},
disconnect: () => {
Expand Down

0 comments on commit 7e403ed

Please sign in to comment.