Skip to content

Commit

Permalink
Merge pull request #20 from Rise-Vision/fix/ls-out-quota-issue
Browse files Browse the repository at this point in the history
Implemented ability to tell which storage, local or session to be used
  • Loading branch information
rodrigopavezi authored Jul 27, 2017
2 parents e8fca76 + c818600 commit 875ca6c
Show file tree
Hide file tree
Showing 10 changed files with 386 additions and 31 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rise-data",
"version": "1.3.7",
"version": "1.3.8",
"authors": [
"Rise Vision"
],
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rise-data",
"version": "1.3.7",
"version": "1.3.8",
"description": "Web component for storing data",
"scripts": {
"test": "gulp test",
Expand Down Expand Up @@ -28,7 +28,7 @@
"devDependencies": {
"colors": "~1.1.0",
"del": "~1.1.1",
"eslint": "^3.8.1",
"eslint": "^3.19.0",
"eslint-config-idiomatic": "^2.1.0",
"eslint-plugin-html": "^1.6.0",
"gulp": "~3.8.10",
Expand All @@ -37,7 +37,7 @@
"gulp-eslint": "^3.0.1",
"gulp-html-replace": "~1.6.1",
"run-sequence": "~1.1.0",
"web-component-tester": "*",
"web-component-tester": "^6.0.0",
"widget-tester": "git://github.com/Rise-Vision/widget-tester.git"
}
}
73 changes: 50 additions & 23 deletions rise-data.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
</dom-module>

<!-- build:version -->
<script>var dataVersion = "1.3.7";</script>
<script>var dataVersion = "1.3.8";</script>
<!-- endbuild -->

<script>
Expand All @@ -43,14 +43,6 @@

var BQ_TABLE_NAME = "component_data_events";

function supportsLocalStorage() {
try {
return "localStorage" in window && window.localStorage !== null;
} catch ( e ) {
return false;
}
}

Polymer( {
is: "rise-data",

Expand All @@ -70,8 +62,15 @@
usage: {
type: String,
value: ""
}
},

/**
* The optional browser local storage type: None, Session or Local
*/
storageType: {
type: String,
value: "none"
}
},

_isCacheRunning: false,
Expand Down Expand Up @@ -100,6 +99,14 @@
return usage === "standalone" || usage === "widget";
},

_isSessionStorage: function() {
return this.storageType === "session"
},

_isLocalStorage: function() {
return this.storageType === "local"
},

