🧠
ngx-crud
  • Introduction
  • API
    • HTTP Operations
    • HTTP Aborting
    • HTTP Caching
    • HTTP Observing
    • HTTP Options
    • HTTP Context
    • HTTP Headers
    • HTTP Params
    • Service Instance
    • Service Options
  • Reference
    • Modules
      • AbortModule
      • CacheModule
      • ObserveModule
      • CrudModule
    • Services
      • AbortService
      • CacheService
      • CommonService
      • CrudService
      • CustomService
      • DeleteService
      • FindService
      • GetService
      • ObserveService
      • PatchService
      • PostService
      • PutService
    • Interceptors
      • AbortInterceptor
      • CacheInterceptor
      • ObserveInterceptor
    • Decorators
      • @ApiUrl
      • @ApiRoute
    • Helpers
      • createUrl
      • createUrlWithId
      • stripUrlParams
  • Examples
    • Services
      • Fully Typed Collection
      • Limited Singleton
    • Components
      • Loader
    • Effects
      • ProfilerEffect
      • ErrorEffect
      • OfflineEffect
  • Links
    • GitHub
Powered by GitBook
On this page
  1. Examples
  2. Effects

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');
		}
	}
}
PreviousEffectsNextErrorEffect

Last updated 3 months ago