import { Injectable } from '@angular/core' ; import { Language } from '@data/poe/schema' ; import { Observable } from 'rxjs' ; import { map , shareReplay } from 'rxjs/operators' ; import { ContextService } from '../../context' ; import { TradeStatic , TradeStaticGroup } from './trade-statics' ; import { TradeStaticsProvider } from './trade-statics.provider' ; const CACHE_SIZE = 1 ; interface TradeStaticCache { ids : { [ key ] : TradeStatic ; } ; name : { [ key ] : TradeStatic ; } ; } interface TradeStaticsCache { [ language ] : Observable < TradeStaticCache > ; } @ Injectable ( { providedIn : 'root' } ) export class TradeStaticsService { private readonly cache : TradeStaticsCache = { } ; constructor ( private readonly context , private readonly items ) { } public get ( language ? ) { language = language || this . context . get ( ) . language ; return this . items . provide ( language ) ; } public match ( id , language ? ) { language = language || this . context . get ( ) . language ; if ( ! this . cache [ language ] ) { this . cache [ language ] = this . buildCache ( language ) . pipe ( shareReplay ( CACHE_SIZE ) ) ; } return this . cache [ language ] . pipe ( map ( cache => cache . ids [ id ] ) ) ; } public find ( name , language ? ) { language = language || this . context . get ( ) . language ; if ( ! this . cache [ language ] ) { this . cache [ language ] = this . buildCache ( language ) . pipe ( shareReplay ( CACHE_SIZE ) ) ; } return this . cache [ language ] . pipe ( map ( cache => cache . name [ name ] ) ) ; } private buildCache ( language ) { return this . items . provide ( language ) . pipe ( map ( groups => { const cache = { ids : { } , name : { } } ; groups . forEach ( group => group . items . forEach ( item => { cache . ids [ item . id ] = item ; cache . name [ item . name ] = item ; } ) ) ; return cache ; } ) ) ; } }