// Compiled by ClojureScript 1.9.562 {}
goog.provide('rum.derived_atom');
goog.require('cljs.core');
rum.derived_atom.derived_atom = (function rum$derived_atom$derived_atom(var_args){
var args36666 = [];
var len__24559__auto___36686 = arguments.length;
var i__24560__auto___36687 = (0);
while(true){
if((i__24560__auto___36687 < len__24559__auto___36686)){
args36666.push((arguments[i__24560__auto___36687]));

var G__36688 = (i__24560__auto___36687 + (1));
i__24560__auto___36687 = G__36688;
continue;
} else {
}
break;
}

var G__36668 = args36666.length;
switch (G__36668) {
case 3:
return rum.derived_atom.derived_atom.cljs$core$IFn$_invoke$arity$3((arguments[(0)]),(arguments[(1)]),(arguments[(2)]));

break;
case 4:
return rum.derived_atom.derived_atom.cljs$core$IFn$_invoke$arity$4((arguments[(0)]),(arguments[(1)]),(arguments[(2)]),(arguments[(3)]));

break;
default:
throw (new Error([cljs.core.str.cljs$core$IFn$_invoke$arity$1("Invalid arity: "),cljs.core.str.cljs$core$IFn$_invoke$arity$1(args36666.length)].join('')));

}
});

rum.derived_atom.derived_atom.cljs$core$IFn$_invoke$arity$3 = (function (refs,key,f){
return rum.derived_atom.derived_atom.call(null,refs,key,f,cljs.core.PersistentArrayMap.EMPTY);
});

