ProfilerEffect

Show an info message on unusually slow requests:

import { Injectable } from '@angular/core';
import { HttpContextToken, HttpErrorResponse, HttpRequest, HttpResponse } from '@angular/common/http';
import { ObserveBeforeEffect, ObserveAfterEffect } from 'ngx-crud';

import { NotifierService } from './notifier.service';

@Injectable()
export class ProfilerEffect implements ObserveBeforeEffect, ObserveAfterEffect
{
	protected defaultContext : number = 0;
	protected token : HttpContextToken<number> = new HttpContextToken<number>(() => this.defaultContext);

	constructor(protected notifierService : NotifierService) {}

	before<T>(request : HttpRequest<T>) : HttpRequest<T>
	{
		request.context.set(this.token, Date.now());
		return request;
	}

	after<T>(request : HttpRequest<T>, response : HttpResponse<T> | HttpErrorResponse) : void
	{
		const requestTime : number = Date.now() - request.context.get(this.token);

		if (requestTime > 500)
		{
			notifierService.info('REQUEST_SLOW');
		}
	}
}

Last updated