javascript reference with promise and event
[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 fire(awaitens, name, data) {
84                 var a = awaitens[name];
85                 if (a)
86                         a.forEach(function(handler){handler(data);});
87                 var i = name.indexOf("/");
88                 if (i >= 0) {
89                         a = awaitens[name.substring(0,i)];
90                         if (a)
91                                 a.forEach(function(handler){handler(data);});
92                 }
93                 a = awaitens["*"];
94                 if (a)
95                         a.forEach(function(handler){handler(data);});
96         }
97
98         function reply(pendings, id, ans, offset) {
99                 if (id in pendings) {
100                         var p = pendings[id];
101                         delete pendings[id];
102                         var f = p[offset];
103                         f(ans);
104                 }
105         }
106
107         function onmessage(event) {
108                 var obj = JSON.parse(event.data);
109                 var code = obj[0];
110                 var id = obj[1];
111                 var ans = obj[2];
112                 AFB_context.token = obj[3];
113                 switch (code) {
114                 case RETOK:
115                         reply(this.pendings, id, ans, 0);
116                         break; 
117                 case RETERR:
118                         reply(this.pendings, id, ans, 1);
119                         break; 
120                 case EVENT:
121                 default:
122                         fire(this.awaitens, id, ans);
123                         break; 
124                 }
125         }
126
127         function close() {
128                 this.ws.close();
129         }
130
131         function call(method, request) {
132                 return new Promise((function(resolve, reject){
133                         var id, arr;
134                         do {
135                                 id = String(this.counter = 4095 & (this.counter + 1));
136                         } while (id in this.pendings);
137                         this.pendings[id] = [ resolve, reject ];
138                         arr = [CALL, id, method, request ];
139                         if (AFB_context.token) arr.push(AFB_context.token);
140                         this.ws.send(JSON.stringify(arr));
141                 }).bind(this));
142         }
143
144         function onevent(name, handler) {
145                 var id = name;
146                 var list = this.awaitens[id] || (this.awaitens[id] = []);
147                 list.push(handler);
148         }
149
150         AFB_websocket.prototype = {
151                 close: close,
152                 call: call,
153                 onevent: onevent
154         };
155 }
156 /*********************************************/
157 /****                                     ****/
158 /****                                     ****/
159 /****                                     ****/
160 /*********************************************/
161 return {
162         context: AFB_context,
163         ws: AFB_websocket
164 };
165 };
166