Fork me on GitHub
#reagent
<
2016-12-31
>
polymeris04:12:17

Hi. Am trying to use https://github.com/ariutta/svg-pan-zoom in a reagent application. It works correctly, until I turn on optimizations. I have created an externs file including the library's sole function: function svgPanZoom() {};

polymeris04:12:50

The thing is, that function returns an object with methods to set callbacks

polymeris05:12:42

I tried setting those callbacks in the :component-did-mount lifecycle part, e.g. :

:component-did-mount
(fn []
(.setOnZoom @pan-zoom (fn [level]
                                                      (reset! zoom-level (cond (< level 4) "wide"
                                                                               (< level 10) "mid"
                                                                               :default "narrow"))))
;...

polymeris05:12:25

That is failiing with TypeError: (intermediate value).pk is not a function

polymeris05:12:41

Which makes me think, I have to export that inner anonymous function

polymeris05:12:54

(fn ^:export [level] ... does not work, though

gadfly36105:12:43

@polymeris do you mind showing the rest of the component so i can try and replicate?

polymeris05:12:15

maybe I can't ^:export anonymous functions?

gadfly36105:12:41

My first inclination would be to think there is something missing in the externs file. I haven't needed to export an inner anonymous function before, but first times for everything.

polymeris05:12:47

Uhm... I have very litte experience with externs (or with Javascript in general)

polymeris05:12:56

My externs file is just function svgPanZoom() {};

polymeris05:12:27

do I have to extern .setOnZoom and the other methods, too?

polymeris05:12:34

Will give that a try

polymeris05:12:24

do you have an example of an externs file that specifies the methods of an object returned by another function?

polymeris05:12:06

in the leaflet example, for instance, there is an L.map object, with a setView function... but in my case I don't have a name for the object svgPanZoom(...) returns

polymeris05:12:22

so how can I tell the closure compiler what methods it will have?

gadfly36105:12:17

Admittedly I'd be resorting to "try something and see if it works" which is definitely not helpful lol šŸ˜ž Hopefully someone else can chime in that is better at externs than I am

polymeris05:12:43

Yeah, that's exactly what I am doing šŸ˜

polymeris05:12:56

am at my third thing

gadfly36105:12:00

I tend to resort to looking thru cljsjs (for example https://github.com/cljsjs/packages/blob/master/chance/resources/cljsjs/common/chance.ext.js) and their respective libraries and blindly try to pattern match ...

polymeris05:12:21

I don't even grok the whole prototype thing -- am doing CLJS because I wanted to avoid JS šŸ˜›

gadfly36105:12:41

im with you there lol

mikethompson05:12:18

(I'm not sure if it has already been used?)

polymeris05:12:42

no, haven't used that yet

polymeris05:12:57

was thinking something like that would be useful

gadfly36105:12:22

@mikethompson nice tip, haven't seen that before!

mikethompson05:12:06

:pseudo-names true
:pretty-print true

mikethompson05:12:24

That will help a lot

polymeris05:12:56

Yeah. there are a lot of options. Also will try setting :closure-warnings {:externs-validation :warning}, whatever that means

mikethompson06:12:49

The two above will make it pretty clear, pretty fast, where your problem is

polymeris06:12:24

yeah, had an ohhh moment, as soon as I saw the pseudo-name in the warning

mikethompson06:12:19

The :pretty-print will also help to contexualize the source of the exception (I assume you are getting an exception), which may add further information.

polymeris06:12:01

šŸŽ‰ got it to work! thanks, @gadfly361 and @mikethompson

polymeris06:12:26

var svgPanZoom = function() {
    return {'getSizes': function() {
                            return {'x': 0,
                                    'y': 0,
                                    'viewBox': {'x': 0, 'y': 0, 'width': 0, 'height': 0},
                                    'realZoom': 0}
    }};
};

polymeris06:12:55

those compiler option tricks really helped a lot~

david_clojurian09:12:01

Hello, I have a question. I use goog.events to handle browser events. When I register a handler for the mouse_up event and change the state of an atom inside this handler, the components will not be rerendered immediately. In the repl I see that the value of the atom is already changed. Only when I do an interaction the view will be updated and the atom takes affect. Is it possible to force reagent to do a check of pending changes? In advance: reagent/flush do it not. Only reagent/force-update-all works, but the performance is horrible and not for production use. Any ideas?

metametadata10:12:32

@david_clojurian it's hard to tell. maybe you could provide a minimum example code?

david_clojurian10:12:34

@metametadata OK, I will create some example code. Give me some time please šŸ™‚

metametadata12:12:46

a wild suggestion: try using (doall (for instead of (for

metametadata12:12:11

@david_clojurian or start with a simplest [:p (get @state :during)] component to check that your view tracks state changes on mouse moves. If it works then gradually enhance the code until the bug starts reproducing (this way you'll pinpoint the cause of the bug). If it doesn't work then the problem is not in the component code šŸ™‚

metametadata12:12:59

also look in the browser console - maybe there are some warnings from React

david_clojurian15:12:45

@metametadata Thanks for the advices. I will try it, but today there is no time for it šŸ™‚

david_clojurian15:12:15

@metametadata Thank you so much! The advice to put (doall before (for solves my problem.

Yehonathan Sharvit23:12:35

Take a look how simple it is to embed editable reagent components in a blog post with #klipse https://twitter.com/viebel/status/815332417808531457

gadfly36123:12:22

@viebel I am so excited for this!!

Yehonathan Sharvit23:12:30

@gadfly361 Iā€™m waiting for your (interactive) blog post šŸ™‚

gadfly36123:12:11

@viebel Sounds good to me! I'll try and get something out next month or so :)

richiardiandrea23:12:34

It would be great to retrofit any good change you needed in Klipse to replumb, there is a PR open on custom eval that was abandoned but it was a good idea šŸ‘