initial event handler
[src/app-framework-binder.git] / test / AFB.js
1 AFB = function(base, initialtoken){
2
3 var urlws = "ws://"+window.location.host+"/"+base;
4 var urlhttp = "http://"+window.location.host+"/"+base;
5
6 /*********************************************/
7 /****                                     ****/
8 /****             AFB_context             ****/
9 /****                                     ****/
10 /*********************************************/
11 var AFB_context;
12 {
13         var UUID = undefined;
14         var TOKEN = initialtoken;
15
16         var context = function(token, uuid) {
17                 this.token = token;
18                 this.uuid = uuid;
19         }
20
21         context.prototype = {
22                 get token() {return TOKEN;},
23                 set token(tok) {if(tok) TOKEN=tok;},
24                 get uuid() {return UUID;},
25                 set uuid(id) {if(id) UUID=id;}
26         };
27
28         AFB_context = new context();
29 }
30 /*********************************************/
31 /****                                     ****/
32 /****             AFB_websocket           ****/
33 /****                                     ****/
34 /*********************************************/
35 var AFB_websocket;
36 {
37         var CALL = 2;
38         var RETOK = 3;
39         var RETERR = 4;
40         var EVENT = 5;
41
42         var PROTO1 = "x-afb-ws-json1";
43
44         AFB_websocket = function(onopen, onabort) {
45                 this.ws = new WebSocket(urlws, [ PROTO1 ]);
46                 this.pendings = {};
47                 this.awaitens = {};
48                 this.counter = 0;
49                 this.ws.onopen = onopen.bind(this);
50                 this.ws.onerror = onerror.bind(this);
51                 this.ws.onclose = onclose.bind(this);
52                 this.ws.onmessage = onmessage.bind(this);
53                 this.onopen = onopen;
54                 this.onabort = onabort;
55         }
56
57         function onerror(event) {
58                 var f = this.onabort;
59                 if (f) {
60                         delete this.onopen;
61                         delete this.onabort;
62                         f && f(this);
63                 }
64                 this.onerror && this.onerror(this);
65         }
66
67         function onopen(event) {
68                 var f = this.onopen;
69                 delete this.onopen;
70                 delete this.onabort;
71                 f && f(this);
72         }
73
74         function onclose(event) {
75                 for (var id in this.pendings) {
76                         var ferr = this.pendings[id].onerror;
77                         ferr && ferr(null, this);
78                 }
79                 this.pendings = {};
80                 this.onclose && this.onclose();
81         }
82
83         function onmessage(event) {
84                 var obj = JSON.parse(event.data);
85                 var code = obj[0];
86                 var id = obj[1];
87                 var ans = obj[2];
88                 AFB_context.token = obj[3];
89                 var pend;
90                 if (id && id in this.pendings) {
91                         pend = this.pendings[id];
92                         delete this.pendings[id];
93                 }
94                 switch (code) {
95                 case EVENT:
96                         var a = this.awaitens[id];
97                         if (a)
98                                 a.forEach(function(handler){handler(ans);});
99                 case RETOK:
100                         pend && pend.onsuccess && pend.onsuccess(ans, this);
101                         break; 
102                 case RETERR:
103                 default:
104                         pend && pend.onerror && pend.onerror(ans, this);
105                         break; 
106                 }
107         }
108
109         function close() {
110                 this.ws.close();
111         }
112
113         function call(api, verb, request, onsuccess, onerror) {
114                 var id = String(++this.counter);
115                 this.pendings[id] = { onsuccess: onsuccess, onerror: onerror };
116                 var arr = [CALL, id, api+"/"+verb, request ];
117                 if (AFB_context.token) arr.push(AFB_context.token);
118                 this.ws.send(JSON.stringify(arr));
119         }
120
121         function onevent(api, name, handler) {
122                 var id = api+"/"+name;
123                 var list = this.awaitens[id] || (this.awaitens[id] = []);
124                 list.push(handler);
125         }
126
127         AFB_websocket.prototype = {
128                 close: close,
129                 call: call,
130                 onevent: onevent
131         };
132 }
133 /*********************************************/
134 /****                                     ****/
135 /****                                     ****/
136 /****                                     ****/
137 /*********************************************/
138 return {
139         context: AFB_context,
140         ws: AFB_websocket
141 };
142 };
143