-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.js
More file actions
55 lines (48 loc) · 1.39 KB
/
Copy pathServer.js
File metadata and controls
55 lines (48 loc) · 1.39 KB
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
const EventEmitter = require('events');
class Server extends EventEmitter {
constructor(client){
super();
this.tasks = {};
this.taskId = 1;
process.nextTick(()=> {
this.emit(
'response',
'Type a command (help to list commands)'
);
});
client.on('command', (command,args) => {
switch(command){
case 'help':
case 'add':
case 'ls':
case 'delete':
this[command](args);
break;
default:
this.emit('response','Unknown Command...')
}
});
}
taskString() {
return Object.keys(this.tasks).map(key => {
return `${key}: ${this.tasks[key]}`;
}).join('\n');
}
help() {this.emit('response', `Available Commands:
add task
ls
delete :id`)}
add(args) {
this.tasks[this.taskId] = args.join(' ');
this.emit('response', `Added task ${this.taskId}`);
this.taskId++;
}
ls() {
this.emit('response', `Tasks:\n${this.taskString()}`)
}
delete(args) {
delete(this.tasks[args[0]]);
this.emit('response', `Deleted task ${args[0]} ...`)
}
}
module.exports = (client) => new Server(client);