Fork me on GitHub
#om
<
2017-08-08
>
Garrett Hopper15:08:15

Is there a solution to calling transact! inside a merge function? The problem is the state immediately gets reset to what it was before the mutation, because the merge was passed that state.

alpox17:08:40

Is there a special reason for that mutations in om/next are always called twice? (It is most likely my mistake, yet i don't see it :D) As example in:

(defui Counter
  static om/IQuery
  (query [this]
         [:board])
  Object
  (componentDidMount [this]
                     (go-loop []
                       (<! (timeout speed))
                       (om/transact! this '[(board/evolve)])
                       (recur)))
  (render [this]
          (let [{:keys [board]} (om/props this)]
            (apply dom/div #js {:style board-style}
                   (map-indexed (partial Cell this) board)))
          ))
i call the board/evolve mutation every timeslot, but each timeslot the mutation function is called twice

sundarj17:08:53

once for remote, once for local

sundarj17:08:53

if you aren't returning an {:action (fn [])} from the mutation then it'll be run twice on local

alpox17:08:46

Hmm must be once remote and once local then since i do return that

alpox17:08:25

My gui seems to show only each 2. state it should have though

alpox17:08:32

As if it would do both in the browser

sundarj17:08:54

is the :action thunk run twice? or just the mutation?

sundarj17:08:06

i think it should only do the latter

alpox17:08:35

let me take a look

alpox17:08:52

Seems to be the action too

sundarj17:08:03

have you set remote: true?

alpox17:08:06

[  7.045s] [om.next] transacted '[(board/evolve)], #uuid "f7fdc5ce-df82-4349-a19c-661eb9c46a7b"
client.cljs?rel=1502190513674:34 i was here
client.cljs?rel=1502190513674:34 i was here
(defmethod mutate 'board/evolve
  [{:keys [state] :as env} _ _]
  (let [st @state
        temp-board (:temp-board st)
        new-board (evolve (:board st) temp-board)]
    {:action
     (let []
       (print "i was here")
       (swap! state assoc :board new-board
            assoc :temp-board (generate-temp-board new-board)))}))

alpox17:08:32

Hmm where would is set that? 😄

alpox17:08:47

(Sorry - i'm new to the environment)

sundarj17:08:55

afaik, that should only be printing once, maybe i'm wrong

alpox17:08:36

Hmm well thats what i would have said too, but the console tells me wrong xD

alpox17:08:44

And my GUI :x

sundarj17:08:58

ah, by default there's a single remote database, so it will be run twice

alpox17:08:23

Hmm but why would i get only each 2. state if one of them runs in the remote

sundarj17:08:05

if you mean the state is what it should be, i think that's because om makes sure the state is only mutated once

alpox17:08:59

Hmm no the state is not as it should be. The problem is that of a series of states 1, 2, 3, 4..... i get only states 1, 3, 5...

sundarj17:08:00

try putting your (let [st @state... inside the thunk

alpox18:08:21

d'oh... i put everything except the st @state already in the thunk (the other 2 bindings) and it didn't work... Now i put that one there too because you say so... and it works!

alpox18:08:45

@sundarj thank you! 😄 I would like to know why this is though - its not mentioned in the docs as far as i know

sundarj18:08:56

i am also curious

alpox18:08:56

Aand was happy too early... seems its still broken there 😕 i'm starting to think there is some weird logic mistake on my side

sundarj18:08:00

oh wait, your swap! is wrong

sundarj18:08:07

swap! can only take a single function

alpox18:08:37

yea i changed that already 😄

alpox18:08:44

I noticed it right after posting - sry

alpox18:08:12

I did a bit of refactoring and it looks now like this:

(defmethod mutate 'board/evolve
  [{:keys [state] :as env} _ _]
    {:action
     (let [st @state
           new-board (evolve (:board st))]
       (swap! state assoc :board new-board))})

sundarj18:08:29

looks ok to me

alpox18:08:24

I'd like it more if it wouldn't 😄

sundarj18:08:02

haha sorry 😅

alpox18:08:40

No problem 😄 thanks for your time though!

sundarj18:08:22

no worries

alpox18:08:57

@sundarj fixed it 😄

alpox18:08:18

It was my dumbess of ignoring some facts from the tutorial

alpox18:08:43

In the mutate function i just had to put a # before (let --> the action has to be a function 😄

alpox18:08:51

I don't know how i missed that

sundarj18:08:08

you and me both

sundarj18:08:29

glad you figured it out 😄

sundarj18:08:12

we kept saying 'thunk' too hahahaha

alpox18:08:54

Uhm i just took that from you, my english is not so good so i went with it 😄

sundarj18:08:18

a thunk is a term for a function with no arguments 😛

alpox18:08:28

Aha 😄 learnt something more ^^

samcf18:08:50

that's not true, is it?

samcf18:08:30

as i understand it, a thunk returns a closure that serves to be called at some other time

rads20:08:52

a thunk is a computation waiting to be performed. the closure is the thunk

Garrett Hopper19:08:18

So, it appears that when a component of a union query (branch for routing) gets updated on its own, without its parent, the :query of the read isn't the normal union, but the result of the union. Is that normal?

Garrett Hopper20:08:15

It shows up in the :ast as a join, but its :query is a vector.

rads20:08:52

a thunk is a computation waiting to be performed. the closure is the thunk

rads20:08:42

I wouldn't consider a function like println a thunk, though, even though you can call it as (println)

sundarj21:08:53

right, yeah. it's a little more nuanced than 'function with no args', but thats close enough imo

alpox22:08:19

Fair enough 😄