-
Notifications
You must be signed in to change notification settings - Fork 378
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
Observable Properties #951
Comments
Re option 2, Proxy exotic objects on their own cannot exhibit the characteristics you’re looking for (i.e., by design — this is very fundamental for a variety of reasons, including proxy’s own core use cases). Membranes can, though that’s unlikely to be worthwhile here. The “monkey patching” in “option 3” is effectively equiv to part of what a membrane entails. (Side note: @@hasInstance is implemented by the RHS operand, not the left. If providing a custom impl in a class body, it’d be a static method.) If you want to observe specific/knowable properties in a custom element it is pretty straightforward to use some “meta” helper to declare/define/install them and their change effects (as various libraries like lit do) to avoid boilerplate. If you really want totally dynamic access, it is possible to achieve that with a prototype that’s itself a proxy, sorta, but it’s pretty chaotic and may defy expectations in various ways, e.g. |
Thanks!
I had not thought of that, I'll be keeping that in mind for other projects too. I'm always down to experiment with something chaotic. I'll see if I can get it to be reliable. If I can intercept the inherited Node properties on HTMLElement I'll consider that a pretty good start. Otherwise I'll probably finish working on #3 |
I shoulda also mentioned regarding @@hasInstance that because the default behavior (reflected at |
@bathos thank you, I was actually experimenting with that based on your first comment and realized it did nothing. I've been carrying that approach around for a while not realizing it was a false positive from whenever I last was trying to override instance of. |
Neither JavaScript nor the DOM have a way to observe properties generally from outside the objects. This is what Object.observe() added for a subset of objects. What's the use case? |
I'm not sure exactly what is meant by outside the object. My only goal is getting proxy-like control over a custom component. We can assume I have full control over the definition of the custom component. Object.observe() would solve my issues, except that it's deprecated. As for usecase, it's the same as the usecase for proxy objects in general (I use them for a bunch of behaviors) |
The platform does include such APIs. In Web IDL parlance they’re called indexed properties (like Array index keys) and named properties (any other string key) and are defined in terms of the These are generally considered a legacy design pattern which isn’t used for new interfaces. It tends to lead to surprise collisions with “normal” members and even though the algorithms are well-defined, no two agents actually implement them the same (try messing with
Custom elements cannot recreate those APIs (without “deep” virtualization anyway) because the instance returned from the constructor cannot be a new object. I don’t think there would be vendor interest in enabling this given the sentiment that they probably shouldn’t have existed to begin with and because of their high complexity. |
@jeff-hykin Yeah, The only alternative is to patch the objects by replacing their properties with accessors. Here's an example: And here's what the usage looks like in the unit tests: https://github.com/trusktr/james-bond/blob/master/src/observe.test.ts const el = document.querySelector('.some-element')
observe(el, ['someProperty', 'otherProperty'], (prop, value) => {
console.log('prop changed:', prop, value)
}) The only real downsides are:
I use this in practice to make my element behaviors observe their host element's properties. |
TDLR: I don't think there is a way to watch all properties
Workaround #1
Downsides
Workaround #2
Downsides
Example:
Workaround #3
Downsides
$0
that many browsers have when inspecting an element. It would return the non-proxy-wrapped elementThe text was updated successfully, but these errors were encountered: