mirror of
https://github.com/futurepress/epub.js.git
synced 2025-10-05 15:32:55 +02:00
split out retrive and put methods
This commit is contained in:
parent
363be16fd9
commit
51a573dd90
2 changed files with 70 additions and 45 deletions
59
src/store.js
59
src/store.js
|
@ -103,7 +103,25 @@ class Store {
|
|||
}
|
||||
|
||||
/**
|
||||
* Request a url from storage
|
||||
* Put binary data from a url to storage
|
||||
* @param {string} url a url to request from storage
|
||||
* @return {Promise<Blob>}
|
||||
*/
|
||||
put(url) {
|
||||
let encodedUrl = window.encodeURIComponent(url);
|
||||
|
||||
return localforage.getItem(encodedUrl).then((result) => {
|
||||
if (!result) {
|
||||
return this.requester(url, "binary", withCredentials, headers).then((data) => {
|
||||
return localforage.setItem(encodedUrl, data);
|
||||
});
|
||||
}
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Request a url
|
||||
* @param {string} url a url to request from storage
|
||||
* @param {string} [type] specify the type of the returned result
|
||||
* @param {boolean} [withCredentials]
|
||||
|
@ -111,26 +129,31 @@ class Store {
|
|||
* @return {Promise<Blob | string | JSON | Document | XMLDocument>}
|
||||
*/
|
||||
request(url, type, withCredentials, headers){
|
||||
if (this.online) {
|
||||
// From network
|
||||
return this.requester(url, type, withCredentials, headers).then((data) => {
|
||||
// save to store if not present
|
||||
this.put(url);
|
||||
return data;
|
||||
})
|
||||
} else {
|
||||
// From store
|
||||
return this.retrieve(url, type);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Request a url from storage
|
||||
* @param {string} url a url to request from storage
|
||||
* @param {string} [type] specify the type of the returned result
|
||||
* @return {Promise<Blob | string | JSON | Document | XMLDocument>}
|
||||
*/
|
||||
retrieve(url, type) {
|
||||
var deferred = new defer();
|
||||
var response;
|
||||
var path = new Path(url);
|
||||
|
||||
if (this.online) {
|
||||
return this.requester(url, type, withCredentials, headers).then((data) => {
|
||||
// from network
|
||||
let encodedUrl = window.encodeURIComponent(url);
|
||||
|
||||
localforage.getItem(encodedUrl).then((result) => {
|
||||
if (!result) {
|
||||
this.requester(url, "binary", withCredentials, headers).then((data) => {
|
||||
localforage.setItem(encodedUrl, data);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return data;
|
||||
})
|
||||
} else {
|
||||
// If type isn't set, determine it from the file extension
|
||||
if(!type) {
|
||||
type = path.extension;
|
||||
|
@ -159,8 +182,6 @@ class Store {
|
|||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the response from request
|
||||
* @private
|
||||
|
|
4
types/store.d.ts
vendored
4
types/store.d.ts
vendored
|
@ -6,8 +6,12 @@ export default class Store {
|
|||
|
||||
add(resources: Resources, force?: boolean): Promise<Array<object>>;
|
||||
|
||||
put(url: string): Promise<Blob>;
|
||||
|
||||
request(url: string, type?: string, withCredentials?: boolean, headers?: object): Promise<Blob | string | JSON | Document | XMLDocument>;
|
||||
|
||||
retrieve(url: string, type?: string): Promise<Blob | string | JSON | Document | XMLDocument>;
|
||||
|
||||
getBlob(url: string, mimeType?: string): Promise<Blob>;
|
||||
|
||||
getText(url: string): Promise<string>;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue