-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.js
93 lines (79 loc) · 1.89 KB
/
handler.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
'use strict';
const EventEmitter = require('events');
var Sync = require('sync')
module.exports.Store = class Store extends EventEmitter {
constructor(handlers){
super();
var that = this;
this.on('data',(data,cb) => {
for(var i of handlers){
i.cb = cb;
i.emit('data',data);
}
});
console.log('finished with all other handlers')
}
}
module.exports.Handler = class Handler extends EventEmitter {
constructor(intent){
super();
this.intent = intent;
this.params = {};
this.template = this.template();
this.on('data',this.dataHandler);
this.on('success',this.successFunction);
this.on('error',this.failureFunction);
}
dataHandler(data){
this.data = data;
this.templateParser(data);
}
successFunction(successObject){
this.respond(successObject,this.cb);
}
failureFunction(failureObject){
console.log('failure')
}
emitSuccess(){
this.emit('success',{
state:'success',
intent:this.intent
});
}
emitFailure(){
this.emit('error',{
state:'error',
intent:this.intent
})
}
templateParser(data){
data = data.split(" ");
for(var i of this.template){
i = i.split(" ");
for(var x in i){
if(i[x].includes('}}')){
var varname = i[x].match("\{{([^}]*)\}}");
var left = i[x-1];
var right = i[x+1];
var sliced = data.slice(data.indexOf(left)+1,data.indexOf(right)-1)
data.splice(data.indexOf(left)+1,data.indexOf(right)-data.indexOf(left)+2)
console.log(data,i)
console.log(sliced)
this.params[varname[1]] = sliced.join(" ");
}
else if(i[x]!=data[x])
break;
if(x == i.length-1){
this.emitSuccess();
return;
}
}
}
}
template(){
return [
'template plz',
'template',
]
}
}