import { Injectable } from '@nestjs/common' ; import { BrakesFactory } from './brakes.factory' ; import { BrakesRegistry } from './brakes.registry' ; import { Fallback } from './interfaces/fallback.interface' ; @ Injectable ( ) export class Brakes { constructor ( private readonly brakesFactory , private readonly brakesRegistry ) { } slave ( name , fallback , ref ) ; slave ( fallback , ref ) ; slave ( name , ref ) ; slave ( nameOrFallback , fallbackOrRef , func ? ) { const [ name , fallback , ref ] = typeof nameOrFallback === 'string' && func ? [ nameOrFallback as string , fallbackOrRef as Fallback , func ] : typeof nameOrFallback === 'string' && ! func ? [ nameOrFallback , undefined , fallbackOrRef as Function ] : [ undefined , nameOrFallback as Fallback , fallbackOrRef as Function ] ; let brakes = this . brakesRegistry . getBrakes ( name ) ; if ( ! brakes ) { brakes = this . brakesFactory . create ( name ) ; } let circuit ; if ( fallback ) { circuit = brakes . slaveCircuit ( ref , fallback . fallback , fallback . config ? fallback . config ( ) : { } ) ; } else { circuit = brakes . slaveCircuit ( ref ) ; } return circuit . exec . bind ( circuit ) ; } }