-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathSCxmlConnect.js
54 lines (48 loc) · 1.57 KB
/
SCxmlConnect.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
SCxml.invokeTypes["event-stream"]=
{
name: 'http://www.jsscxml.org/event-stream/',
open: function(target, data, id, psc){
try{ var stream=new EventSource(target) }
catch(err){ psc.error("communication."+id, this, err) }
stream.iid=id
psc.invoked[id]=stream
stream.parent=psc
stream.sharedData=data
stream.onmessage=SCxml.invokeTypes["event-stream"].onmessage
stream.onerror=SCxml.invokeTypes["event-stream"].onerror
stream.clean=SCxml.invokeTypes["event-stream"].clean
stream.fireEvent=SCxml.invokeTypes["event-stream"].noSend
return stream
},
noSend: function(){
this.parent.error("communication."+this.iid, this, new Error(
"Cannot send events over this connection"))
},
onmessage: function(message){
var name=message.data.match(/^\s*(\w+(?:\.\w+)?)/)
name=name?name[1]:"message."+this.iid
var data=message.data.replace(/^.*\n/, "")
if(data) try{ data=JSON.parse(data) }catch(err){}
else data=undefined
var e=new SCxml.ExternalEvent(name, "#_"+this.iid, "event-stream",
this.iid, data)
e.timeStamp=message.timeStamp
// done.* event => terminate the connection after queuing it
if(/^done\b/.test(name)) this.sendNoMore=true
this.parent.fireEvent(e)
if(/^done\b/.test(name)) this.clean()
},
onerror: function(){
if(this.readyState==EventSource.CLOSED){
this.parent.error("communication."+this.iid, this, new Error(
"Cannot establish event-stream connection to "+this.url), true)
this.clean()
}
},
clean: function(){
this.sendNoMore=true
this.close()
delete this.parent.invoked[this.iid]
delete this.parent
}
}