Functional Javascript BaconJS, how can I push more values to an event stream?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Bacon.js, you do not usually "push" values into every event stream directly. Most streams are derived from other sources, and only certain primitives, such as buses, are designed to accept manual pushes. If you need to inject additional values at runtime, the normal solution is to feed a Bacon.Bus or merge multiple streams into a new derived stream.
Understand Derived Streams Versus Input Sources
Most Bacon.js streams are immutable transformations. Once you create a stream from events or from another stream, you typically do not mutate it directly.
Example derived stream:
positions is derived from clicks. You do not push extra values into positions. Instead, you create another source or merge with one.
Use Bacon.Bus for Manual Pushes
Bacon.Bus is the standard Bacon.js type for imperatively pushing events.
If you need programmatic event injection, start with a bus instead of trying to mutate a derived stream.
Merge Manual Values with Existing Streams
If you already have an event stream and want to add more values, merge it with a bus or another stream.
This is the Bacon.js way to extend behavior without breaking stream immutability.
Plug Existing Streams into a Bus
A bus can also accept another stream with plug, which is useful when you want one sink for both declarative and imperative sources.
This keeps external inputs and manual injections in one observable stream.
Use Buses Carefully
Buses are powerful, but they introduce imperative behavior into reactive code. That is sometimes necessary, but overuse can make the data flow harder to reason about.
A good rule:
- Use plain streams when data flow is purely derived.
- Use a bus when the application truly needs external event injection.
That keeps Bacon code closer to functional-reactive style.
Example: UI Actions Plus Programmatic Resets
A common real-world pattern is combining user actions with programmatic commands.
Here the stream carries both natural UI input and explicit reset commands.
Ending and Error Semantics
If you manually push values into a bus, you also control when it ends and whether it emits errors.
Once a bus ends, further pushes do not behave like normal live events. Treat end state as final.
Design Advice for Maintainable FRP Code
If you find yourself pushing values into many places manually, step back and look at the design. It often means the stream graph should be reorganized so more behavior is expressed as transformations and fewer points require imperative injection.
One bus at a system boundary is often fine. Several buses deep in application logic often signal unnecessary complexity.
Common Pitfalls
- Trying to push values into a derived event stream instead of using a bus.
- Using
Bacon.Buseverywhere and turning reactive code back into imperative event plumbing. - Forgetting that a bus can be ended and then no longer behaves like a live stream.
- Merging manual and derived events without documenting what the combined stream represents.
- Treating buses as shared global state instead of localized event boundaries.
Summary
- Most Bacon.js streams are derived and are not meant for direct manual pushes.
- Use
Bacon.Buswhen you need to inject values imperatively. - Merge or plug streams instead of mutating existing derived streams.
- Keep buses near application boundaries to preserve clear reactive flow.
- If you need many manual pushes, reconsider the stream design.
Related reading
- Futures vs. Promises
- Generate a Hash from string in Javascript
- Generate SHA-1 for Flutter/React-Native/Android-Native app
- Generator return doesn't work in for-await-of loop
- Get a random item from a JavaScript array
- Get an array of property values from an object array
- Get an array of property values from an object array
- get and set in TypeScript
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.