mirror of
https://github.com/zen-browser/pdf.js.git
synced 2025-07-09 17:55:37 +02:00
Merge pull request #4752 from yurydelendik/refmsghdlr
Refactors MessageHandler.send to remove callbacks
This commit is contained in:
commit
44cd0f4a76
5 changed files with 175 additions and 215 deletions
|
@ -387,7 +387,8 @@ var PDFDocumentProxy = (function PDFDocumentProxyClosure() {
|
|||
* @class
|
||||
*/
|
||||
var PDFPageProxy = (function PDFPageProxyClosure() {
|
||||
function PDFPageProxy(pageInfo, transport) {
|
||||
function PDFPageProxy(pageIndex, pageInfo, transport) {
|
||||
this.pageIndex = pageIndex;
|
||||
this.pageInfo = pageInfo;
|
||||
this.transport = transport;
|
||||
this.stats = new StatTimer();
|
||||
|
@ -403,7 +404,7 @@ var PDFPageProxy = (function PDFPageProxyClosure() {
|
|||
* @return {number} Page number of the page. First page is 1.
|
||||
*/
|
||||
get pageNumber() {
|
||||
return this.pageInfo.pageIndex + 1;
|
||||
return this.pageIndex + 1;
|
||||
},
|
||||
/**
|
||||
* @return {number} The number of degrees the page is rotated clockwise.
|
||||
|
@ -443,14 +444,13 @@ var PDFPageProxy = (function PDFPageProxyClosure() {
|
|||
* annotation objects.
|
||||
*/
|
||||
getAnnotations: function PDFPageProxy_getAnnotations() {
|
||||
if (this.annotationsCapability) {
|
||||
return this.annotationsCapability.promise;
|
||||
if (this.annotationsPromise) {
|
||||
return this.annotationsPromise;
|
||||
}
|
||||
|
||||
var capability = createPromiseCapability();
|
||||
this.annotationsCapability = capability;
|
||||
this.transport.getAnnotations(this.pageInfo.pageIndex);
|
||||
return capability.promise;
|
||||
var promise = this.transport.getAnnotations(this.pageIndex);
|
||||
this.annotationsPromise = promise;
|
||||
return promise;
|
||||
},
|
||||
/**
|
||||
* Begins the process of rendering a page to the desired context.
|
||||
|
@ -546,15 +546,9 @@ var PDFPageProxy = (function PDFPageProxyClosure() {
|
|||
* object that represent the page text content.
|
||||
*/
|
||||
getTextContent: function PDFPageProxy_getTextContent() {
|
||||
return new Promise(function (resolve) {
|
||||
this.transport.messageHandler.send('GetTextContent', {
|
||||
pageIndex: this.pageNumber - 1
|
||||
},
|
||||
function textContentCallback(textContent) {
|
||||
resolve(textContent);
|
||||
}
|
||||
);
|
||||
}.bind(this));
|
||||
return this.transport.messageHandler.sendWithPromise('GetTextContent', {
|
||||
pageIndex: this.pageNumber - 1
|
||||
});
|
||||
},
|
||||
/**
|
||||
* Destroys resources allocated by the page.
|
||||
|
@ -582,6 +576,7 @@ var PDFPageProxy = (function PDFPageProxyClosure() {
|
|||
delete this.intentStates[intent];
|
||||
}, this);
|
||||
this.objs.clear();
|
||||
this.annotationsPromise = null;
|
||||
this.pendingDestroy = false;
|
||||
},
|
||||
/**
|
||||
|
@ -637,7 +632,7 @@ var WorkerTransport = (function WorkerTransportClosure() {
|
|||
this.commonObjs = new PDFObjects();
|
||||
|
||||
this.pageCache = [];
|
||||
this.pageCapabilities = [];
|
||||
this.pagePromises = [];
|
||||
this.downloadInfoCapability = createPromiseCapability();
|
||||
this.passwordCallback = null;
|
||||
|
||||
|
@ -682,7 +677,7 @@ var WorkerTransport = (function WorkerTransportClosure() {
|
|||
// Some versions of Opera throw a DATA_CLONE_ERR on serializing the
|
||||
// typed array. Also, checking if we can use transfers.
|
||||
try {
|
||||
messageHandler.send('test', testObj, null, [testObj.buffer]);
|
||||
messageHandler.send('test', testObj, [testObj.buffer]);
|
||||
} catch (ex) {
|
||||
info('Cannot use postMessage transfers');
|
||||
testObj[0] = 0;
|
||||
|
@ -705,9 +700,9 @@ var WorkerTransport = (function WorkerTransportClosure() {
|
|||
WorkerTransport.prototype = {
|
||||
destroy: function WorkerTransport_destroy() {
|
||||
this.pageCache = [];
|
||||
this.pageCapabilities = [];
|
||||
this.pagePromises = [];
|
||||
var self = this;
|
||||
this.messageHandler.send('Terminate', null, function () {
|
||||
this.messageHandler.sendWithPromise('Terminate', null).then(function () {
|
||||
FontLoader.clear();
|
||||
if (self.worker) {
|
||||
self.worker.terminate();
|
||||
|
@ -827,20 +822,6 @@ var WorkerTransport = (function WorkerTransportClosure() {
|
|||
this.downloadInfoCapability.resolve(data);
|
||||
}, this);
|
||||
|
||||
messageHandler.on('GetPage', function transportPage(data) {
|
||||
var pageInfo = data.pageInfo;
|
||||
var page = new PDFPageProxy(pageInfo, this);
|
||||
this.pageCache[pageInfo.pageIndex] = page;
|
||||
var promise = this.pageCapabilities[pageInfo.pageIndex];
|
||||
promise.resolve(page);
|
||||
}, this);
|
||||
|
||||
messageHandler.on('GetAnnotations', function transportAnnotations(data) {
|
||||
var annotations = data.annotations;
|
||||
var promise = this.pageCache[data.pageIndex].annotationsCapability;
|
||||
promise.resolve(annotations);
|
||||
}, this);
|
||||
|
||||
messageHandler.on('StartRenderPage', function transportRender(data) {
|
||||
var page = this.pageCache[data.pageIndex];
|
||||
|
||||
|
@ -934,9 +915,9 @@ var WorkerTransport = (function WorkerTransportClosure() {
|
|||
this.workerReadyCapability.reject(data);
|
||||
}, this);
|
||||
|
||||
messageHandler.on('PageError', function transportError(data, intent) {
|
||||
messageHandler.on('PageError', function transportError(data) {
|
||||
var page = this.pageCache[data.pageNum - 1];
|
||||
var intentState = page.intentStates[intent];
|
||||
var intentState = page.intentStates[data.intent];
|
||||
if (intentState.displayReadyCapability.promise) {
|
||||
intentState.displayReadyCapability.reject(data.error);
|
||||
} else {
|
||||
|
@ -944,40 +925,46 @@ var WorkerTransport = (function WorkerTransportClosure() {
|
|||
}
|
||||
}, this);
|
||||
|
||||
messageHandler.on('JpegDecode', function(data, deferred) {
|
||||
messageHandler.on('JpegDecode', function(data) {
|
||||
var imageUrl = data[0];
|
||||
var components = data[1];
|
||||
if (components != 3 && components != 1) {
|
||||
error('Only 3 component or 1 component can be returned');
|
||||
return Promise.reject(
|
||||
new Error('Only 3 components or 1 component can be returned'));
|
||||
}
|
||||
|
||||
var img = new Image();
|
||||
img.onload = (function messageHandler_onloadClosure() {
|
||||
var width = img.width;
|
||||
var height = img.height;
|
||||
var size = width * height;
|
||||
var rgbaLength = size * 4;
|
||||
var buf = new Uint8Array(size * components);
|
||||
var tmpCanvas = createScratchCanvas(width, height);
|
||||
var tmpCtx = tmpCanvas.getContext('2d');
|
||||
tmpCtx.drawImage(img, 0, 0);
|
||||
var data = tmpCtx.getImageData(0, 0, width, height).data;
|
||||
var i, j;
|
||||
return new Promise(function (resolve, reject) {
|
||||
var img = new Image();
|
||||
img.onload = function () {
|
||||
var width = img.width;
|
||||
var height = img.height;
|
||||
var size = width * height;
|
||||
var rgbaLength = size * 4;
|
||||
var buf = new Uint8Array(size * components);
|
||||
var tmpCanvas = createScratchCanvas(width, height);
|
||||
var tmpCtx = tmpCanvas.getContext('2d');
|
||||
tmpCtx.drawImage(img, 0, 0);
|
||||
var data = tmpCtx.getImageData(0, 0, width, height).data;
|
||||
var i, j;
|
||||
|
||||
if (components == 3) {
|
||||
for (i = 0, j = 0; i < rgbaLength; i += 4, j += 3) {
|
||||
buf[j] = data[i];
|
||||
buf[j + 1] = data[i + 1];
|
||||
buf[j + 2] = data[i + 2];
|
||||
if (components == 3) {
|
||||
for (i = 0, j = 0; i < rgbaLength; i += 4, j += 3) {
|
||||
buf[j] = data[i];
|
||||
buf[j + 1] = data[i + 1];
|
||||
buf[j + 2] = data[i + 2];
|
||||
}
|
||||
} else if (components == 1) {
|
||||
for (i = 0, j = 0; i < rgbaLength; i += 4, j++) {
|
||||
buf[j] = data[i];
|
||||
}
|
||||
}
|
||||
} else if (components == 1) {
|
||||
for (i = 0, j = 0; i < rgbaLength; i += 4, j++) {
|
||||
buf[j] = data[i];
|
||||
}
|
||||
}
|
||||
deferred.resolve({ data: buf, width: width, height: height});
|
||||
}).bind(this);
|
||||
img.src = imageUrl;
|
||||
resolve({ data: buf, width: width, height: height});
|
||||
};
|
||||
img.onerror = function () {
|
||||
reject(new Error('JpegDecode failed to load image'));
|
||||
};
|
||||
img.src = imageUrl;
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
|
@ -1009,96 +996,67 @@ var WorkerTransport = (function WorkerTransportClosure() {
|
|||
}
|
||||
|
||||
var pageIndex = pageNumber - 1;
|
||||
if (pageIndex in this.pageCapabilities) {
|
||||
return this.pageCapabilities[pageIndex].promise;
|
||||
if (pageIndex in this.pagePromises) {
|
||||
return this.pagePromises[pageIndex];
|
||||
}
|
||||
capability = createPromiseCapability();
|
||||
this.pageCapabilities[pageIndex] = capability;
|
||||
this.messageHandler.send('GetPageRequest', { pageIndex: pageIndex });
|
||||
return capability.promise;
|
||||
var promise = this.messageHandler.sendWithPromise('GetPage', {
|
||||
pageIndex: pageIndex
|
||||
}).then(function (pageInfo) {
|
||||
var page = new PDFPageProxy(pageIndex, pageInfo, this);
|
||||
this.pageCache[pageIndex] = page;
|
||||
return page;
|
||||
}.bind(this));
|
||||
this.pagePromises[pageIndex] = promise;
|
||||
return promise;
|
||||
},
|
||||
|
||||
getPageIndex: function WorkerTransport_getPageIndexByRef(ref) {
|
||||
return new Promise(function (resolve) {
|
||||
this.messageHandler.send('GetPageIndex', { ref: ref },
|
||||
function (pageIndex) {
|
||||
resolve(pageIndex);
|
||||
}
|
||||
);
|
||||
}.bind(this));
|
||||
return this.messageHandler.sendWithPromise('GetPageIndex', { ref: ref });
|
||||
},
|
||||
|
||||
getAnnotations: function WorkerTransport_getAnnotations(pageIndex) {
|
||||
this.messageHandler.send('GetAnnotationsRequest',
|
||||
return this.messageHandler.sendWithPromise('GetAnnotations',
|
||||
{ pageIndex: pageIndex });
|
||||
},
|
||||
|
||||
getDestinations: function WorkerTransport_getDestinations() {
|
||||
return new Promise(function (resolve) {
|
||||
this.messageHandler.send('GetDestinations', null,
|
||||
function transportDestinations(destinations) {
|
||||
resolve(destinations);
|
||||
}
|
||||
);
|
||||
}.bind(this));
|
||||
return this.messageHandler.sendWithPromise('GetDestinations', null);
|
||||
},
|
||||
|
||||
getAttachments: function WorkerTransport_getAttachments() {
|
||||
return new Promise(function (resolve) {
|
||||
this.messageHandler.send('GetAttachments', null,
|
||||
function transportAttachments(attachments) {
|
||||
resolve(attachments);
|
||||
}
|
||||
);
|
||||
}.bind(this));
|
||||
return this.messageHandler.sendWithPromise('GetAttachments', null);
|
||||
},
|
||||
|
||||
getJavaScript: function WorkerTransport_getJavaScript() {
|
||||
return new Promise(function (resolve) {
|
||||
this.messageHandler.send('GetJavaScript', null,
|
||||
function transportJavaScript(js) {
|
||||
resolve(js);
|
||||
}
|
||||
);
|
||||
}.bind(this));
|
||||
return this.messageHandler.sendWithPromise('GetJavaScript', null);
|
||||
},
|
||||
|
||||
getOutline: function WorkerTransport_getOutline() {
|
||||
return new Promise(function (resolve) {
|
||||
this.messageHandler.send('GetOutline', null,
|
||||
function transportOutline(outline) {
|
||||
resolve(outline);
|
||||
}
|
||||
);
|
||||
}.bind(this));
|
||||
return this.messageHandler.sendWithPromise('GetOutline', null);
|
||||
},
|
||||
|
||||
getMetadata: function WorkerTransport_getMetadata() {
|
||||
return new Promise(function (resolve) {
|
||||
this.messageHandler.send('GetMetadata', null,
|
||||
function transportMetadata(results) {
|
||||
resolve({
|
||||
info: results[0],
|
||||
metadata: (results[1] ? new PDFJS.Metadata(results[1]) : null)
|
||||
});
|
||||
}
|
||||
);
|
||||
}.bind(this));
|
||||
return this.messageHandler.sendWithPromise('GetMetadata', null).
|
||||
then(function transportMetadata(results) {
|
||||
return {
|
||||
info: results[0],
|
||||
metadata: (results[1] ? new PDFJS.Metadata(results[1]) : null)
|
||||
};
|
||||
});
|
||||
},
|
||||
|
||||
startCleanup: function WorkerTransport_startCleanup() {
|
||||
this.messageHandler.send('Cleanup', null,
|
||||
function endCleanup() {
|
||||
for (var i = 0, ii = this.pageCache.length; i < ii; i++) {
|
||||
var page = this.pageCache[i];
|
||||
if (page) {
|
||||
page.destroy();
|
||||
}
|
||||
this.messageHandler.sendWithPromise('Cleanup', null).
|
||||
then(function endCleanup() {
|
||||
for (var i = 0, ii = this.pageCache.length; i < ii; i++) {
|
||||
var page = this.pageCache[i];
|
||||
if (page) {
|
||||
page.destroy();
|
||||
}
|
||||
this.commonObjs.clear();
|
||||
FontLoader.clear();
|
||||
}.bind(this)
|
||||
);
|
||||
}
|
||||
this.commonObjs.clear();
|
||||
FontLoader.clear();
|
||||
}.bind(this));
|
||||
}
|
||||
};
|
||||
return WorkerTransport;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue