Comments (6)
That is a very good question! You're indeed right that the function passed to flyd.stream
needs to have its dependents in scope to be of much use. But let me show you why I think that actually isn't a real issue.
The documentation sadly doesn't explain this properly (sorry), but flyd.stream
is considered only to be the low level building block of Flyd. You should use flyd.stream
to create modules for Flyd – but nothing else. flyd.stream
is a meant to be a powerful way to implement new abstractions in modules.
This is how I would use it to tackle the problem in your specific example. First of all, note the following things about the function you're exporting in callback.js
:
- It actually doesn't need access to
self
andchanged
. - It operates on two streams.
- The function would be more naturally expressed as a function taking two arguments:
function(x, y) { return x + y; }
Now, what if we had an abstraction that allowed us to apply a function taking two arguments to two streams and get a new stream? Such that we could simply define the exported function as above and invoke it with the two streams. Building this abstraction with flyd.stream
is very easy:
module.exports = function(fn, stream1, stream2) {
return flyd.stream([stream1, stream2], function() {
return fn(stream1(), stream2());
});
};
With this function your example would be implemented like this:
// callback.js
module.exports = function(x, y) {
return x + y;
}
// index.js
var lift2 = require('./lift2'); // The function defined above
var callback = require('./callback');
var x = flyd.stream(4);
var y = flyd.stream(6);
var sum = lift2(callback, x, y);
A more general function like the above would work with functions taking any number of arguments. Such a function is implemented in the module flyd-lift.
I hope the above explanation makes sense to you. Do you still think it is a problem that the callback to flyd.stream
needs to have its dependent streams in scope? It hasn't been a problem for me so far but you might see something I've missed. If is actually is a problem then passing the dependent streams to the callback after self
and changed
might be the solution.
If you have any ideas about how I could make the documentation clearer and more easy to understand please let me know :)
from flyd.
Oh, and btw: When I say that flyd.stream
is primarily for implementing modules I only mean the stream(dependencies, fn)
variant. flyd.stream(constant)
should generally be used for creating what I refer to as "top level" streams in the tutorial.
from flyd.
Not really sure if this is a problem to be honest, I posted the issue because this jumped to my attention when reading the readme. Feel free to close it.
from flyd.
Ok. I will keep it in mind but for now I don't think this is an actual problem.
from flyd.
Oh, and btw: When I say that flyd.stream is primarily for implementing modules I only mean the stream(dependencies, fn) variant. flyd.stream(constant) should generally be used for creating what I refer to as "top level" streams in the tutorial.
@paldepind I found this issue after having some of these same questions and seeing other people react skeptically when I showed them the readme.
I agree it's not an actual problem as a low-level utility function, but I might suggest that we simply move its description down to the bottom of the readme under a section such as "Creating Modules" or "Extending Functionality", and instead put examples for higher level abstractions like map
, merge
, lift
, etc at the top.
from flyd.
I think that is made clear in the readme:
This is not a demonstration of how you would write code with Flyd on a day to day basis. For that take a look at the examples.
This tutorial will however introduce you to the minimal but powerful core that Flyd provides and show you how it can be used to build FRP abstractions.
But I like your suggestion about adding examples at the top.
from flyd.
Related Issues (20)
- add more dependency for existing combine HOT 7
- Merge stream delay HOT 6
- add package.module
- Drag 'n Drop example is broken HOT 2
- Compare previous state with current state HOT 14
- Feature Request - Supply metadata for a combined stream HOT 2
- Help to build control flow func HOT 3
- Do you think this Promise -> Stream conversion is a right alternative way? HOT 6
- Wrong chain + setImmediate behavior HOT 1
- Benchmark against Most.js HOT 3
- Can flyd streams be made lazy ? HOT 9
- HELP: Trouble reading api documentation HOT 1
- Enhancement: Add Readonly-Streams (at least in Typings) HOT 3
- What's the motivation for ending a stream? HOT 3
- question on serialize stream
- from Node stream HOT 2
- Question: Required (code) complexity for higher-order streams HOT 10
- Support multiple operators for Pipe HOT 4
- [Help Needed] CombineLatest in the style of RxJs HOT 17
- Switch Latest without Ramda Dependency HOT 6
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 flyd.