Comments (3)
Hi Grieb --
Surplus has a different methodology for computation lifecycle than mithril. So the short answer is no. For the particular case you show, you could get a similar effect just by setting the onchanges parameter to true:
const pending = S.data();
S.root(() => {
S.on(pending, () => {
console.log("should not have ran...");
}, null, true);
});
That won't run until pending()
is set. But that's not a 'true' pending computation for two reasons: 1) mithril would run the computation if the source stream had been previously set, whereas this will always wait until the next set, and 2) it doesn't generalize to multiple streams:
const pending1 = S.data(),
pending2 = S.data();
S.root(() => {
S.on([pending1, pending2], () => {
console.log("should not have ran...");
}, null, true);
});
The computation will run as soon as the first source is set, whereas mithril will wait until all have been set.
The main reason Surplus takes this path is that pending streams are incompatible with automatic dependency detection. We can't defer execution until all sources are active if we don't know what the sources are until we've run. In general, automatic dependencies, the kind you get with S(() => ...), are the core of S, because they enable synchronous declarative programming. S.on() is provided for cases where you are doing event-driven programming. There's nothing wrong with using it -- if you really are doing event driven programming, then it's the right tool -- but if that's not true, S() will often lead to easier-to-reason-about code. S.on() says "this runs when X happens." S() says "this is the way things are."
All that said, you can actually re-create the mithril behavior using two data signals per stream and a subclock for dependent computations. Mithril streams really carry two pieces of data: their state and their value, so you need two data signals, because in Surplus, a data signal only carries one piece of data. Then for dependent computations, you need a utility that a) declares dependencies explicitly, a la S.on(), and b) uses a subclock to defer computation until all the sources are active.
I was influenced a lot by mithril a couple years ago, when I was starting S and Surplus, but haven't followed it as much over the last year. Curious to hear how your translation goes. Feel free to share code if its useful.
-- Adam
from s.
Thanks for the explanation @adamhaile! A thoroughly answer really adds to your work, congratulations!!
I will keep studying it, but for my specific case now your solution is more than enough. It forces me to keep using S.on and not S() tough, right?
Grieb
from s.
Yeah, using S.on() when you want to run something on an event is fine, the intended way in fact.
from s.
Related Issues (20)
- Karma / Jest testing HOT 9
- Just took it for a first drive....
- Push to array data signal HOT 1
- Creating computations when resolving Promises HOT 2
- Maps HOT 12
- Are subclocks generally available? HOT 4
- Computations creating signals HOT 10
- Make methods treeshake-able HOT 3
- Root + Async = Headaches HOT 5
- Cleanup single computation HOT 1
- S.value multiple updates with no listeners fail HOT 1
- Conditionally triggering downstream computations HOT 3
- Can this nested computation behavior be explained? HOT 2
- Recommended way to deal with "Runaway clock detected"? HOT 1
- Handling Nested Computations under Branching Logic HOT 13
- Why prevent signals from setting different pending values in a single clock tick? HOT 1
- how to implement the rxjs .scan operator with S.js? HOT 7
- Local state inside effect / hooks HOT 2
- Compute value with prev != next check
- cleanup and update phases are intermingled HOT 2
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 s.