Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mirka committed Jul 19, 2021
1 parent aceef27 commit 948bb07
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
9 changes: 1 addition & 8 deletions src/components/block-editor-contents/use-yjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,12 @@ function initYDoc( { initialBlocks, onRemoteDataChange, channelId, transport } )
},
} );

window.addEventListener( 'storage', ( event ) => {
if ( event.storageArea !== localStorage ) return;
if ( event.key === 'isoEditorYjsMessage' && event.newValue ) {
doc.receiveMessage( JSON.parse( event.newValue ) );
}
} );

doc.onRemoteDataChange( ( changes ) => {
debug( 'remote change received', changes );
onRemoteDataChange( changes.blocks );
} );

return transport.connect( channelId ).then( ( { isFirstInChannel } ) => {
return transport.connect( { channelId, onReceiveMessage: doc.receiveMessage } ).then( ( { isFirstInChannel } ) => {
debug( `connected (channelId: ${ channelId })` );

if ( isFirstInChannel ) {
Expand Down
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,12 @@ import './style.scss';
* Transport module for real time collaboration
* @typedef CollaborationTransport
* @property {(message: object) => void} sendMessage
* @property {(channelId?: string) => Promise<{isFirstInChannel: boolean}>} connect
* @property {(options: CollaborationTransportConnectOpts) => Promise<{isFirstInChannel: boolean}>} connect
* @property {() => Promise<void>} disconnect
*
* @typedef CollaborationTransportConnectOpts
* @property {string} [channelId]
* @property {(message: object) => void} onReceiveMessage
*/

/**
Expand Down
17 changes: 16 additions & 1 deletion src/stories/collab/mock-transport.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
const listeners = [];

/** @type {import("../../components/block-editor-contents/use-yjs").CollaborationTransport} */
const mockTransport = {
sendMessage: ( message ) => {
window.localStorage.setItem( 'isoEditorYjsMessage', JSON.stringify( message ) );
},
connect: ( channelId ) => {
connect: ( { channelId, onReceiveMessage } ) => {
listeners.push( {
event: 'storage',
listener: window.addEventListener( 'storage', ( event ) => {
if ( event.storageArea !== localStorage ) return;
if ( event.key === 'isoEditorYjsMessage' && event.newValue ) {
onReceiveMessage( JSON.parse( event.newValue ) );
}
} ),
} );

window.localStorage.setItem(
'isoEditorClients',
( parseInt( window.localStorage.getItem( 'isoEditorClients' ) || '0' ) + 1 ).toString()
Expand All @@ -11,6 +24,8 @@ const mockTransport = {
return Promise.resolve( { isFirstInChannel } );
},
disconnect: () => {
listeners.forEach( ( { event, listener } ) => window.removeEventListener( event, listener ) );

const clientCount = Math.max( 0, parseInt( window.localStorage.getItem( 'isoEditorClients' ) || '0' ) - 1 );
return Promise.resolve( window.localStorage.setItem( 'isoEditorClients', clientCount.toString() ) );
},
Expand Down

0 comments on commit 948bb07

Please sign in to comment.