// Compiled by ClojureScript 0.0-2138
goog.provide('lt.objs.clients.ws');
goog.require('cljs.core');
goog.require('lt.util.js');
goog.require('lt.objs.files');
goog.require('lt.util.js');
goog.require('cljs.reader');
goog.require('clojure.string');
goog.require('lt.objs.files');
goog.require('lt.objs.clients');
goog.require('lt.util.load');
goog.require('clojure.string');
goog.require('lt.object');
goog.require('cljs.reader');
goog.require('lt.object');
goog.require('lt.util.load');
goog.require('lt.objs.clients');
lt.objs.clients.ws.port = 0;
lt.objs.clients.ws.sockets = cljs.core.atom.call(null,cljs.core.PersistentArrayMap.EMPTY);
lt.objs.clients.ws.io = lt.util.load.node_module.call(null,"socket.io");
lt.objs.clients.ws.net = require("net");
lt.objs.clients.ws.send_to = (function send_to(sock,data){if(cljs.core.truth_(sock))
{return sock.emit(cljs.core.second.call(null,data),data);
} else
{return console.log([cljs.core.str("No such client: "),cljs.core.str(sock)].join(''));
}
});
lt.objs.clients.ws.__GT_client = (function __GT_client(data){var d = cljs.core.js__GT_clj.call(null,data,new cljs.core.Keyword(null,"keywordize-keys","keywordize-keys",4191781672),true);return cljs.core.assoc.call(null,d,new cljs.core.Keyword(null,"type","type",1017479852),new cljs.core.Keyword(null,"ws","ws",1013908046));
});
lt.objs.clients.ws.store_client_BANG_ = (function store_client_BANG_(socket,data){var data__$1 = cljs.core.js__GT_clj.call(null,data,new cljs.core.Keyword(null,"keywordize-keys","keywordize-keys",4191781672),true);var client = lt.objs.clients.by_name.call(null,new cljs.core.Keyword(null,"name","name",1017277949).cljs$core$IFn$_invoke$arity$1(data__$1));var data__$2 = ((cljs.core.not.call(null,new cljs.core.Keyword(null,"tags","tags",1017456523).cljs$core$IFn$_invoke$arity$1(data__$1)))?cljs.core.assoc.call(null,data__$1,new cljs.core.Keyword(null,"tags","tags",1017456523),new cljs.core.PersistentVector(null, 1, 5, cljs.core.PersistentVector.EMPTY_NODE, [new cljs.core.Keyword(null,"ws.client","ws.client",4342632079)], null)):cljs.core.assoc.call(null,data__$1,new cljs.core.Keyword(null,"tags","tags",1017456523),cljs.core.map.call(null,cljs.core.keyword,new cljs.core.Keyword(null,"tags","tags",1017456523).cljs$core$IFn$_invoke$arity$1(data__$1))));socket.on("disconnect",(function (){var temp__4092__auto__ = lt.objs.clients.by_name.call(null,new cljs.core.Keyword(null,"name","name",1017277949).cljs$core$IFn$_invoke$arity$1(data__$2));if(cljs.core.truth_(temp__4092__auto__))
{var cur = temp__4092__auto__;if(cljs.core._EQ_.call(null,socket,new cljs.core.Keyword(null,"socket","socket",4411822821).cljs$core$IFn$_invoke$arity$1(cljs.core.deref.call(null,cur))))
{return lt.objs.clients.rem_BANG_.call(null,cur);
} else
{return null;
}
} else
{return null;
}
}));
if(cljs.core.truth_(lt.objs.clients.available_QMARK_.call(null,client)))
{return lt.object.merge_BANG_.call(null,client,new cljs.core.PersistentArrayMap(null, 1, [new cljs.core.Keyword(null,"socket","socket",4411822821),socket], null));
} else
{return lt.objs.clients.handle_connection_BANG_.call(null,cljs.core.assoc.call(null,data__$2,new cljs.core.Keyword(null,"socket","socket",4411822821),socket,new cljs.core.Keyword(null,"type","type",1017479852),new cljs.core.Keyword(null,"websocket","websocket",784338745)));
}
});
lt.objs.clients.ws.on_result = (function on_result(socket,data){return lt.object.raise.call(null,lt.objs.clients.clients,new cljs.core.Keyword(null,"message","message",1968829305),cljs.core.js__GT_clj.call(null,data,new cljs.core.Keyword(null,"keywordize-keys","keywordize-keys",4191781672),true));
});
lt.objs.clients.ws.on_connect = (function on_connect(socket){socket.on("result",(function (p1__14670_SHARP_){return lt.objs.clients.ws.on_result.call(null,socket,p1__14670_SHARP_);
}));
return socket.on("init",cljs.core.partial.call(null,lt.objs.clients.ws.store_client_BANG_,socket));
});
lt.objs.clients.ws.__BEH__send_BANG_ = (function __BEH__send_BANG_(this$,msg){return lt.objs.clients.ws.send_to.call(null,new cljs.core.Keyword(null,"socket","socket",4411822821).cljs$core$IFn$_invoke$arity$1(cljs.core.deref.call(null,this$)),[new cljs.core.Keyword(null,"cb","cb",1013907409).cljs$core$IFn$_invoke$arity$1(msg),new cljs.core.Keyword(null,"command","command",1964298941).cljs$core$IFn$_invoke$arity$1(msg),cljs.core.clj__GT_js.call(null,new cljs.core.Keyword(null,"data","data",1016980252).cljs$core$IFn$_invoke$arity$1(msg))]);
});
lt.object.behavior_STAR_.call(null,new cljs.core.Keyword("lt.objs.clients.ws","send!","lt.objs.clients.ws/send!",4061093404),new cljs.core.Keyword(null,"reaction","reaction",4441361819),lt.objs.clients.ws.__BEH__send_BANG_,new cljs.core.Keyword(null,"triggers","triggers",2516997421),new cljs.core.PersistentHashSet(null, new cljs.core.PersistentArrayMap(null, 1, [new cljs.core.Keyword(null,"send!","send!",1123226891),null], null), null));
lt.objs.clients.ws.server = (function (){try{var ws = lt.objs.clients.ws.io.listen(5678);ws.set("log level",1);
ws.server.on("error",(function (p1__14671_SHARP_){if(cljs.core._EQ_.call(null,p1__14671_SHARP_.code,"EADDRINUSE"))
{console.log("Default socket.io port already used. Retrying with a random port.");
return ws.server.listen(0);
} else
{throw p1__14671_SHARP_;
}
}));
ws.server.on("listening",(function (){return lt.objs.clients.ws.port = ws.server.address().port;
}));
(ws["static"]).add("/lighttable/ws.js",cljs.core.clj__GT_js.call(null,new cljs.core.PersistentArrayMap(null, 1, [new cljs.core.Keyword(null,"file","file",1017047278),lt.objs.files.lt_home.call(null,"core/node_modules/lighttable/ws.js")], null)));
ws.sockets.on("connection",lt.objs.clients.ws.on_connect);
return ws;
}catch (e14672){var e = e14672;return console.error.call(null,"Error starting socket.io server",e);
}})();
lt.objs.clients.ws.__BEH__kill_on_closed = (function __BEH__kill_on_closed(app){return lt.objs.clients.ws.server.close();
});
lt.object.behavior_STAR_.call(null,new cljs.core.Keyword("lt.objs.clients.ws","kill-on-closed","lt.objs.clients.ws/kill-on-closed",1548479082),new cljs.core.Keyword(null,"reaction","reaction",4441361819),lt.objs.clients.ws.__BEH__kill_on_closed,new cljs.core.Keyword(null,"triggers","triggers",2516997421),new cljs.core.PersistentHashSet(null, new cljs.core.PersistentArrayMap(null, 1, [new cljs.core.Keyword(null,"closed","closed",3951351006),null], null), null));
