Fork me on GitHub
#re-frame
<
2018-12-21
>
joshkh12:12:11

something i've always wondered about but never found an elegant solution: i have a simple event to update a text filter, and then a few dynamic subscriptions to filter a collection based on the filter value (reg-ex string matching). as i update the filter value, the text filter component drops input characters while the subscriptions do their job. i don't think this is a use case for ^{:flush-dom} (or is it?). should i bite the bullet and move to web workers?

joshkh12:12:36

i realise this is a single thread problem and irrespective of the framework, but i was wondering if there's a nifty re-frame way to handle it. 🙂

mikethompson12:12:42

Try to do a dispatch-sync instead of a dispatch in that one case.

mikethompson12:12:57

Where performance is needed

mikethompson12:12:28

So I'm guessing that it isn't the subscriptions

mikethompson12:12:08

(Although obviously I don't know enough to really be sure)

joshkh12:12:18

thanks mike. specifically, a dispatch-sync instead of a dispatch when firing the event to update the text filter value based on the input textbox?

mikethompson12:12:39

Yeah if you are dispatching on each keypress

mikethompson12:12:55

and you are droping chars

mikethompson13:12:02

try dispatch-sync instead

joshkh13:12:07

thanks, i'll give it a shot

joshkh13:12:19

hmm. still dropping characters.

[:input.form-control.form-control-lg.mb-4
        {:type      "text"
         :value     @(subscribe [::events/text-filter database])
         :on-change (fn [e] (dispatch-sync [::events/set-text-filter database (oget e :target :value)]))}]

joshkh13:12:00

not binding :value to a subscription solves the problem, but it's still a little choppy

valtteri13:12:00

I’ve solved text-field problems by binding :value to a ratom and using goog.functions/debounce to ‘flush’ the ratom state to app-db using dispatch after 200ms or so.

valtteri13:12:57

This is not super elegant but it works. No dropping characters or any other symptoms (that I’m aware of).

joshkh13:12:43

good thinking. i tried that, but found i was still dropping characters between when the debounce eventually fired and when a new char was inputted directly thereafter. less characters lost but still one every once in a while. if it worked for you then i must be doing something wrong. will review my code 🙂

valtteri13:12:06

It’s doing tons of other stuff as well so it’s a bit messy example.

joshkh13:12:25

happy to see an open source example! thanks @valtteri, i'll check it out.

valtteri13:12:40

You’re welcome!

pre20:12:59

I’m using events.cljc to define events and using fn-traced from day8.re-frame.tracing (0.5.1). However the code won’t compile and throws a java.lang.ClassNotFoundException: debux.dbgn. Code compiles when I rename my file to events.cljs, indicating that the original tracing.cljc might not be compatible on both platforms.` For instance, debux.dbgn is written in clj.

(ns app.events
  (:require
   [re-frame.core :as reframe :refer [reg-event-db reg-event-fx inject-cofx path after]]
   [cljs.spec.alpha :as s]
   [app.db :as db]
#?(:clj
   [day8.re-frame.tracing :refer [fn-traced defn-traced]]
   :cljs
   [day8.re-frame.tracing :refer-macros [fn-traced defn-traced]])))

(reframe/reg-event-db
 ::initialize-db
 (fn-traced [_ _]
            db/default-db))

Exception occurs at fn-traced. What am I missing?

frenata16:12:22

Late to the party @U055C1RAD, but I've always just done (:require [day8.re-frame.tracing :refer [fn-traced defn-traced]]) without any reader conditionals without any problems -- although I also typically write event handlers in a cljs file.

pre16:12:02

as i said, this works in cljs not clj (hence the post)

frenata16:12:24

grabbing a small project and renaming my events.cljs to events.cljc doesn't seem to provoke any problems

frenata16:12:31

But I suppose it wouldn't unless events was actually being required in by some other clj namespace.

pre17:12:12

yes, once another ns requires it and you run the project …it throws the above error. I’ll create an issue for this after no responses here.