0

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);

Amit kumar
6,1676 gold badges32 silver badges40 bronze badges
asked Feb 11, 2017 at 18:05
13
  • 1
    I'm sure to get to the core of the problem you could remove 2/3 of the code. I find it quite confusing what the problem actually is from all the noise. To share data between services you can just inject a service into another as long as you don't cause circular dependencies. Commented Feb 11, 2017 at 18:21
  • What do you mean with "adding to the constructor"? Why would you need a call to new WebTrackAudio(variable, variable) if it is a service? Commented Feb 11, 2017 at 18:22
  • I actually removed about 90% of the code already (you can see the ...s). I guess it's not a service? What is it then? I'm still getting my feet wet with angular2 Commented Feb 11, 2017 at 18:24
  • It's not a Singleton service but I think it's still a service... correct me if I'm wrong though Commented Feb 11, 2017 at 18:25
  • 1
    Adding service to what? Perhaps you mean injecting the service to the class that contains the code new WebAudioTrack(track.src, track.preload); and then pass the service like new WebAudioTrack(track.src, track.preload, sharedService);. There is no way to automagically get a service passed to a class instance you create with new. Commented Feb 11, 2017 at 18:44

1 Answer 1

2
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 {}
answered Feb 11, 2017 at 18:33
Sign up to request clarification or add additional context in comments.

3 Comments

This technique is leading me to get the following TS error: Supplied parameters do not match any signature of call target. Edited the post to reflect the details
@user2476265 you can't declare required arguments after optional ones.
Optional parameters like these don't make sense in services at all. It would be necessary to clarify how the class is actually used.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.