Last active
February 17, 2016 11:03
-
-
Save kxc0re/d6fd510a744aa04cee06 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @name DataService | |
* @author kxc0re | |
* | |
*/ | |
(function () { | |
'use strict'; | |
angular | |
.module('app.core') | |
.factory('DataService', dataService); | |
dataService.$inject = ['$localStorage', '$log', '$http', '$q']; | |
/* @ngInject */ | |
function dataService($localStorage, $log, $http, $q) { | |
var dataPromise; // local data "cache" | |
var infoPromise;// local info "cache" | |
var service = { | |
init:init, | |
getAllCategories: getAllCategories, | |
getCategory:getCategory, | |
getAllElements:getAllElements, | |
getElement:getElement, | |
getAllImages:getAllImages, | |
getImage:getImage | |
}; | |
return service; | |
//////////////// | |
/** | |
* INIT | |
* initializes stuff | |
* sets default values | |
*/ | |
function init() { | |
$log.info('INIT'); | |
$localStorage.$default( | |
{ | |
info: { | |
path: '/data/', | |
version: -1 | |
}, | |
data: { | |
categories: [] | |
} | |
} | |
); | |
$log.debug('default loaded'); | |
//TODO: initialize stuff | |
update(); //update if new version is available | |
$log.info('INIT END'); | |
} | |
/** | |
* returns local dataPromise | |
* @returns {promise} | |
*/ | |
function getDataPromise() { | |
dataPromise = dataPromise || fetchLocalData(); | |
$log.info('getting DataPromise'); | |
$log.debug(dataPromise); | |
return dataPromise; | |
} | |
/** | |
* returns local infoPromise | |
* @returns {promise} | |
*/ | |
function getInfoPromise() { | |
infoPromise = infoPromise || fetchLocalInfo(); | |
$log.info('getting InfoPromise'); | |
$log.debug(infoPromise); | |
return infoPromise; | |
} | |
/** | |
* fetching local data from LocalStorage | |
* @returns {promise} | |
*/ | |
function fetchLocalData() { | |
var data = $localStorage.data; | |
$log.info('fetching localData'); | |
$log.debug(data); | |
return $q.resolve(data); | |
} | |
/** | |
* fetching local info from localStorage | |
* @returns {promise} | |
*/ | |
function fetchLocalInfo() { | |
var info = $localStorage.info; | |
$log.info('fetching localInfo'); | |
$log.debug(info); | |
return $q.resolve(info); | |
} | |
/** | |
* getting Path from local infoPromise | |
* @returns {*} | |
*/ | |
function getPath() { | |
return getInfoPromise() | |
.then(function (result) { | |
$log.info('getting Path'); | |
$log.debug(result.path); | |
return result.path; | |
}); | |
} | |
/** | |
* getting Version from local infoPromise | |
* @returns {*} | |
*/ | |
function getVersion() { | |
return getInfoPromise() | |
.then(function (result) { | |
$log.info('getting Version'); | |
$log.debug(result.version); | |
return result.version; | |
}); | |
} | |
/** | |
* "deletes" dataPromise | |
*/ | |
function emptyDataPromise() { | |
dataPromise = null; | |
$log.info('emptying DataPromise'); | |
$log.debug(dataPromise); | |
} | |
/** | |
* "deletes" infoPromise | |
*/ | |
function emptyInfoPromise() { | |
infoPromise = null; | |
$log.info('emptying InfoPromise'); | |
$log.debug(infoPromise); | |
} | |
/** | |
* - fetching remote Data from the path saved in infoPromise | |
* - saves it to localStorage | |
* - "deletes" dataPromise | |
* @returns {promise} | |
*/ | |
function fetchRemoteData() { | |
return getPath() | |
.then(function (path) { | |
return $http.get(path + 'data.json') | |
.then(function (result) { | |
$localStorage.data = result.data; | |
$log.info('fetching remoteData'); | |
$log.debug(result); | |
emptyDataPromise(); | |
return result; | |
}); | |
}); | |
} | |
/** | |
* fetching remote info from the path in infoPromise | |
* @returns {promise} | |
*/ | |
function fetchRemoteInfo() { | |
return getPath() | |
.then(function (path) { | |
$log.info('fetching RemoteInfo'); | |
$log.debug('from', path); | |
return $http.get(path + 'info.json'); | |
}); | |
} | |
/** | |
* Checks for new version | |
* resolves if there is a new version, rejects if not | |
* @returns {promise} | |
*/ | |
function isOutdated() { | |
return fetchRemoteInfo() | |
.then(function (remoteInfo) { | |
$log.info('remoteInfo', remoteInfo); | |
return getVersion() | |
.then(function (localVersion) { | |
$log.info('isOutdated?', remoteInfo.version > localVersion); | |
$log.debug('remote', remoteInfo.version, 'local', localVersion); | |
return remoteInfo.version > localVersion ? | |
$q.resolve(remoteInfo) : $q.reject(remoteInfo); | |
}); | |
}); | |
} | |
/** | |
* Update Function | |
* - checking for new version | |
* - fetching data from remote | |
* - saving remote info to local info | |
* - emptying local promises to force reload | |
*/ | |
function update() { | |
$log.info('UPDATE'); | |
isOutdated() | |
.then(function (info) { | |
return fetchRemoteData() | |
.then(function (remote) { | |
$log.info('update chain resolved'); | |
$log.debug('info:',info); | |
$localStorage.info = info; | |
emptyInfoPromise(); | |
return remote; | |
}); | |
}); | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment