Comments (9)
you could maybe do something like:
const start = flyd.stream();
const readFile = (file)=> {
const s = flyd.stream();
fs.readFile(file, s);
}
const read_hello = start.chain(()=> readFile('./hello.txt'));
read_hello.map(fileContents => console.log(fileContents));
start(true);
from flyd.
Hey @sourcevault ! Glad you found a solution!
I think you might be able to make a generic nodeback -> Stream function.
function fromNode(fn) {
return (...args)=> {
const s = stream()
fn(...args, (err, data)=> s(data))
return s
}
}
Then you could use that as:
const readFile = fromNode(fs.readFile)
readFile('file1.txt')
.map(x => x.toString())
.map(console.log)
from flyd.
don't worry, I understand what you're trying to accomplish.
I'm curious about your use case though, what use case do you have that requires lazy streams?
from flyd.
Afaik, flyd is push by it's nature.
Check for pull-stream, Highland.js (they're oriented to pull strategy) and most.js (starting cold).
from flyd.
@StreetStrider it should be possible to express a push stream as a pull stream given that their operations are similar. I do not know what the theoretical equivalence law is.
@nordfjord yep - that seems like a cool workaround :D using chains !
Thank you both !
from flyd.
Hi @nordfjord !
const read_hello = start.chain(()=> readFile('./hello.txt'));
readFile
would return undefined
not a flyd stream.
I can think of creating a wrapper and create .run
method but there might be a clever trick.
from flyd.
var flyd = require('flyd')
var fs = require('fs')
readfile = function(name){
var s = flyd.stream()
var F = fs.readFile(name, function(err, data){return s(data.toString())})
setTimeout(F, 0);
return s;
};
stream = readfile('file1.txt');
stream.map(function(data){
console.log(data);
});
Using setTimeout
as a hack.
from flyd.
Hallo @nordfjord !! π
const readFile = fromNode(fs.readFile)
readFile('file1.txt') // <-- 1) This still runs immediately
.map(x => x.toString()) // <-- 2) This runs after [1]
.map(console.log)
I don't think there is a way without using setTimeout
to delay [1].
@nordfjord are you familiar with mostjs
?
https://github.com/cujojs/most
It runs / delays all side effects till all the .maps
etc are defined, its said to be lazy.
flyd is more 'active', but I am trying to find a way to make it lazy
, I hope I am making some sense.
from flyd.
It is more of a intellectual curiosity when you corner me like that @nordfjord
I have no immediate usecase, most streams work fine on node
events like fs.readFile
.
But then things like fs.watch
flyd is a better choice.
I was hoping if there was a way to make flyd work for all most.js
usecases.
I have looked at some libuv
internals and using setTimeout 0
with flyd
should be fine.
libuv
runs everything with setTimeout 0
in the next tick, making it lazy
so to speak.
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
- 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.