import { PromiseCacheService } from "app/shared/promiseCache.service" ; export function CacheParam ( target , propertyKey , index ) { const metadataKey = `__cache_${ propertyKey }_` ; Array . isArray ( target [ metadataKey ] ) ? target [ metadataKey ] . push ( index ) : target [ metadataKey ] = [ index ] ; } export function PromiseCache ( key ? ) { return function ( target , propertyKey , descriptor ) : any | Promise < any > { const method = descriptor . value ; descriptor . value = function ( ... args ) : any | Promise < any > { const className = this . constructor . name ; const metadataKey = `__cache_${ propertyKey }_` ; const paramKey = args . join ( '_' ) ; const fullKey = key || `${ className }_${ propertyKey }_${ paramKey }` ; let promiseCache = PromiseCacheService . getInstance ( ) ; if ( promiseCache . has ( fullKey ) ) { return promiseCache . get ( fullKey ) ; } if ( key ) { promiseCache . map ( key , fullKey ) ; } return promiseCache . set ( fullKey , new Promise < any > ( async ( resolve ) => { resolve ( method . apply ( this , args ) ) ; } ) ) ; } ; return descriptor ; } ; }