Liking cljdoc? Tell your friends :D

re-frame-lib.std-interceptors

contains re-frame supplied, standard interceptors

contains re-frame supplied, standard interceptors
raw docstring

afterclj/s

(after f)

returns an interceptor which runs a given function f in the :after position, presumably for side effects.

f is called with two arguments: the :effects value for :db (or the coeffect value of db if no db effect is returned) and the event. Its return value is ignored, so f can only side-effect.

Examples use can be seen in the /examples/todomvc:

  • f runs schema validation (reporting any errors found).
  • f writes to localstorage.
returns an interceptor which runs a given function `f` in the `:after`
position, presumably for side effects.

`f` is called with two arguments: the `:effects` value for `:db`
(or the `coeffect` value of db if no db effect is returned) and the event.
Its return value is ignored, so `f` can only side-effect.

Examples use can be seen in the /examples/todomvc:
   - `f` runs schema validation (reporting any errors found).
   - `f` writes to localstorage.
sourceraw docstring

ctx-handler->interceptorclj/s

(ctx-handler->interceptor handler-fn)

Returns an interceptor which wraps the kind of event handler given to reg-event-ctx. These advanced handlers take one argument: context and they return a modified context. Example: (fn [context] (enqueue context [more interceptors]))

Returns an interceptor which wraps the kind of event handler given to `reg-event-ctx`.
These advanced handlers take one argument: `context` and they return a modified `context`.
Example:
   (fn [context]
      (enqueue context [more interceptors]))
sourceraw docstring

db-handler->interceptorclj/s

(db-handler->interceptor handler-fn)

Returns an interceptor which wraps the kind of event handler given to reg-event-db.

These handlers take two arguments; db and event, and they return db.

(fn [db event] ....)

So, the interceptor wraps the given handler:

  1. extracts two :coeffects keys: db and event
  2. calls handler-fn
  3. stores the db result back into context's :effects
Returns an interceptor which wraps the kind of event handler given to `reg-event-db`.

These handlers take two arguments;  `db` and `event`, and they return `db`.

(fn [db event]
   ....)

So, the interceptor wraps the given handler:
   1. extracts two `:coeffects` keys: db and event
   2. calls handler-fn
   3. stores the db result back into context's `:effects`
sourceraw docstring

debugclj/s

An interceptor which logs/instruments an event handler's actions to js/console.debug. See examples/todomvc/src/events.cljs for use.

Output includes:

  1. the event vector
  2. a clojure.data/diff of db, before vs after, which shows the changes caused by the event handler. You will absolutely have to understand https://clojuredocs.org/clojure.data/diff to understand the output.

You'd typically include this interceptor after (to the right of) any path interceptor.

Warning: calling clojure.data/diff on large, complex data structures can be slow. So, you won't want this interceptor present in production code. So condition it out like this :

(re-frame-lib.core/reg-event-db :evt-id [(when ^boolean goog.DEBUG re-frame-lib.core/debug)] ;; <-- conditional (fn [db v] ...))

To make this code fragment work, you'll also have to set goog.DEBUG to false in your production builds - look in project.clj of /examples/todomvc.

An interceptor which logs/instruments an event handler's actions to
`js/console.debug`. See examples/todomvc/src/events.cljs for use.

Output includes:
1. the event vector
2. a `clojure.data/diff` of db, before vs after, which shows
   the changes caused by the event handler.  You will absolutely have
   to understand https://clojuredocs.org/clojure.data/diff to
   understand the output.

You'd typically include this interceptor after (to the right of) any
path interceptor.

Warning:  calling clojure.data/diff on large, complex data structures
can be slow. So, you won't want this interceptor present in production
code. So condition it out like this :

  (re-frame-lib.core/reg-event-db
     :evt-id
     [(when ^boolean goog.DEBUG re-frame-lib.core/debug)]  ;; <-- conditional
     (fn [db v]
       ...))

