mirror of
https://github.com/futurepress/epub.js.git
synced 2025-10-03 14:59:18 +02:00
added queue for loading and offline storage of parsed info
This commit is contained in:
parent
ca3e2c24c2
commit
d90fd14ad2
12 changed files with 10017 additions and 47 deletions
|
@ -16,6 +16,7 @@ FP.storage = function(){
|
|||
|
||||
//-- Handle load errors
|
||||
this._store.failed = _error;
|
||||
|
||||
}
|
||||
|
||||
function get(path, callback) {
|
||||
|
@ -26,6 +27,14 @@ FP.storage = function(){
|
|||
return this._store.preload(path, callback);
|
||||
}
|
||||
|
||||
function batch(group, callback) {
|
||||
return this._store.batch(group, callback);
|
||||
}
|
||||
|
||||
function replace(path, content, callback) {
|
||||
//return this._store.batch(group, callback);
|
||||
}
|
||||
|
||||
function _error(err){
|
||||
console.log("error", err);
|
||||
}
|
||||
|
@ -33,15 +42,18 @@ FP.storage = function(){
|
|||
return {
|
||||
"get" : get,
|
||||
"preload" : preload,
|
||||
"batch" : batch,
|
||||
"storageMethod": storageMethod
|
||||
}
|
||||
|
||||
}();
|
||||
|
||||
|
||||
/*
|
||||
FP.storage.ram = function() {
|
||||
var _store = {},
|
||||
_blobs = {};
|
||||
_blobs = {},
|
||||
_queue = new FP.Queue("loader_ram.js", 3);
|
||||
|
||||
|
||||
//-- TODO: this should be prototypes?
|
||||
|
||||
|
@ -53,6 +65,10 @@ FP.storage.ram = function() {
|
|||
request(path);
|
||||
}
|
||||
}
|
||||
|
||||
function batch(group, callback){
|
||||
_queue.addGroup(group)
|
||||
}
|
||||
|
||||
//-- Fetches url
|
||||
function get(path, callback) {
|
||||
|
@ -148,6 +164,7 @@ FP.storage.ram = function() {
|
|||
"preload" : preload
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
FP.storage.none = function() {
|
||||
var _store = {};
|
||||
|
@ -221,4 +238,133 @@ FP.storage.none = function() {
|
|||
"get" : get,
|
||||
"preload" : preload
|
||||
}
|
||||
}
|
||||
|
||||
FP.storage.ram = function() {
|
||||
var _store = {},
|
||||
_blobs = {},
|
||||
_queue = new FP.Queue(loader, 6);
|
||||
//-- max of 6 concurrent requests: http://www.browserscope.org/?category=network
|
||||
|
||||
|
||||
function loader(msg, callback){
|
||||
var e = {"data":null},
|
||||
fromCache = check(msg);
|
||||
|
||||
if(fromCache){
|
||||
e.data = fromCache;
|
||||
callback(e);
|
||||
}else{
|
||||
request(msg, function(file){
|
||||
e.data = file;
|
||||
callback(e);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function preload(path) {
|
||||
var fromCache = check(path);
|
||||
|
||||
if(!fromCache){
|
||||
_queue.add(path);
|
||||
}
|
||||
}
|
||||
|
||||
function batch(group, callback){
|
||||
_queue.addGroup(group, callback);
|
||||
}
|
||||
|
||||
//-- Fetches url
|
||||
function get(path, callback) {
|
||||
var fromCache = check(path),
|
||||
url;
|
||||
|
||||
if(fromCache){
|
||||
url = getURL(path, fromCache);
|
||||
if(typeof(callback) != "undefined"){
|
||||
callback(url);
|
||||
}
|
||||
}else{
|
||||
_queue.add(path, function(file){
|
||||
url = getURL(path, file);
|
||||
if(typeof(callback) != "undefined"){
|
||||
callback(url);
|
||||
}
|
||||
}, true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function check(path) {
|
||||
var file = _store[path];
|
||||
|
||||
if(typeof(file) != "undefined"){
|
||||
return file;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function request(path, callback) {
|
||||
var xhr = new FP.core.loadFile(path);
|
||||
|
||||
xhr.succeeded = function(file) {
|
||||
//console.log("file", file)
|
||||
cache(path, file);
|
||||
if(typeof(callback) != "undefined"){
|
||||
callback(file);
|
||||
}
|
||||
}
|
||||
|
||||
xhr.failed = _error;
|
||||
|
||||
xhr.start();
|
||||
}
|
||||
|
||||
function cache(path, file) {
|
||||
if(_store[path]) return;
|
||||
|
||||
_store[path] = file;
|
||||
}
|
||||
|
||||
function getURL(path, file){
|
||||
var url;
|
||||
|
||||
if(typeof(_blobs[path]) != "undefined"){
|
||||
return _blobs[path];
|
||||
}
|
||||
|
||||
url = this._URL.createObjectURL(file);
|
||||
|
||||
//-- need to revokeObjectURL previous urls, but only when cleaning cache
|
||||
// this.createdURLs.forEach(function(url){
|
||||
// this._URL.revokeObjectURL(url);
|
||||
// });
|
||||
|
||||
_blobs[path] = url;
|
||||
|
||||
return url;
|
||||
}
|
||||
|
||||
// this.succeeded = function(){
|
||||
// console.log("loaded");
|
||||
// }
|
||||
//
|
||||
// this.failed = function(){
|
||||
// console.log("loaded");
|
||||
// }
|
||||
|
||||
function _error(err){
|
||||
if(typeof(this.failed) == "undefined"){
|
||||
console.log("Error: ", err);
|
||||
}else{
|
||||
this.failed(err);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
"get" : get,
|
||||
"preload" : preload,
|
||||
"batch" : batch
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue