Convert the WorkerTransport
class, in src/display/api.js
, to ES6 syntax
This commit is contained in:
parent
9c37599fd3
commit
4874e9ace0
@ -1629,8 +1629,8 @@ var PDFWorker = (function PDFWorkerClosure() {
|
||||
* For internal use only.
|
||||
* @ignore
|
||||
*/
|
||||
var WorkerTransport = (function WorkerTransportClosure() {
|
||||
function WorkerTransport(messageHandler, loadingTask, networkStream, params) {
|
||||
class WorkerTransport {
|
||||
constructor(messageHandler, loadingTask, networkStream, params) {
|
||||
this.messageHandler = messageHandler;
|
||||
this.loadingTask = loadingTask;
|
||||
this.commonObjs = new PDFObjects();
|
||||
@ -1655,8 +1655,8 @@ var WorkerTransport = (function WorkerTransportClosure() {
|
||||
|
||||
this.setupMessageHandler();
|
||||
}
|
||||
WorkerTransport.prototype = {
|
||||
destroy: function WorkerTransport_destroy() {
|
||||
|
||||
destroy() {
|
||||
if (this.destroyCapability) {
|
||||
return this.destroyCapability.promise;
|
||||
}
|
||||
@ -1669,7 +1669,7 @@ var WorkerTransport = (function WorkerTransportClosure() {
|
||||
new Error('Worker was destroyed during onPassword callback'));
|
||||
}
|
||||
|
||||
var waitOn = [];
|
||||
const waitOn = [];
|
||||
// We need to wait for all renderings to be completed, e.g.
|
||||
// timeout/rAF can take a long time.
|
||||
this.pageCache.forEach(function(page) {
|
||||
@ -1680,7 +1680,7 @@ var WorkerTransport = (function WorkerTransportClosure() {
|
||||
this.pageCache = [];
|
||||
this.pagePromises = [];
|
||||
// We also need to wait for the worker to finish its long running tasks.
|
||||
var terminated = this.messageHandler.sendWithPromise('Terminate', null);
|
||||
const terminated = this.messageHandler.sendWithPromise('Terminate', null);
|
||||
waitOn.push(terminated);
|
||||
Promise.all(waitOn).then(() => {
|
||||
this.fontLoader.clear();
|
||||
@ -1695,11 +1695,10 @@ var WorkerTransport = (function WorkerTransportClosure() {
|
||||
this.destroyCapability.resolve();
|
||||
}, this.destroyCapability.reject);
|
||||
return this.destroyCapability.promise;
|
||||
},
|
||||
}
|
||||
|
||||
setupMessageHandler: function WorkerTransport_setupMessageHandler() {
|
||||
var messageHandler = this.messageHandler;
|
||||
var loadingTask = this.loadingTask;
|
||||
setupMessageHandler() {
|
||||
const { messageHandler, loadingTask, } = this;
|
||||
|
||||
messageHandler.on('GetReader', function(data, sink) {
|
||||
assert(this._networkStream);
|
||||
@ -1731,21 +1730,19 @@ var WorkerTransport = (function WorkerTransportClosure() {
|
||||
}, this);
|
||||
|
||||
messageHandler.on('ReaderHeadersReady', function(data) {
|
||||
let headersCapability = createPromiseCapability();
|
||||
let fullReader = this._fullReader;
|
||||
const headersCapability = createPromiseCapability();
|
||||
const fullReader = this._fullReader;
|
||||
fullReader.headersReady.then(() => {
|
||||
// If stream or range are disabled, it's our only way to report
|
||||
// loading progress.
|
||||
if (!fullReader.isStreamingSupported ||
|
||||
!fullReader.isRangeSupported) {
|
||||
if (this._lastProgress) {
|
||||
let loadingTask = this.loadingTask;
|
||||
if (loadingTask.onProgress) {
|
||||
loadingTask.onProgress(this._lastProgress);
|
||||
}
|
||||
}
|
||||
fullReader.onProgress = (evt) => {
|
||||
let loadingTask = this.loadingTask;
|
||||
if (loadingTask.onProgress) {
|
||||
loadingTask.onProgress({
|
||||
loaded: evt.loaded,
|
||||
@ -1767,11 +1764,11 @@ var WorkerTransport = (function WorkerTransportClosure() {
|
||||
|
||||
messageHandler.on('GetRangeReader', function(data, sink) {
|
||||
assert(this._networkStream);
|
||||
let _rangeReader =
|
||||
const rangeReader =
|
||||
this._networkStream.getRangeReader(data.begin, data.end);
|
||||
|
||||
sink.onPull = () => {
|
||||
_rangeReader.read().then(function({ value, done, }) {
|
||||
rangeReader.read().then(function({ value, done, }) {
|
||||
if (done) {
|
||||
sink.close();
|
||||
return;
|
||||
@ -1784,24 +1781,21 @@ var WorkerTransport = (function WorkerTransportClosure() {
|
||||
};
|
||||
|
||||
sink.onCancel = (reason) => {
|
||||
_rangeReader.cancel(reason);
|
||||
rangeReader.cancel(reason);
|
||||
};
|
||||
}, this);
|
||||
|
||||
messageHandler.on('GetDoc', function transportDoc({ pdfInfo, }) {
|
||||
messageHandler.on('GetDoc', function({ pdfInfo, }) {
|
||||
this.numPages = pdfInfo.numPages;
|
||||
var loadingTask = this.loadingTask;
|
||||
var pdfDocument = new PDFDocumentProxy(pdfInfo, this, loadingTask);
|
||||
this.pdfDocument = pdfDocument;
|
||||
loadingTask._capability.resolve(pdfDocument);
|
||||
this.pdfDocument = new PDFDocumentProxy(pdfInfo, this, loadingTask);
|
||||
loadingTask._capability.resolve(this.pdfDocument);
|
||||
}, this);
|
||||
|
||||
messageHandler.on('PasswordRequest',
|
||||
function transportPasswordRequest(exception) {
|
||||
messageHandler.on('PasswordRequest', function(exception) {
|
||||
this._passwordCapability = createPromiseCapability();
|
||||
|
||||
if (loadingTask.onPassword) {
|
||||
var updatePassword = (password) => {
|
||||
const updatePassword = (password) => {
|
||||
this._passwordCapability.resolve({
|
||||
password,
|
||||
});
|
||||
@ -1818,80 +1812,76 @@ var WorkerTransport = (function WorkerTransportClosure() {
|
||||
return this._passwordCapability.promise;
|
||||
}, this);
|
||||
|
||||
messageHandler.on('PasswordException',
|
||||
function transportPasswordException(exception) {
|
||||
messageHandler.on('PasswordException', function(exception) {
|
||||
loadingTask._capability.reject(
|
||||
new PasswordException(exception.message, exception.code));
|
||||
}, this);
|
||||
|
||||
messageHandler.on('InvalidPDF', function transportInvalidPDF(exception) {
|
||||
this.loadingTask._capability.reject(
|
||||
messageHandler.on('InvalidPDF', function(exception) {
|
||||
loadingTask._capability.reject(
|
||||
new InvalidPDFException(exception.message));
|
||||
}, this);
|
||||
|
||||
messageHandler.on('MissingPDF', function transportMissingPDF(exception) {
|
||||
this.loadingTask._capability.reject(
|
||||
messageHandler.on('MissingPDF', function(exception) {
|
||||
loadingTask._capability.reject(
|
||||
new MissingPDFException(exception.message));
|
||||
}, this);
|
||||
|
||||
messageHandler.on('UnexpectedResponse',
|
||||
function transportUnexpectedResponse(exception) {
|
||||
this.loadingTask._capability.reject(
|
||||
messageHandler.on('UnexpectedResponse', function(exception) {
|
||||
loadingTask._capability.reject(
|
||||
new UnexpectedResponseException(exception.message, exception.status));
|
||||
}, this);
|
||||
|
||||
messageHandler.on('UnknownError',
|
||||
function transportUnknownError(exception) {
|
||||
this.loadingTask._capability.reject(
|
||||
messageHandler.on('UnknownError', function(exception) {
|
||||
loadingTask._capability.reject(
|
||||
new UnknownErrorException(exception.message, exception.details));
|
||||
}, this);
|
||||
|
||||
messageHandler.on('DataLoaded', function transportPage(data) {
|
||||
messageHandler.on('DataLoaded', function(data) {
|
||||
this.downloadInfoCapability.resolve(data);
|
||||
}, this);
|
||||
|
||||
messageHandler.on('StartRenderPage', function transportRender(data) {
|
||||
messageHandler.on('StartRenderPage', function(data) {
|
||||
if (this.destroyed) {
|
||||
return; // Ignore any pending requests if the worker was terminated.
|
||||
}
|
||||
var page = this.pageCache[data.pageIndex];
|
||||
|
||||
const page = this.pageCache[data.pageIndex];
|
||||
page._stats.timeEnd('Page Request');
|
||||
page._startRenderPage(data.transparency, data.intent);
|
||||
}, this);
|
||||
|
||||
messageHandler.on('RenderPageChunk', function transportRender(data) {
|
||||
messageHandler.on('RenderPageChunk', function(data) {
|
||||
if (this.destroyed) {
|
||||
return; // Ignore any pending requests if the worker was terminated.
|
||||
}
|
||||
var page = this.pageCache[data.pageIndex];
|
||||
|
||||
const page = this.pageCache[data.pageIndex];
|
||||
page._renderPageChunk(data.operatorList, data.intent);
|
||||
}, this);
|
||||
|
||||
messageHandler.on('commonobj', function transportObj(data) {
|
||||
messageHandler.on('commonobj', function(data) {
|
||||
if (this.destroyed) {
|
||||
return; // Ignore any pending requests if the worker was terminated.
|
||||
}
|
||||
|
||||
var id = data[0];
|
||||
var type = data[1];
|
||||
const [id, type, exportedData] = data;
|
||||
if (this.commonObjs.hasData(id)) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case 'Font':
|
||||
var exportedData = data[2];
|
||||
let params = this._params;
|
||||
const params = this._params;
|
||||
|
||||
if ('error' in exportedData) {
|
||||
var exportedError = exportedData.error;
|
||||
warn('Error during font loading: ' + exportedError);
|
||||
const exportedError = exportedData.error;
|
||||
warn(`Error during font loading: ${exportedError}`);
|
||||
this.commonObjs.resolve(id, exportedError);
|
||||
break;
|
||||
}
|
||||
var fontRegistry = null;
|
||||
|
||||
let fontRegistry = null;
|
||||
if (params.pdfBug && globalScope.FontInspector &&
|
||||
globalScope.FontInspector.enabled) {
|
||||
fontRegistry = {
|
||||
@ -1900,44 +1890,40 @@ var WorkerTransport = (function WorkerTransportClosure() {
|
||||
},
|
||||
};
|
||||
}
|
||||
var font = new FontFaceObject(exportedData, {
|
||||
const font = new FontFaceObject(exportedData, {
|
||||
isEvalSupported: params.isEvalSupported,
|
||||
disableFontFace: params.disableFontFace,
|
||||
ignoreErrors: params.ignoreErrors,
|
||||
onUnsupportedFeature: this._onUnsupportedFeature.bind(this),
|
||||
fontRegistry,
|
||||
});
|
||||
var fontReady = (fontObjs) => {
|
||||
const fontReady = (fontObjs) => {
|
||||
this.commonObjs.resolve(id, font);
|
||||
};
|
||||
|
||||
this.fontLoader.bind([font], fontReady);
|
||||
break;
|
||||
case 'FontPath':
|
||||
this.commonObjs.resolve(id, data[2]);
|
||||
this.commonObjs.resolve(id, exportedData);
|
||||
break;
|
||||
default:
|
||||
throw new Error(`Got unknown common object type ${type}`);
|
||||
}
|
||||
}, this);
|
||||
|
||||
messageHandler.on('obj', function transportObj(data) {
|
||||
messageHandler.on('obj', function(data) {
|
||||
if (this.destroyed) {
|
||||
return; // Ignore any pending requests if the worker was terminated.
|
||||
}
|
||||
|
||||
var id = data[0];
|
||||
var pageIndex = data[1];
|
||||
var type = data[2];
|
||||
var pageProxy = this.pageCache[pageIndex];
|
||||
var imageData;
|
||||
const [id, pageIndex, type, imageData] = data;
|
||||
const pageProxy = this.pageCache[pageIndex];
|
||||
if (pageProxy.objs.hasData(id)) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case 'JpegStream':
|
||||
imageData = data[3];
|
||||
return new Promise((resolve, reject) => {
|
||||
const img = new Image();
|
||||
img.onload = function() {
|
||||
@ -1955,11 +1941,10 @@ var WorkerTransport = (function WorkerTransportClosure() {
|
||||
pageProxy.objs.resolve(id, img);
|
||||
});
|
||||
case 'Image':
|
||||
imageData = data[3];
|
||||
pageProxy.objs.resolve(id, imageData);
|
||||
|
||||
// heuristics that will allow not to store large data
|
||||
var MAX_IMAGE_SIZE_TO_STORE = 8000000;
|
||||
// Heuristic that will allow us not to store large data.
|
||||
const MAX_IMAGE_SIZE_TO_STORE = 8000000;
|
||||
if (imageData && 'data' in imageData &&
|
||||
imageData.data.length > MAX_IMAGE_SIZE_TO_STORE) {
|
||||
pageProxy.cleanupAfterRender = true;
|
||||
@ -1970,12 +1955,11 @@ var WorkerTransport = (function WorkerTransportClosure() {
|
||||
}
|
||||
}, this);
|
||||
|
||||
messageHandler.on('DocProgress', function transportDocProgress(data) {
|
||||
messageHandler.on('DocProgress', function(data) {
|
||||
if (this.destroyed) {
|
||||
return; // Ignore any pending requests if the worker was terminated.
|
||||
}
|
||||
|
||||
var loadingTask = this.loadingTask;
|
||||
if (loadingTask.onProgress) {
|
||||
loadingTask.onProgress({
|
||||
loaded: data.loaded,
|
||||
@ -1984,13 +1968,13 @@ var WorkerTransport = (function WorkerTransportClosure() {
|
||||
}
|
||||
}, this);
|
||||
|
||||
messageHandler.on('PageError', function transportError(data) {
|
||||
messageHandler.on('PageError', function(data) {
|
||||
if (this.destroyed) {
|
||||
return; // Ignore any pending requests if the worker was terminated.
|
||||
}
|
||||
|
||||
var page = this.pageCache[data.pageNum - 1];
|
||||
var intentState = page.intentStates[data.intent];
|
||||
const page = this.pageCache[data.pageNum - 1];
|
||||
const intentState = page.intentStates[data.intent];
|
||||
|
||||
if (intentState.displayReadyCapability) {
|
||||
intentState.displayReadyCapability.reject(data.error);
|
||||
@ -2001,7 +1985,7 @@ var WorkerTransport = (function WorkerTransportClosure() {
|
||||
if (intentState.operatorList) {
|
||||
// Mark operator list as complete.
|
||||
intentState.operatorList.lastChunk = true;
|
||||
for (var i = 0; i < intentState.renderTasks.length; i++) {
|
||||
for (let i = 0; i < intentState.renderTasks.length; i++) {
|
||||
intentState.renderTasks[i].operatorListChanged();
|
||||
}
|
||||
}
|
||||
@ -2020,37 +2004,35 @@ var WorkerTransport = (function WorkerTransportClosure() {
|
||||
return Promise.reject(new Error('"document" is not defined.'));
|
||||
}
|
||||
|
||||
var imageUrl = data[0];
|
||||
var components = data[1];
|
||||
const [imageUrl, components] = data;
|
||||
if (components !== 3 && components !== 1) {
|
||||
return Promise.reject(
|
||||
new Error('Only 3 components or 1 component can be returned'));
|
||||
}
|
||||
|
||||
return new Promise(function (resolve, reject) {
|
||||
var img = new Image();
|
||||
const 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 Uint8ClampedArray(size * components);
|
||||
var tmpCanvas = document.createElement('canvas');
|
||||
const width = img.width;
|
||||
const height = img.height;
|
||||
const size = width * height;
|
||||
const rgbaLength = size * 4;
|
||||
const buf = new Uint8ClampedArray(size * components);
|
||||
const tmpCanvas = document.createElement('canvas');
|
||||
tmpCanvas.width = width;
|
||||
tmpCanvas.height = height;
|
||||
var tmpCtx = tmpCanvas.getContext('2d');
|
||||
const tmpCtx = tmpCanvas.getContext('2d');
|
||||
tmpCtx.drawImage(img, 0, 0);
|
||||
var data = tmpCtx.getImageData(0, 0, width, height).data;
|
||||
var i, j;
|
||||
const data = tmpCtx.getImageData(0, 0, width, height).data;
|
||||
|
||||
if (components === 3) {
|
||||
for (i = 0, j = 0; i < rgbaLength; i += 4, j += 3) {
|
||||
for (let 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++) {
|
||||
for (let i = 0, j = 0; i < rgbaLength; i += 4, j++) {
|
||||
buf[j] = data[i];
|
||||
}
|
||||
}
|
||||
@ -2071,21 +2053,20 @@ var WorkerTransport = (function WorkerTransportClosure() {
|
||||
name: data.name,
|
||||
});
|
||||
}, this);
|
||||
},
|
||||
}
|
||||
|
||||
_onUnsupportedFeature({ featureId, }) {
|
||||
if (this.destroyed) {
|
||||
return; // Ignore any pending requests if the worker was terminated.
|
||||
}
|
||||
let loadingTask = this.loadingTask;
|
||||
if (loadingTask.onUnsupportedFeature) {
|
||||
loadingTask.onUnsupportedFeature(featureId);
|
||||
if (this.loadingTask.onUnsupportedFeature) {
|
||||
this.loadingTask.onUnsupportedFeature(featureId);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
getData: function WorkerTransport_getData() {
|
||||
getData() {
|
||||
return this.messageHandler.sendWithPromise('GetData', null);
|
||||
},
|
||||
}
|
||||
|
||||
getPage(pageNumber) {
|
||||
if (!Number.isInteger(pageNumber) ||
|
||||
@ -2093,74 +2074,74 @@ var WorkerTransport = (function WorkerTransportClosure() {
|
||||
return Promise.reject(new Error('Invalid page request'));
|
||||
}
|
||||
|
||||
var pageIndex = pageNumber - 1;
|
||||
const pageIndex = pageNumber - 1;
|
||||
if (pageIndex in this.pagePromises) {
|
||||
return this.pagePromises[pageIndex];
|
||||
}
|
||||
var promise = this.messageHandler.sendWithPromise('GetPage', {
|
||||
const promise = this.messageHandler.sendWithPromise('GetPage', {
|
||||
pageIndex,
|
||||
}).then((pageInfo) => {
|
||||
if (this.destroyed) {
|
||||
throw new Error('Transport destroyed');
|
||||
}
|
||||
let page = new PDFPageProxy(pageIndex, pageInfo, this,
|
||||
const page = new PDFPageProxy(pageIndex, pageInfo, this,
|
||||
this._params.pdfBug);
|
||||
this.pageCache[pageIndex] = page;
|
||||
return page;
|
||||
});
|
||||
this.pagePromises[pageIndex] = promise;
|
||||
return promise;
|
||||
},
|
||||
}
|
||||
|
||||
getPageIndex: function WorkerTransport_getPageIndexByRef(ref) {
|
||||
getPageIndex(ref) {
|
||||
return this.messageHandler.sendWithPromise('GetPageIndex', {
|
||||
ref,
|
||||
}).catch(function(reason) {
|
||||
return Promise.reject(new Error(reason));
|
||||
});
|
||||
},
|
||||
}
|
||||
|
||||
getAnnotations: function WorkerTransport_getAnnotations(pageIndex, intent) {
|
||||
getAnnotations(pageIndex, intent) {
|
||||
return this.messageHandler.sendWithPromise('GetAnnotations', {
|
||||
pageIndex,
|
||||
intent,
|
||||
});
|
||||
},
|
||||
}
|
||||
|
||||
getDestinations: function WorkerTransport_getDestinations() {
|
||||
getDestinations() {
|
||||
return this.messageHandler.sendWithPromise('GetDestinations', null);
|
||||
},
|
||||
}
|
||||
|
||||
getDestination: function WorkerTransport_getDestination(id) {
|
||||
getDestination(id) {
|
||||
if (typeof id !== 'string') {
|
||||
return Promise.reject(new Error('Invalid destination request.'));
|
||||
}
|
||||
return this.messageHandler.sendWithPromise('GetDestination', {
|
||||
id,
|
||||
});
|
||||
},
|
||||
}
|
||||
|
||||
getPageLabels: function WorkerTransport_getPageLabels() {
|
||||
getPageLabels() {
|
||||
return this.messageHandler.sendWithPromise('GetPageLabels', null);
|
||||
},
|
||||
}
|
||||
|
||||
getPageMode() {
|
||||
return this.messageHandler.sendWithPromise('GetPageMode', null);
|
||||
},
|
||||
}
|
||||
|
||||
getAttachments: function WorkerTransport_getAttachments() {
|
||||
getAttachments() {
|
||||
return this.messageHandler.sendWithPromise('GetAttachments', null);
|
||||
},
|
||||
}
|
||||
|
||||
getJavaScript: function WorkerTransport_getJavaScript() {
|
||||
getJavaScript() {
|
||||
return this.messageHandler.sendWithPromise('GetJavaScript', null);
|
||||
},
|
||||
}
|
||||
|
||||
getOutline: function WorkerTransport_getOutline() {
|
||||
getOutline() {
|
||||
return this.messageHandler.sendWithPromise('GetOutline', null);
|
||||
},
|
||||
}
|
||||
|
||||
getMetadata: function WorkerTransport_getMetadata() {
|
||||
getMetadata() {
|
||||
return this.messageHandler.sendWithPromise('GetMetadata', null).
|
||||
then((results) => {
|
||||
return {
|
||||
@ -2170,16 +2151,16 @@ var WorkerTransport = (function WorkerTransportClosure() {
|
||||
this._fullReader.filename : null),
|
||||
};
|
||||
});
|
||||
},
|
||||
}
|
||||
|
||||
getStats: function WorkerTransport_getStats() {
|
||||
getStats() {
|
||||
return this.messageHandler.sendWithPromise('GetStats', null);
|
||||
},
|
||||
}
|
||||
|
||||
startCleanup: function WorkerTransport_startCleanup() {
|
||||
startCleanup() {
|
||||
this.messageHandler.sendWithPromise('Cleanup', null).then(() => {
|
||||
for (var i = 0, ii = this.pageCache.length; i < ii; i++) {
|
||||
var page = this.pageCache[i];
|
||||
for (let i = 0, ii = this.pageCache.length; i < ii; i++) {
|
||||
const page = this.pageCache[i];
|
||||
if (page) {
|
||||
page.cleanup();
|
||||
}
|
||||
@ -2187,21 +2168,18 @@ var WorkerTransport = (function WorkerTransportClosure() {
|
||||
this.commonObjs.clear();
|
||||
this.fontLoader.clear();
|
||||
});
|
||||
},
|
||||
}
|
||||
|
||||
get loadingParams() {
|
||||
let params = this._params;
|
||||
const params = this._params;
|
||||
return shadow(this, 'loadingParams', {
|
||||
disableAutoFetch: params.disableAutoFetch,
|
||||
disableCreateObjectURL: params.disableCreateObjectURL,
|
||||
disableFontFace: params.disableFontFace,
|
||||
nativeImageDecoderSupport: params.nativeImageDecoderSupport,
|
||||
});
|
||||
},
|
||||
};
|
||||
return WorkerTransport;
|
||||
|
||||
})();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A PDF document and page is built of many objects. E.g. there are objects
|
||||
|
Loading…
x
Reference in New Issue
Block a user