Skip to content

Commit

Permalink
chore: pass executor fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzianis Dashkevich committed Sep 26, 2024
1 parent 44bb818 commit 339d09e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
9 changes: 9 additions & 0 deletions packages/playback/src/lib/network/networkManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,22 @@ export interface NetworkManagerDependencies {
networkInterceptorsProvider: INetworkInterceptorsProvider;
eventEmitter: IEventEmitter<NetworkEventMap>;
configuration: PlayerNetworkConfiguration;
executor: (request: Request) => Promise<Response>;
}

export class NetworkManager implements INetworkManager {
private readonly logger_: ILogger;
private readonly networkInterceptorsProvider_: INetworkInterceptorsProvider;
private readonly eventEmitter_: IEventEmitter<NetworkEventMap>;
private readonly executor_: (request: Request) => Promise<Response>;
private configuration_: PlayerNetworkConfiguration;

public constructor(dependencies: NetworkManagerDependencies) {
this.logger_ = dependencies.logger;
this.networkInterceptorsProvider_ = dependencies.networkInterceptorsProvider;
this.eventEmitter_ = dependencies.eventEmitter;
this.configuration_ = dependencies.configuration;
this.executor_ = dependencies.executor;
}

public updateConfiguration(configuration: PlayerNetworkConfiguration): void {
Expand Down Expand Up @@ -61,6 +64,7 @@ export class NetworkManager implements INetworkManager {
networkInterceptorsProvider: this.networkInterceptorsProvider_,
eventEmitter: this.eventEmitter_,
configuration: { ...this.configuration_[payload.requestType] },
executor: (request) => this.sendRequest_(request),

Check warning on line 67 in packages/playback/src/lib/network/networkManager.ts

View check run for this annotation

Codecov / codecov/patch

packages/playback/src/lib/network/networkManager.ts#L67

Added line #L67 was not covered by tests
});
}

Expand All @@ -70,6 +74,11 @@ export class NetworkManager implements INetworkManager {
networkInterceptorsProvider: this.networkInterceptorsProvider_,
eventEmitter: this.eventEmitter_,
configuration: { ...this.configuration_[payload.requestType] },
executor: (request) => this.sendRequest_(request),

Check warning on line 77 in packages/playback/src/lib/network/networkManager.ts

View check run for this annotation

Codecov / codecov/patch

packages/playback/src/lib/network/networkManager.ts#L77

Added line #L77 was not covered by tests
});
}

protected sendRequest_(request: Request): Promise<Response> {
return this.executor_(request);
}

Check warning on line 83 in packages/playback/src/lib/network/networkManager.ts

View check run for this annotation

Codecov / codecov/patch

packages/playback/src/lib/network/networkManager.ts#L82-L83

Added lines #L82 - L83 were not covered by tests
}
7 changes: 5 additions & 2 deletions packages/playback/src/lib/network/networkRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ interface NetworkRequestDependencies {
networkInterceptorsProvider: INetworkInterceptorsProvider;
eventEmitter: IEventEmitter<NetworkEventMap>;
configuration: NetworkConfiguration;
executor: (request: Request) => Promise<Response>;
}

abstract class NetworkRequest<T> implements INetworkRequest<T> {
Expand All @@ -40,6 +41,7 @@ abstract class NetworkRequest<T> implements INetworkRequest<T> {
protected readonly networkInterceptorsProvider_: INetworkInterceptorsProvider;
protected readonly eventEmitter_: IEventEmitter<NetworkEventMap>;
protected readonly wrappedSendRequest_: () => Promise<Response>;
protected readonly executor_: (request: Request) => Promise<Response>;

public abstract readonly done: Promise<T>;

Expand All @@ -59,11 +61,12 @@ abstract class NetworkRequest<T> implements INetworkRequest<T> {
this.networkInterceptorsProvider_ = networkInterceptorsProvider;
this.eventEmitter_ = eventEmitter;
this.abortController_ = new AbortController();
this.executor_ = dependencies.executor;

Check warning on line 64 in packages/playback/src/lib/network/networkRequest.ts

View check run for this annotation

Codecov / codecov/patch

packages/playback/src/lib/network/networkRequest.ts#L64

Added line #L64 was not covered by tests
this.requestType = requestType;
this.configuration = configuration;

const retryWrapper = new RetryWrapper(configuration);
const request = new Request(url, requestInit);
const request = new Request(url, { ...requestInit, signal: this.abortController_.signal });

Check warning on line 69 in packages/playback/src/lib/network/networkRequest.ts

View check run for this annotation

Codecov / codecov/patch

packages/playback/src/lib/network/networkRequest.ts#L69

Added line #L69 was not covered by tests

this.wrappedSendRequest_ = retryWrapper.wrap<Response>({
target: () => this.sendRequest_(request, timeout),
Expand Down Expand Up @@ -137,7 +140,7 @@ abstract class NetworkRequest<T> implements INetworkRequest<T> {
};

this.eventEmitter_.emitEvent(new NetworkRequestStartedEvent(requestInfo));
return fetch(finalRequest).then(onCompleted, onFailed);
return this.executor_(finalRequest).then(onCompleted, onFailed);

Check warning on line 143 in packages/playback/src/lib/network/networkRequest.ts

View check run for this annotation

Codecov / codecov/patch

packages/playback/src/lib/network/networkRequest.ts#L143

Added line #L143 was not covered by tests
});
});
}
Expand Down
3 changes: 2 additions & 1 deletion packages/playback/src/lib/serviceLocator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class ServiceLocator {
public readonly networkManager: INetworkManager;

public constructor() {
const { console } = window;
const { console, fetch } = window;

this.configurationManager = this.createConfigurationManager_();

Expand All @@ -44,6 +44,7 @@ export class ServiceLocator {
logger: this.logger.createSubLogger('NetworkManager'),
eventEmitter: this.eventEmitter,
configuration: configuration.network,
executor: (request) => fetch(request),
networkInterceptorsProvider: {
getNetworkRequestInterceptors: (): Set<InterceptorTypeToInterceptorMap[InterceptorType.NetworkRequest]> =>
this.interceptorsStorage.getInterceptorsSet(InterceptorType.NetworkRequest),
Expand Down

0 comments on commit 339d09e

Please sign in to comment.