File

src/app/core/services/transfer-state.service.ts

Index

Methods

Constructor

constructor(transferState: TransferState, platformId: Object)
Parameters :
Name Type Optional
transferState TransferState no
platformId Object no

Methods

getCache
getCache(key: string)

Returns stored cache only when it's running on browser

Parameters :
Name Type Optional
key string no
Returns : any

cachedData

setCache
setCache(key: string, data: any)

Set cache only when it's running on server

Parameters :
Name Type Optional Description
key string no
data any no

Data to store to cache

Returns : void
import { Inject, Injectable, PLATFORM_ID } from '@angular/core';
import { TransferState, makeStateKey } from '@angular/platform-browser';
import { isPlatformBrowser } from '@angular/common';

/**
 * Keep caches (makeStateKey) into it in each `setCache` function call
 * @type {any[]}
 */
const transferStateCache: String[] = [];

@Injectable()
export class TransferStateService {
  constructor(private transferState: TransferState,
    @Inject(PLATFORM_ID) private platformId: Object,
    // @Inject(APP_ID) private _appId: string
  ) {
  }

  /**
   * Set cache only when it's running on server
   * @param {string} key
   * @param data Data to store to cache
   */
  setCache(key: string, data: any) {
    if (!isPlatformBrowser(this.platformId)) {
      transferStateCache[key] = makeStateKey<any>(key);
      this.transferState.set(transferStateCache[key], data);
    }
  }


  /**
   * Returns stored cache only when it's running on browser
   * @param {string} key
   * @returns {any} cachedData
   */
  getCache(key: string): any {
    if (isPlatformBrowser(this.platformId)) {
      const cachedData: any = this.transferState['store'][key];
      /**
       * Delete the cache to request the data from network next time which is the
       * user's expected behavior
       */
      delete this.transferState['store'][key];
      return cachedData;
    }
  }
}

results matching ""

    No results matching ""