forked from Touffy/JSSCxml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SCxmlInvoke.js
96 lines (85 loc) · 2.52 KB
/
SCxmlInvoke.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
SCxml.invokeTypes={
scxml: {
name: 'http://www.w3.org/TR/scxml',
instantiate: function(src, data, id, psc){
var sc=new SCxml(src, null, null, true)
sc.iid=id
sc.name=id
sc.parent=psc
sc.sharedData=data
return sc
}
}
}
SCxml.prototype.invokeAll=function()
{
var invs=this.dom.querySelectorAll("*[active] > invoke")
for(var i=0; i<invs.length; i++){
try{ this.invoke(invs[i]) } catch(err){}
}
return this.toInvoke.length
}
SCxml.prototype.cancelInvoke=function(inv)
{
if(!(inv in this.invoked))
return false
try{ this.invoked[inv].clean() } catch(err){}
}
SCxml.prototype.invoke=function(inv)
{
var id=getId(inv), loc
if(id in this.invoked || id in this.toInvoke.items) return;
if(loc=inv.getAttribute("idlocation"))
this.expr(loc+'="'+id+'"')
var type=inv.getAttribute("type")
||this.expr(inv.getAttribute("typeexpr"), inv)
||"scxml"
if(!(type in SCxml.invokeTypes)){
type=type.replace(/\/$/, "")
for(var st in SCxml.invokeTypes)
if(SCxml.invokeTypes[st].name==type){ type=st; break }
}
if(!(type in SCxml.invokeTypes))
this.error("execution",inv,
new Error('unsupported invoke type "'+type+'"'))
var namelist=inv.getAttribute("namelist")
var data={}
if(namelist)
{
namelist=namelist.split(" ")
for(var i=0, name; name=namelist[i]; i++)
data[name]=this.expr(name)
}
if('open' in SCxml.invokeTypes[type]){
// we're dealing with a connection-like invoke
var src=inv.getAttribute("target")
||this.expr(inv.getAttribute("targetexpr"), inv)
if(!src) this.error("execution",inv, new Error('target required'))
data=this.readParams(inv, data, true)
}
else{
var src=inv.getAttribute("src")
||this.expr(inv.getAttribute("srcexpr"), inv)
var c=this.dom.querySelector("[id='"+id+"'] > content")
if(c){
if(c.hasAttribute("expr"))
src=this.expr(c.getAttribute("expr"), c)
else if(!c.firstElementChild) src=c.textContent
else src=new XMLSerializer().serializeToString(c.firstElementChild)
}
data=this.readParams(inv, data)
this.toInvoke.add(id) // we won't continue interpretation of the parent
// until the invoked session has become stable
}
// now create the invoked session
var invoked=SCxml.invokeTypes[type][('open' in SCxml.invokeTypes[type])?
"open":"instantiate"](src, data, id, this)
invoked.af=inv.hasAttribute('autoforward')
}
SCxml.prototype.emptyFinalize=function(event)
{
var inv=this.invoked[event.invokeid]
if(!inv.sharedData || !event.data) return;
for(var i in inv.sharedData) if(i in event.data)
this.assign(i, event.data[i])
}