How can I apply Facade Pattern to Subject-Observable (the Publish/Subscribe) Code below?
We have parent component subscribing to many services. Now learned more sibling and other components in different functional areas require Some or All of the service subscriptions. What is a good way to manage and follow dry (don't repeat yourself principal).
Do we have to resubscribe to Same long list of services in the different components? The services may start looking like a vast interconected cobweb, so trying to be careful.
Trying to refer off this website:
Parent Receiver:
constructor
(
@Inject('AddressMailingStandardService') private addressMailingStandardService: BasicService,
@Inject('AddressMailingRuralService') private addressMailingRuralService: BasicService,
@Inject('TypeService') private typeService: BasicService,
@Inject('SourceService') private sourceService: BasicService,
@Inject('PurposeOfUseService') private purposeOfUseService: BasicService,
ngOnInit() {
this.typeService.currentMessage.subscribe(currentAddressTypeMessage => {
this.addressTypeMessage = currentAddressTypeMessage;
this.addressTypeData = this.addressTypeMessage;
});
this.sourceService.currentMessage.subscribe(currentAddressSourceMessage => {
this.sourceMessage = currentAddressSourceMessage;
this.sourceData = this.addressSourceMessage;
});
this.purposeOfUseService.currentMessage.subscribe(currentAddressPurposeMessage => {
this.purposeOfUseMessage = currentAddressPurposeMessage;
this.purposeOfUseData = this.addressPurposeOfUseMessage;
});
this.addressMailingStandardService.currentMessage.subscribe(currentMessage => {
this.addressStandardMessage = currentMessage;
this.addressMailingStandardData = new AddressMailingData(this.addressStandardMessage.value);
});
this.addressMailingRuralService.currentMessage.subscribe(currentRuralMessage => {
this.addressRuralMessage = currentRuralMessage;
this.addressMailingRuralData = new AddressMailingData(this.addressRuralMessage.value);
});
Basic Service:
export class BasicService {
private messageSource = new Subject();
currentMessage = this.messageSource.asObservable();
constructor() { }
changeMessage(currentMessage) {
this.messageSource.next(currentMessage);
}
}
Sender Example:
AddressMailingStandardService
this.subs.add(this.standardAddressForm.valueChanges.subscribe(data => {
this.basicService.changeMessage(this.editAddressForm);
}));
TypeService
statusSelectedItemChanged(e) {
this.selectedItemOutput = e;
this.typeService.changeMessage(e);
}
from Angular 8: Create Facade Pattern for Subject-Observable design, Publish/Subscribe
No comments:
Post a Comment