-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
118 lines (102 loc) · 3.23 KB
/
server.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
// Setting up express to work with socket.io
var express = require('express');
var app = express();
var path = require('path');
// These are our routes, connecting GET requests
// to the appropriate files.
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname + '/index.html'));
});
app.get('/client.js', function(req, res) {
res.sendFile(path.join(__dirname + '/client.js'));
});
app.get('/style/style.css', function(req, res) {
res.sendFile(path.join(__dirname + '/style/style.css'));
});
// Setting up port 3003 to be listened to,
// and hooking socket.io up to it
var io = app.listen(3003);
var server = require('socket.io').listen(io);
// Setup data
const firstTodos = require('./data');
// Todo class
const Todo = require('./todo');
// Found this cool idea online to use a flag
// to tell if the server has been reset or not
let serverStarted = false;
// First, we start the DB off empty
let DB = [];
server.on('connection', (client) => {
// This is going to be our fake 'database' for this application
// Parse all default Todo's from db
// Then, if the server was restarted, we populate the
// DB with firstTodos array
if (!serverStarted) {
DB = firstTodos.map((t) => {
// Form new Todo objects
return new Todo(t.title);
});
}
// And now, we know the server is running, so we set
// our flag to true!
serverStarted = true;
// Sends a message to the client to reload all todos
const reloadTodos = () => {
server.emit('load', DB);
};
// Sends a message to the client to add a single todo
const appendTodo = (todo) => {
server.emit('loadSingle', todo);
};
// Accepts when a client makes a new todo
client.on('make', (t) => {
// Make a new todo
const newTodo = new Todo(title=t.title);
// Push this newly created todo to our database
DB.push(newTodo);
// Send just the newly made todo to the client
appendTodo(newTodo);
});
// When a client deletes a todo
client.on('delete', (t) => {
// Given the todo, delete it from the list
const newDB = DB.filter((item) => {
return item.id !== t.id;
});
DB = newDB;
//Update the other clients
reloadTodos();
});
// When a client updates a todo
client.on("update", (t) => {
// Given the todo, set it's 'complete' value to true
const newDB = DB.map( (item) => {
if (item.id === t.id) {
item.completed = t.completed;
}
return item;
});
// Set the DB to the appropriate array
DB = newDB;
//Update the other clients
reloadTodos();
});
// Set all todo's to complete
client.on('completeAll', () => {
const newDB = DB.map( (item) => {
item.completed = true;
return item;
});
// Set the DB to the appropriate array
DB = newDB;
//Update the other clients
reloadTodos();
});
// Delete all todo's
client.on('deleteAll', () => {
DB = [];
reloadTodos();
});
// Send the DB downstream on connect
reloadTodos();
});