Repository navigation
Observable - syntax #6
Replies: 13 comments
|
Hi @redblobgames! Yes, the value getter / setter syntax was one of my considerations for my API, similar to VanJS, Vue, and Preact. I ultimately decided on value for the getter and update() for the setter as the syntax got very tricky when it comes to nested reactivity. For example, stuff like I ultimately decided on just I think the flexibility of allowing people different ways to mutate / change state and have the program "just work" is better than having to learn new conventions. I'm generally optimizing for junior developer experience as I work with those folks a lot. |
BTW, Vue's const user = reactive(…)
if (user.name == 'John') {
user.name = 'Jane';
user.age = 31;
user.address.street = '456 Elm St';
user.address.city = 'Othertown';
user.address.country = 'Canada';
user.address.postalCode = '67890';
user.address.coordinates.lat = '51.5074';
user.address.coordinates.long = '0.1278';
} else {
user.name = 'John';
user.age = 30;
user.address.street = '123 Main St';
user.address.city = 'Anytown';
user.address.country = 'USA';
user.address.postalCode = '12345';
user.address.coordinates.lat = '40.7128';
user.address.coordinates.long = '74.0060';
}[Side note: I would prefer that Vue's ref/reactive were merged though, as I'd prefer just one type of observable than two, but … they had to split it because they didn't want to use
I think that's a great design tradeoff. Vue and React seem to be moving away from that, and I've been looking for something simpler. |
|
Thanks for that! I'll take a closer look into Vue's implementation and see if there's something we can use. |
|
It'd be cool if cami this.location.update(value => 'airplane'); // top level
this.user.update(value => { // nested
value.status = 'traveling';
});
// two different update() callsvs vue const location = ref('') // top level uses ref(), can't use reactive()
location.value = 'airplane' // need to use .value with ref()
const user = reactive({status: ''}) // nested uses ref() _or_ reactive()
user.status = 'traveling' // don't use .value with reactive()vs what I would consider the most natural: // 'this' is a reactive() that has everything, so no need for .value or .update
this.location = 'airplane'; // top level
this.user.status = 'traveling'; // nestedHowever I haven't dug into ES6 Proxy to figure out whether this is possible… |
|
@redblobgames it's possible with ES6 Proxies. Here's a spike i had: Below defines Here's the usage code. The effect is just a simple console.log. The constraint with this design is that it only works with objects, if you pass primitives such as strings, numbers, etc, it does not work as javascript primitives are immutable (reactivity can only work with mutable structures like objects). I think Vue's new In Vue, this is how normal count & nested object reactivity looks like: Vue count: Vue deep reactivity: And this is how it looks like for Cami (FYI - I plan to release the underlying Cami counter: Cami deep reactivity: The Cami syntax is slightly less natural, but it's is immutable under the hood with immer. It's helpful with undo / redo features and time-travelling through states (haven't exposed the time-travelling features yet, but in the works. Mostly a devtools thing). FWIW, I think Svelte (before runes) has the most natural API: Svelte counter: Svelte deep reactivity: However, Svelte's ergonomics come at a cost -- they did this at compile-time. Considering the project's "no build" goal, that highly ergonomic API isn't possible as we do things at runtime. That said, it's interesting to note that Svelte is now moving away from this and into signal-style API called runes. I believe one motivation for changing this is due to maintainability concerns, as the "it looks like a variable" type of API introduces some readability problems the larger the project is. In case you're curious, Vue has an interesting section on API trade-offs here. |
|
I thought about this a bit more and I'm warming up to the idea of having a more granular state change API to make common tasks more convenient. For example: Before: After: But beyond that, we can also use object methods / array methods for assigning as well: Before: After: I actually just pushed a small subset so far to the latest CDN (it doesn't affect existing API). Turns out it wasn't such a big deal to implement as these all used |
I think one possible advantage cami has over vue is that because you provide a ReactiveElement class, you could wrap the entire class object inside observable(). Then I admit I may be overly optimistic about this. And there's the downside that there's no way to put non-reactive data into the ReactiveElement. |
|
That's super interesting -- just had to try it out :) I spiked a very magical API that replaced However, there's a middle ground that's not so bad I think, which is inspired by MobX's It's arguably more readable in the long run when you have lots of computed / observable definitions Before: After: While similar to MobX, it's still better DX I think as we don't have to define actions, and we don't have to add observer / autorun pass a function component to it like MobX and React. Since we're working with a |
|
Oh that's really interesting! I hadn't thought about custom elements not going through the proxy. I knew about MobX but had never looked at it before. Both makeObservable and makeAutoObservable look interesting |
|
Dug a little deeper today and it looks like we can do away with We can do a MobX-like convention in the following way (i.e. Cami should have other goodies as well like getting values from HTML attributes (just need to define a parse function). Overall, the future API should feel very natural (see playlist example below). Just "mutate" and it will automatically render. There are a few "gotchas" as it's immutable under the hood, but it only affects nested key updates in large object (for which, the The eventual trick was to use getter / setter traps (like Vue's I haven't released the above yet (will sleep on it). If you don't mind, I'll add you as a co-author in v0.1.0. Your questions / comments have been helpful :) |
|
Ahh, cool! So the Proxy can't wrap a custom element, but you can insert getter/setter traps for the top level, and use Proxy for the levels underneath? That seems like it should work. I thought a few nights of sleep would give me more ideas but I don't have a lot to offer right now… |
Yup, you got that right! |
|
For those who may be following this thread, we've made another simplification where you don't need to define the constructor anymore, leading to this simpler API: More details in this announcement thread. |
Uh oh!
There was an error while loading. Please reload this page.
Do you have any thoughts on
vs
(as used in Vue and Preact) I think the advantage of the function is that you might want to run it later. So after that statement, this.count might still have the old value. But the advantage of the assignment is that it looks like ordinary variable assignment, and you expect that after that statement, this.count will have the new value. That seems simpler to reason about.
All reactions