-
Notifications
You must be signed in to change notification settings - Fork 4
/
communicationService.js
146 lines (121 loc) · 4.47 KB
/
communicationService.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import config from '../../app.config'
import io from "socket.io-client";
import {ModelService} from "./modelService";
import * as Rx from "rxjs";
window.navigator.userAgent = 'react-native';
const scalaJS = require('./scalaJS/scala-js-fastopt.js');
let instance = null;
// Singleton
export class CommunicationService {
modelService;
contactsStatusChangeSubject;
newMessagesSubject;
constructor() {
this.socket = io(config.proxyHost + ":" + config.proxyPort);
this.transportService = new scalaJS.TransportService(this.socket);
if (!instance) {
instance = this;
}
return instance;
}
connected() {
return this.socket && this.socket.connected;
}
contactsUpdated() {
return this.modelService.contactsUpdatedSubject
}
newMessage() {
return this.modelService.newMessagesSubject
}
newDialog() {
return this.modelService.newDialogSubject
}
initModel(contactInfo) {
this.modelService = new ModelService();
// set user
this.modelService.setContactInfo(contactInfo);
this.listen();
// set contacts
return this.transportService.listContacts().then(contacts => {
this.modelService.setContacts(contacts);
// set dialogs
return this.transportService.listDialogs(contactInfo.contactReference)
.then(dialogs => {
this.modelService.setDialogs(dialogs);
// set messages
dialogs.forEach(dialogInfo => {
return this.transportService.listMessages(dialogInfo.dialogReference)
.then(messages => this.modelService.setMessages(dialogInfo.dialogReference, messages))
});
})
.then(() => this.modelService.getContactInfo());
})
}
listen() {
this.contactsStatusChangeSubject = new Rx.Subject();
this.transportService.listenNewUsers(this.contactsStatusChangeSubject);
this.contactsStatusChangeSubject.subscribe(statusChangedEvent => {
console.log('this.contactsStatusChangeSubject', statusChangedEvent);
if (statusChangedEvent.online) {
this.modelService.addContact(statusChangedEvent.contact);
} else {
this.modelService.removeContact(statusChangedEvent.contact)
}
});
this.newMessagesSubject = new Rx.Subject();
this.transportService.listenNewMessages(this.getUser().contactReference, this.newMessagesSubject);
this.newMessagesSubject.subscribe(message => {
this.modelService.addMessage(message);
})
}
login(userInfo) {
return this.transportService.addUser(userInfo).then(contactInfo => {
this.transportService.sendConnect(contactInfo.contactReference);
if (contactInfo.name === userInfo.name && contactInfo.avatar === userInfo.avatarUrl) {
return this.initModel(contactInfo).then(contactInfo => {
return contactInfo;
});
}
});
}
logout() {
this.transportService.logout(this.getUser());
this.contactsStatusChangeSubject.complete();
this.newMessagesSubject.complete();
this.modelService = null;
}
getUser() {
return this.modelService.getContactInfo();
}
listContacts() {
return this.modelService.getContacts();
}
listDialogs() {
return this.modelService.getDialogs();
}
getDialog(dialogReference) {
return this.modelService.getDialog(dialogReference);
}
isContactOnline(contactReference) {
return this.modelService.isContactOnline(contactReference);
}
processDialog(contactReference) {
return this.transportService.getOrCreateDialog(this.modelService.getContactInfo().contactReference, contactReference)
.then(dialogInfo => {
this.modelService.addDialog(dialogInfo);
return dialogInfo.dialogReference;
})
}
getMessages(dialogReference) {
return this.modelService.getMessages(dialogReference);
}
sendMessage(dialogReference, value) {
let message = {
dialogReference: dialogReference,
contactInfo: this.modelService.getContactInfo(),
value: value,
date: ""
};
this.transportService.sendMessage(message)
}
}