From 4013d3528d7eaa489607e3fd0db75c69b8570b00 Mon Sep 17 00:00:00 2001 From: yann300 Date: Wed, 24 May 2017 12:48:56 +0200 Subject: [PATCH] return a promise if mappings alrady requested --- src/storage/storageViewer.js | 48 +++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/src/storage/storageViewer.js b/src/storage/storageViewer.js index fde0d56e1e..5aeeece5fb 100644 --- a/src/storage/storageViewer.js +++ b/src/storage/storageViewer.js @@ -11,6 +11,7 @@ class StorageViewer { constructor (_context, _storageResolver, _traceManager) { this.context = _context this.storageResolver = _storageResolver + this.completeMapingsLocationPromise = null _traceManager.accumulateStorageChanges(this.context.stepIndex, this.context.address, {}, (error, storageChanges) => { if (!error) { this.storageChanges = storageChanges @@ -71,30 +72,33 @@ class StorageViewer { * @param {Function} callback */ async mappingsLocation () { - return new Promise((resolve, reject) => { - if (this.completeMappingsLocation) { - return resolve(this.completeMappingsLocation) - } - this.storageResolver.initialPreimagesMappings(this.context.tx, this.context.stepIndex, this.context.address, (error, initialMappingsLocation) => { - if (error) { - reject(error) - } else { - this.extractMappingsLocationChanges(this.storageChanges, (error, mappingsLocationChanges) => { - if (error) { - return reject(error) - } - this.completeMappingsLocation = Object.assign({}, initialMappingsLocation) - for (var key in mappingsLocationChanges) { - if (!initialMappingsLocation[key]) { - initialMappingsLocation[key] = {} - } - this.completeMappingsLocation[key] = Object.assign({}, initialMappingsLocation[key], mappingsLocationChanges[key]) - } - resolve(this.completeMappingsLocation) - }) + if (!this.completeMapingsLocationPromise) { + this.completeMapingsLocationPromise = new Promise((resolve, reject) => { + if (this.completeMappingsLocation) { + return this.completeMappingsLocation } + this.storageResolver.initialPreimagesMappings(this.context.tx, this.context.stepIndex, this.context.address, (error, initialMappingsLocation) => { + if (error) { + reject(error) + } else { + this.extractMappingsLocationChanges(this.storageChanges, (error, mappingsLocationChanges) => { + if (error) { + return reject(error) + } + this.completeMappingsLocation = Object.assign({}, initialMappingsLocation) + for (var key in mappingsLocationChanges) { + if (!initialMappingsLocation[key]) { + initialMappingsLocation[key] = {} + } + this.completeMappingsLocation[key] = Object.assign({}, initialMappingsLocation[key], mappingsLocationChanges[key]) + } + resolve(this.completeMappingsLocation) + }) + } + }) }) - }) + } + return this.completeMapingsLocationPromise } /**