Re: [WICG/webcomponents] Observable Properties (Issue #951)

@jeff-hykin Yeah, `Object.observe()` would have been the solution, and not just for DOM, but for all of JS.

The only alternative is to patch the objects by replacing their properties with accessors. Here's an example:

https://github.com/trusktr/james-bond/blob/43659f83ddbdc0cac0c98f05035c800e5efc205c/src/observe.ts#L12

And here's what the usage looks like in the unit tests:

https://github.com/trusktr/james-bond/blob/master/src/observe.test.ts

The only real downsides are:

- make sure the patching is done properly, it should not ruin any features of the object. For example, if the object already inherits and uses accessors, then you need to make sure to call those inherited accessors.
- you changed the descriptors of the object, which in rare cases could change how the author's program works. This is rare.
- unlike Proxy membranes, this does not handle arbitrary properties added later, you must specify which properties to observe ahead of time

I use this in practice to make my [element behaviors](https://github.com/lume/element-behaviors/) [observe their host element's properties](https://github.com/lume/lume/blob/develop/src/behaviors/PropReceiver.ts).

-- 
Reply to this email directly or view it on GitHub:
https://github.com/WICG/webcomponents/issues/951#issuecomment-1199970523
You are receiving this because you are subscribed to this thread.

Message ID: <WICG/webcomponents/issues/951/1199970523@github.com>

Received on Friday, 29 July 2022 21:56:50 UTC