I've looked around a bit but there seems to be no material on sharing data between services in Angular2. I have two @Injectable() services, where one is a Singleton that broadcasts data. In the other service, I have code that updates a variable called progress as a web audio track plays:
import { SharedService } from '../shared.service';
...
@Injectable()
export class WebAudioTrack implements IAudioTrack {
public _progress: number = 0;
...
constructor(public src: string, @Optional() public preload: string = 'none', @Optional() private ctx: AudioContext = undefined ) {
// audio context not needed for now
// this.ctx = this.ctx || new AudioContext();
this.createAudio();
}
private onTimeUpdate(e: Event) {
if (this.isPlaying && this.audio.currentTime > 0) {
# I want to do something like SharedService.setProgress(this.audio.currentTime) here
this._progress = this.audio.currentTime;
this._completed = this.audio.duration > 0 ? Math.trunc (this.audio.currentTime / this.audio.duration * 100)/100 : 0;
}
}
I'm looking for a way to broadcast the _progress variable in the code above to another @Injectable called SharedService. I don't even need to set up a BehaviorSubject in the code above because I should just be able to do something like SharedService.setProgress(time) every time the onTimeUpdate fires.
The question is, how can I add the SharedService to the code above? When I try to add it to the constructor it complains because there are calls to "new WebTrackAudio(variable, variable)". Here's the other service code which is incomplete and might be slightly confusing:
@Injectable()
export class SharedService {
progress: number;
setProgress(progress: number) {
console.log(progress);
this.progress = progress;
}
getProgress() {
return this._webTrack._progress;
}
}
Thanks!
When I update the constructor in the first service to include SharedService like so:
constructor(public src: string, @Optional() public preload: string = 'none', @Optional() private ctx: AudioContext = undefined, public sharedService: SharedService ) {
// audio context not needed for now
// this.ctx = this.ctx || new AudioContext();
this.createAudio();
}
I get the following error:
Typescript Error
Supplied parameters do not match any signature of call target.
.../src/app/ionic-audio/ionic-audio-providers.ts
create(track: ITrackConstraint) {
let audioTrack = new WebAudioTrack(track.src, track.preload);
Object.assign(audioTrack, track);
1 Answer 1
import { SharedService } from '../shared.service';
...
@Injectable()
export class WebAudioTrack implements IAudioTrack {
public _progress: number = 0;
...
constructor(private sharedService:SharedService) {}
private onTimeUpdate(e: Event) {
if (this.isPlaying && this.audio.currentTime > 0) {
# I want to do something like this.sharedService.setProgress(this.audio.currentTime) here
this._progress = this.audio.currentTime;
this._completed = this.audio.duration > 0 ? Math.trunc (this.audio.currentTime / this.audio.duration * 100)/100 : 0;
}
}
ensure both services are provided like
@NgModule({
...,
providers: [WebAudioTrack, SharedService],
})
export class AppModule {}
new WebTrackAudio(variable, variable)if it is a service?new WebAudioTrack(track.src, track.preload);and then pass the service likenew WebAudioTrack(track.src, track.preload, sharedService);. There is no way to automagically get a service passed to a class instance you create withnew.