import { Injectable } from '@angular/core' ; import { Storage } from '../storage' ; export type SearchSuggestionType = 'text' | 'publisher' ; @ Injectable ( ) export class RecentService { constructor ( private storage ) { } store ( key , entry , cleanupFn ? ) { let data = this . read ( key ) ; if ( cleanupFn ) { data = data . filter ( e => ! cleanupFn ( e ) ) ; } data . unshift ( entry ) ; this . write ( key , data ) ; return this ; } fetch ( key , limit ? ) { let data = this . read ( key ) ; if ( limit ) { data . splice ( 0 , data . length - limit ) ; } return data ; } splice ( key , deleteCount ) { this . write ( key , this . read ( key ) . splice ( 0 , deleteCount ) ) ; return this ; } clear ( key ) { this . delete ( key ) ; return this ; } /**
     * Store suggestion
     */ storeSuggestion ( type , suggestionObj , cleanupFn ? ) { const key = `suggestions:${ type }` ; let entry = suggestionObj ; entry [ 'recent_timestamp' ] = Date . now ( ) ; if ( type === 'text' ) { entry [ 'type' ] = 'text' ; } this . store ( key , entry , cleanupFn ) ; this . splice ( key , 50 ) ; } /**
     * Get a list of suggestions in reverse order of their recent_timestamps
     */ fetchSuggestions ( ) { const textSuggestions = this . read ( 'suggestions:text' ) ; const publishersSuggestions = this . read ( 'suggestions:publisher' ) . filter ( user => ! user . blocked ) ; let suggestions = textSuggestions . concat ( publishersSuggestions ) ; if ( ! suggestions . length ) return ; suggestions . sort ( ( a , b ) => b . recent_timestamp - a . recent_timestamp ) ; return suggestions . splice ( 0 , 20 ) ; } clearSuggestions ( ) { this . clear ( 'suggestions:publisher' ) ; this . clear ( 'suggestions:text' ) ; this . clear ( 'recent:text' ) ; this . clear ( 'recent' ) ; } private read ( key ) { return JSON . parse ( this . storage . get ( `recent:${ key }` ) || '[]' ) ; } private write ( key , data ) { this . storage . set ( `recent:${ key }` , JSON . stringify ( data ) ) ; } private delete ( key ) { this . storage . destroy ( `recent:${ key }` ) ; } static _ ( storage ) { return new RecentService ( storage ) ; } }