[MediaWiki-commits] [Gerrit] mediawiki/core[master]: mediawiki.storage: Provide a wrapper for sessionStorage too
jenkins-bot has submitted this change and it was merged. Change subject: mediawiki.storage: Provide a wrapper for sessionStorage too .. mediawiki.storage: Provide a wrapper for sessionStorage too T119146 provides a use-case for using sessionStorage. So far mw.storage was localStorage-specific. With a small modification, we can allow the Storage object to passed to the constructor, which allows us to create a wrapper around sessionStorage (mw.storage.session) with minimal code duplication. Bug: T121646 Change-Id: I73bc82d9fa2359148fe1e50b6535bfa0dbe8bd3e --- M maintenance/jsduck/categories.json M resources/src/mediawiki/mediawiki.storage.js M tests/qunit/suites/resources/mediawiki/mediawiki.storage.test.js 3 files changed, 112 insertions(+), 65 deletions(-) Approvals: BryanDavis: Looks good to me, approved jenkins-bot: Verified diff --git a/maintenance/jsduck/categories.json b/maintenance/jsduck/categories.json index aad85da..9fe5009 100644 --- a/maintenance/jsduck/categories.json +++ b/maintenance/jsduck/categories.json @@ -27,6 +27,7 @@ "mw.notification", "mw.Notification_", "mw.storage", + "mw.storage.session", "mw.user", "mw.util", "mw.plugin.*", diff --git a/resources/src/mediawiki/mediawiki.storage.js b/resources/src/mediawiki/mediawiki.storage.js index a9d17ff..20f8efb 100644 --- a/resources/src/mediawiki/mediawiki.storage.js +++ b/resources/src/mediawiki/mediawiki.storage.js @@ -1,65 +1,91 @@ ( function ( mw ) { 'use strict'; - /** -* Library for storing device specific information. It should be used for storing simple -* strings and is not suitable for storing large chunks of data. -* -* @class mw.storage -* @singleton -*/ - mw.storage = { - - localStorage: ( function () { - // Catch exceptions to avoid fatal in Chrome's "Block data storage" mode - // which throws when accessing the localStorage property itself, as opposed - // to the standard behaviour of throwing on getItem/setItem. (T148998) + // Catch exceptions to avoid fatal in Chrome's "Block data storage" mode + // which throws when accessing the localStorage property itself, as opposed + // to the standard behaviour of throwing on getItem/setItem. (T148998) + var + localStorage = ( function () { try { return window.localStorage; } catch ( e ) {} }() ), - - /** -* Retrieve value from device storage. -* -* @param {string} key Key of item to retrieve -* @return {string|boolean} False when localStorage not available, otherwise string -*/ - get: function ( key ) { + sessionStorage = ( function () { try { - return mw.storage.localStorage.getItem( key ); + return window.sessionStorage; } catch ( e ) {} - return false; - }, + }() ); - /** - * Set a value in device storage. - * - * @param {string} key Key name to store under - * @param {string} value Value to be stored - * @return {boolean} Whether the save succeeded or not - */ - set: function ( key, value ) { - try { - mw.storage.localStorage.setItem( key, value ); - return true; - } catch ( e ) {} - return false; - }, + /** +* A wrapper for an HTML5 Storage interface (`localStorage` or `sessionStorage`) +* that is safe to call on all browsers. +* +* @class mw.SafeStorage +* @private +*/ - /** - * Remove a value from device storage. - * - * @param {string} key Key of item to remove - * @return {boolean} Whether the save succeeded or not - */ - remove: function ( key ) { - try { - mw.storage.localStorage.removeItem( key ); - return true; - } catch ( e ) {} - return false; - } + /** +* @ignore +*
[MediaWiki-commits] [Gerrit] mediawiki/core[master]: mediawiki.storage: provide a wrapper for sessionStorage
Krinkle has uploaded a new change for review. https://gerrit.wikimedia.org/r/323344 Change subject: mediawiki.storage: provide a wrapper for sessionStorage .. mediawiki.storage: provide a wrapper for sessionStorage T119146 provides a use-case for using sessionStorage. But the wrapper that we use to make web storage operations safe to call on all browsers is localStorage-specific. With a small modification, we can allow the storage engine to be specified on object creation, which allows us to create a wrapper around sessionStorage (mw.storage.local) with minimal code duplication. Change-Id: I73bc82d9fa2359148fe1e50b6535bfa0dbe8bd3e --- M maintenance/jsduck/categories.json M resources/src/mediawiki/mediawiki.storage.js M tests/qunit/suites/resources/mediawiki/mediawiki.storage.test.js 3 files changed, 34 insertions(+), 34 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core refs/changes/44/323344/1 diff --git a/maintenance/jsduck/categories.json b/maintenance/jsduck/categories.json index aad85da..278d932 100644 --- a/maintenance/jsduck/categories.json +++ b/maintenance/jsduck/categories.json @@ -26,7 +26,7 @@ "mw.messagePoster.*", "mw.notification", "mw.Notification_", - "mw.storage", + "SafeStorage", "mw.user", "mw.util", "mw.plugin.*", diff --git a/resources/src/mediawiki/mediawiki.storage.js b/resources/src/mediawiki/mediawiki.storage.js index a9d17ff..47655f0 100644 --- a/resources/src/mediawiki/mediawiki.storage.js +++ b/resources/src/mediawiki/mediawiki.storage.js @@ -2,22 +2,19 @@ 'use strict'; /** -* Library for storing device specific information. It should be used for storing simple -* strings and is not suitable for storing large chunks of data. +* A wrapper for an HTML5 Storage interface (`localStorage` or `sessionStorage`) +* that is safe to call on all browsers. * -* @class mw.storage -* @singleton +* @private +* @class SafeStorage +* +* @constructor +* @param {Object} [store] The Storage instance to wrap around */ - mw.storage = { + function SafeStorage( store ) { + var self = this; - localStorage: ( function () { - // Catch exceptions to avoid fatal in Chrome's "Block data storage" mode - // which throws when accessing the localStorage property itself, as opposed - // to the standard behaviour of throwing on getItem/setItem. (T148998) - try { - return window.localStorage; - } catch ( e ) {} - }() ), + self.store = store; /** * Retrieve value from device storage. @@ -25,12 +22,12 @@ * @param {string} key Key of item to retrieve * @return {string|boolean} False when localStorage not available, otherwise string */ - get: function ( key ) { + self.get = function ( key ) { try { - return mw.storage.localStorage.getItem( key ); + return self.store.getItem( key ); } catch ( e ) {} return false; - }, + }; /** * Set a value in device storage. @@ -39,13 +36,13 @@ * @param {string} value Value to be stored * @return {boolean} Whether the save succeeded or not */ - set: function ( key, value ) { + self.set = function ( key, value ) { try { - mw.storage.localStorage.setItem( key, value ); + self.store.setItem( key, value ); return true; } catch ( e ) {} return false; - }, + }; /** * Remove a value from device storage. @@ -53,13 +50,16 @@ * @param {string} key Key of item to remove * @return {boolean} Whether the save succeeded or not */ - remove: function ( key ) { + self.remove = function ( key ) { try { - mw.storage.localStorage.removeItem( key ); + self.store.removeItem( key );