Add Objects.setData and Promise.data to set the data before the object/promise is resolved
This commit is contained in:
parent
6dcf9f42a5
commit
86681a8d25
4
fonts.js
4
fonts.js
@ -192,7 +192,6 @@ var FontLoader = {
|
|||||||
for (var i = 0; i < this.waitingFontObjs.length; i++) {
|
for (var i = 0; i < this.waitingFontObjs.length; i++) {
|
||||||
var fontObj = this.waitingFontObjs[i];
|
var fontObj = this.waitingFontObjs[i];
|
||||||
var rule = this.bindDOM(fontObj);
|
var rule = this.bindDOM(fontObj);
|
||||||
this.fonts[objIds[i]] = fontObj;
|
|
||||||
names.push(fontObj.loadedName);
|
names.push(fontObj.loadedName);
|
||||||
rules.push(rule);
|
rules.push(rule);
|
||||||
}
|
}
|
||||||
@ -205,8 +204,7 @@ var FontLoader = {
|
|||||||
fontLoadEvent: function(objIds) {
|
fontLoadEvent: function(objIds) {
|
||||||
for (var i = 0; i < objIds.length; i++) {
|
for (var i = 0; i < objIds.length; i++) {
|
||||||
var objId = objIds[i];
|
var objId = objIds[i];
|
||||||
Objects.resolve(objId, this.fonts[objId]);
|
Objects.resolve(objId);
|
||||||
delete this.fonts[objId];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.fontsLoading = false;
|
this.fontsLoading = false;
|
||||||
|
8
pdf.js
8
pdf.js
@ -4884,12 +4884,8 @@ var CanvasGraphics = (function() {
|
|||||||
var deps = argsArray[i];
|
var deps = argsArray[i];
|
||||||
for (var n = 0; n < deps.length; n++) {
|
for (var n = 0; n < deps.length; n++) {
|
||||||
var depObjId = deps[n];
|
var depObjId = deps[n];
|
||||||
var promise;
|
var promise = Objects.getPromise(depObjId);
|
||||||
if (!Objects[depObjId]) {
|
|
||||||
promise = Objects[depObjId] = new Promise(depObjId);
|
|
||||||
} else {
|
|
||||||
promise = Objects[depObjId];
|
|
||||||
}
|
|
||||||
// If the promise isn't resolved yet, add the continueCallback
|
// If the promise isn't resolved yet, add the continueCallback
|
||||||
// to the promise and bail out.
|
// to the promise and bail out.
|
||||||
if (!promise.isResolved) {
|
if (!promise.isResolved) {
|
||||||
|
41
worker.js
41
worker.js
@ -60,6 +60,19 @@ var WorkerPage = (function() {
|
|||||||
|
|
||||||
// This holds a list of objects the IR queue depends on.
|
// This holds a list of objects the IR queue depends on.
|
||||||
var Objects = {
|
var Objects = {
|
||||||
|
getPromise: function(objId) {
|
||||||
|
if (Objects[objId]) {
|
||||||
|
return this[objId];
|
||||||
|
} else {
|
||||||
|
return this[objId] = new Promise(objId);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
setData: function(objId, data) {
|
||||||
|
var promise = this.getPromise(objId);
|
||||||
|
promise.data = data;
|
||||||
|
},
|
||||||
|
|
||||||
resolve: function(objId, data) {
|
resolve: function(objId, data) {
|
||||||
// In case there is a promise already on this object, just resolve it.
|
// In case there is a promise already on this object, just resolve it.
|
||||||
if (Objects[objId]) {
|
if (Objects[objId]) {
|
||||||
@ -79,19 +92,39 @@ var Objects = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
var Promise = (function() {
|
var Promise = (function() {
|
||||||
|
var EMPTY_PROMISE = {};
|
||||||
|
|
||||||
function Promise(name, data) {
|
function Promise(name, data) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
// If you build a promise and pass in some data it's already resolved.
|
// If you build a promise and pass in some data it's already resolved.
|
||||||
if (data != null) {
|
if (data != null) {
|
||||||
this.isResolved = true;
|
this.isResolved = true;
|
||||||
this.data = data;
|
this.$data = data;
|
||||||
} else {
|
} else {
|
||||||
this.isResolved = false;
|
this.isResolved = false;
|
||||||
|
this.$data = EMPTY_PROMISE;
|
||||||
}
|
}
|
||||||
this.callbacks = [];
|
this.callbacks = [];
|
||||||
};
|
};
|
||||||
|
|
||||||
Promise.prototype = {
|
Promise.prototype = {
|
||||||
|
set data(data) {
|
||||||
|
if (data === undefined) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (this.$data !== EMPTY_PROMISE) {
|
||||||
|
throw "Promise " + this.name + ": Cannot set the data of a promise twice";
|
||||||
|
}
|
||||||
|
this.$data = data;
|
||||||
|
},
|
||||||
|
|
||||||
|
get data() {
|
||||||
|
if (this.$data === EMPTY_PROMISE) {
|
||||||
|
throw "Promise " + this.name + ": Cannot get data that isn't set";
|
||||||
|
}
|
||||||
|
return this.$data;
|
||||||
|
},
|
||||||
|
|
||||||
resolve: function(data) {
|
resolve: function(data) {
|
||||||
if (this.isResolved) {
|
if (this.isResolved) {
|
||||||
throw "A Promise can be resolved only once";
|
throw "A Promise can be resolved only once";
|
||||||
@ -109,7 +142,8 @@ var Promise = (function() {
|
|||||||
then: function(callback) {
|
then: function(callback) {
|
||||||
// If the promise is already resolved, call the callback directly.
|
// If the promise is already resolved, call the callback directly.
|
||||||
if (this.isResolved) {
|
if (this.isResolved) {
|
||||||
callback.call(null, this.data);
|
var data = this.data;
|
||||||
|
callback.call(null, data);
|
||||||
} else {
|
} else {
|
||||||
this.callbacks.push(callback);
|
this.callbacks.push(callback);
|
||||||
}
|
}
|
||||||
@ -128,7 +162,7 @@ var WorkerPDFDoc = (function() {
|
|||||||
|
|
||||||
this.pageCache = [];
|
this.pageCache = [];
|
||||||
|
|
||||||
var useWorker = true;
|
var useWorker = false;
|
||||||
|
|
||||||
if (useWorker) {
|
if (useWorker) {
|
||||||
var worker = new Worker("../worker/boot_processor.js");
|
var worker = new Worker("../worker/boot_processor.js");
|
||||||
@ -181,6 +215,7 @@ var WorkerPDFDoc = (function() {
|
|||||||
if (!fontObj.str) {
|
if (!fontObj.str) {
|
||||||
Objects.resolve(objId, fontObj);
|
Objects.resolve(objId, fontObj);
|
||||||
} else {
|
} else {
|
||||||
|
Objects.setData(objId, fontObj);
|
||||||
FontLoader.bind(objId, fontObj);
|
FontLoader.bind(objId, fontObj);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user