-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Array operation callbacks #968
Comments
Love this idea 🎉 Looking at the API like I suppose that could be rewritten as an object operation like |
I wonder if we could generalize and expend on this and have the output be an operation on the output prop, thinking of the Ramda prototype presentation @chriddyp did a while back, before clientside callbacks. The back-end provides a syntax, the renderer reconciles the action on the data instead of data directly. Providing the API in the BE would make it easier to write and debug than using operation strings. For example, an input could become With the callback returning, say: The input/state part could be made to work for free with clientside callbacks, the output would need operations of its own in JS. I think we could cover both partial props and array operations with a single feature. |
Building on the transforms. If components could define special transformations We could get something like
|
In a lot of cases you need to make a relatively small change to a large array. For example, adding a row to a table, or expanding one row into multiple rows as in drill-down interactions; adding a new section to a page, or new options to a dropdown menu. Any array prop might benefit from this -
children
,DataTable.data
,Checklist.options
etc...The way we do this today is to provide the whole array as
State
, modify it, and return the whole modified thing asOutput
. If you could just specify an alteration, it cut down data transfer a huge amount, and possibly improve rendering performance.Seems like the most general operation we'd need to support is what JS calls
Array.splice
:That covers append (if we define a
start_index
for "the end" -null
/None
?), prepend, extend (what's pre-extend, pretend??), insert, delete, replace, and generic splice == replace-with-a-different-length - at least as long as the modifications are contiguous. If we need to support multi-region splice, we could support all three of those args being arrays.I'd propose the renderer only support
splice
itself, which the back end could turn into all the above variants. For an API, how about:Now, what if you need some information about the existing array in order to figure out what to return? You can store that info separately from the array - and in some cases the info you'll need isn't in the array at all, like if you're looking for new events in a database, you might want to store the server timestamp when the query was last run (don't use a global var for this!!!) which could be stashed in a
Store
and used as bothState
andOutput
. But in other cases you might want info that's already in the array and don't want to duplicate it. Seems to me there's always an out here, so this could be omitted from the initial feature, if it's included at all. But for completeness, here are items that occur to me as possibly useful, with proposed API:State("my-table", "data.length")
State("my-table", "data.slice(-1, 1)")
tell which row to operate on).
State("my-table", "data.pluck('id')")
Split out from #475 (Wildcard callbacks) where @chriddyp started discussing this a little - will likely be used together with wildcards but the implementation should be independent.
The text was updated successfully, but these errors were encountered: