paldepind / flyd

The minimalistic but powerful, modular, functional reactive programming library in JavaScript.
MIT License
1.56k stars 84 forks source link

Consider exposing an API to monitor all streams #181

Closed shinkathe closed 6 years ago

shinkathe commented 6 years ago

Use case: I would like to add a listener to all streams that have been created all around the application to be able to create aggregate reports of streams that are updated the most vs least and be able to find spots where updates are being unnecessarily made or find optimization points that are the highest impact.

nordfjord commented 6 years ago

This sounds like something that might kill garbage collection.

Any reason you couldn't monkeypatch flyd for this?

import flyd from 'flyd';

const _stream = flyd.stream;
const streams = [];
flyd.stream = (...args)=> {
  const s = _stream(...args);
  streams.push(s);
  return s;
}

const expandStream = s => [s, s.listeners && s.listeners.map(expandStream), s.deps && s.deps.map(expandStream)];

export const getAllStreams = ()=> {
   return R.uniq(R.flatten(streams.map(expandStream))).filter(Boolean);
}
shinkathe commented 6 years ago

This works, thanks.

paldepind commented 6 years ago

That is a very nice trick @nordfjord. Thank you for asking a very good question @shinpou. I've never thought of doing profiling like that myself but I can definitely see how it could be useful.