Skip to content

Instantly share code, notes, and snippets.

@itsmunim
Created November 29, 2016 19:35
Show Gist options
  • Save itsmunim/ba43b2f98574540c4ee2b2a33876537c to your computer and use it in GitHub Desktop.
Save itsmunim/ba43b2f98574540c4ee2b2a33876537c to your computer and use it in GitHub Desktop.
A simple angular service wrapper for $localStorage to make things more understandable and manageable.
(function () {
'use strict';
angular.module('storage.service', []);
angular.module('storage.service')
.factory('localStorageService', localStorageService);
function localStorageService($localStorage) {
function retrieve(key) {
return $localStorage.hasOwnProperty(key) ? $localStorage[key] : {};
}
function store(key, obj) {
$localStorage[key] = obj;
}
return {
retrieve: retrieve,
store: store
};
}
})();
// Usage
(function () {
'use strict';
angular.module('your.app', ['storage.service']);
angular.module('your.app')
.controller('MainController', MainCtrl);
MainCtrl.$inject = ['localStorageService', '$timeout'];
function MainCtrl(localStorageSvc, $timeout) {
var vm = this;
vm.publishFailureLogs = []
vm.republish = republish;
function republish(republishLogId) {
var selectedRepublishFailure = vm.republishFailureLogs.filter(function (rfl) { return rfl.id === republishLogId; })[0];
selectedRepublishFailure.buttonState = 'inactive';
_saveOnGoingRepublish(selectedRepublishFailure); // save it in local storage
// for next five minutes the button should be inactive
$timeout(function () {
selectedRepublishFailure.buttonState = 'active';
_saveOnGoingRepublish(selectedRepublishFailure); // save it in local storage
}, 5000);
_republishFailed(selectedRepublishFailure); // network call
}
function _saveOnGoingRepublish(republishLog) {
var timestamp = Date.now().getTime();
// fetch the already saved all states
var rootStateDescriptor = localStorageSvc.retrieve('publishLogState');
// update it with the new info
rootStateDescriptor[republishLog.id] = {
timestamp: timestamp,
log: republishLog
};
// re-save the descriptor to keep it persistent
localStorageSvc.store('publishLogState', rootStateDescriptor);
}
function _init() {
// do all initilization in a similar block
// _fetchAllRepublishFailures is a network call and fetches all the publish failure logs
vm.publishFailureLogs = _syncWithPrevStates(_fetchAllRepublishFailures());
}
function _syncWithPrevStates(publishFailureLogs) {
var prevStates = localStorageSvc.retrieve('publishLogState');
angular.forEach(publishFailureLogs, function (publishFailure) {
// if any state for this id is saved, fetch it and update the button state
if (prevStates.hasOwnProperty(publishFailure.id)) {
var savedState = prevStates[publishFailure.id];
var currentTime = Date.now().getTime();
if (currentTime - savedState.timestamp > 5000) {
// the five minutes wait time is over, set it active irrespective of previous state
savedState.buttonState = 'active';
}
publishFailure.buttonState = savedState.buttonState;
}
});
// prevStates might have updated during the forEach, save it as updated in localstorage
localStorageSvc.store('publishLogState', prevStates);
return publishFailureLogs;
}
_init();
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment