Skip to content

Instantly share code, notes, and snippets.

@blowery
Created December 19, 2019 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blowery/47b5dd56a6520bd1c84b4536144fe881 to your computer and use it in GitHub Desktop.
Save blowery/47b5dd56a6520bd1c84b4536144fe881 to your computer and use it in GitHub Desktop.
diff --git a/client/lib/browser-storage/index.ts b/client/lib/browser-storage/index.ts
index 61dfb87f03..9b2eb97305 100644
--- a/client/lib/browser-storage/index.ts
+++ b/client/lib/browser-storage/index.ts
@@ -17,7 +17,7 @@ const STORE_NAME = 'calypso_store';
const SANITY_TEST_KEY = 'browser-storage-sanity-test';
-const getDB = once( () => {
+const getDB = () => {
const request = indexedDB.open( DB_NAME, DB_VERSION );
return new Promise< IDBDatabase >( ( resolve, reject ) => {
try {
@@ -37,7 +37,7 @@ const getDB = once( () => {
reject( error );
}
} );
-} );
+};
const supportsIDB = once( async () => {
if ( typeof window === 'undefined' || ! window.indexedDB ) {
@@ -62,8 +62,8 @@ function idbGet< T >( key: string ): Promise< T | undefined > {
const transaction = db.transaction( STORE_NAME, 'readonly' );
const get = transaction.objectStore( STORE_NAME ).get( key );
- const success = () => resolve( get.result );
- const error = () => reject( transaction.error );
+ const success = () => { db.close(); resolve( get.result ); }
+ const error = () => { db.close(); reject( transaction.error ); }
transaction.oncomplete = success;
transaction.onabort = error;
@@ -80,8 +80,8 @@ function idbSet< T >( key: string, value: T ): Promise< void > {
const transaction = db.transaction( STORE_NAME, 'readwrite' );
transaction.objectStore( STORE_NAME ).put( value, key );
- const success = () => resolve();
- const error = () => reject( transaction.error );
+ const success = () => { db.close(); resolve(); }
+ const error = () => { db.close(); reject( transaction.error ); }
transaction.oncomplete = success;
transaction.onabort = error;
diff --git a/client/state/initial-state.js b/client/state/initial-state.js
index 3e398d0489..1a2e4abffd 100644
--- a/client/state/initial-state.js
+++ b/client/state/initial-state.js
@@ -165,7 +165,7 @@ function persistentStoreState( reduxStateKey, storageKey, state, _timestamp ) {
if ( storageKey !== 'root' ) {
reduxStateKey += ':' + storageKey;
}
-
+ console.log( 'saving %s: %d bytes', reduxStateKey, JSON.stringify( state ).length );
return setStoredItem( reduxStateKey, Object.assign( {}, state, { _timestamp } ) );
}
@@ -176,37 +176,41 @@ export function persistOnChange( reduxStore ) {
let prevState = null;
- const throttledSaveState = throttle(
- function() {
- const state = reduxStore.getState();
- if ( state === prevState ) {
- return;
- }
+ const saveState = ( force = false ) => {
+ const state = reduxStore.getState();
+ if ( !force && state === prevState ) {
+ return;
+ }
+
+ const reduxStateKey = getReduxStateKey();
+ if ( ! isValidReduxKeyAndState( reduxStateKey, state ) ) {
+ return;
+ }
- const reduxStateKey = getReduxStateKey();
- if ( ! isValidReduxKeyAndState( reduxStateKey, state ) ) {
- return;
- }
+ prevState = state;
- prevState = state;
+ const serializedState = serialize( state, reduxStore.getCurrentReducer() );
+ const _timestamp = Date.now();
- const serializedState = serialize( state, reduxStore.getCurrentReducer() );
- const _timestamp = Date.now();
+ console.log( 'saving state' );
+ const storeTasks = map( serializedState.get(), ( data, storageKey ) =>
+ persistentStoreState( reduxStateKey, storageKey, data, _timestamp )
+ );
- const storeTasks = map( serializedState.get(), ( data, storageKey ) =>
- persistentStoreState( reduxStateKey, storageKey, data, _timestamp )
- );
+ Promise.all( storeTasks ).catch( setError =>
+ debug( 'failed to set redux-store state', setError )
+ );
+ };
- Promise.all( storeTasks ).catch( setError =>
- debug( 'failed to set redux-store state', setError )
- );
- },
+ const throttledSaveState = throttle(
+ saveState,
SERIALIZE_THROTTLE,
{ leading: false, trailing: true }
);
if ( typeof window !== 'undefined' ) {
window.addEventListener( 'beforeunload', throttledSaveState.flush );
+ setInterval( () => saveState( true ), 10000 );
}
reduxStore.subscribe( throttledSaveState );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment