bashoto-js
Version:
Bashoto Javascript client
2 lines • 3 kB
JavaScript
/*! bashoto-js - v0.0.2 - 2014-11-22 - Buck Heroux */
(function(){function o(o){"use strict";function e(o){var e={error:function(o){console.log(o)}};return o.position&&(e.position=o.position),o.permisson&&(e.permission=o.permission),o.timeout&&(e.timeout=o.timeout),o.unknown&&(e.unknown=o.unknown),o.unsupported&&(e.unsupported=o.unsupported),o.error&&(e.error=o.error),e}var t=o.Bashoto=function(o,t){return t=t||{},this._appKey=o,this._errorHandlers=e(t.errors||{}),t.locate&&this.locate(t.locate),this};t.HOST="https://bashoto.com",t.HYPERLOCAL=Math.pow(10,2),t.LOCAL=Math.pow(10,3),t.SUBREGIONAL=Math.pow(10,4),t.REGIONAL=Math.pow(10,5),t.CONTINENTAL=Math.pow(10,6),t.HEMISPHERE=Math.pow(10,7),t.GLOBAL=0,t.prototype.getAppKey=function(){return this._appKey},t.prototype.locate=function(o){var r=o||{},n=r.range||t.LOCAL,s=r.success||function(){},i=e(r.errors||{});i.error=r.error||i.error;var a=this;navigator.geolocation||(i.unsupported?i.unsupported({}):i.error({message:"The browser does not support Geolocation"})),navigator.geolocation.getCurrentPosition(function(o){a._geo=o.coords,a._geo.range=n,s()},function(o){switch(o.code){case o.PERMISSION_DENIED:i.permission?i.permission(o):i.error(o);break;case o.POSITION_UNAVAILABLE:i.position?i.permission(o):i.error(o);break;case o.TIMEOUT:i.timeout?i.timeout(o):i.error(o);break;case o.UNKNOWN_ERROR:i.unknown?i.unknown(o):i.error(o)}})}}function e(o){"use strict";function e(){}function t(o){var t={message:e,open:e,close:e,error:e};return o.hasOwnProperty("message")&&(t.message=o.message),o.hasOwnProperty("open")&&(t.open=o.open),o.hasOwnProperty("close")&&(t.close=o.close),o.hasOwnProperty("error")&&(t.error=o.error),t}var r=o.Bashoto,n=r.Topic=function(o,e,r){return r=r||{},e=e||{},this._handlers=t(e),this._openqueue=[],this._ons={},this._bindsocket(o,r),this};n.prototype.publish=function(o){"object"==typeof o&&(o=JSON.stringify(o)),this.isOpen()?this._socket.send(o):this._openqueue.push(o)},n.prototype.close=function(){this.isOpen()&&this._socket.close()},n.prototype.isOpen=function(){return this._socket&&1===this._socket.readyState},n.prototype._bindsocket=function(o,e){var t=this,n=r.HOST+"/io/topic/"+o+"?callback=?";$.getJSON(n,function(o){var r=o.response.url+"?"+$.param(e),n=new WebSocket(r);n.onopen=function(o){for(;t._openqueue.length>0;)t.publish(t._openqueue.pop());t._handlers.open(o)},n.onclose=function(o){t._handlers.close(o)},n.onerror=function(o){t._handlers.error(o)},n.onmessage=function(o){var e=JSON.parse(o.data),r=e.msg;try{r=JSON.parse(e.msg)}catch(n){}t._handlers.message(r)},t._socket=n})},r.prototype.subscribe=function(o,e){var t=e||{};this._geo&&(t.lat=t.lat||this._geo.latitude,t.lon=t.lon||this._geo.longitude,t.range=t.range||this._geo.range),t.global&&delete t.range;var n=new r.Topic(this.getAppKey(),o,t);return n}}var t=Function;new t("return this")();var r=function(t){return o(t),e(t),t.Bashoto};"function"==typeof define&&define.amd?define(function(){return r({})}):r(this)})(this);