Skip to content

Commit

Permalink
Performance improvement by limiting calls to localStorage.getItem().
Browse files Browse the repository at this point in the history
  • Loading branch information
Francisc authored and 0x-r4bbit committed May 1, 2014
1 parent 97e2241 commit 24a0798
Showing 1 changed file with 38 additions and 30 deletions.
68 changes: 38 additions & 30 deletions src/service/storage-local.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,44 @@ angular.module('pascalprecht.translate')
.factory('$translateLocalStorage', ['$window', '$translateCookieStorage', function ($window, $translateCookieStorage) {

// Setup adapter
var localStorageAdapter = {
/**
* @ngdoc function
* @name pascalprecht.translate.$translateLocalStorage#get
* @methodOf pascalprecht.translate.$translateLocalStorage
*
* @description
* Returns an item from localStorage by given name.
*
* @param {string} name Item name
* @return {string} Value of item name
*/
get: function (name) {
return $window.localStorage.getItem(name);
},
/**
* @ngdoc function
* @name pascalprecht.translate.$translateLocalStorage#set
* @methodOf pascalprecht.translate.$translateLocalStorage
*
* @description
* Sets an item in localStorage by given name.
*
* @param {string} name Item name
* @param {string} value Item value
*/
set: function (name, value) {
$window.localStorage.setItem(name, value);
}
};
var localStorageAdapter = (function(){
var langKey;
return {
/**
* @ngdoc function
* @name pascalprecht.translate.$translateLocalStorage#get
* @methodOf pascalprecht.translate.$translateLocalStorage
*
* @description
* Returns an item from localStorage by given name.
*
* @param {string} name Item name
* @return {string} Value of item name
*/
get: function (name) {
if(!langKey) {
langKey = $window.localStorage.getItem(name);
}

return langKey;
},
/**
* @ngdoc function
* @name pascalprecht.translate.$translateLocalStorage#set
* @methodOf pascalprecht.translate.$translateLocalStorage
*
* @description
* Sets an item in localStorage by given name.
*
* @param {string} name Item name
* @param {string} value Item value
*/
set: function (name, value) {
langKey=value;
$window.localStorage.setItem(name, value);
}
};
}());

var $translateLocalStorage = ('localStorage' in $window && $window.localStorage !== null) ?
localStorageAdapter : $translateCookieStorage;
Expand Down

0 comments on commit 24a0798

Please sign in to comment.