Comments (6)
I use .onChange
quite a bit, and would be sad to see this functionality go. It's very useful.
What if we had State:OnChange(callback)
instead so that the State
object is given the function and can handle it better, calling and clearing as needed.
from fusion.
One API design I'm currently considering is introducing a new reactive object, Compat
, which lets you watch for these kinds of lifecycle events that shouldn't normally be used in Fusion code:
local numCoins = State(50)
local doubleCoins = Computed(function() return numCoins:get() * 2 end)
local compat = Compat(doubleCoins)
local disconnect = compat:onChange(function()
print("doubleCoins has changed")
end)
(this isn't a final API design by the way, it's just a vague idea that took me 5 minutes to come up with)
This is, technically speaking, not really different from onChange
- it has the same uses and problems. However, there's benefits to adding this intermediate step:
- reactive objects (State/Computed/ComputedPairs/Tween/Spring) no longer have to maintain their own implementation of onChange - there's now just one implementation inside Compat
- the name 'Compat' shows that this API is intended for making non-Fusion UIs compatible with Fusion, rather than presenting itself as an API intended for common use
- it adds friction to the process of using these events, which can push developers towards the simpler and cleaner reactive solutions; not all APIs should be frictionless to use!
Of course, this is very much a work in progress, and I'd love to hear more feedback around this.
from fusion.
This seems like a much nicer alternative of “embedded”onChange
, especially since it can be used with non-Fusion UIs.
This also means onChange
won’t exist when it’s not needed, which I guess is great?
I’m 100% for this change.
from fusion.
I'm wondering if it's worth shipping the first experimental version of Fusion without support for this use case?
from fusion.
After further investigation this is definitely important enough to support, because it's a vital part of getting non-Fusion UI and Fusion UI to work together.
To be more specific about the kinds of anti-patterns I've noticed onChange causes, take this code example:
local numCoins = State(50)
local doubleCoins = State(numCoins:get() * 2)
numCoins.onChange:Connect(function()
doubleCoins:set(numCoins:get() * 2)
end)
In the above code, the developer was attempting to express a relationship between doubleCoins
and numCoins
- specifically, they were trying to say "doubleCoins
should always be numCoins * 2
".
While the above code works, it's smelly:
- wet - we're writing the same calculation twice, and they need to be identical. However, we could accidentally forget to change one, or make a typo, or introduce some other discrepancy.
- implicit - the relationship between the two objects is not explicit. Because we're handling updating
doubleCoins
ourselves, Fusion can't see that it's directly dependent onnumCoins
, and so can't optimise. - writable - even though we should only set
doubleCoins
tonumCoins * 2
, and we expect it to equal that all the time, it's still possible for another part of the code to set it to another unrelated value and break everything.
Fusion already has a far better object for expressing these kinds of relationships cleanly, explicitly and safely: computed objects. The above example can be written much more cleanly using a computed object:
local numCoins = State(50)
local doubleCoins = Computed(function()
return numCoins:get() * 2
end)
This is much less smelly:
- dry - we only write the calculation once, so we can't accidentally mess it up
- explicit - Fusion can now detect and optimise the dependency on
numCoins
, making this faster to update - read-only - computed objects can't be written to, so the above relationship is guaranteed to always hold
This is an example of where .onChange has allowed for developers to write less sound code by accident. I consider this to be a failure of API design; it's too easy and enticing to use onChange rather than using a proper reactive solution.
I think, if I replace this with another API, it needs to explicitly and visibly be for compatibility with non-Fusion code. It shouldn't appear next to reactive objects as .onChange does now, because that makes it seem like it's alright to mix in with reactive code. In reality, that's rarely the case, and a reactive solution exists and works better.
from fusion.
Made the change 🎉
from fusion.
Related Issues (20)
- Look into fuzzing as part of our testing suite
- Add explanations and breakdowns of example place files
- Spring/Tween flickering with hydrated reference HOT 2
- Seems that Fusion 0.3 rbxm file is marked as 0.2 HOT 2
- Add unit tests for NaN self-similarity
- Computed does not capture dependency when Value created inside the callback HOT 2
- Fusion.Tween does not catch state update in certain cases
- Frozen tables should use reference equality
- Fusion.Children should parent things in batching order HOT 7
- Allow T everywhere we publicly accept StateObject<T> HOT 1
- Dissolve state objects that don't deal with state HOT 4
- Rename CanBeState<T> to be more concise HOT 7
- Dealing with re-entrant updates HOT 2
- Introduce a more efficient connector between state and data model properties
- Heuristic memoisation for state objects HOT 1
- Add debugging protocol for non-invasive inspection
- Change design of Observer to allow pooling listeners HOT 1
- Reuse scopes from primitive computed objects HOT 3
- Test what an ideal scope pool size is HOT 1
- Integrate under-documented APIs into tutorials
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fusion.