don't enforce to refresh the token
[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                 var u = urlws;
46                 if (AFB_context.token) {
47                         u = u + '?x-afb-token=' + AFB_context.token;
48                         if (AFB_context.uuid)
49                                 u = u + '&x-afb-uuid=' + AFB_context.uuid;
50                 }
51                 this.ws = new WebSocket(u, [ PROTO1 ]);
52                 this.pendings = {};
53                 this.awaitens = {};
54                 this.counter = 0;
55                 this.ws.onopen = onopen.bind(this);
56                 this.ws.onerror = onerror.bind(this);
57                 this.ws.onclose = onclose.bind(this);
58                 this.ws.onmessage = onmessage.bind(this);
59                 this.onopen = onopen;
60                 this.onabort = onabort;
61         }
62
63         function onerror(event) {
64                 var f = this.onabort;
65                 if (f) {
66                         delete this.onopen;
67                         delete this.onabort;
68                         f && f(this);
69                 }
70                 this.onerror && this.onerror(this);
71         }
72
73         function onopen(event) {
74                 var f = this.onopen;
75                 delete this.onopen;
76                 delete this.onabort;
77                 f && f(this);
78         }
79
80         function onclose(event) {
81                 for (var id in this.pendings) {
82                         var ferr = this.pendings[id].onerror;
83                         ferr && ferr(null, this);
84                 }
85                 this.pendings = {};
86                 this.onclose && this.onclose();
87         }
88
89         function fire(awaitens, name, data) {
90                 var a = awaitens[name];
91                 if (a)
92                         a.forEach(function(handler){handler(data);});
93                 var i = name.indexOf("/");
94                 if (i >= 0) {
95                         a = awaitens[name.substring(0,i)];
96                         if (a)
97                                 a.forEach(function(handler){handler(data);});
98                 }
99                 a = awaitens["*"];
100                 if (a)
101                         a.forEach(function(handler){handler(data);});
102         }
103
104         function reply(pendings, id, ans, offset) {
105                 if (id in pendings) {
106                         var p = pendings[id];
107                         delete pendings[id];
108                         var f = p[offset];
109                         f(ans);
110                 }
111         }
112
113         function onmessage(event) {
114                 var obj = JSON.parse(event.data);
115                 var code = obj[0];
116                 var id = obj[1];
117                 var ans = obj[2];
118                 AFB_context.token = obj[3];
119                 switch (code) {
120                 case RETOK:
121                         reply(this.pendings, id, ans, 0);
122                         break; 
123                 case RETERR:
124                         reply(this.pendings, id, ans, 1);
125                         break; 
126                 case EVENT:
127                 default:
128                         fire(this.awaitens, id, ans);
129                         break; 
130                 }
131         }
132
133         function close() {
134                 this.ws.close();
135         }
136
137         function call(method, request) {
138                 return new Promise((function(resolve, reject){
139                         var id, arr;
140                         do {
141                                 id = String(this.counter = 4095 & (this.counter + 1));
142                         } while (id in this.pendings);
143                         this.pendings[id] = [ resolve, reject ];
144                         arr = [CALL, id, method, request ];
145                         if (AFB_context.token) arr.push(AFB_context.token);
146                         this.ws.send(JSON.stringify(arr));
147                 }).bind(this));
148         }
149
150         function onevent(name, handler) {
151                 var id = name;
152                 var list = this.awaitens[id] || (this.awaitens[id] = []);
153                 list.push(handler);
154         }
155
156         AFB_websocket.prototype = {
157                 close: close,
158                 call: call,
159                 onevent: onevent
160         };
161 }
162 /*********************************************/
163 /****                                     ****/
164 /****                                     ****/
165 /****                                     ****/
166 /*********************************************/
167 return {
168         context: AFB_context,
169         ws: AFB_websocket
170 };
171 };
172