Skip to content

Commit

Permalink
Disable LocalStorage
Browse files Browse the repository at this point in the history
See #80
  • Loading branch information
eliemichel committed Oct 1, 2015
1 parent bc35384 commit 4ae9d4a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
37 changes: 33 additions & 4 deletions js/StationStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ var LocalStationStorage = function() {
* Load station list from local storage
*/
api.load = function() {
return Promise.reject('Local storage disabled (See issue #80)')

return new Promise(function(resolve, reject) {
if (!localStorage) {
return reject("Local storage not available");
Expand All @@ -204,7 +206,19 @@ var LocalStationStorage = function() {
return reject("Local storage data is considered as obsolated (Config.localStationStorageTimeout = " + Config.localStationStorageTimeout + ")");
}
api.stations = JSON.parse(localStorage.getItem('stations'));
api.starredStations = JSON.parse(localStorage.getItem('starredStations'));
var starredStationsIds = JSON.parse(localStorage.getItem('starredStationsIds'));

api.starredStations = starredStationsIds.map(function(id) {
var matches = $.grep(api.stations, function(station) {
return station.number == id;
});
if (matches.length > 0) {
return matches[0];
} else {
reject("Local storage not consistent (Starred Station not found wth id " + id+ ")");
}
})

if (api.starredStations == null) {
api.starredStations = [];
}
Expand All @@ -217,8 +231,20 @@ var LocalStationStorage = function() {
* Serialize station list and save it back to local storage
*/
api.save = function() {
// Remove not cachable information
api.stations.forEach(function(station) {
station.availableStands = -1;
station.availableBikes = -1;
});

// Save only IDs (avoid data replication on persistent storage)
var starredStationsIds = api.starredStations.map(function(station) {
return station.number;
});


localStorage.setItem('stations', JSON.stringify(api.stations));
localStorage.setItem('starredStations', JSON.stringify(api.starredStations));
localStorage.setItem('starredStationsIds', JSON.stringify(starredStationsIds));
localStorage.setItem('lastStationsUpdate', Date.now());

return Promise.resolve();
Expand Down Expand Up @@ -277,9 +303,12 @@ var MockStationStorage = function() {
}
];

//api.starredStations = api.stations;
api.starredStations = api.stations;

api.load = function() {
return Promise.reject("Mock stations disabled");
};

api.stations = null; // Disables mock storage

return api;
};
Expand Down
3 changes: 0 additions & 3 deletions js/Views.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ var Views = (function() {
stationStorage.load()
.then(function() { // on success
Log.info("StationStorage loaded successfully");
})
.catch(function(err) { // on error
Log.error("Unable to load station storage: " + err);
});

// Main templates
Expand Down

0 comments on commit 4ae9d4a

Please sign in to comment.