File

projects/storefrontlib/context/context.service.ts

Description

Generic service for resolving the context for the UI components.

Index

Methods

Constructor

constructor(routingContextService: RoutingContextService)
Parameters :
Name Type Optional
routingContextService RoutingContextService No

Methods

get
get(contextToken: ContextToken)
Type parameters :
  • T

Returns the context for the given token.

Parameters :
Name Type Optional
contextToken ContextToken No
Protected resolveContext
resolveContext(contextToken: ContextToken)
Type parameters :
  • T

Resolves the context for the given token.

Parameters :
Name Type Optional
contextToken ContextToken No
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { distinctUntilChanged, shareReplay } from 'rxjs/operators';
import { ContextToken } from './context.model';
import { RoutingContextService } from './routing-context.service';

/**
 * Generic service for resolving the context for the UI components.
 */
@Injectable({ providedIn: 'root' })
export class ContextService {
  constructor(protected routingContextService: RoutingContextService) {}

  /**
   * Returns the context for the given token.
   */
  get<T>(contextToken: ContextToken): Observable<T | undefined> {
    return this.resolveContext<T>(contextToken).pipe(
      distinctUntilChanged(),
      shareReplay({ refCount: true, bufferSize: 1 })
    );
  }

  /**
   * Resolves the context for the given token.
   */
  protected resolveContext<T>(
    contextToken: ContextToken
  ): Observable<T | undefined> {
    return this.routingContextService.get(contextToken);
  }
}

result-matching ""

    No results matching ""