I'm looking for a way to have my observable emit on intervals. I have a service that handles it, and components that use the subject to emit values and a component that need to recieve it on a 6 second interval.
export class Service {
constructor() {}
private subject = new Subject<string>();
public observable$ = this.toastMessage.asObservable();
updateSubject(data) {
this.subject.next(data);
}
}
// somewhere else
export class Component {
constructor(private srv: Service) {}
ngOnInit() {
this.srv.observable$.subscribe(data => {
console.log(data, 'should emit once every 6 seconds');
})
}
}
// also elsewhere
export class ComponentUpdater {
constructor(private srv: Service) {}
ngOnInit() {
this.srv.updateSubject('way');
this.srv.updateSubject('to');
this.srv.updateSubject('fast');
}
}
Does anyone know how I can do it with Subject?