From deda82d2a959b3c0848cde4dc75cdae14a3c6faf Mon Sep 17 00:00:00 2001 From: Fred Chasen Date: Mon, 29 Oct 2018 11:05:31 -0700 Subject: [PATCH] Fix localforage require --- src/store.js | 51 ++++++++++++++++++++++++++++++------------------ types/book.d.ts | 3 ++- types/store.d.ts | 4 ++-- 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/src/store.js b/src/store.js index 83761cb..58f03da 100644 --- a/src/store.js +++ b/src/store.js @@ -7,12 +7,18 @@ import EventEmitter from "event-emitter"; /** * Handles saving and requesting files from local storage * @class + * @param {string} name This should be the name of the application for modals + * @param {function} [requester] + * @param {function} [resolver] */ class Store { constructor(name, requester, resolver) { this.urlCache = {}; + this.storage = undefined; + + this.name = name; this.requester = requester || httpRequest; this.resolver = resolver; @@ -20,11 +26,6 @@ class Store { this.checkRequirements(); - // This should be the name of the application for modals - localforage.config({ - name: name || 'epubjs' - }); - this.addListeners(); } @@ -35,9 +36,15 @@ class Store { */ checkRequirements(){ try { - if (typeof localForage === "undefined") { - let localForage = require("localforage"); + let store; + if (typeof localforage === "undefined") { + store = require("localforage"); + } else { + store = localforage; } + this.storage = store.createInstance({ + name: this.name + }); } catch (e) { throw new Error("localForage lib not loaded"); } @@ -87,11 +94,11 @@ class Store { let url = this.resolver(href); let encodedUrl = window.encodeURIComponent(url); - return localforage.getItem(encodedUrl).then((item) => { + return this.storage.getItem(encodedUrl).then((item) => { if (!item || force) { return this.requester(url, "binary") .then((data) => { - return localforage.setItem(encodedUrl, data); + return this.storage.setItem(encodedUrl, data); }); } else { return item; @@ -105,15 +112,17 @@ class Store { /** * Put binary data from a url to storage * @param {string} url a url to request from storage + * @param {boolean} [withCredentials] + * @param {object} [headers] * @return {Promise} */ - put(url) { + put(url, withCredentials, headers) { let encodedUrl = window.encodeURIComponent(url); - return localforage.getItem(encodedUrl).then((result) => { + return this.storage.getItem(encodedUrl).then((result) => { if (!result) { return this.requester(url, "binary", withCredentials, headers).then((data) => { - return localforage.setItem(encodedUrl, data); + return this.storage.setItem(encodedUrl, data); }); } return result; @@ -222,7 +231,7 @@ class Store { getBlob(url, mimeType){ let encodedUrl = window.encodeURIComponent(url); - return localforage.getItem(encodedUrl).then(function(uint8array) { + return this.storage.getItem(encodedUrl).then(function(uint8array) { if(!uint8array) return; mimeType = mimeType || mime.lookup(url); @@ -243,12 +252,14 @@ class Store { mimeType = mimeType || mime.lookup(url); - return localforage.getItem(encodedUrl).then(function(uint8array) { + return this.storage.getItem(encodedUrl).then(function(uint8array) { var deferred = new defer(); var reader = new FileReader(); - var blob = new Blob([uint8array], {type : mimeType}); + var blob; - if(!blob) return; + if(!uint8array) return; + + blob = new Blob([uint8array], {type : mimeType}); reader.addEventListener("loadend", () => { deferred.resolve(reader.result); @@ -271,12 +282,14 @@ class Store { mimeType = mimeType || mime.lookup(url); - return localforage.getItem(encodedUrl).then((uint8array) => { + return this.storage.getItem(encodedUrl).then((uint8array) => { var deferred = new defer(); var reader = new FileReader(); - var blob = new Blob([uint8array], {type : mimeType}); + var blob; - if(!blob) return; + if(!uint8array) return; + + blob = new Blob([uint8array], {type : mimeType}); reader.addEventListener("loadend", () => { deferred.resolve(reader.result); diff --git a/types/book.d.ts b/types/book.d.ts index 8d79262..194b377 100644 --- a/types/book.d.ts +++ b/types/book.d.ts @@ -23,7 +23,8 @@ export interface BookOptions { encoding?: string, replacements?: string, canonical?: (path: string) => string, - openAs?: string + openAs?: string, + store?: string } export default class Book { diff --git a/types/store.d.ts b/types/store.d.ts index e9fde8e..655a17d 100644 --- a/types/store.d.ts +++ b/types/store.d.ts @@ -2,11 +2,11 @@ import localForage = require('localforage'); import Resources from "./resources"; export default class Store { - constructor(); + constructor(name: string, request?: Function, resolver?: Function); add(resources: Resources, force?: boolean): Promise>; - put(url: string): Promise; + put(url: string, withCredentials?: boolean, headers?: object): Promise; request(url: string, type?: string, withCredentials?: boolean, headers?: object): Promise;