// 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 args36566 = [];
var len__24559__auto___36586 = arguments.length;
var i__24560__auto___36587 = (0);
while(true){
if((i__24560__auto___36587 < len__24559__auto___36586)){
args36566.push((arguments[i__24560__auto___36587]));

var G__36588 = (i__24560__auto___36587 + (1));
i__24560__auto___36587 = G__36588;
continue;
} else {
}
break;
}

var G__36568 = args36566.length;
switch (G__36568) {
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(args36566.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__36569 = opts;
var map__36569__$1 = ((((!((map__36569 == null)))?((((map__36569.cljs$lang$protocol_mask$partition0$ & (64))) || ((cljs.core.PROTOCOL_SENTINEL === map__36569.cljs$core$ISeq$)))?true:false):false))?cljs.core.apply.call(null,cljs.core.hash_map,map__36569):map__36569);
var ref = cljs.core.get.call(null,map__36569__$1,new cljs.core.Keyword(null,"ref","ref",1289896967));
var check_equals_QMARK_ = cljs.core.get.call(null,map__36569__$1,new cljs.core.Keyword(null,"check-equals?","check-equals?",-2005755315),true);
var recalc = (function (){var G__36571 = cljs.core.count.call(null,refs);
switch (G__36571) {
case (1):
var vec__36572 = refs;
var a = cljs.core.nth.call(null,vec__36572,(0),null);
return ((function (vec__36572,a,G__36571,map__36569,map__36569__$1,ref,check_equals_QMARK_){
return (function (){
return f.call(null,cljs.core.deref.call(null,a));
});
;})(vec__36572,a,G__36571,map__36569,map__36569__$1,ref,check_equals_QMARK_))

break;
case (2):
var vec__36575 = refs;
var a = cljs.core.nth.call(null,vec__36575,(0),null);
var b = cljs.core.nth.call(null,vec__36575,(1),null);
return ((function (vec__36575,a,b,G__36571,map__36569,map__36569__$1,ref,check_equals_QMARK_){
return (function (){
return f.call(null,cljs.core.deref.call(null,a),cljs.core.deref.call(null,b));
});
;})(vec__36575,a,b,G__36571,map__36569,map__36569__$1,ref,check_equals_QMARK_))

break;
case (3):
var vec__36578 = refs;
var a = cljs.core.nth.call(null,vec__36578,(0),null);
var b = cljs.core.nth.call(null,vec__36578,(1),null);
var c = cljs.core.nth.call(null,vec__36578,(2),null);
return ((function (vec__36578,a,b,c,G__36571,map__36569,map__36569__$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__36578,a,b,c,G__36571,map__36569,map__36569__$1,ref,check_equals_QMARK_))

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

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

return G__36581;
})():cljs.core.atom.call(null,recalc.call(null)));
var watch = (cljs.core.truth_(check_equals_QMARK_)?((function (map__36569,map__36569__$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__36569,map__36569__$1,ref,check_equals_QMARK_,recalc,sink))
:((function (map__36569,map__36569__$1,ref,check_equals_QMARK_,recalc,sink){
return (function (_,___$1,___$2,___$3){
return cljs.core.reset_BANG_.call(null,sink,recalc.call(null));
});})(map__36569,map__36569__$1,ref,check_equals_QMARK_,recalc,sink))
);
var seq__36582_36591 = cljs.core.seq.call(null,refs);
var chunk__36583_36592 = null;
var count__36584_36593 = (0);
var i__36585_36594 = (0);
while(true){
if((i__36585_36594 < count__36584_36593)){
var ref_36595__$1 = cljs.core._nth.call(null,chunk__36583_36592,i__36585_36594);
cljs.core.add_watch.call(null,ref_36595__$1,key,watch);

var G__36596 = seq__36582_36591;
var G__36597 = chunk__36583_36592;
var G__36598 = count__36584_36593;
var G__36599 = (i__36585_36594 + (1));
seq__36582_36591 = G__36596;
chunk__36583_36592 = G__36597;
count__36584_36593 = G__36598;
i__36585_36594 = G__36599;
continue;
} else {
var temp__5278__auto___36600 = cljs.core.seq.call(null,seq__36582_36591);
if(temp__5278__auto___36600){
var seq__36582_36601__$1 = temp__5278__auto___36600;
if(cljs.core.chunked_seq_QMARK_.call(null,seq__36582_36601__$1)){
var c__24249__auto___36602 = cljs.core.chunk_first.call(null,seq__36582_36601__$1);
var G__36603 = cljs.core.chunk_rest.call(null,seq__36582_36601__$1);
var G__36604 = c__24249__auto___36602;
var G__36605 = cljs.core.count.call(null,c__24249__auto___36602);
var G__36606 = (0);
seq__36582_36591 = G__36603;
chunk__36583_36592 = G__36604;
count__36584_36593 = G__36605;
i__36585_36594 = G__36606;
continue;
} else {
var ref_36607__$1 = cljs.core.first.call(null,seq__36582_36601__$1);
cljs.core.add_watch.call(null,ref_36607__$1,key,watch);

var G__36608 = cljs.core.next.call(null,seq__36582_36601__$1);
var G__36609 = null;
var G__36610 = (0);
var G__36611 = (0);
seq__36582_36591 = G__36608;
chunk__36583_36592 = G__36609;
count__36584_36593 = G__36610;
i__36585_36594 = G__36611;
continue;
}
} else {
}
}
break;
}

return sink;
});

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

