Skip to content

Commit

Permalink
feat(authorizenet): add state config for accept retry backoff (#2541)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 committed Aug 11, 2023
1 parent 4ead116 commit 9dd40b7
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 3 deletions.
24 changes: 22 additions & 2 deletions libs/authorizenet/state/src/authorize-net-state.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { NgModule } from '@angular/core';
import {
ModuleWithProviders,
NgModule,
} from '@angular/core';
import { EffectsModule } from '@ngrx/effects';
import { StoreModule } from '@ngrx/store';

import {
DaffAuthorizeNetStateConfig,
daffAuthorizeNetStateDefaultConfig,
provideDaffAuthorizeNetStateConfig,
} from './config/public_api';
import { DaffAuthorizeNetEffects } from './effects/authorize-net.effects';
import { DAFF_AUTHORIZENET_STORE_FEATURE_KEY } from './reducers/authorizenet-store-feature-key';
import { DAFF_AUTHORIZE_NET_REDUCERS } from './reducers/token/reducers.token';
Expand All @@ -12,4 +20,16 @@ import { DAFF_AUTHORIZE_NET_REDUCERS } from './reducers/token/reducers.token';
EffectsModule.forFeature([DaffAuthorizeNetEffects]),
],
})
export class DaffAuthorizeNetStateModule { }
export class DaffAuthorizeNetStateModule {
static withConfig(config: Partial<DaffAuthorizeNetStateConfig> = {}): ModuleWithProviders<DaffAuthorizeNetStateModule> {
return {
ngModule: DaffAuthorizeNetStateModule,
providers: [
provideDaffAuthorizeNetStateConfig({
...daffAuthorizeNetStateDefaultConfig,
...config,
}),
],
};
}
}
6 changes: 6 additions & 0 deletions libs/authorizenet/state/src/config/default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { DaffAuthorizeNetStateConfig } from './type';

export const daffAuthorizeNetStateDefaultConfig: DaffAuthorizeNetStateConfig = {
acceptMaxRetries: 5,
acceptBackoffTiming: 30,
};
6 changes: 6 additions & 0 deletions libs/authorizenet/state/src/config/public_api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export { daffAuthorizeNetStateDefaultConfig } from './default';
export {
DAFF_AUTHORIZE_NET_STATE_CONFIG,
provideDaffAuthorizeNetStateConfig,
} from './token';
export { DaffAuthorizeNetStateConfig } from './type';
14 changes: 14 additions & 0 deletions libs/authorizenet/state/src/config/token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {
InjectionToken,
ValueProvider,
} from '@angular/core';

import { daffAuthorizeNetStateDefaultConfig } from './default';
import { DaffAuthorizeNetStateConfig } from './type';

export const DAFF_AUTHORIZE_NET_STATE_CONFIG = new InjectionToken('DAFF_AUTHORIZE_NET_STATE_CONFIG', { factory: () => daffAuthorizeNetStateDefaultConfig });

export const provideDaffAuthorizeNetStateConfig = (config: DaffAuthorizeNetStateConfig): ValueProvider => ({
provide: DAFF_AUTHORIZE_NET_STATE_CONFIG,
useValue: config,
});
13 changes: 13 additions & 0 deletions libs/authorizenet/state/src/config/type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export interface DaffAuthorizeNetStateConfig {
/**
* How many times Daffodil will retry the Accept.js load.
* Defaults to 5.
*/
acceptMaxRetries: number;

/**
* The Accept.js loading retry backoff timing in milliseconds.
* Defaults to 30.
*/
acceptBackoffTiming: number;
}
7 changes: 6 additions & 1 deletion libs/authorizenet/state/src/effects/authorize-net.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ import {
DaffLoadAcceptJsFailure,
DaffLoadAcceptJsSuccess,
} from '../actions/authorizenet.actions';
import {
DAFF_AUTHORIZE_NET_STATE_CONFIG,
DaffAuthorizeNetStateConfig,
} from '../config/public_api';

@Injectable()
export class DaffAuthorizeNetEffects<T extends DaffAuthorizeNetTokenRequest = DaffAuthorizeNetTokenRequest> {
Expand All @@ -62,6 +66,7 @@ export class DaffAuthorizeNetEffects<T extends DaffAuthorizeNetTokenRequest = Da
@Inject(DaffAuthorizeNetDriver) private driver: DaffAuthorizeNetService<T>,
@Inject(DaffAuthorizeNetPaymentId) private authorizeNetPaymentId: string,
@Inject(DAFF_AUTHORIZENET_ERROR_MATCHER) private errorMatcher: ErrorTransformer,
@Inject(DAFF_AUTHORIZE_NET_STATE_CONFIG) private config: DaffAuthorizeNetStateConfig,
private acceptJsLoadingService: DaffAcceptJsLoadingService,
) {}

Expand Down Expand Up @@ -105,7 +110,7 @@ export class DaffAuthorizeNetEffects<T extends DaffAuthorizeNetTokenRequest = Da
));


loadAcceptJs$ = createEffect(() => (maxTries = 3, ms = 30): Observable<any> => this.actions$.pipe(
loadAcceptJs$ = createEffect(() => (maxTries = this.config.acceptMaxRetries, ms = this.config.acceptBackoffTiming): Observable<any> => this.actions$.pipe(
ofType(DaffAuthorizeNetActionTypes.LoadAcceptJsAction),
tap((action: DaffLoadAcceptJs) => this.acceptJsLoadingService.load()),
switchMap(() => defer(() => of(this.acceptJsLoadingService.getAccept())).pipe(
Expand Down
1 change: 1 addition & 0 deletions libs/authorizenet/state/src/public_api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './actions/authorizenet.actions';
export * from './selectors/authorize-net.selector';
export * from './reducers/public_api';
export * from './config/public_api';

export { DaffAuthorizeNetFacade } from './facades/authorize-net.facade';
export { DaffAuthorizeNetFacadeInterface } from './facades/authorize-net-facade.interface';
Expand Down

0 comments on commit 9dd40b7

Please sign in to comment.