2
0
Fork 0

Added clearCache function

This commit is contained in:
Willem 2016-01-24 19:44:00 +01:00
parent 191ad1e4ee
commit 4fbf705ae5
4 changed files with 75 additions and 15 deletions

View file

@ -106,6 +106,12 @@ A javascript library providing server defined loading of assets for a single pag
* cache.css = The cache backend for for css, null is auto select,false is disable. (default: null) * cache.css = The cache backend for for css, null is auto select,false is disable. (default: null)
* cache.cssData = The cache backend for for cssData, null is auto select,false is disable. (default: null) * cache.cssData = The cache backend for for cssData, null is auto select,false is disable. (default: null)
## Functions
The functions iin FFSpaLoader.*;
* clearServerUrl(cb) = Clears the cached server url so after reload user get promted again.
* clearCache(cb) = Clears the cached values so after reload all assets get refetched.
## Factory ## Factory
@ -196,6 +202,7 @@ Add unit tests for any new or changed functionality. Lint and test your code.
* Changed websql option openDatabase to returning open function. * Changed websql option openDatabase to returning open function.
* Fixed sqlitePlugin open function was typo on openDatabase. * Fixed sqlitePlugin open function was typo on openDatabase.
* Added websql table option. (defaults to 'cache_store') * Added websql table option. (defaults to 'cache_store')
* Added clearCache function.
### 0.1.0 ### 0.1.0
* Moved options.server.question to options.question. * Moved options.server.question to options.question.

View file

@ -431,7 +431,7 @@
}); });
}; };
var cleanupCache = function (resources, cb) { var cleanupCache = function (resources, deleteAll, cb) {
var typedKeys = {}; var typedKeys = {};
resources.forEach(function (r) { resources.forEach(function (r) {
var typeKey = typedKeys[r.type]; var typeKey = typedKeys[r.type];
@ -456,11 +456,11 @@
return cb(err); return cb(err);
} }
if (value === null) { if (value === null) {
return cb(null); return cb(null); // meta has no keys
} }
var diff = []; var diff = [];
for (var i=0;i<value.length;i++) { for (var i=0;i<value.length;i++) {
if (typeKey.keys.indexOf(value[i]) === -1) { if (deleteAll || typeKey.keys.indexOf(value[i]) === -1) {
diff.push(value[i]); diff.push(value[i]);
} }
} }
@ -621,17 +621,14 @@
} }
return; return;
} }
var resources = []; var resources = JSON.parse(httpRequest.responseText).data.resources;
JSON.parse(httpRequest.responseText).data.resources.forEach(function (r) {
resources.push(r);
});
utilDebug('startLoader resources '+resources.length); utilDebug('startLoader resources '+resources.length);
if (cacheHasService('meta')) { if (cacheHasService('meta')) {
cacheSetValue('meta','server_resources',resources, function (err) { cacheSetValue('meta','server_resources',resources, function (err) {
if (err !== null) { return cb(err); } if (err !== null) { return cb(err); }
utilRunStack('loadResources', resources, loadResource , function (err) { utilRunStack('loadResources', resources, loadResource , function (err) {
if (err !== null) { return cb(err); } if (err !== null) { return cb(err); }
cleanupCache(resources,cb); // only clean when fetched + cached cleanupCache(resources,false,cb); // only clean when fetched + cached
}); });
}); });
} else { } else {
@ -894,6 +891,40 @@
} }
}; };
/**
* Clears the cache.
*
* @param {function} cb Optional Error first callback gets called when cache is cleared.
*/
var clearCache = function(cb2) {
if (cb2 === undefined) {
cb2 = function() {};
}
var cb = function(err) {
setTimeout(function() {cb2(err);}); // next tick to flush transactions.
};
if (cacheHasService('meta')) {
cacheGetValue('meta','server_resources',function(err, value) {
if (err !== null) {
return cb(err);
}
if (value === null) {
return cb(new Error('No cache value')); // TODO: check this or cb(null) or do fetch ?
}
cleanupCache(value,true,cb);
});
} else {
utilHttpFetch(options.server.url + options.server.assets,function(err, httpRequest) {
if (err !== null) {
return cb(err);
}
var resources = JSON.parse(httpRequest.responseText).data.resources;
cleanupCache(resources,false,cb);
});
}
};
/** /**
* Boots angular modules if enabled. * Boots angular modules if enabled.
* *
@ -959,6 +990,7 @@
options: options, options: options,
factory: factory, factory: factory,
start: start, start: start,
clearServerUrl: clearServerUrl clearServerUrl: clearServerUrl,
clearCache: clearCache
}; };
}); });

View file

@ -1,7 +1,9 @@
var tpl = '<div><h2>Foo</h2><p>Welcome to the foo.</p></div>'; var tpl = '<div><h2>Foo</h2><p>Welcome to the foo.</p></div>';
tpl += '<input type=\"button\" class=\"btn btn-default\" ng-click=\"doReboot()\" value=\"Reboot\"></input>'; tpl += '<input type=\"button\" class=\"btn btn-default\" ng-click=\"doReload()\" value=\"Reload\"></input>';
tpl += '<input type=\"button\" class=\"btn btn-default\" ng-click=\"doClearServerUrl()\" value=\"Clear Server\"></input>'; tpl += '<input type=\"button\" class=\"btn btn-default\" ng-click=\"doClearServerUrl()\" value=\"Clear Server Url\"></input>';
tpl += '<input type=\"button\" class=\"btn btn-default\" ng-click=\"doClearCache()\" value=\"Clear Cache\"></input>';
tpl += '<p>{{message}}</p>';
pageRouteInit.push(function ($routeProvider, $locationProvider) { pageRouteInit.push(function ($routeProvider, $locationProvider) {
$routeProvider.when('/example-ui/foo', { $routeProvider.when('/example-ui/foo', {
@ -11,17 +13,30 @@ pageRouteInit.push(function ($routeProvider, $locationProvider) {
}); });
function PageFoo($scope) { function PageFoo($scope) {
$scope.doReboot = function ( path ) { $scope.message = '';
$scope.doReload = function () {
window.location.reload(true); window.location.reload(true);
}; };
$scope.doClearServerUrl = function ( path ) { $scope.doClearServerUrl = function () {
FFSpaLoader.clearServerUrl(function(err) { FFSpaLoader.clearServerUrl(function(err) {
if (err) { if (err) {
window.alert(err); $scope.message = 'Error: '+err;
} else { } else {
window.location.reload(true); $scope.message = 'Cleared server url';
} }
$scope.$apply();
});
};
$scope.doClearCache = function () {
FFSpaLoader.clearCache(function(err) {
if (err) {
$scope.message = 'Error: '+err;
} else {
$scope.message = 'Cleared cache';
}
$scope.$apply();
}); });
}; };
} }

View file

@ -22,5 +22,11 @@ describe('Check module exports', function() {
it('FFSpaLoader.clearServerUrl should be function', function() { it('FFSpaLoader.clearServerUrl should be function', function() {
assert.equal(true, typeof FFSpaLoader.clearServerUrl === 'function'); assert.equal(true, typeof FFSpaLoader.clearServerUrl === 'function');
}); });
it('FFSpaLoader.clearCache should be defined', function() {
assert.equal(false, FFSpaLoader.clearCache === undefined);
});
it('FFSpaLoader.clearCache should be function', function() {
assert.equal(true, typeof FFSpaLoader.clearCache === 'function');
});
}); });