Fork me on GitHub
#re-frame
<
2018-04-16
>
mccraigmccraig13:04:05

i've come across a couple of errors in our app recently of the "handler switches view, but some of the require data is missing and bork" variety. ad-hoc solutions are straightforward, but i'm wondering about a generic solution which would hold a ref to the app-db value before update, and if any sub or view run consequently to the app-db update throws an exception then rollback the change and dispatch a descriptive error event

mccraigmccraig13:04:32

has anyone implement such a thing already - @mikethompson ?

gadfly36116:04:37

Hey all, Just made Re-Pressed, a keyboard event library for re-frame. https://github.com/gadfly361/re-pressed

👍 36
reefersleep20:04:49

Very relevant for me! I just implemented keyboard control in a small app of mine and was surprised with the unaligned keyCode/keyChar values of events in various browsers.

reefersleep19:04:56

@gadfly361 do you have any general advice on keys to avoid overriding or stuff like that?

reefersleep19:04:24

e.g., I’ve got an app where I’m assigning the space button to cycle through items, and I don’t ever expect the app to need the normal space behaviour, scrolling down. But I don’t know if it’s generally a bad idea to do, maybe for reasons yet unknown to me 🙂

reefersleep19:04:31

Maybe you know some sources on the topic

gadfly36121:04:37

@U0AQ3HP9U this is a list of shared shortcuts in browsers that I'd probably recommend not overriding unless you need to: https://www.google.com/amp/s/www.howtogeek.com/114518/47-keyboard-shortcuts-that-work-in-all-web-browsers/amp/

gadfly36121:04:04

@U0AQ3HP9U and this one is decent for general perspective. My take aways are don't forget multiple character shortcuts, and also discoverability of the shortcuts themselves

reefersleep10:04:01

I plan to utilize re-learn to inform people about the shortcuts. The app has a very narrow scope - the shortcuts are your main interaction in the app, so it’s not like they’ll be too hidden

reefersleep10:04:07

Cheers, thanks a lot!

sveri19:04:20

@gadfly361 That looks really nice. I have one question. If I read the code correctly it goes like this: "key-pressed" -> "lookup event table for key pressed" -> "excute event". But what about different contexts. Especially the Enter key may execute different events depending on the current form, etc. Am I missing something?

gadfly36119:04:00

@sveri the event table is dynamic, so you'd just change it depending on context with set-keydown-event (or with the key press keyup variants). I usually set it explicitly for each page (but there is nothing stopping you from doing it more or less often)

sveri19:04:09

Ah, so you basically create an event table for every "view", correct?

gadfly36119:04:33

Yeah, that's what I'd recommend

sveri19:04:53

Ok, that sounds reasonable, I was just unsure, because in your documentation, you have this line: (re-frame/dispatch-sync [::rp/add-keyboard-event-listener "keydown"])in the init function. Which, if you follow the general re-frame templates, is only called on pageload, but not when fighweel reloads the page, which would be the mount-rootfunction.

sveri19:04:11

Thanks for the library, I will definitly give it a try.

gadfly36119:04:49

Thanks for the feedback, I should make the docs more clear. The gist is 1. Set an event listener for keydown, keypress, or keyup (once when app starts) 2. Set / update event rules table (whenever and as many times as you want)

escherize20:04:39

Looks awesome imo

kenny22:04:08

Are there any potential problems from doing something like this within a sub handler function?

(ratom/make-reaction
  (fn []
    @(ratom/make-reaction
       (fn []
         ;; do stuff
         ))))

kenny22:04:22

The main thing I am concerned about is a possible memory leak from the inner make-reaction. Perhaps Reagent handles this without a problem?