In my application, I have some objects which represent local currency, and other objects which represent currency exchange rates.
My question is, if my local currency objects subscribe to a single subject on the currency object to be alerted to rate changes (but the money objects don't actually save the subscription) and then the single currency instance defines the Subject of all those subscriptions is set to null, do all those 'subscriptions' disappear if I don't call unsubscribe on each of the 50,000 money objects?
For a concrete (simplified) example, this:
import { Subject } from 'rxjs'
interface MyChangeEvent {
oldValue : number;
newValue : number;
}
export class Currency {
rateSubject : Subject<MyChangeEvent>;
private _rate : number;
private _name : string;
constructor(name : string, rate : number) {
this.rateSubject = new Subject();
this._rate= rate;
this._name = name;
}
get rate() : number {
return this._rate;
}
set rate(v : number) {
let oldrate = this.rate;
this._rate = v;
let ce : MyChangeEvent
ce = {} as MyChangeEvent;
ce.newValue = v;
ce.oldValue = oldrate;
this.rateSubject.next(ce);
}
}
export class Money {
private _rate : number = 1;
private _localCurr : number = 0;
get dollarValue() {
return this._localCurr * this._rate;
}
constructor(localCurr : number, curr : Currency) {
this._localCurr = localCurr;
this._rate = curr.rate;
curr.rateSubject.subscribe((change)=>{
this._rate = change.newValue;
})
}
}
const test = function() {
let c = new Currency("USD", 1);
let m = new Money(500, c);
c.rate = .5;
c=null;
}
So my question is, let's say I have 50,000 money objects in my application, and then I set c=null
as in the last line here. Do the 50,000 listeners I've set up for all those money objects persist somewhere in memory? Are they all garbage collected when the Currency object goes out of scope?