import { Injectable } from "@angular/core" ; import { DataStore } from "../data-store" ; import { StorageLocationModel } from "../storage-location.model" ; import * as _ from "lodash" ; import { AppUsage } from "../../models/app-usage.model" ; import { AppUsageDetails } from "../../models/app-usage-details.model" ; @ Injectable ( ) export class ChromeDataStore < T > extends DataStore < T > { return { Promise < T [ ] | T > } public fetch ( storageLocation , query , defaultStorageValue ) { return new Promise < T [ ] | T > ( ( resolve , reject ) => { if ( _ . isEmpty ( query ) ) { query = null ; } this . chromeLocalStorageArea ( ) . get ( query , ( result ) => { const error = this . getLastError ( ) ; if ( error ) { reject ( error . message ) ; } else { if ( storageLocation . key ) { resolve ( result [ storageLocation . key ] ? result [ storageLocation . key ] : defaultStorageValue ) ; } else { resolve ( ! _ . isEmpty ( result ) ? result : defaultStorageValue ) ; } } } ) ; } ) ; } param storageLocation param value param defaultStorageValue return { Promise < T [ ] | T > } public save ( storageLocation , value , defaultStorageValue ) { return new Promise < T [ ] | T > ( ( resolve , reject ) => { let saveQuery ; if ( storageLocation . key ) { saveQuery = { } ; saveQuery [ storageLocation . key ] = value ; } else { saveQuery = value ; } this . chromeLocalStorageArea ( ) . set ( saveQuery , ( ) => { const error = this . getLastError ( ) ; if ( error ) { reject ( error . message ) ; } else { const query = ( storageLocation . key ) ? ( storageLocation . key ) : null ; this . fetch ( storageLocation , query , defaultStorageValue ) . then ( ( response ) => { resolve ( response ) ; } , error => reject ( error ) ) ; } } ) ; } ) ; } param storageLocation param path param value param defaultStorageValue public upsertProperty < V > ( storageLocation , path , value , defaultStorageValue ) { const query = ( storageLocation . key ) ? storageLocation . key : null ; return this . fetch ( storageLocation , query , defaultStorageValue ) . then ( ( dataStore ) => { if ( _ . isArray ( dataStore ) ) { return Promise . reject ( "Cannot save property to a storage type 'vector'" ) ; } dataStore = _ . set ( dataStore as Object , path , value ) as T ; return this . save ( storageLocation , dataStore , defaultStorageValue ) . then ( ( dataStoreSaved ) => { return Promise . resolve ( < T > dataStoreSaved ) ; } ) ; } ) ; } param storageLocation public clear ( storageLocation ) { return new Promise < void > ( ( resolve , reject ) => { if ( storageLocation . key ) { this . chromeLocalStorageArea ( ) . remove ( storageLocation . key , ( ) => { const error = this . getLastError ( ) ; ( error ) ? reject ( error . message ) : resolve ( ) ; } ) ; } else { this . chromeLocalStorageArea ( ) . clear ( ( ) => { const error = this . getLastError ( ) ; if ( error ) { reject ( error . message ) ; } else { resolve ( ) ; } } ) ; } } ) ; } param type public getAppUsageDetails ( ) { return new Promise < AppUsageDetails > ( ( resolve ) => { const localStorageArea = this . chromeLocalStorageArea ( ) ; localStorageArea . getBytesInUse ( ( bytesInUse ) => { const appUsage = new AppUsage ( bytesInUse , localStorageArea . QUOTA_BYTES ) ; const megaBytesInUse = appUsage . bytesInUse / ( 1024 * 1024 ) ; const percentUsage = appUsage . bytesInUse / appUsage . quotaBytes * 100 ; const appUsageDetails = new AppUsageDetails ( appUsage , megaBytesInUse , percentUsage ) ; resolve ( appUsageDetails ) ; } ) ; } ) ; } returns { chrome . storage . LocalStorageArea } public chromeLocalStorageArea ( ) { return chrome . storage . local ; } returns { chrome . storage . SyncStorageArea } public chromeSyncStorageArea ( ) { return chrome . storage . sync ; } public getLastError ( ) { return chrome . runtime . lastError ; } }