@@ -6,6 +6,17 @@ const AttributePool = require('./AttributePool');
66const assert = require ( 'assert' ) ;
77const superagent = require ( 'superagent' ) ;
88
9+
10+ const randomString = ( len = 10 ) => {
11+ const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' ;
12+ let randomstring = '' ;
13+ for ( let i = 0 ; i < len ; i ++ ) {
14+ const rnum = Math . floor ( Math . random ( ) * chars . length ) ;
15+ randomstring += chars . substring ( rnum , rnum + 1 ) ;
16+ }
17+ return randomstring ;
18+ } ;
19+
920exports . connect = ( host ) => {
1021 // Create an event emitter
1122 const ee = new EventEmitter ( ) ;
@@ -24,8 +35,8 @@ exports.connect = (host) => {
2435 const padIdParam = '/p/' ;
2536 const indexOfPadId = parsed . pathname . indexOf ( padIdParam ) ;
2637 if ( indexOfPadId >= 0 ) {
27- padState . path = parsed . pathname . substring ( 0 , indexOfPadId )
28- padState . padId = parsed . pathname . substring ( indexOfPadId + padIdParam . length )
38+ padState . path = parsed . pathname . substring ( 0 , indexOfPadId ) ;
39+ padState . padId = parsed . pathname . substring ( indexOfPadId + padIdParam . length ) ;
2940 } else {
3041 padState . path = '' ;
3142 padState . padId = randomString ( ) ;
@@ -63,10 +74,54 @@ exports.connect = (host) => {
6374 socket . json . send ( msg ) ;
6475 } ) ;
6576
77+
78+ // sends a message to the server.
79+ // Can be used without parameters to try sending outgoing message
80+ const sendMessage = ( optMsg ) => {
81+ // console.log("sending message: ",
82+ // optMsg && (optMsg.changeset + '[' + optMsg.baseRev + ']'),
83+ // '; inflight: ' + (padState.inFlight && (
84+ // padState.inFlight.changeset + '[' + padState.inFlight.baseRev + ']')),
85+ // '; outgoing: ' + (padState.outgoing && (
86+ // padState.outgoing.changeset + '[' + padState.outgoing.baseRev + ']'))
87+ // );
88+ if ( optMsg ) {
89+ if ( padState . outgoing ) {
90+ assert ( optMsg . baseRev === padState . outgoing . baseRev ,
91+ 'should append to the same document version' ) ;
92+
93+ padState . outgoing . changeset = Changeset . compose (
94+ padState . outgoing . changeset , optMsg . changeset , padState . apool ) ;
95+ } else {
96+ padState . outgoing = optMsg ;
97+ }
98+ }
99+
100+ if ( ! padState . inFlight && padState . outgoing ) {
101+ padState . inFlight = padState . outgoing ;
102+ padState . outgoing = null ;
103+ socket . json . send ( {
104+ type : 'COLLABROOM' ,
105+ component : 'pad' ,
106+ data : JSON . parse ( JSON . stringify ( padState . inFlight ) ) ,
107+ } ) ;
108+ }
109+ } ;
110+
66111 socket . on ( 'message' , ( obj ) => {
67112 // message emitter sends all messages should they be required
68113 ee . emit ( 'message' , obj ) ;
69114
115+ // Both server and client edited the document.
116+ // We need to update local change with remote changes
117+ // and visa versa
118+ const transformX = ( client , server ) => {
119+ const _c = Changeset . follow ( server . changeset , client . changeset , false , padState . pool ) ;
120+ const _s = Changeset . follow ( client . changeset , server . changeset , true , padState . pool ) ;
121+ client . changeset = _c ;
122+ server . changeset = _s ;
123+ } ;
124+
70125
71126 // Client is connected so we should start sending messages at the server
72127 if ( obj . type === 'CLIENT_VARS' ) {
@@ -175,57 +230,6 @@ exports.connect = (host) => {
175230 } ;
176231 sendMessage ( msg ) ;
177232 } ;
178-
179- // Both server and client edited the document.
180- // We need to update local change with remote changes
181- // and visa versa
182- const transformX = ( client , server ) => {
183- const _c = Changeset . follow ( server . changeset , client . changeset , false , padState . pool ) ;
184- const _s = Changeset . follow ( client . changeset , server . changeset , true , padState . pool ) ;
185- client . changeset = _c ;
186- server . changeset = _s ;
187- } ;
188-
189- // sends a message to the server. Can be used without parameters to try sending outgoing message
190- const sendMessage = ( optMsg ) => {
191- // console.log("sending message: ", optMsg && (optMsg.changeset + '[' + optMsg.baseRev + ']'),
192- // '; inflight: ' + (padState.inFlight && (
193- // padState.inFlight.changeset + '[' + padState.inFlight.baseRev + ']')),
194- // '; outgoing: ' + (padState.outgoing && (
195- // padState.outgoing.changeset + '[' + padState.outgoing.baseRev + ']'))
196- // );
197- if ( optMsg ) {
198- if ( padState . outgoing ) {
199- assert ( optMsg . baseRev === padState . outgoing . baseRev ,
200- 'should append to the same document version' ) ;
201-
202- padState . outgoing . changeset = Changeset . compose (
203- padState . outgoing . changeset , optMsg . changeset , padState . apool ) ;
204- } else {
205- padState . outgoing = optMsg ;
206- }
207- }
208-
209- if ( ! padState . inFlight && padState . outgoing ) {
210- padState . inFlight = padState . outgoing ;
211- padState . outgoing = null ;
212- socket . json . send ( {
213- type : 'COLLABROOM' ,
214- component : 'pad' ,
215- data : JSON . parse ( JSON . stringify ( padState . inFlight ) ) ,
216- } ) ;
217- }
218- } ;
219233 } ) ;
220234 return ee ;
221235} ;
222-
223- const randomString = ( len = 10 ) => {
224- const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' ;
225- let randomstring = '' ;
226- for ( let i = 0 ; i < len ; i ++ ) {
227- const rnum = Math . floor ( Math . random ( ) * chars . length ) ;
228- randomstring += chars . substring ( rnum , rnum + 1 ) ;
229- }
230- return randomstring ;
231- } ;
0 commit comments