Skip to content

Commit 29dc4ae

Browse files
committed
minor fixes
1 parent 31914e9 commit 29dc4ae

File tree

4 files changed

+34
-18
lines changed

4 files changed

+34
-18
lines changed

server/src/main/java/pl/piotrowski/remotetexteditor/controller/DocumentsController.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22

33
import org.springframework.beans.factory.annotation.Autowired;
44
import org.springframework.http.ResponseEntity;
5+
import org.springframework.messaging.handler.annotation.DestinationVariable;
56
import org.springframework.messaging.handler.annotation.MessageMapping;
7+
import org.springframework.messaging.handler.annotation.Payload;
68
import org.springframework.messaging.handler.annotation.SendTo;
79
import org.springframework.stereotype.Controller;
810
import org.springframework.web.bind.annotation.*;
9-
import pl.piotrowski.remotetexteditor.model.Document;
1011
import pl.piotrowski.remotetexteditor.application.DocumentsService;
12+
import pl.piotrowski.remotetexteditor.model.Document;
1113
import pl.piotrowski.remotetexteditor.service.exceptions.DocumentAlreadyExistsException;
1214
import pl.piotrowski.remotetexteditor.service.exceptions.DocumentNotFoundException;
1315

1416
import java.util.HashMap;
1517
import java.util.HashSet;
16-
import java.util.concurrent.CompletableFuture;
1718

1819

1920
@Controller
@@ -31,16 +32,21 @@ public DocumentsController(DocumentsService documentsService) {
3132
@Override
3233
@MessageMapping("/update/{name}")
3334
@SendTo("/topic/updates/{name}")
34-
public String updateDocumentsContent(@PathVariable String name, @RequestBody String content
35+
public String updateDocumentsContent(@DestinationVariable String name, @Payload(required = false) String content
3536
// , @RequestParam(defaultValue = "0") int position, @RequestParam(defaultValue = "true") boolean isReplacing
3637
) {
37-
CompletableFuture<Document> completableFuture = CompletableFuture.supplyAsync(()-> {
38-
try {
39-
return documentsService.updateDocumentsContent(name, content);
40-
} catch (DocumentNotFoundException e) {
41-
throw new RuntimeException("Error");
42-
}
43-
});
38+
Document document;
39+
40+
if (content==null){
41+
content="";
42+
}
43+
44+
try {
45+
document = documentsService.updateDocumentsContent(name, content);
46+
} catch (DocumentNotFoundException e) {
47+
e.printStackTrace();
48+
}
49+
4450

4551
return content;
4652
}
@@ -60,7 +66,7 @@ public ResponseEntity<Document> getDocument(@PathVariable String name) {
6066
@PostMapping
6167
public ResponseEntity<Document> createDocument(@RequestBody Document document) {
6268
try {
63-
document = documentsService.addDocument(document);
69+
document = documentsService.addDocument(document);
6470
} catch (DocumentAlreadyExistsException e) {
6571
e.printStackTrace();
6672
}

webclient/src/main/web/src/app/document/document.component.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,17 @@ export class DocumentComponent implements OnInit {
112112

113113
documentChanged() {
114114
console.log("changed");
115+
const initialContent = this.currentDocument.content;
115116
this.documentService.connectToWs(this.currentDocument);
117+
this.documentService.getDocument(this.currentDocument.name).subscribe(
118+
(document: Document)=>{
119+
console.log(initialContent);
120+
console.log(this.currentDocument.content);
121+
if(initialContent==this.currentDocument.content){
122+
this.currentDocument.content=document.content
123+
}
124+
}
125+
);
116126
this.startUpdating();
117127
}
118128
}

webclient/src/main/web/src/app/services/document.service.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ export class DocumentService implements OnInit{
3232
return this.http.get<Document[]>(this.webConfig.restUrl);
3333
}
3434

35+
getDocument(name: String): Observable<Document>{
36+
return this.http.get<Document>(this.webConfig.restUrl+"/"+name)
37+
}
38+
3539
addNewDocument(document: Document): Observable<Document> {
3640
return this.http.post<Document>(this.webConfig.restUrl, document);
3741
}
@@ -57,7 +61,9 @@ export class DocumentService implements OnInit{
5761
}
5862

5963
connectToWs(document: Document) {
64+
console.log("check 0");
6065
if (this.websocket){
66+
console.log("check 1");
6167
this.websocket.complete()
6268
}
6369
this.websocket = this.websocketService.getConnection(document);

webclient/src/main/web/src/app/services/web-socket.service.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,6 @@ export class WebSocketService {
2626
const ws = new SockJS(this.webConfig.wsUrl);
2727
const observable = Rx.Observable.create((obs) => {
2828

29-
if(this.stomp){
30-
this.stomp.disconnect(()=>{
31-
console.log("disconnecting...")
32-
})
33-
}
34-
35-
3629
this.stomp = Stomp.over(ws);
3730
this.stomp.connect({}, (frame) => {
3831
console.log(frame);
@@ -47,6 +40,7 @@ export class WebSocketService {
4740
this.stomp.send(this.webConfig.messageUrl + "/" + document.name, {}, data)
4841
},
4942
complete: ()=>{
43+
console.log("check2");
5044
this.stomp.disconnect(()=>{
5145
console.log("disconnecting...");
5246
ws.close();

0 commit comments

Comments
 (0)