Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80698 views
1
/**
2
* This file is provided by Facebook for testing and evaluation purposes
3
* only. Facebook reserves all rights not expressly granted.
4
*
5
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
6
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
7
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
8
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
9
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
10
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11
*/
12
13
var ChatServerActionCreators = require('../actions/ChatServerActionCreators');
14
15
// !!! Please Note !!!
16
// We are using localStorage as an example, but in a real-world scenario, this
17
// would involve XMLHttpRequest, or perhaps a newer client-server protocol.
18
// The function signatures below might be similar to what you would build, but
19
// the contents of the functions are just trying to simulate client-server
20
// communication and server-side processing.
21
22
module.exports = {
23
24
getAllMessages: function() {
25
// simulate retrieving data from a database
26
var rawMessages = JSON.parse(localStorage.getItem('messages'));
27
28
// simulate success callback
29
ChatServerActionCreators.receiveAll(rawMessages);
30
},
31
32
createMessage: function(message, threadName) {
33
// simulate writing to a database
34
var rawMessages = JSON.parse(localStorage.getItem('messages'));
35
var timestamp = Date.now();
36
var id = 'm_' + timestamp;
37
var threadID = message.threadID || ('t_' + Date.now());
38
var createdMessage = {
39
id: id,
40
threadID: threadID,
41
threadName: threadName,
42
authorName: message.authorName,
43
text: message.text,
44
timestamp: timestamp
45
};
46
rawMessages.push(createdMessage);
47
localStorage.setItem('messages', JSON.stringify(rawMessages));
48
49
// simulate success callback
50
setTimeout(function() {
51
ChatServerActionCreators.receiveCreatedMessage(createdMessage);
52
}, 0);
53
}
54
55
};
56
57