2

I have a Bacon.Bus within an AMD module.

define( 'bus', [ 'bacon' ], function( Bacon ){
    return new Bacon.Bus();
} );

Other modules push values onto the bus.

define( 'pusher', [ 'bus' ], function( bus ){
    // ...
    bus.push( value );
    // ...
} );

While other modules listen for values.

define( 'listener', [ 'bus' ], function( bus ){
    bus.onValue( function( value ){
        // Consume value
    } );
} );

Any modules that are currently loaded receive the pushed value; however any modules loaded afterwards do not.

I tried creating a Bacon.Property to hold the current value.

define( 'bus', [ 'bacon' ], function( Bacon ){
    var bus = new Bacon.Bus();

    bus.current = bus.toProperty();

    return bus;
} );

// The pusher is unchanged

define( 'listener', [ 'bus' ], function( bus ){
    bus.current.onValue( function( value ){
        // Consume value
    } );
} );

This still did not solve the problem though. Whether I attach onValue to bus or bus.current, modules loaded after the fact do not get triggered.

What am I missing?

Jeff Rose
  • 163
  • 1
  • 10
  • Can you please show us the listener code (i.e., the asynchronously loaded module)? – Bergi Mar 18 '14 at 15:24
  • I added an example listener module. Whether the listener is loaded at the time of the `push` or after the fact, the code is the same. – Jeff Rose Mar 18 '14 at 16:18

1 Answers1

4

This is practically the same old laziness issue: https://github.com/baconjs/bacon.js/wiki/FAQ#why-isnt-my-property-updated

As a workaround, you can add a no-op subscriber to the property to make sure it gets updated even when there are no actual subscribers. Or you can use https://github.com/baconjs/bacon.model

raimohanska
  • 3,265
  • 17
  • 28
  • Thanks! That did indeed solve my issue. I added an empty `onValue` to `current` in the `bus` module and that worked. I will look into using `Bacon.Model`. – Jeff Rose Mar 19 '14 at 13:49