I have a callback method in a service which have code like below:
@Injectable({
providedIn: 'root'
})
export class AService {
counter: number = 0;
anObject = new anObject();
dataObservable = {} as Observable<anObject>;
dataSubject = new Subject<anObject>();
aMethod = () => {
this.anObject.a += 4;
this.anObject.b += 3;
//- With subject, works fine and I can use anObject's data in component
this.dataSubject.next(this.abObject);
//- but when I try with observable, it doesn't work
this.dataObservable = new Observable((observer) => observer.next(this.anObject));
this.counter++;
if (this.counter < 100 ) {
this.aMethod;
}
}
In short, I have a method in service which will run 100 times and will update value with every 100 ms in component, however this works fine with 'Subject' but doesn't work with 'Observable'.
I don't like to use 'Subject' here, because there is only single subscriber, which will use the service.
Thanks in advance of any suggestion..