rum.derived_atom.derived_atom.cljs$core$IFn$_invoke$arity$4 = (function (refs,key,f,opts){
var map__36669 = opts;
var map__36669__$1 = ((((!((map__36669 == null)))?((((map__36669.cljs$lang$protocol_mask$partition0$ & (64))) || ((cljs.core.PROTOCOL_SENTINEL === map__36669.cljs$core$ISeq$)))?true:false):false))?cljs.core.apply.call(null,cljs.core.hash_map,map__36669):map__36669);
var ref = cljs.core.get.call(null,map__36669__$1,new cljs.core.Keyword(null,"ref","ref",1289896967));
var check_equals_QMARK_ = cljs.core.get.call(null,map__36669__$1,new cljs.core.Keyword(null,"check-equals?","check-equals?",-2005755315),true);
var recalc = (function (){var G__36671 = cljs.core.count.call(null,refs);
switch (G__36671) {
case (1):
var vec__36672 = refs;
var a = cljs.core.nth.call(null,vec__36672,(0),null);
return ((function (vec__36672,a,G__36671,map__36669,map__36669__$1,ref,check_equals_QMARK_){
return (function (){
return f.call(null,cljs.core.deref.call(null,a));
});
;})(vec__36672,a,G__36671,map__36669,map__36669__$1,ref,check_equals_QMARK_))

break;
case (2):
var vec__36675 = refs;
var a = cljs.core.nth.call(null,vec__36675,(0),null);
var b = cljs.core.nth.call(null,vec__36675,(1),null);
return ((function (vec__36675,a,b,G__36671,map__36669,map__36669__$1,ref,check_equals_QMARK_){
return (function (){
return f.call(null,cljs.core.deref.call(null,a),cljs.core.deref.call(null,b));
});
;})(vec__36675,a,b,G__36671,map__36669,map__36669__$1,ref,check_equals_QMARK_))

break;
case (3):
var vec__36678 = refs;
var a = cljs.core.nth.call(null,vec__36678,(0),null);
var b = cljs.core.nth.call(null,vec__36678,(1),null);
var c = cljs.core.nth.call(null,vec__36678,(2),null);
return ((function (vec__36678,a,b,c,G__36671,map__36669,map__36669__$1,ref,check_equals_QMARK_){
return (function (){
return f.call(null,cljs.core.deref.call(null,a),cljs.core.deref.call(null,b),cljs.core.deref.call(null,c));
});
;})(vec__36678,a,b,c,G__36671,map__36669,map__36669__$1,ref,check_equals_QMARK_))

break;
default:
return ((function (G__36671,map__36669,map__36669__$1,ref,check_equals_QMARK_){
return (function (){
return cljs.core.apply.call(null,f,cljs.core.map.call(null,cljs.core.deref,refs));
});
;})(G__36671,map__36669,map__36669__$1,ref,check_equals_QMARK_))

}
})();
var sink = (cljs.core.truth_(ref)?(function (){var G__36681 = ref;
cljs.core.reset_BANG_.call(null,G__36681,recalc.call(null));

return G__36681;
})():cljs.core.atom.call(null,recalc.call(null)));
var watch = (cljs.core.truth_(check_equals_QMARK_)?((function (map__36669,map__36669__$1,ref,check_equals_QMARK_,recalc,sink){
return (function (_,___$1,___$2,___$3){
var new_val = recalc.call(null);
if(cljs.core.not_EQ_.call(null,cljs.core.deref.call(null,sink),new_val)){
return cljs.core.reset_BANG_.call(null,sink,new_val);
} else {
return null;
}
});})(map__36669,map__36669__$1,ref,check_equals_QMARK_,recalc,sink))
:((function (map__36669,map__36669__$1,ref,check_equals_QMARK_,recalc,sink){
return (function (_,___$1,___$2,___$3){
return cljs.core.reset_BANG_.call(null,sink,recalc.call(null));
});})(map__36669,map__36669__$1,ref,check_equals_QMARK_,recalc,sink))
);
var seq__36682_36691 = cljs.core.seq.call(null,refs);
var chunk__36683_36692 = null;
var count__36684_36693 = (0);
var i__36685_36694 = (0);
while(true){
if((i__36685_36694 < count__36684_36693)){
var ref_36695__$1 = cljs.core._nth.call(null,chunk__36683_36692,i__36685_36694);
cljs.core.add_watch.call(null,ref_36695__$1,key,watch);

var G__36696 = seq__36682_36691;
var G__36697 = chunk__36683_36692;
var G__36698 = count__36684_36693;
var G__36699 = (i__36685_36694 + (1));
seq__36682_36691 = G__36696;
chunk__36683_36692 = G__36697;
count__36684_36693 = G__36698;
i__36685_36694 = G__36699;
continue;
} else {
var temp__5278__auto___36700 = cljs.core.seq.call(null,seq__36682_36691);
if(temp__5278__auto___36700){
var seq__36682_36701__$1 = temp__5278__auto___36700;
if(cljs.core.chunked_seq_QMARK_.call(null,seq__36682_36701__$1)){
var c__24249__auto___36702 = cljs.core.chunk_first.call(null,seq__36682_36701__$1);
var G__36703 = cljs.core.chunk_rest.call(null,seq__36682_36701__$1);
var G__36704 = c__24249__auto___36702;
var G__36705 = cljs.core.count.call(null,c__24249__auto___36702);
var G__36706 = (0);
seq__36682_36691 = G__36703;
chunk__36683_36692 = G__36704;
count__36684_36693 = G__36705;
i__36685_36694 = G__36706;
continue;
} else {
var ref_36707__$1 = cljs.core.first.call(null,seq__36682_36701__$1);
cljs.core.add_watch.call(null,ref_36707__$1,key,watch);

var G__36708 = cljs.core.next.call(null,seq__36682_36701__$1);
var G__36709 = null;
var G__36710 = (0);
var G__36711 = (0);
seq__36682_36691 = G__36708;
chunk__36683_36692 = G__36709;
count__36684_36693 = G__36710;
i__36685_36694 = G__36711;
continue;
}
} else {
}
}
break;
}

return sink;
});

rum.derived_atom.derived_atom.cljs$lang$maxFixedArity = 4;

