import { ITheme } from './types' ; export interface XCache < T > { has ( key ) : boolean ; set ( key , value ) : void ; get ( key ) : T | undefined ; } interface ThemeCache { [ key ] : XCache < any > ; } const cacheSupported = typeof Map !== 'undefined' && typeof WeakMap !== 'undefined' ; const caches = cacheSupported ? new WeakMap < ITheme , ThemeCache > ( ) : null ; const getThemeCache = ( theme ) : ThemeCache | null => { if ( caches === null ) return null ; if ( caches . has ( theme ) ) return caches . get ( theme ) || null ; const cache = { } ; caches . set ( theme , cache ) ; return cache ; } ; const noopCache = { has : ( ) => false , set : ( ) => undefined , get : ( ) => undefined , } ; export const getCache = < T > ( theme , namespace ) : XCache < T > => { if ( ! theme ) return noopCache ; const cache = getThemeCache ( theme ) ; if ( ! cache || ! theme ) return noopCache ; cache [ namespace ] = cache [ namespace ] || new Map ( ) ; return cache [ namespace ] ; } ;