import { toString } from 'lodash' ; import { LRUMap } from 'lru_map' ; export function safeApiCall ( client , method , formatter ? , ... args ) { return cache ( method , args , client [ method ] . apply ( client , args ) . then ( ( response ) => { return Promise . resolve ( ! ! formatter ? formatter ( response ) : response . body ) ; } ) ) ; } let cacheMap = new LRUMap ( 50 ) ; export function clearCache ( ) { cacheMap . clear ( ) ; } function cache ( method , args , value ) { let key = toString ( [ method ] . concat ( args ) ) ; if ( cacheMap . has ( key ) ) { return Promise . resolve ( cacheMap . get ( key ) ) ; } else { return new Promise ( ( resolve , reject ) => { value . then ( ( val ) => { cacheMap . set ( key , val ) ; resolve ( cacheMap . get ( key ) ) ; } , reject ) ; } ) ; } }