GithubHelp home page GithubHelp logo

Comments (3)

eric-basley avatar eric-basley commented on July 30, 2024

I've made this implementation, but behavior of count() is very weird !!!

const throughLimit= (fn, stream, maxConcurrency) => {
const running = flyd.stream(0);
const cache = [];
return flyd.combine((s, count, self, changed) => {
if(count() < maxConcurrency) {
count(count() + 1);
const value = includes(s, changed) ? s() : cache.pop();
if(value) fn(value, res => { count(count() - 1); self(res) });
}else if (includes(s, changed)){
cache.unshift(s());
}
}, [stream, running]);
};

Can we update a dependant stream inside a combine() body ?

from flyd.

eric-basley avatar eric-basley commented on July 30, 2024

let's forget the first buggy implementation!
here's a first version, with a mix of stream and recursive callbacks.

it's called as: const resStream = throughLimit(makeRequest, ulrsStream, 10)

Does anyone have a better idea ?

const throughLimit= (fn, stream, maxConcurrency) => {
let running = 0;
const cache = [];
const callFn = (value, cb) => {
fn(value, res => {
cb(res);
if(cache.length) {
const value = cache.pop();
running++;
callFn(value, cb);
}
});
};
return flyd.combine((s, self, changed) => {
if(running < maxConcurrency) {
running++;
callFn(s(), res => {
running--;
self(res);
});
}else{
cache.unshift(s());
}
}, [stream]);
};

from flyd.

nordfjord avatar nordfjord commented on July 30, 2024

You could use ramda to help you there

Maybe something like this could do the trick for you?

const urlStream = flyd.stream() // Stream string

const joinedUrlStream = urlStream
  .pipe(flyd.scan(R.flip(R.append), [])) // Stream [string]

const requestStream = joinedUrlStream
  .map(R.splitEvery(1000)) // Stream [[string]]
  .map(async splitUrls => {
    let result = []
    for (const urls of splitUrls) {
      result = result.concat(await Promise.all(urls.map(makeRequest)))
    }
    return result
  }) // Stream Promise [Result]

const responseStream = requestStream
  .chain(flyd.fromPromise) // Stream [Result]

from flyd.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.