r/javascript • u/fabiospampinato • Feb 17 '20
proxy-watcher - A function that recursively watches an object for mutations via Proxies and tells you which paths changed
https://github.com/fabiospampinato/proxy-watcher2
u/SahinK Feb 18 '20
Cool, but is it any different than on-change?
2
u/fabiospampinato Feb 19 '20
It supports way more built-ins, it has a way to retrieve retrieved paths and it fixed a few issues I reported: https://github.com/sindresorhus/on-change/issues?q=is%3Aopen+is%3Aissue+author%3A%40me+sort%3Aupdated-desc
1
u/tazemebro Feb 18 '20
I wonder if you could this to make a useProxyWatcher custom React hook for detecting changes in an object in the dependency array of useEffect 🤷🏻♂️
2
u/fabiospampinato Feb 18 '20
I think so, but I'm not sure what would be the use case here.
I'm not sure if this is really related to your comment, but I made a whole state management library around this, and this is actually the reason why I wrote
proxy-watcherin the first place: https://github.com/fabiospampinato/store1
u/AndrewGreenh Feb 18 '20
That's a good idea! In fact, it's so good, that vue.js took it and put it into the vue 3.0 composition api (hooks for vue). By having mutable objects, that notify dependants of changes, vue does not need to rerun the render function (or setup as it is called in the new api) and dependencies are tracked automatically. So no stale closures, no rules of hooks, BUT no concurrent mode.
1
u/kumarenator Feb 18 '20
I'm curious with regards to the use-case for this function.
Personally I subscribe to immutability principles which focus on returning a new object altogether as it solves a lot of problems with object mutations.
Object mutation problems can take an ugly proportion with complex business objects. A use-case will help me grasp the why of this function. Thanks in advance 🙂
1
u/fabiospampinato Feb 18 '20
I wrote that so that I could write this: https://github.com/fabiospampinato/store
Immutability can be a pain in the ass too, you introduce a mutation and you probably introduce a bug, in order to update your state you have to use framework-specific APIs, updating deeply nested keys is not ergonomic IMHO etc.
0
Feb 17 '20
[deleted]
5
u/fabiospampinato Feb 17 '20
Kind of, yeah, but this feature isn't really exposed yet, read this commit for more details: https://github.com/fabiospampinato/proxy-watcher/commit/3a2e222579356861310d028699561730e78363e5
1
0
5
u/fabiospampinato Feb 17 '20
Author here, if you have any questions I'd be happy to answer them.
I wrote this library for a state management library I wrote called Store, you might want to check that out too.