To make this code fragment work, you'll also have to set goog.DEBUG to
false in your production builds - look in `project.clj` of /examples/todomvc.
sourceraw docstring

enrichclj/s

(enrich f)

Interceptor factory which runs the given function f in the after handler position. f is called with two arguments: db and v, and is expected to return a modified db.

Unlike the after interceptor which is only about side effects, enrich expects f to process and alter the given db coeffect in some useful way, contributing to the derived data, flowing vibe.

Example Use:

Imagine that todomvc needed to do duplicate detection - if any two todos had the same text, then highlight their background, and report them via a warning at the bottom of the panel.

Almost any user action (edit text, add new todo, remove a todo) requires a complete reassessment of duplication errors and warnings. Eg: that edit just made might have introduced a new duplicate, or removed one. Same with any todo removal. So we need to re-calculate warnings after any CRUD events associated with the todos list.

Unless we are careful, we might end up coding subtly different checks for each kind of CRUD operation. The duplicates check made after 'delete todo' event might be subtly different to that done after an editing operation. Nice and efficient, but fiddly. A bug generator approach.

So, instead, we create an f which recalculates ALL warnings from scratch every time there is ANY change. It will inspect all the todos, and reset ALL FLAGS every time (overwriting what was there previously) and fully recalculate the list of duplicates (displayed at the bottom?).

https://twitter.com/nathanmarz/status/879722740776939520

By applying f in an :enrich interceptor, after every CRUD event, we keep the handlers simple and yet we ensure this important step (of getting warnings right) is not missed on any change.

We can test f easily - it is a pure function - independently of any CRUD operation.

This brings huge simplicity at the expense of some re-computation each time. This may be a very satisfactory trade-off in many cases.

Interceptor factory which runs the given function `f` in the `after handler`
position.  `f` is called with two arguments: `db` and `v`, and is expected to
return a modified `db`.

Unlike the `after` interceptor which is only about side effects, `enrich`
expects `f` to process and alter the given `db` coeffect in some useful way,
contributing to the derived data, flowing vibe.

Example Use:
------------

Imagine that todomvc needed to do duplicate detection - if any two todos had
the same text, then highlight their background, and report them via a warning
at the bottom of the panel.

Almost any user action (edit text, add new todo, remove a todo) requires a
complete reassessment of duplication errors and warnings. Eg: that edit
just made might have introduced a new duplicate, or removed one. Same with
any todo removal. So we need to re-calculate warnings after any CRUD events
associated with the todos list.

Unless we are careful, we might end up coding subtly different checks
for each kind of CRUD operation.  The duplicates check made after
'delete todo' event might be subtly different to that done after an
editing operation. Nice and efficient, but fiddly. A bug generator
approach.

So, instead, we create an `f` which recalculates ALL warnings from scratch
every time there is ANY change. It will inspect all the todos, and
reset ALL FLAGS every time (overwriting what was there previously)
and fully recalculate the list of duplicates (displayed at the bottom?).

https://twitter.com/nathanmarz/status/879722740776939520

By applying `f` in an `:enrich` interceptor, after every CRUD event,
we keep the handlers simple and yet we ensure this important step
(of getting warnings right) is not missed on any change.

We can test `f` easily - it is a pure function - independently of
any CRUD operation.

This brings huge simplicity at the expense of some re-computation
each time. This may be a very satisfactory trade-off in many cases.
sourceraw docstring

fx-handler->interceptorclj/s

(fx-handler->interceptor handler-fn)

Returns an interceptor which wraps the kind of event handler given to reg-event-fx.

These handlers take two arguments; coeffects and event, and they return effects.

(fn [coeffects event] {:db ... :dispatch ...})

Wrap handler in an interceptor so it can be added to (the RHS) of a chain:

  1. extracts :coeffects
  2. call handler-fn giving coeffects
  3. stores the result back into the :effects
Returns an interceptor which wraps the kind of event handler given to `reg-event-fx`.

