1
0
Fork 0
mirror of https://github.com/futurepress/epub.js.git synced 2025-10-03 14:59:18 +02:00

Switch to Native Promises, add polyfills.js in dist folder

This commit is contained in:
Fred Chasen 2016-10-27 00:46:56 +02:00
parent a4e76029d4
commit 151dc97d19
28 changed files with 677 additions and 3049 deletions

View file

@ -26,6 +26,7 @@
"examples"
],
"dependencies": {
"rsvp": "~3.0.13"
"rsvp": "~3.0.13",
"es6-promise": "~4.0.5"
}
}

3522
dist/epub.js vendored

File diff suppressed because it is too large Load diff

2
dist/epub.js.map vendored

File diff suppressed because one or more lines are too long

19
dist/epub.min.js vendored

File diff suppressed because one or more lines are too long

View file

@ -85,7 +85,7 @@
// Add several scripts / css
rendition.hooks.content.register(function(view){
var loaded = RSVP.all([
var loaded = Promise.all([
view.addScript("http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"),
view.addCss("http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.css")
]);

View file

@ -31,9 +31,6 @@ watchConfig.watch = true;
// create a single instance of the compiler to allow caching
var watchCompiler = webpack(watchConfig);
// https://github.com/mishoo/UglifyJS2/pull/265
// uglify.AST_Node.warn_function = function() {};
// Lint JS
gulp.task('lint', function() {
return gulp.src('src/*.js')
@ -63,9 +60,9 @@ gulp.task('minify', ['bundle'], function(){
mangle: true,
preserveComments : "license"
};
return gulp.src('dist/epub.js')
return gulp.src(['dist/epub.js', 'dist/polyfills.js'])
.pipe(plumber({ errorHandler: onError }))
.pipe(rename('epub.min.js'))
.pipe(rename({suffix: '.min'}))
// .pipe(sourcemaps.init({loadMaps: true}))
.pipe(uglify(uglifyOptions))
// .pipe(sourcemaps.write('./'))
@ -75,7 +72,7 @@ gulp.task('minify', ['bundle'], function(){
// Watch Our Files
gulp.task('watch', function(cb) {
watchCompiler(watchConfig, function(err, stats) {
webpack(webpackConfig, function(err, stats) {
if(err) {
throw new gutil.PluginError("webpack", err);
}
@ -136,7 +133,7 @@ function bundle(done) {
webpackConfig.watch = false;
}
watchCompiler(watchConfig, function(err, stats) {
webpack(webpackConfig, function(err, stats) {
if(err) throw new gutil.PluginError("webpack", err);
gutil.log("[webpack]", stats.toString({
colors: true,

View file

@ -21,6 +21,9 @@ module.exports = function(config) {
{pattern: 'node_modules/jszip/dist/jszip.js', watched: false, included: true, served: true},
{pattern: 'node_modules/es6-promise/dist/es6-promise.auto.js', watched: false, included: true, served: true}
],
// list of files to exclude

View file

@ -10,7 +10,7 @@
"scripts": {
"test": "./node_modules/.bin/karma start --single-run --browsers PhantomJS",
"start": "webpack-dev-server --inline --hot",
"build": "gulp minify"
"build": "./node_modules/.bin/gulp minify"
},
"author": "fchasen@gmail.com",
"license": "MIT",
@ -51,6 +51,7 @@
"webpack-dev-server": "^1.16.2"
},
"dependencies": {
"es6-promise": "^4.0.5",
"event-emitter": "^0.3.4",
"jszip": "^3.1.1",
"rsvp": "^3.0.18",

View file

@ -1,4 +1,3 @@
var RSVP = require('rsvp');
var EventEmitter = require('event-emitter');
var URI = require('urijs');
var core = require('./core');
@ -21,19 +20,19 @@ function Book(_url, options){
// Promises
this.opening = new RSVP.defer();
this.opening = new core.defer();
this.opened = this.opening.promise;
this.isOpen = false;
this.url = undefined;
this.loading = {
manifest: new RSVP.defer(),
spine: new RSVP.defer(),
metadata: new RSVP.defer(),
cover: new RSVP.defer(),
navigation: new RSVP.defer(),
pageList: new RSVP.defer()
manifest: new core.defer(),
spine: new core.defer(),
metadata: new core.defer(),
cover: new core.defer(),
navigation: new core.defer(),
pageList: new core.defer()
};
this.loaded = {
@ -45,7 +44,14 @@ function Book(_url, options){
pageList: this.loading.pageList.promise
};
this.ready = RSVP.hash(this.loaded);
// this.ready = RSVP.hash(this.loaded);
this.ready = Promise.all([this.loaded.manifest,
this.loaded.spine,
this.loaded.metadata,
this.loaded.cover,
this.loaded.navigation,
this.loaded.pageList ]);
// Queue for methods used before opening
this.isRendered = false;
@ -333,16 +339,3 @@ module.exports = Book;
//-- Enable binding events to book
EventEmitter(Book.prototype);
//-- Handle RSVP Errors
RSVP.on('error', function(event) {
console.error(event);
});
RSVP.configure('instrument', false); //-- true | will logging out all RSVP rejections
// RSVP.on('created', listener);
// RSVP.on('chained', listener);
// RSVP.on('fulfilled', listener);
RSVP.on('rejected', function(event){
console.error(event.detail.message, event.detail.stack);
});

View file

@ -1,4 +1,3 @@
var RSVP = require('rsvp');
var EventEmitter = require('event-emitter');
var core = require('./core');
var EpubCFI = require('./epubcfi');
@ -406,7 +405,7 @@ Contents.prototype.locationOf = function(target, ignoreClass) {
};
Contents.prototype.addStylesheet = function(src) {
return new RSVP.Promise(function(resolve, reject){
return new Promise(function(resolve, reject){
var $stylesheet;
var ready = false;
@ -469,7 +468,7 @@ Contents.prototype.addStylesheetRules = function(rules) {
Contents.prototype.addScript = function(src) {
return new RSVP.Promise(function(resolve, reject){
return new Promise(function(resolve, reject){
var $script;
var ready = false;

View file

@ -1,4 +1,3 @@
var RSVP = require('rsvp');
var base64 = require('base64-js');
var requestAnimationFrame = (typeof window != 'undefined') ? (window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame) : false;
@ -505,6 +504,36 @@ function blob2base64(blob, cb) {
}
}
function defer() {
// From: https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Promise.jsm/Deferred#backwards_forwards_compatible
/* A method to resolve the associated Promise with the value passed.
* If the promise is already settled it does nothing.
*
* @param {anything} value : This value is used to resolve the promise
* If the value is a Promise then the associated promise assumes the state
* of Promise passed as value.
*/
this.resolve = null;
/* A method to reject the assocaited Promise with the value passed.
* If the promise is already settled it does nothing.
*
* @param {anything} reason: The reason for the rejection of the Promise.
* Generally its an Error object. If however a Promise is passed, then the Promise
* itself will be the reason for rejection no matter the state of the Promise.
*/
this.reject = null;
/* A newly created Pomise object.
* Initially in pending state.
*/
this.promise = new Promise(function(resolve, reject) {
this.resolve = resolve;
this.reject = reject;
}.bind(this));
Object.freeze(this);
}
module.exports = {
// 'uri': uri,
// 'folder': folder,
@ -536,5 +565,6 @@ module.exports = {
'qsa' : qsa,
'qsp' : qsp,
'blob2base64' : blob2base64,
'createBase64Url': createBase64Url
'createBase64Url': createBase64Url,
'defer': defer
};

View file

@ -2,7 +2,6 @@ var Book = require('./book');
var EpubCFI = require('./epubcfi');
var Rendition = require('./rendition');
var Contents = require('./contents');
var RSVP = require('rsvp');
function ePub(_url) {
return new Book(_url);
@ -13,7 +12,6 @@ ePub.VERSION = "0.3.0";
ePub.CFI = EpubCFI;
ePub.Rendition = Rendition;
ePub.Contents = Contents;
ePub.RSVP = RSVP;
ePub.ViewManagers = {};
ePub.Views = {};

View file

@ -1,5 +1,3 @@
var RSVP = require('rsvp');
//-- Hooks allow for injecting functions that must all complete in order before finishing
// They will execute in parallel but all must finish before continuing
// Functions may return a promise if they are asycn.
@ -44,7 +42,7 @@ Hook.prototype.trigger = function(){
});
return RSVP.all(promises);
return Promise.all(promises);
};
// Adds a function to be run before a hook completes

View file

@ -1,5 +1,4 @@
var core = require('./core');
var RSVP = require('rsvp');
function Layout(settings){
this.name = settings.layout || "reflowable";

View file

@ -1,7 +1,6 @@
var core = require('./core');
var Queue = require('./queue');
var EpubCFI = require('./epubcfi');
var RSVP = require('rsvp');
var EventEmitter = require('event-emitter');
function Locations(spine, request) {

View file

@ -1,4 +1,3 @@
var RSVP = require('rsvp');
var core = require('../../core');
var DefaultViewManager = require('../default');
@ -50,7 +49,7 @@ ContinuousViewManager.prototype.display = function(section, target){
};
ContinuousViewManager.prototype.fill = function(_full){
var full = _full || new RSVP.defer();
var full = _full || new core.defer();
this.check().then(function(result) {
if (result) {
@ -216,7 +215,7 @@ ContinuousViewManager.prototype.update = function(_offset){
var isVisible;
var view;
var updating = new RSVP.defer();
var updating = new core.defer();
var promises = [];
for (var i = 0; i < viewsLength; i++) {
@ -243,7 +242,7 @@ ContinuousViewManager.prototype.update = function(_offset){
}
if(promises.length){
return RSVP.all(promises);
return Promise.all(promises);
} else {
updating.resolve();
return updating.promise;
@ -254,7 +253,7 @@ ContinuousViewManager.prototype.update = function(_offset){
ContinuousViewManager.prototype.check = function(_offsetLeft, _offsetTop){
var last, first, next, prev;
var checking = new RSVP.defer();
var checking = new core.defer();
var newViews = [];
var horizontal = (this.settings.axis === "horizontal");
@ -291,7 +290,7 @@ ContinuousViewManager.prototype.check = function(_offsetLeft, _offsetTop){
}
if(newViews.length){
// RSVP.all(promises)
// Promise.all(promises)
// .then(function() {
// Check to see if anything new is on screen after rendering
return this.q.enqueue(function(){
@ -310,7 +309,7 @@ ContinuousViewManager.prototype.check = function(_offsetLeft, _offsetTop){
};
ContinuousViewManager.prototype.trim = function(){
var task = new RSVP.defer();
var task = new core.defer();
var displayed = this.views.displayed();
var first = displayed[0];
var last = displayed[displayed.length-1];

View file

@ -1,4 +1,3 @@
var RSVP = require('rsvp');
var EventEmitter = require('event-emitter');
var core = require('../../core');
var EpubCFI = require('../../epubcfi');
@ -140,7 +139,7 @@ DefaultViewManager.prototype.createView = function(section) {
DefaultViewManager.prototype.display = function(section, target){
var displaying = new RSVP.defer();
var displaying = new core.defer();
var displayed = displaying.promise;
// Check to make sure the section we want isn't already shown

View file

@ -76,8 +76,6 @@ Views.prototype.remove = function(view) {
};
Views.prototype.destroy = function(view) {
view.off("resized");
if(view.displayed){
view.destroy();
}

View file

@ -1,4 +1,3 @@
var RSVP = require('rsvp');
var EventEmitter = require('event-emitter');
var core = require('../../core');
var EpubCFI = require('../../epubcfi');
@ -370,7 +369,7 @@ IframeView.prototype.reframe = function(width, height) {
IframeView.prototype.load = function(contents) {
var loading = new RSVP.defer();
var loading = new core.defer();
var loaded = loading.promise;
if(!this.iframe) {
@ -478,7 +477,7 @@ IframeView.prototype.removeListeners = function(layoutFunc) {
};
IframeView.prototype.display = function(request) {
var displayed = new RSVP.defer();
var displayed = new core.defer();
if (!this.displayed) {

View file

@ -1,4 +1,3 @@
var RSVP = require('rsvp');
var EventEmitter = require('event-emitter');
var core = require('../../core');
var EpubCFI = require('../../epubcfi');
@ -278,7 +277,7 @@ InlineView.prototype.resize = function(width, height) {
InlineView.prototype.load = function(contents) {
var loading = new RSVP.defer();
var loading = new core.defer();
var loaded = loading.promise;
var doc = core.parse(contents, "text/html");
var body = core.qs(doc, "body");
@ -332,7 +331,7 @@ InlineView.prototype.removeListeners = function(layoutFunc) {
};
InlineView.prototype.display = function(request) {
var displayed = new RSVP.defer();
var displayed = new core.defer();
if (!this.displayed) {

View file

@ -1,6 +1,5 @@
var core = require('./core');
var Parser = require('./parser');
var RSVP = require('rsvp');
var URI = require('urijs');
function Navigation(_package, _request){
@ -18,7 +17,7 @@ function Navigation(_package, _request){
this.nav = {};
this.nav.load = function(_request){
var loading = new RSVP.defer();
var loading = new core.defer();
var loaded = loading.promise;
request(navigation.navUrl, 'xml').then(function(xml){
@ -37,7 +36,7 @@ function Navigation(_package, _request){
this.ncx = {};
this.ncx.load = function(_request){
var loading = new RSVP.defer();
var loading = new core.defer();
var loaded = loading.promise;
request(navigation.ncxUrl, 'xml').then(function(xml){
@ -62,7 +61,7 @@ Navigation.prototype.load = function(_request) {
} else if(this.ncx) {
loading = this.ncx.load();
} else {
loaded = new RSVP.defer();
loaded = new core.defer();
loaded.resolve([]);
loading = loaded.promise;
}

View file

@ -1,4 +1,3 @@
var RSVP = require('rsvp');
var core = require('./core');
function Queue(_context){
@ -26,7 +25,7 @@ Queue.prototype.enqueue = function() {
if(typeof task === "function"){
deferred = new RSVP.defer();
deferred = new core.defer();
promise = deferred.promise;
queued = {
@ -88,7 +87,7 @@ Queue.prototype.dequeue = function(){
}
} else {
inwait = new RSVP.defer();
inwait = new core.defer();
inwait.deferred.resolve();
return inwait.promise;
}
@ -108,7 +107,7 @@ Queue.prototype.run = function(){
if(!this.running){
this.running = true;
this.defered = new RSVP.defer();
this.defered = new core.defer();
}
this.tick.call(window, function() {
@ -174,7 +173,7 @@ function Task(task, args, context){
return function(){
var toApply = arguments || [];
return new RSVP.Promise(function(resolve, reject) {
return new Promise(function(resolve, reject) {
var callback = function(value){
resolve(value);
};

View file

@ -1,4 +1,3 @@
var RSVP = require('rsvp');
var EventEmitter = require('event-emitter');
var URI = require('urijs');
var core = require('./core');
@ -55,7 +54,7 @@ function Rendition(book, options) {
this.q.enqueue(this.book.opened);
// Block the queue until rendering is started
// this.starting = new RSVP.defer();
// this.starting = new core.defer();
// this.started = this.starting.promise;
this.q.enqueue(this.start);
@ -170,7 +169,7 @@ Rendition.prototype.display = function(target){
Rendition.prototype._display = function(target){
var isCfiString = this.epubcfi.isCfiString(target);
var displaying = new RSVP.defer();
var displaying = new core.defer();
var displayed = displaying.promise;
var section;
var moveTo;
@ -449,7 +448,7 @@ Rendition.prototype.replacements = function(){
var replacementUrls;
// After all the urls are created
return RSVP.all(processing)
return Promise.all(processing)
.then(function(_replacementUrls) {
var replaced = [];
@ -460,7 +459,7 @@ Rendition.prototype.replacements = function(){
replaced.push(this.replaceCss(href, urls, replacementUrls));
}.bind(this));
return RSVP.all(replaced);
return Promise.all(replaced);
}.bind(this))
.then(function () {
@ -513,7 +512,7 @@ Rendition.prototype.replaceCss = function(href, urls, replacementUrls){
replacementUrls[indexInUrls] = newUrl;
}
return new RSVP.Promise(function(resolve, reject){
return new Promise(function(resolve, reject){
resolve(urls, replacementUrls);
});
@ -555,7 +554,7 @@ Rendition.prototype.adjustImages = function(view) {
["max-height", (view.layout.height) + "px"]
]
]);
return new RSVP.Promise(function(resolve, reject){
return new Promise(function(resolve, reject){
// Wait to apply
setTimeout(function() {
resolve();

View file

@ -1,4 +1,3 @@
var RSVP = require('rsvp');
var URI = require('urijs');
var core = require('./core');
@ -7,7 +6,7 @@ function request(url, type, withCredentials, headers) {
var BLOB_RESPONSE = supportsURL ? "blob" : "arraybuffer";
var uri;
var deferred = new RSVP.defer();
var deferred = new core.defer();
var xhr = new XMLHttpRequest();

View file

@ -1,4 +1,3 @@
var RSVP = require('rsvp');
var URI = require('urijs');
var core = require('./core');
var EpubCFI = require('./epubcfi');
@ -29,7 +28,7 @@ function Section(item, hooks){
Section.prototype.load = function(_request){
var request = _request || this.request || require('./request');
var loading = new RSVP.defer();
var loading = new core.defer();
var loaded = loading.promise;
if(this.contents) {
@ -57,7 +56,7 @@ Section.prototype.load = function(_request){
};
Section.prototype.base = function(_document){
var task = new RSVP.defer();
var task = new core.defer();
var base = _document.createElement("base"); // TODO: check if exists
var head;
console.log(window.location.origin + "/" +this.url);
@ -83,7 +82,7 @@ Section.prototype.beforeSectionLoad = function(){
};
Section.prototype.render = function(_request){
var rendering = new RSVP.defer();
var rendering = new core.defer();
var rendered = rendering.promise;
this.output; // TODO: better way to return this from hooks?

View file

@ -1,4 +1,3 @@
var RSVP = require('rsvp');
var core = require('./core');
var EpubCFI = require('./epubcfi');
var Hook = require('./hook');

View file

@ -1,4 +1,3 @@
var RSVP = require('rsvp');
var URI = require('urijs');
var core = require('./core');
var request = require('./request');
@ -36,7 +35,7 @@ Unarchive.prototype.open = function(zipUrl, isBase64){
};
Unarchive.prototype.request = function(url, type){
var deferred = new RSVP.defer();
var deferred = new core.defer();
var response;
var r;
@ -128,7 +127,7 @@ Unarchive.prototype.getBase64 = function(url, _mimeType){
};
Unarchive.prototype.createUrl = function(url, options){
var deferred = new RSVP.defer();
var deferred = new core.defer();
var _URL = window.URL || window.webkitURL || window.mozURL;
var tempUrl;
var blob;

View file

@ -7,10 +7,12 @@ var port = "8080";
module.exports = {
entry: {
epub: "./src/epub.js",
polyfills: ["./node_modules/es6-promise/dist/es6-promise.auto.js"]
},
devtool: 'source-map',
output: {
path: path.resolve("./dist"),
// path: path.resolve("./dist"),
path: "./dist",
filename: "[name].js",
sourceMapFilename: "[name].js.map",
library: "ePub",