There is a second issue mentioned in Caret position and line detection we need to implement if we want to give the user a non obtrusive user experience in the collaborative editor. See the following scenario:
1. We are typing some content
2. The periodic refresh get what we have typed and send it to the server
3. The server checks what use case is going on and send the proper response back
4. During all this proces on the server side, we havent stopped to introduce new content on the client side
5. The functions that insert the response into the textarea dont take into account if the client is typing or not
6. When the insertion occurs the user caret is moved three or four characters backwards and the last characters typed disappear
One way to avoid this result is waiting until the users stops typing and then insert the response from the server. But, we need a way to know if the user has typed anything new from the start of the asynchronous call.
Thus we call a function which compares the content of the text area and the last saved content we have to know if there is a change before the asynchronous call and then check if it's the same after the asynchronous call
Since multiple asynchronous calls can happen while user is typing we cannot set the "newChanges" variable as true or false. In every asynchronous call we need to know if a change is different from the one of the previous asynchronous call. So we need to use a counter that will be checked before and after the async call and if it matches the user has been typing something new then
The counter will be set to 0 next time we reload the site
See checkChanges() in the javascript file
Note: For now, the debug variable "phase" doesnt show "case 6" when it happens. The use case sequence shown in i.e. user test1 is: 2 -> (test2 inserts content, test1 starts typing) -> 1 -> 3 -> (test1 stops typing) -> 2