From 0cc022b0a806f5c5859125778812eb5da41c29e9 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 2 May 2017 11:14:53 +0200 Subject: [PATCH] Replace unnecessary `bind(this)` and `var self = this` statements with arrow functions in remaining `src/core/` files --- src/core/annotation.js | 34 +++++++++++-------------- src/core/chunked_stream.js | 4 +-- src/core/document.js | 51 ++++++++++++++------------------------ src/core/fonts.js | 4 +-- src/core/obj.js | 22 +++++++--------- 5 files changed, 47 insertions(+), 68 deletions(-) diff --git a/src/core/annotation.js b/src/core/annotation.js index d394370ccc025..d33cd08210122 100644 --- a/src/core/annotation.js +++ b/src/core/annotation.js @@ -417,20 +417,17 @@ var Annotation = (function AnnotationClosure() { }, loadResources: function Annotation_loadResources(keys) { - return new Promise(function (resolve, reject) { - this.appearance.dict.getAsync('Resources').then(function (resources) { - if (!resources) { - resolve(); - return; - } - var objectLoader = new ObjectLoader(resources.map, - keys, - resources.xref); - objectLoader.load().then(function() { - resolve(resources); - }, reject); - }, reject); - }.bind(this)); + return this.appearance.dict.getAsync('Resources').then((resources) => { + if (!resources) { + return; + } + var objectLoader = new ObjectLoader(resources.map, + keys, + resources.xref); + return objectLoader.load().then(function() { + return resources; + }); + }); }, getOperatorList: function Annotation_getOperatorList(evaluator, task, @@ -454,15 +451,14 @@ var Annotation = (function AnnotationClosure() { var bbox = appearanceDict.getArray('BBox') || [0, 0, 1, 1]; var matrix = appearanceDict.getArray('Matrix') || [1, 0, 0, 1, 0, 0]; var transform = getTransformMatrix(data.rect, bbox, matrix); - var self = this; - return resourcesPromise.then(function(resources) { + return resourcesPromise.then((resources) => { var opList = new OperatorList(); opList.addOp(OPS.beginAnnotation, [data.rect, transform, matrix]); - return evaluator.getOperatorList(self.appearance, task, - resources, opList).then(function () { + return evaluator.getOperatorList(this.appearance, task, + resources, opList).then(() => { opList.addOp(OPS.endAnnotation, []); - self.appearance.reset(); + this.appearance.reset(); return opList; }); }); diff --git a/src/core/chunked_stream.js b/src/core/chunked_stream.js index fe53699b8e904..2b1539cf87205 100644 --- a/src/core/chunked_stream.js +++ b/src/core/chunked_stream.js @@ -334,12 +334,12 @@ var ChunkedStreamManager = (function ChunkedStreamManagerClosure() { }; rangeReader.read().then(readChunk, reject); }); - promise.then(function (data) { + promise.then((data) => { if (this.aborted) { return; // ignoring any data after abort } this.onReceiveData({ chunk: data, begin, }); - }.bind(this)); + }); // TODO check errors }, diff --git a/src/core/document.js b/src/core/document.js index 415705e27c600..c338f704557b2 100644 --- a/src/core/document.js +++ b/src/core/document.js @@ -225,18 +225,15 @@ var Page = (function PageClosure() { // TODO: add async getInheritedPageProp and remove this. this.resourcesPromise = this.pdfManager.ensure(this, 'resources'); } - return this.resourcesPromise.then(function resourceSuccess() { + return this.resourcesPromise.then(() => { var objectLoader = new ObjectLoader(this.resources.map, keys, this.xref); return objectLoader.load(); - }.bind(this)); + }); }, - getOperatorList: function Page_getOperatorList(handler, task, intent, - renderInteractiveForms) { - var self = this; - + getOperatorList(handler, task, intent, renderInteractiveForms) { var pdfManager = this.pdfManager; var contentStreamPromise = pdfManager.ensure(this, 'getContentStream', []); @@ -259,17 +256,16 @@ var Page = (function PageClosure() { this.evaluatorOptions); var dataPromises = Promise.all([contentStreamPromise, resourcesPromise]); - var pageListPromise = dataPromises.then(function(data) { - var contentStream = data[0]; - var opList = new OperatorList(intent, handler, self.pageIndex); + var pageListPromise = dataPromises.then(([contentStream]) => { + var opList = new OperatorList(intent, handler, this.pageIndex); handler.send('StartRenderPage', { - transparency: partialEvaluator.hasBlendModes(self.resources), - pageIndex: self.pageIndex, + transparency: partialEvaluator.hasBlendModes(this.resources), + pageIndex: this.pageIndex, intent, }); return partialEvaluator.getOperatorList(contentStream, task, - self.resources, opList).then(function () { + this.resources, opList).then(function () { return opList; }); }); @@ -278,10 +274,7 @@ var Page = (function PageClosure() { // page's operator list to render them. var annotationsPromise = pdfManager.ensure(this, 'annotations'); return Promise.all([pageListPromise, annotationsPromise]).then( - function(datas) { - var pageOpList = datas[0]; - var annotations = datas[1]; - + function ([pageOpList, annotations]) { if (annotations.length === 0) { pageOpList.flush(true); return pageOpList; @@ -310,11 +303,7 @@ var Page = (function PageClosure() { }); }, - extractTextContent: function Page_extractTextContent(handler, task, - normalizeWhitespace, - combineTextItems) { - var self = this; - + extractTextContent(handler, task, normalizeWhitespace, combineTextItems) { var pdfManager = this.pdfManager; var contentStreamPromise = pdfManager.ensure(this, 'getContentStream', []); @@ -325,20 +314,18 @@ var Page = (function PageClosure() { 'Font' ]); - var dataPromises = Promise.all([contentStreamPromise, - resourcesPromise]); - return dataPromises.then(function(data) { - var contentStream = data[0]; - var partialEvaluator = new PartialEvaluator(pdfManager, self.xref, - handler, self.pageIndex, - self.idFactory, - self.fontCache, - self.builtInCMapCache, - self.evaluatorOptions); + var dataPromises = Promise.all([contentStreamPromise, resourcesPromise]); + return dataPromises.then(([contentStream]) => { + var partialEvaluator = new PartialEvaluator(pdfManager, this.xref, + handler, this.pageIndex, + this.idFactory, + this.fontCache, + this.builtInCMapCache, + this.evaluatorOptions); return partialEvaluator.getTextContent(contentStream, task, - self.resources, + this.resources, /* stateManager = */ null, normalizeWhitespace, combineTextItems); diff --git a/src/core/fonts.js b/src/core/fonts.js index eb2480f0e968e..78292f8307dff 100644 --- a/src/core/fonts.js +++ b/src/core/fonts.js @@ -633,7 +633,7 @@ var Font = (function FontClosure() { properties.differences); } else { glyphsUnicodeMap = getGlyphsUnicode(); - this.toUnicode.forEach(function(charCode, unicodeCharCode) { + this.toUnicode.forEach((charCode, unicodeCharCode) => { if (!this.composite) { glyphName = (properties.differences[charCode] || properties.defaultEncoding[charCode]); @@ -643,7 +643,7 @@ var Font = (function FontClosure() { } } this.toFontChar[charCode] = unicodeCharCode; - }.bind(this)); + }); } this.loadedName = fontName.split('-')[0]; this.loading = false; diff --git a/src/core/obj.js b/src/core/obj.js index 17915c66fb852..18d017a6000d3 100644 --- a/src/core/obj.js +++ b/src/core/obj.js @@ -429,27 +429,24 @@ var Catalog = (function CatalogClosure() { this.fontCache.forEach(function (promise) { promises.push(promise); }); - return Promise.all(promises).then(function (translatedFonts) { + return Promise.all(promises).then((translatedFonts) => { for (var i = 0, ii = translatedFonts.length; i < ii; i++) { var font = translatedFonts[i].dict; delete font.translated; } this.fontCache.clear(); this.builtInCMapCache = Object.create(null); - }.bind(this)); + }); }, getPage: function Catalog_getPage(pageIndex) { if (!(pageIndex in this.pagePromises)) { this.pagePromises[pageIndex] = this.getPageDict(pageIndex).then( - function (a) { - var dict = a[0]; - var ref = a[1]; - return this.pageFactory.createPage(pageIndex, dict, ref, - this.fontCache, - this.builtInCMapCache); - }.bind(this) - ); + ([dict, ref]) => { + return this.pageFactory.createPage(pageIndex, dict, ref, + this.fontCache, + this.builtInCMapCache); + }); } return this.pagePromises[pageIndex]; }, @@ -1772,8 +1769,7 @@ var ObjectLoader = (function() { } if (pendingRequests.length) { - this.xref.stream.manager.requestRanges(pendingRequests).then( - function pendingRequestCallback() { + this.xref.stream.manager.requestRanges(pendingRequests).then(() => { nodesToVisit = nodesToRevisit; for (var i = 0; i < nodesToRevisit.length; i++) { var node = nodesToRevisit[i]; @@ -1784,7 +1780,7 @@ var ObjectLoader = (function() { } } this._walk(nodesToVisit); - }.bind(this), this.capability.reject); + }, this.capability.reject); return; } // Everything is loaded.