only one context
[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
41         var PROTO1 = "x-afb-ws-json1";
42
43         AFB_websocket = function(onopen, onabort) {
44                 this.ws = new WebSocket(urlws, [ PROTO1 ]);
45                 this.pendings = {};
46                 this.counter = 0;
47                 this.ws.onopen = onopen.bind(this);
48                 this.ws.onerror = onerror.bind(this);
49                 this.ws.onclose = onclose.bind(this);
50                 this.ws.onmessage = onmessage.bind(this);
51                 this.onopen = onopen;
52                 this.onabort = onabort;
53         }
54
55         function onerror(event) {
56                 var f = this.onabort;
57                 if (f) {
58                         delete this.onopen;
59                         delete this.onabort;
60                         f && f(this);
61                 }
62                 this.onerror && this.onerror(this);
63         }
64
65         function onopen(event) {
66                 var f = this.onopen;
67                 delete this.onopen;
68                 delete this.onabort;
69                 f && f(this);
70         }
71
72         function onclose(event) {
73                 for (var id in this.pendings) {
74                         var ferr = this.pendings[id].onerror;
75                         ferr && ferr(null, this);
76                 }
77                 this.pendings = {};
78                 this.onclose && this.onclose();
79         }
80
81         function onmessage(event) {
82                 var obj = JSON.parse(event.data);
83                 var code = obj[0];
84                 var id = obj[1];
85                 var ans = obj[2];
86                 AFB_context.token = obj[3];
87                 var pend;
88                 if (id && id in this.pendings) {
89                         pend = this.pendings[id];
90                         delete this.pendings[id];
91                 }
92                 switch (code) {
93                 case RETOK:
94                         pend && pend.onsuccess && pend.onsuccess(ans, this);
95                         break; 
96                 case RETERR:
97                 default:
98                         pend && pend.onerror && pend.onerror(ans, this);
99                         break; 
100                 }
101         }
102
103         function close() {
104                 this.ws.close();
105         }
106
107         function call(api, verb, request, onsuccess, onerror) {
108                 var id = String(++this.counter);
109                 this.pendings[id] = { onsuccess: onsuccess, onerror: onerror };
110                 var arr = [CALL, id, api+"/"+verb, request ];
111                 if (AFB_context.token) arr.push(AFB_context.token);
112                 this.ws.send(JSON.stringify(arr));
113         }
114
115         AFB_websocket.prototype = {
116                 close: close,
117                 call: call
118         };
119 }
120 /*********************************************/
121 /****                                     ****/
122 /****                                     ****/
123 /****                                     ****/
124 /*********************************************/
125 return {
126         context: AFB_context,
127         ws: AFB_websocket
128 };
129 };
130