question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

To "streamify" a map of callbacks

See original GitHub issue

Hi guys,

I was doing a facade on a legacy API where a map of callback is passed in as an arg like:

const myStupidListeners = {
    aHappened: e => {},
    bHappened: e => {},
    cHappened: e => {}
}

invokeLegacyAPI("start", myStupidListeners)

I was trying to wrap this API to multiple event streams using bacon, so for each xHappened callback, an event stream shall be created.

But however I just can’t spin my head around how to “streamify” those callbacks using factory functions provided by Bacon, at best I’m thinking of kicking start a Bus for each callbacks and pushing events to that bus in those callbacks.

Am I missing somehing form the doc and is there a better way to do this?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
philipnilssoncommented, May 20, 2016

Probably a Bus is the easiest solution. There are other options but since you have three callbacks you want to pass simultaneously it’s a bit trickier.

const onA = new Bacon.Bus();
const onB = new Bacon.Bus();
const onC = new Bacon.Bus();

const myStupidListeners = {
    aHappened: ev => onA.push(ev),
    bHappened: ev => onB.push(ev),
    cHappened: ev => onC.push(ev),
};

invokeLegacyAPI("start", myStupidListeners);
1reaction
raimohanskacommented, May 20, 2016

Optionally you can

allEvents = B.fromBinder(function(sink) { invokeLegacyAPI({a: sink, b: sink, c: sink}) })

This ways you’ll get all events into one stream. Then you need to split it into three by applying different filters. Note that you could also can tag your data like

allEvents = B.fromBinder(function(sink) { invokeLegacyAPI({
  a: function(a) { sink ({a: a}) }, 
  b: function(b) { sink ({b: b}) }, 
  c: function(b) { sink ({c: c}) }
})})

So that you allEvents stream will output stuff like

{a: "value from event a" }
{c: "something else from c"}

The Bus approach seems more approachable to me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Streaming Callback Function | LumenVox Knowledgebase
The callback function is called by the speech port each time there is a change in the stream status. Primarily this is used...
Read more >
Add callback function to Java stream - Stack Overflow
Let's say I want to perform the following operations: List the files in a given directory (as a stream); Map each file (Path)...
Read more >
Stream | Node.js v19.3.0 Documentation
The stream/promises API provides an alternative set of asynchronous utility functions for streams that return Promise objects rather than using callbacks. The ...
Read more >
Callbacks, Part 3: Promise, Event, and Stream (Functional ...
Event is a truly great alternative when you realize the problem of using the standard tools, but not yet willing to use Streams...
Read more >
Async mapping with promises · Issue #517 · caolan/highland
getUserFromId returns a promise _([1,2,3,4]).map(id... ... the promise handler streamifyAsync and the callback handler streamify though.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found