2 * Copyright (C) 2017, 2018 "IoT.bzh"
3 * Author: José Bollo <jose.bollo@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 AFB = function(base, initialtoken){
19 if (typeof base != "object")
20 base = { base: base, token: initialtoken };
23 base: base.base || "api",
24 token: base.token || initialtoken || "HELLO",
25 host: base.host || window.location.host,
26 url: base.url || undefined
29 var urlws = initial.url || "ws://"+initial.host+"/"+initial.base;
31 /*********************************************/
33 /**** AFB_context ****/
35 /*********************************************/
39 var TOKEN = initial.token;
41 var context = function(token, uuid) {
47 get token() {return TOKEN;},
48 set token(tok) {if(tok) TOKEN=tok;},
49 get uuid() {return UUID;},
50 set uuid(id) {if(id) UUID=id;}
53 AFB_context = new context();
55 /*********************************************/
57 /**** AFB_websocket ****/
59 /*********************************************/
67 var PROTO1 = "x-afb-ws-json1";
69 AFB_websocket = function(on_open, on_abort) {
71 if (AFB_context.token) {
72 u = u + '?x-afb-token=' + AFB_context.token;
74 u = u + '&x-afb-uuid=' + AFB_context.uuid;
76 this.ws = new WebSocket(u, [ PROTO1 ]);
81 this.ws.onopen = onopen.bind(this);
82 this.ws.onerror = onerror.bind(this);
83 this.ws.onclose = onclose.bind(this);
84 this.ws.onmessage = onmessage.bind(this);
85 this.onopen = on_open;
86 this.onabort = on_abort;
89 function onerror(event) {
96 this.onerror && this.onerror(this);
99 function onopen(event) {
106 function onclose(event) {
107 for (var id in this.pendings) {
108 try { this.pendings[id][1](); } catch (x) {/*TODO?*/}
111 this.onclose && this.onclose();
114 function fire(awaitens, name, data) {
115 var a = awaitens[name];
117 a.forEach(function(handler){handler(data);});
118 var i = name.indexOf("/");
120 a = awaitens[name.substring(0,i)];
122 a.forEach(function(handler){handler(data);});
126 a.forEach(function(handler){handler(data);});
129 function reply(pendings, id, ans, offset) {
130 if (id in pendings) {
131 var p = pendings[id];
133 try { p[offset](ans); } catch (x) {/*TODO?*/}
137 function onmessage(event) {
138 var obj = JSON.parse(event.data);
142 AFB_context.token = obj[3];
145 reply(this.pendings, id, ans, 0);
148 reply(this.pendings, id, ans, 1);
152 fire(this.awaitens, id, ans);
164 this.onabort = function(){};
167 function call(method, request, callid) {
168 return new Promise((function(resolve, reject){
172 if (id in this.pendings)
173 throw new Error("pending callid("+id+") exists");
176 id = String(this.counter = 4095 & (this.counter + 1));
177 } while (id in this.pendings);
179 this.pendings[id] = [ resolve, reject ];
180 arr = [CALL, id, method, request ];
181 if (AFB_context.token) arr.push(AFB_context.token);
182 this.ws.send(JSON.stringify(arr));
186 function onevent(name, handler) {
188 var list = this.awaitens[id] || (this.awaitens[id] = []);
192 AFB_websocket.prototype = {
198 /*********************************************/
202 /*********************************************/
204 context: AFB_context,