_isRiseCacheSchemeEnabled: function() {
try {
if ( top.enableRiseCacheScheme ) {
Expand Down Expand Up @@ -131,16 +138,24 @@
return body;
},

_getCachedDataFromLocalStorage: function( key, cb ) {
_getCachedDataFromStorage: function( key, cb ) {
var data = null;

if ( supportsLocalStorage() ) {
// retrieve cached data and parse back
data = JSON.parse( localStorage.getItem( key ), this._dateReviver );
cb( data );
} else {
cb( data );
if ( this._isLocalStorage() ) {
try {
data = JSON.parse( localStorage.getItem( key ), this._dateReviver );
} catch ( e ) {
console.warn( e ); // eslint-disable-line no-console
}
} else if ( this._isSessionStorage() ) {
try {
data = JSON.parse( sessionStorage.getItem( key ), this._dateReviver );
} catch ( e ) {
console.warn( e ); // eslint-disable-line no-console
}
}

cb( data );
},

_getParamsForLoggingError: function( message ) {
Expand All @@ -165,7 +180,7 @@
if ( this.$.cache.method === "GET" && this._callback && typeof this._callback === "function" ) {

// fallback to get data from local storage
this._getCachedDataFromLocalStorage( this._key, this._callback );
this._getCachedDataFromStorage( this._key, this._callback );

// don't continue with logging if 404
if ( resp.request.status === 404 ) {
Expand Down Expand Up @@ -389,11 +404,17 @@
this._save( key, data );
}
}
if ( supportsLocalStorage() ) {
if ( this._isLocalStorage() ) {
try {
localStorage.setItem( key, JSON.stringify( data ) );
} catch ( e ) {
// TODO: use logger to log failure?
console.warn( e ); // eslint-disable-line no-console
}
} else if ( this._isSessionStorage() ) {
try {
sessionStorage.setItem( key, JSON.stringify( data ) );
} catch ( e ) {
console.warn( e ); // eslint-disable-line no-console
}
}
}
Expand Down Expand Up @@ -429,7 +450,7 @@
if ( this._pingReceived && key && cb && typeof cb === "function" ) {
if ( !this._isCacheRunning ) {

this._getCachedDataFromLocalStorage( key, cb );
this._getCachedDataFromStorage( key, cb );

} else {
if ( this.endpoint ) {
Expand All @@ -454,11 +475,17 @@
}
}

if ( supportsLocalStorage() ) {
if ( this._isLocalStorage() ) {
try {
localStorage.removeItem( key );
} catch ( ex ) {
// TODO: use logger to log failure?
console.warn( ex ); // eslint-disable-line no-console
}
} else if ( this._isSessionStorage() ) {
try {
sessionStorage.removeItem( key );
} catch ( ex ) {
console.warn( ex ); // eslint-disable-line no-console
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/rise-data-cache.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</head>
<body>

<rise-data id="request"></rise-data>
<rise-data id="request" storage-type="local"></rise-data>

<script src="../data/sheet.js"></script>
<script src="../../node_modules/widget-tester/mocks/localStorage-mock.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion test/unit/rise-data-local-storage-rc-running.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</head>
<body>

<rise-data id="request"></rise-data>
<rise-data id="request" storage-type="local"></rise-data>

<script src="../data/sheet.js"></script>
<script src="../../node_modules/widget-tester/mocks/localStorage-mock.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion test/unit/rise-data-local-storage.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</head>
<body>

<rise-data id="request"></rise-data>
<rise-data id="request" storage-type="local"></rise-data>

<script src="../data/sheet.js"></script>
<script src="../../node_modules/widget-tester/mocks/localStorage-mock.js"></script>
Expand Down
106 changes: 106 additions & 0 deletions test/unit/rise-data-none-storage-rc-running.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
<title>rise-data</title>

<script src="../../bower_components/webcomponentsjs/webcomponents.min.js"></script>
<script src="../../bower_components/web-component-tester/browser.js"></script>

<link rel="import" href="../../rise-data.html">
</head>
<body>

<rise-data id="request"></rise-data>

<script src="../data/sheet.js"></script>
<script src="../../node_modules/widget-tester/mocks/localStorage-mock.js"></script>
<script src="../../node_modules/widget-tester/mocks/sessionStorage-mock.js"></script>

<script>
/* global sinon, suite, test, assert, suiteSetup, suiteTeardown, setup, teardown, sheetKey, sheetData */

var dataRequest = document.querySelector( "#request" );

// mock logger getting display id and force RC running
sinon.stub( dataRequest.$.logger.$.displayId, "generateRequest", function() {
dataRequest.$.logger._onDisplayIdResponse( null, { response: { displayId: "abc123" } } );
} );

// mock ping and force RC running
sinon.stub( dataRequest.$.ping, "generateRequest", function() {
dataRequest._handlePingResponse( null, { response: { name: "rise-cache-v2", version: "0.0.0" } } );
} );

suite( "sessionStorage when RC is running", function() {
suiteSetup( function() {
dataRequest.endpoint = "spreadsheets"
} );

suiteTeardown( function() {
dataRequest.$.logger.$.displayId.generateRequest.restore();
dataRequest.$.ping.generateRequest.restore();
} );

suite( "RC running", function() {

test( "should not save to session or local storage even though it saves to RC", function() {

var sessionStorageSetItemStub = sinon.stub( sessionStorage, "setItem" ),
localStorageSetItemStub = sinon.stub( localStorage, "setItem" );

dataRequest.saveItem( sheetKey, { results: sheetData.values } );

assert.isFalse( sessionStorageSetItemStub.called );
assert.isFalse( localStorageSetItemStub.called );

} );
} );

suite( "RC fail to get", function() {

setup( function() {
// mock ping and force RC running
sinon.stub( dataRequest.$.cache, "generateRequest", function() {
var resp = { request: { status: 404 } };

dataRequest._handleCacheError( null, resp );
} );

sessionStorage.setItem( sheetKey, JSON.stringify( { results: sheetData.values } ) );
localStorage.setItem( sheetKey, JSON.stringify( { results: sheetData.values } ) );
} );

teardown( function() {
window.sessionStorageError = false;
sessionStorage.removeItem( sheetKey );

window.localStorageError = false;
localStorage.removeItem( sheetKey );
dataRequest.$.cache.generateRequest.restore();
} );

test( "should not get data from session or local storage if it fails to get from RC", function() {

var sessionStorageGetItemStub = sinon.stub( sessionStorage, "getItem", function() {
return JSON.stringify( { results: sheetData.values } );
} ),
localStorageGetItemStub = sinon.stub( localStorage, "getItem", function() {
return JSON.stringify( { results: sheetData.values } );
} );

dataRequest.getItem( sheetKey, function() {} );

assert.isFalse( sessionStorageGetItemStub.called );
assert.isFalse( localStorageGetItemStub.called );

} );
} );



} );
</script>
</body>
</html>
Loading

0 comments on commit 875ca6c

Please sign in to comment.