Hi David,
thanks for the answer. I’d like to contribute on documentation about cursors,
but first can we check if I get this right?
Here’s what I understood from answers here & from source code:
You keep all your application state in a single atom. Components, however, do
not depend on the whole atom, they get modifable parts of it via cursors.
The idea of cursors is to provide part of the modifiable state for reusable
components. For example, text input component needs one string as parameter
(initial value) and some place where to put new value once user changed it on a
page. Cursor provides both.
Cursors are also used to specify component dependency on a state. Each
component get one cursor at construction time and will be automatically
re-rendered when value underneath his cursor changes.
Root component get cursor created from atom itself. Atom and cursors derived
from it stay in sync on modification.
There’re only two types of cursors: maps and vectors.
There’re two ways to get value from a cursor:
During render phase, cursors support all basic map/vector interfaces, so you
can get/get-in in them, check for keys, etc.
If you do get/get-in on a cursor and result is again map/vector, it’ll be a
cursor too. Otherwise, it’ll be an ordinary value.
Outside render phase, you can deref cursor and get ordinary map/vector (not
cursor!).
To get cursor that points at primitive value, wrap it into vector of 1 element:
(def state (atom {:timer [0]}))
transact! is avaliable at both inside and outside of render.
What’s not clear for me:
David, you often mentioned that outside render phase, you cannot do anything
with cursors besides deref/update/transact. But for me these operations covers
everything we need. Can you name what operations are prohibited outside render
phase?
Also you mentioned that there’s two type of values underneath cursor: stale
value and current value. Can you describe via which calls they can be accessed?
Actually, whole state consistency semantic is not clear. What are points when
state can be changed? Which changes and when become visible and for whom? For
example, if I transact inside render, will I see changes in render afterwards?
It would be nice to have it described in several basic principles.
What to do if component depends on two or more cursors? For example, I render
game field and have state like this: {:selected [17 18], :field [[...], ...]}.
But each cell view depends only on one field cell and on selected coordinates,
e.g. on two cursors: [:selected] and [:field 5 2].
--
Note that posts from new members are moderated - please be patient with your
first post.
---
You received this message because you are subscribed to the Google Groups
"ClojureScript" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/clojurescript.