import { Component, Input, Output, EventEmitter, OnChanges } from '@angular/core'; import { RdLib } from '../../base/rdLib'; import { ServiceProxy } from '../../base/serviceCall'; import { RdComponent } from '../../base/rdComponent'; export interface IProviderContent { params: any; response: any; state: { serviceCalled: boolean; waitingForResponse: boolean; responseIsRecieved: boolean; serviceCallFailed: boolean; }; } @Component({ selector: 'rd-service-provider:not([rd-client-side-paging]):not([rd-server-side-paging])', template: "" }) export class ServiceProvider extends RdComponent implements OnChanges { @Input("rd-service-proxy") serviceProxy: ServiceProxy; @Output("rd-success") successEvent: EventEmitter = new EventEmitter(); @Output("rd-error") errorEvent: EventEmitter = new EventEmitter(); ngOnChanges(changes) { if (changes.serviceProxy) { if (!(this.serviceProxy instanceof ServiceProxy)) this.error("rd-service-proxy is not an instance of ServiceProxy"); } } content = { params: null, response: null, state: { waitingForResponse: false, serviceCalled: false, responseIsRecieved: false, serviceCallFailed: false } }; private callState(params) { this.content = { params: RdLib.objectOperations.deepCopy(params), response: null, state: { waitingForResponse: true, serviceCalled: true, responseIsRecieved: false, serviceCallFailed: false } }; } private successState(response) { this.content.response = response; this.content.state = { waitingForResponse: false, serviceCalled: true, responseIsRecieved: true, serviceCallFailed: false }; } private errorState() { this.content.state = { waitingForResponse: false, serviceCalled: true, responseIsRecieved: true, serviceCallFailed: true }; } call(params?) { if (this.content.state.waitingForResponse) return; this.callState(params); this.serviceProxy.call(params, (response) => { this.successState(response); this.successEvent.emit(this.content); }, () => { this.errorState(); this.errorEvent.emit(this.content); }); } }