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