These handlers take two arguments;  `coeffects` and `event`, and they return `effects`.

(fn [coeffects event]
   {:db ...
    :dispatch ...})

 Wrap handler in an interceptor so it can be added to (the RHS) of a chain:
   1. extracts `:coeffects`
   2. call handler-fn giving coeffects
   3. stores the result back into the `:effects`
sourceraw docstring

on-changesclj/s

(on-changes f out-path & in-paths)

Interceptor factory which acts a bit like reaction (but it flows into db, rather than out). It observes N paths within db and if any of them test not identical? to their previous value (as a result of a event handler being run) then it runs f to compute a new value, which is then assoc-ed into the given out-path within db.

Usage:

(defn my-f [a-val b-val] ... some computation on a and b in here)

(on-changes my-f [:c] [:a] [:b])

Put this Interceptor on the right handlers (ones which might change :a or :b). It will:

  • call f each time the value at path [:a] or [:b] changes
  • call f with the values extracted from [:a] [:b]
  • assoc the return value from f into the path [:c]
Interceptor factory which acts a bit like `reaction`  (but it flows into
`db`, rather than out). It observes N paths within `db` and if any of them
test not identical? to their previous value  (as a result of a event handler
being run) then it runs `f` to compute a new value, which is then assoc-ed
into the given `out-path` within `db`.

Usage:

(defn my-f
  [a-val b-val]
  ... some computation on a and b in here)

(on-changes my-f [:c]  [:a] [:b])

Put this Interceptor on the right handlers (ones which might change :a or :b).
It will:
   - call `f` each time the value at path [:a] or [:b] changes
   - call `f` with the values extracted from [:a] [:b]
   - assoc the return value from `f` into the path  [:c]
sourceraw docstring

pathclj/s

(path & args)

returns an interceptor whose :before substitutes the coeffects :db with a sub-path of :db. Within :after it grafts the handler's return value back into db, at the right path.

So, its overall action is to make the event handler behave like the function you might give to clojure's update-in.

Examples: (path :some :path) (path [:some :path]) (path [:some :path] :to :here) (path [:some :path] [:to] :here)

Example Use:

(reg-event-db :event-id (path [:a :b]) ;; used here, in interceptor chain (fn [b v] ;; 1st arg is now not db. Is the value from path [:a :b] within db ... new-b)) ;; returns a new value for that path (not the entire db)

Notes:

  1. path may appear more than once in an interceptor chain. Progressive narrowing.
  2. if :effects contains no :db effect, can't graft a value back in.
returns an interceptor whose `:before` substitutes the coeffects `:db` with
a sub-path of `:db`. Within `:after` it grafts the handler's return value
back into db, at the right path.

So, its overall action is to make the event handler behave like the function
you might give to clojure's `update-in`.

Examples:
  (path :some :path)
  (path [:some :path])
  (path [:some :path] :to :here)
  (path [:some :path] [:to] :here)

Example Use:

  (reg-event-db
    :event-id
    (path [:a :b])  ;; used here, in interceptor chain
    (fn [b v]       ;; 1st arg is now not db. Is the value from path [:a :b] within db
      ... new-b))   ;; returns a new value for that path (not the entire db)

Notes:
  1. `path` may appear more than once in an interceptor chain. Progressive narrowing.
  2. if `:effects` contains no `:db` effect, can't graft a value back in.
sourceraw docstring

trim-vclj/s

An interceptor which removes the first element of the event vector, allowing you to write more aesthetically pleasing event handlers. No leading underscore on the event-v! Your event handlers will look like this:

(defn my-handler
  [db [x y z]]    ;; <-- instead of [_ x y z]
  ....)
An interceptor which removes the first element of the event vector,
allowing you to write more aesthetically pleasing event handlers. No
leading underscore on the event-v!
Your event handlers will look like this:

    (defn my-handler
      [db [x y z]]    ;; <-- instead of [_ x y z]
      ....)
sourceraw docstring

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close