import { ProviderNotRegisteredException } from '../exception' ; import type { AbstractProvider } from '../model' ; import { AbstractDecider } from './abstract-decider' ; export class StatefulDecider extends AbstractDecider { private currentProvider ? : AbstractProvider ; async getProvider ( providers , forceProvider ? ) { if ( forceProvider ) { return ( this . currentProvider = forceProvider ) ; } if ( ! providers . length ) { throw ProviderNotRegisteredException . noProviderRegistered ( ) ; } if ( ! this . currentProvider || providers . length === 1 ) { return ( this . currentProvider = providers [ 0 ] ) ; } return ( this . currentProvider = this . getNextProvider ( providers , this . currentProvider ) ) ; } private getNextProvider ( providers , provider ) { const providerIndex = providers . indexOf ( provider ) ; return providers [ providerIndex + 1 ] || providers [ 0 ] ; } }