Skip to content

refactor: use localstorage replace chrome storage #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/inject/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import { Settings } from './settings';
import { PubSub } from './libs/pubsub';
import { SettingsStore, ISettings } from './settings.store';

declare const chrome, browser;


@autoinject
export class GitLabTree
{
Expand All @@ -28,7 +25,6 @@ export class GitLabTree
rightElement: HTMLDivElement = document.createElement( 'div' );

lastActive: string = '';
storage = (chrome || browser).storage.local

hashChangeListener: () => void;
expandListener: ( e: MouseEvent ) => void;
Expand Down
55 changes: 16 additions & 39 deletions src/inject/settings.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { PubSub } from "./libs/pubsub";
import { autoinject } from "./libs/container";
import { EVENT_SETTINGS_CHANGED } from "./constants";

declare const chrome, browser;

export interface ISettings
{
'single-change': boolean;
Expand All @@ -17,7 +15,7 @@ export class SettingsStore
{
public onceReady; // constructor will assign a promise here

private storage = (chrome || browser).storage.local
private storage = localStorage;

private defaultValues: ISettings =
{
Expand All @@ -43,50 +41,29 @@ export class SettingsStore

pullSettings(): Promise<any>
{
return new Promise( ( resolve, reject ) =>
return new Promise( ( resolve ) =>
{
const callback = values =>
{
// If we are running this for a first time, there might be
// some values undefined. So we try to merge these that are not
// undefined with a previous state (in a first time it's default values).

const newState = Object.assign( {}, this.state );

Object.keys( values ).forEach( key =>
{
if ( values.hasOwnProperty( key ) )
{
const value = values[ key ];

if ( value !== undefined || value !== null )
{
newState[ key ] = value;
}
}
})

this.setState( newState )
resolve( newState )
}


// Chrome uses callbacks, Firefox uses Promises, so this is cross-browser solution
const keys = Object.keys( this.defaultValues );
const storage = this.storage.get( keys, callback )

if ( storage && storage.then )
{
storage.then( callback )
}
const result = Object.assign( {}, this.state )

Object.keys(this.defaultValues).forEach((key) => {
const val = this.storage.getItem(key)
val && (result[key] = val)
})
Copy link
Contributor

@sumthief sumthief Oct 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you have no side effects you could use reduce here:

const result = Object.keys(this.defaultValues).reduce((prev, key) => {
   const val = this.storage.getItem(key);
   if (val) {
       prev[key] = val;
   }
   return prev;
}, { ...this.state });


resolve(result)
})
}


pushSettings( settings )
{
this.setState( settings )
this.storage.set( this.state )

Object.keys(settings).forEach((key) => {
const val = settings[key]
val && this.storage.setItem(key, val)
})

this.pubsub.publish( EVENT_SETTINGS_CHANGED, Object.assign( {}, this.state ) );
}

Expand Down
1 change: 0 additions & 1 deletion src/inject/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export enum EFileSort { AZName, ZAName, AZExt, ZAExt };
export class Settings
{
state = {} as any;
storage = (chrome || browser).storage.local

constructor( private views: Views, private pubsub: PubSub, private settingsStore: SettingsStore )
{
Expand Down