import { Injectable } from '@angular/core' ; import { Storage as IonicStorage } from '@ionic/storage' ; import { Store } from '@ngrx/store' ; import _slice from 'lodash/slice' ; import _uniq from 'lodash/uniq' ; import _pickBy from 'lodash/pickBy' ; import { AppState , IAuthState , IFeed , IUsersState , ITrends , IMentions , ITweet } from './../../reducers' ; import { INIT , ON_BEFORE_UNLOAD } from './../../actions' ; @ Injectable ( ) export class StorageProvider { constructor ( private store , public storage ) { } init ( ) { let defaultState = { } ; const storagePromise = this . storage . forEach ( ( val , key ) => { defaultState [ key ] = val ; } ) ; return storagePromise . then ( ( ) => this . store . dispatch ( { type : INIT , payload : defaultState } ) ) ; } run ( ) { this . store . select ( 'auth' ) . skip ( 1 ) . debounceTime ( 500 ) . subscribe ( ( auth ) => { console . log ( 'saving auth' ) ; this . storage . set ( 'auth' , auth ) ; } ) ; this . store . select ( 'trends' ) . skip ( 1 ) . debounceTime ( 500 ) . subscribe ( ( trends ) => { console . log ( 'saving trends' ) ; this . storage . set ( 'trends' , trends ) ; } ) ; this . store . select ( state => state ) . skip ( 1 ) . debounceTime ( 500 ) . subscribe ( this . cleanupAndSaveState ) ; window . onbeforeunload = ( ) => { this . store . dispatch ( { type : ON_BEFORE_UNLOAD } ) ; } ; } cleanupAndSaveState = ( state ) => { console . log ( 'saving state' ) ; const first20Feed = _slice ( state . feed . list , 0 , 20 ) ; this . storage . set ( 'feed' , { fetching : false , list : first20Feed } ) ; const first20Mentions = _slice ( state . mentions . list , 0 , 20 ) ; this . storage . set ( 'mentions' , { fetching : false , list : first20Mentions } ) ; const tweetIdsToKeep = _uniq ( [ ... first20Feed , ... first20Mentions ] ) ; const tweetsToKeep = _pickBy ( state . tweets , ( v , k ) => tweetIdsToKeep . includes ( k ) ) ; const retweetIdsToKeep = Object . values ( tweetsToKeep ) . filter ( tweet => tweet . retweeted_status_id ) . map ( tweet => tweet . retweeted_status_id ) ; const retweetsToKeep = _pickBy ( state . tweets , ( v , k ) => retweetIdsToKeep . includes ( k ) ) ; const tweetsState = { ... tweetsToKeep , ... retweetsToKeep } ; this . storage . set ( 'tweets' , tweetsState ) ; const userHandlesToKeep = Object . values ( tweetsState ) . map ( tweet => tweet . userHandle ) ; const usersToKeep = _pickBy ( state . users , ( v , k ) => userHandlesToKeep . includes ( k ) ) ; this . storage . set ( 'users' , usersToKeep ) ; } ; }