Fork me on GitHub
#re-frame
<
2017-02-04
>
lsenta11:02:58

Hi reframers, is there any way to add a global interceptor without having to rewrite all my event handlers? I'd like to write something like this:

(reg-global-interceptor
  (fn [new-db old-db event]
     (if (and (not= old-db new-db) (not= event :x))
         (with-messages new-db)
         new-db)))

akiroz12:02:56

@lsenta You probably want to override re-frame's reg-event-db and change the :require statement in all your event files.

mac13:02:44

@lsenta It does require you to add a "prefix" to your dispatchers.

heeton13:02:32

Arch advice please 🙂 I’m prototyping a game that uses realtime checks (i.e. music thing, people need to tap to learn to keep time). Totally ignoring JS timing issues for now, I’m wondering how best to interface this with re-frame. It feels like I should have a game loop running every frame that has nothing to do with re-frame, then dispatch events from that that affect the UI + game state. I initially thought about updating the store every frame with the current time but that seems to go against the idea of an event driven approach.

akiroz13:02:41

@heeton I'm actually trying to design a game based on an event driven arch too, so far I'm having my game loop syncronized to the app-db (reading directly for rendering)

akiroz13:02:28

not sure if there are better ways to do this though.

heeton13:02:40

@akiroz You aren’t using reagent to render, but doing it yourself directly?

akiroz13:02:02

@heeton No I'm using PIXI.js to render on a WebGL canvas with js' requestAnimationFrame calling my game loop

akiroz13:02:14

I'm thinking of deref-ing subs in my game loop now.

heeton13:02:08

What is re-frame giving you then? The user interaction? UI state?

akiroz13:02:07

mainly game state, all game events (e.g. user input, object collision) goes through re-frame

akiroz13:02:32

my "game" is still early in the archetectual design phase tho, I'm just testing out pieces of code here and here.

akiroz13:02:19

(I have zero exp in writing games)

thegeez13:02:24

@lsenta I use this in some debug code that I only load in development to add a debug-interceptor to all the event handlers:

thegeez13:02:28

(swap! re-frame.registrar/kind->id->handler
       (fn [kih]
         (update kih :event
                 (fn [ih]
                   (zipmap (keys ih)
                           (map (fn [ics]
                                  (let [[cofx do-fx & xs] ics]
                                    (assert (= (:id do-fx) :do-fx)
                                            "debug makes assumption of re-frame standard interceptor order")
                                    ;; do-fx halts the interceptor chain, so insert after that one
                                    (seq
                                     (into [cofx
                                            do-fx
                                            debug-interceptor]
                                           xs))))
                                (vals ih)))))))

thegeez14:02:47

^ that places the debug-interceptor in the earliest possible position

lsenta14:02:15

Ho wow, thanks @mac, I haven't thought of doing it this way, and thanks @thegeez: I'd avoid digging into the internals, but if you already made it works, that'll be perfect for my hackish prototype 🙂

qqq16:02:40

does it make sense to use datascript instead of app-db in re-frame ?

akiroz18:02:28

@qqq sure, I remember seeing a wiki page on this.... but can't seem to find it. you just need to provide a custom fx, cofx and sub to datascript.

qqq18:02:33

it's surprising it's not used more; datascript seems like a great idea, and I'm surprised none of the FRP (reagent, re-frame, OM), seems to heavily use it

lockdownz18:02:04

maybe if reframe switches to rum

qqq18:02:58

lol, is rum a "we can rewrite all of react in cljs" instead of "let's just wrap cljs" ?

akiroz18:02:21

Well... most of the time we don't need the extra complexity. I guess it's only gonna be useful if you're writing an app that works offline?

akiroz18:02:06

I still haven't taken a good look at Rum, I heard it's more like a library than a framework?