text: // Compiled by ClojureScript 1.7.122 {}
  goog.provide('re_frame.cofx');
  goog.require('cljs.core');
  goog.require('re_frame.db');
  goog.require('re_frame.interceptor');
  goog.require('re_frame.registrar');
  goog.require('re_frame.loggers');
  re_frame.cofx.kind = new cljs.core.Keyword(null,"cofx","cofx",2013202907);
  if(cljs.core.truth_(re_frame.registrar.kinds.call(null,re_frame.cofx.kind))){
  } else {
  throw (new Error([cljs.core.str("Assert failed: "),cljs.core.str(cljs.core.pr_str.call(null,cljs.core.list(new cljs.core.Symbol("re-frame.registrar","kinds","re-frame.registrar/kinds",526766696,null),new cljs.core.Symbol(null,"kind","kind",923265724,null))))].join('')));
  }
  /**
   * Register the given coeffect `handler` for the given `id`, for later use
   *   within `inject-cofx`.
   * 
   *   `id` is keyword, often namespaced.
   *   `handler` is a function which takes either one or two arguements, the first of which is
   *   always `coeffects` and which returns an updated `coeffects`.
   * 
   *   See the docs for `inject-cofx` for example use.
   */
  re_frame.cofx.reg_cofx = (function re_frame$cofx$reg_cofx(id,handler){
  return re_frame.registrar.register_handler.call(null,re_frame.cofx.kind,id,handler);
  });
  /**
   * Given an `id`, and an optional, arbitrary `value`, returns an interceptor
   * whose `:before` adds to the `:coeffects` (map) by calling a pre-registered
   * 'coeffect handler' identified by the `id`.
   * 
   * The previous association of a `coeffect handler` with an `id` will have
   * happened via a call to `re-frame.core/reg-cofx` - generally on program startup.
   * 
   * Within the created interceptor, this 'looked up' `coeffect handler` will
   * be called (within the `:before`) with two arguments:
   *   - the current value of `:coeffects`
   *   - optionally, the originally supplied arbitrary `value`
   * 
   * This `coeffect handler` is expected to modify and return its first, `coeffects` argument.
   * 
   * Example Of how `inject-cofx` and `reg-cofx` work together
   * ---------------------------------------------------------
   * 
   * 1. Early in app startup, you register a `coeffect handler` for `:datetime`:
   * 
   *    (re-frame.core/reg-cofx
   *      :datetime                        ;; usage  (inject-cofx :datetime)
   *      (fn coeffect-handler
   *        [coeffect]
   *        (assoc coeffect :now (js/Date.))))   ;; modify and return first arg
   * 
   * 2. Later, add an interceptor to an -fx event handler, using `inject-cofx`:
   * 
   *    (re-frame.core/reg-event-fx        ;; we are registering an event handler
   *       :event-id
   *       [ ... (inject-cofx :datetime) ... ]    ;; <-- create an injecting interceptor
   *       (fn event-handler
   *         [coeffect event]
   *         ... in here can access (:now coeffect) to obtain current datetime ... )))
   * 
   * Background
   * ----------
   * 
   * `coeffects` are the input resources required by an event handler
   * to perform its job. The two most obvious ones are `db` and `event`.
   * But sometimes an event handler might need other resources.
   * 
   * Perhaps an event handler needs a random number or a GUID or the current
   * datetime. Perhaps it needs access to a DataScript database connection.
   * 
   * If an event handler directly accesses these resources, it stops being
   * pure and, consequently, it becomes harder to test, etc. So we don't
   * want that.
   * 
   * Instead, the interceptor created by this function is a way to 'inject'
   * 'necessary resources' into the `:coeffects` (map) subsequently given
   * to the event handler at call time.
   */
  re_frame.cofx.inject_cofx = (function re_frame$cofx$inject_cofx(var_args){
  var args24321 = [];
  var len__17951__auto___24324 = arguments.length;
  var i__17952__auto___24325 = (0);
  while(true){
  if((i__17952__auto___24325 < len__17951__auto___24324)){
  args24321.push((arguments[i__17952__auto___24325]));
  
  var G__24326 = (i__17952__auto___24325 + (1));
  i__17952__auto___24325 = G__24326;
  continue;
  } else {
  }
  break;
  }
  
  var G__24323 = args24321.length;
  switch (G__24323) {
  case 1:
  return re_frame.cofx.inject_cofx.cljs$core$IFn$_invoke$arity$1((arguments[(0)]));
  
  break;
  case 2:
  return re_frame.cofx.inject_cofx.cljs$core$IFn$_invoke$arity$2((arguments[(0)]),(arguments[(1)]));
  
  break;
  default:
  throw (new Error([cljs.core.str("Invalid arity: "),cljs.core.str(args24321.length)].join('')));
  
  }
  });
  
  re_frame.cofx.inject_cofx.cljs$core$IFn$_invoke$arity$1 = (function (id){
  return re_frame.interceptor.__GT_interceptor.call(null,new cljs.core.Keyword(null,"id","id",-1388402092),new cljs.core.Keyword(null,"coeffects","coeffects",497912985),new cljs.core.Keyword(null,"before","before",-1633692388),(function re_frame$cofx$coeffects_before(context){
  var temp__4423__auto__ = re_frame.registrar.get_handler.call(null,re_frame.cofx.kind,id);
  if(cljs.core.truth_(temp__4423__auto__)){
  var handler = temp__4423__auto__;
  return cljs.core.update.call(null,context,new cljs.core.Keyword(null,"coeffects","coeffects",497912985),handler);
  } else {
  return re_frame.loggers.console.call(null,new cljs.core.Keyword(null,"error","error",-978969032),"No cofx handler registered for \"",id,"\"");
  }
  }));
  });
  
  re_frame.cofx.inject_cofx.cljs$core$IFn$_invoke$arity$2 = (function (id,value){
  return re_frame.interceptor.__GT_interceptor.call(null,new cljs.core.Keyword(null,"id","id",-1388402092),new cljs.core.Keyword(null,"coeffects","coeffects",497912985),new cljs.core.Keyword(null,"before","before",-1633692388),(function re_frame$cofx$coeffects_before(context){
  var temp__4423__auto__ = re_frame.registrar.get_handler.call(null,re_frame.cofx.kind,id);
  if(cljs.core.truth_(temp__4423__auto__)){
  var handler = temp__4423__auto__;
  return cljs.core.update.call(null,context,new cljs.core.Keyword(null,"coeffects","coeffects",497912985),handler,value);
  } else {
  return re_frame.loggers.console.call(null,new cljs.core.Keyword(null,"error","error",-978969032),"No cofx handler registered for \"",id,"\"");
  }
  }));
  });
  
  re_frame.cofx.inject_cofx.cljs$lang$maxFixedArity = 2;
  re_frame.cofx.reg_cofx.call(null,new cljs.core.Keyword(null,"db","db",993250759),(function re_frame$cofx$db_coeffects_handler(coeffects){
  return cljs.core.assoc.call(null,coeffects,new cljs.core.Keyword(null,"db","db",993250759),cljs.core.deref.call(null,re_frame.db.app_db));
  }));
  re_frame.cofx.inject_db = re_frame.cofx.inject_cofx.call(null,new cljs.core.Keyword(null,"db","db",993250759));
  
  //# sourceMappingURL=cofx.js.map