RxEmitter icon indicating copy to clipboard operation
RxEmitter copied to clipboard

Get last emitted value after subscription

Open unlight opened this issue 8 years ago • 7 comments

const RxEmitter = require('rxemitter').RxEmitter;
RxEmitter.emit("ADD_AN_NUMBER", 1);
RxEmitter.emit("ADD_AN_NUMBER", 2);
RxEmitter.on('ADD_AN_NUMBER').subscribe(x=> console.log(x));

Prints nothing. Is possible to get last value after subscription? So expected behavior is printing 2.

unlight avatar Oct 14 '17 14:10 unlight

Forked unlight/RxEmitter

unlight avatar Oct 17 '17 20:10 unlight

The order of registration. Your advice is good, I have to think about how to achieve. thank you very much

drawcall avatar Oct 21 '17 01:10 drawcall

What is the status/resolution?

unlight avatar Nov 18 '17 10:11 unlight

What is the status/resolution?

unlight avatar Mar 18 '18 16:03 unlight

Sorry, I haven’t come and implemented the logic here. I'm really busy lately, sorry

drawcall avatar Mar 19 '18 02:03 drawcall

To manage this behavior you should replace Subject with BehaviorSubject:

if (!this.cache[eventName].subject) { this.cache[eventName].subject = new BehaviorSubject(null); }

I think this is the only possible solution :)

Hezard avatar Aug 10 '18 10:08 Hezard

@Hezard Yes, and additionally we need add .skip(1) in on() method if subject already exists. See my forked branch https://github.com/unlight/RxEmitter

unlight avatar Aug 10 '18 13:08 unlight