From b40ed13f477c23c69d05d77e03ab5fbe60f92d4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Wed, 21 Mar 2018 19:08:37 +0100 Subject: [PATCH] Do not use step to deal with asyn code --- .../provider/create-layergroup-provider.js | 84 +++++++++---------- .../mapconfig/provider/map-store-provider.js | 81 ++++++++---------- .../mapconfig/provider/named-map-provider.js | 82 ++++++++---------- 3 files changed, 108 insertions(+), 139 deletions(-) diff --git a/lib/cartodb/models/mapconfig/provider/create-layergroup-provider.js b/lib/cartodb/models/mapconfig/provider/create-layergroup-provider.js index 0eef42f8..36298eb1 100644 --- a/lib/cartodb/models/mapconfig/provider/create-layergroup-provider.js +++ b/lib/cartodb/models/mapconfig/provider/create-layergroup-provider.js @@ -27,7 +27,13 @@ module.exports = CreateLayergroupMapConfigProvider; CreateLayergroupMapConfigProvider.prototype.getMapConfig = function(callback) { var self = this; + + if (this.mapConfig && this.params && this.context) { + return callback(null, this.mapConfig, this.params, this.context); + } + var context = {}; + step( function prepareContextLimits() { self.userLimitsApi.getRenderLimits(self.user, self.params.api_key, this); @@ -35,6 +41,7 @@ CreateLayergroupMapConfigProvider.prototype.getMapConfig = function(callback) { function handleRenderLimits(err, renderLimits) { assert.ifError(err); context.limits = renderLimits; + self.context = context; return null; }, function finish(err) { @@ -52,59 +59,50 @@ CreateLayergroupMapConfigProvider.prototype.filter = MapStoreMapConfigProvider.p CreateLayergroupMapConfigProvider.prototype.createKey = MapStoreMapConfigProvider.prototype.createKey; CreateLayergroupMapConfigProvider.prototype.getAffectedTables = function (callback) { - var self = this; + this.getMapConfig((err, mapConfig) => { + if (err) { + return callback(err); + } - const { dbname } = self.params; - const token = self.mapConfig.id(); + const { dbname } = this.params; + const token = mapConfig.id(); - if (self.affectedTablesCache.hasAffectedTables(dbname, token)) { - const affectedTables = self.affectedTablesCache.get(dbname, token); - return callback(null, affectedTables); - } + if (this.affectedTablesCache.hasAffectedTables(dbname, token)) { + const affectedTables = this.affectedTablesCache.get(dbname, token); + return callback(null, affectedTables); + } - step( - function getSql() { - const queries = []; + const queries = []; - self.mapConfig.getLayers().forEach(function(layer) { - queries.push(layer.options.sql); - if (layer.options.affected_tables) { - layer.options.affected_tables.map(table => { - queries.push(`SELECT * FROM ${table} LIMIT 0`); - }); - } - }); - - const sql = queries.length ? queries.join(';') : null; - - if (!sql) { - return callback(); + this.mapConfig.getLayers().forEach(function(layer) { + queries.push(layer.options.sql); + if (layer.options.affected_tables) { + layer.options.affected_tables.map(table => { + queries.push(`SELECT * FROM ${table} LIMIT 0`); + }); } + }); - return sql; - }, - function getAffectedTables(err, sql) { - assert.ifError(err); + const sql = queries.length ? queries.join(';') : null; - step( - function getConnection() { - self.pgConnection.getConnection(self.user, this); - }, - function getAffectedTables(err, connection) { - assert.ifError(err); - QueryTables.getAffectedTablesFromQuery(connection, sql, this); - }, - this - ); - }, - function finish(err, affectedTables) { + if (!sql) { + return callback(); + } + + this.pgConnection.getConnection(this.user, (err, connection) => { if (err) { return callback(err); } - self.affectedTablesCache.set(dbname, token, affectedTables); + QueryTables.getAffectedTablesFromQuery(connection, sql, (err, affectedTables) => { + if (err) { + return callback(err); + } - return callback(null, affectedTables); - } - ); + this.affectedTablesCache.set(dbname, token, affectedTables); + + callback(null, affectedTables); + }); + }); + }); }; diff --git a/lib/cartodb/models/mapconfig/provider/map-store-provider.js b/lib/cartodb/models/mapconfig/provider/map-store-provider.js index 2c94de92..ccf1472e 100644 --- a/lib/cartodb/models/mapconfig/provider/map-store-provider.js +++ b/lib/cartodb/models/mapconfig/provider/map-store-provider.js @@ -90,64 +90,51 @@ MapStoreMapConfigProvider.prototype.createKey = function(base) { }; MapStoreMapConfigProvider.prototype.getAffectedTables = function(callback) { - var self = this; + this.getMapConfig((err, mapConfig) => { + if (err) { + return callback(err); + } - const { dbname, token } = self.params; + const { dbname } = this.params; + const token = mapConfig.id(); - if (self.affectedTablesCache.hasAffectedTables(dbname, token)) { - const affectedTables = self.affectedTablesCache.get(dbname, token); + if (this.affectedTablesCache.hasAffectedTables(dbname, token)) { + const affectedTables = this.affectedTablesCache.get(dbname, token); - return callback(null, affectedTables); - } + return callback(null, affectedTables); + } - step( - function getMapConfig() { - self.getMapConfig(this); - }, - function getSql(err, mapConfig) { - assert.ifError(err); + const queries = []; - const queries = []; - - mapConfig.getLayers().forEach(function(layer) { - queries.push(layer.options.sql); - if (layer.options.affected_tables) { - layer.options.affected_tables.map(table => { - queries.push(`SELECT * FROM ${table} LIMIT 0`); - }); - } - }); - - const sql = queries.length ? queries.join(';') : null; - - if (!sql) { - return callback(); + mapConfig.getLayers().forEach(function(layer) { + queries.push(layer.options.sql); + if (layer.options.affected_tables) { + layer.options.affected_tables.map(table => { + queries.push(`SELECT * FROM ${table} LIMIT 0`); + }); } + }); - return sql; - }, - function getAffectedTables(err, sql) { - assert.ifError(err); + const sql = queries.length ? queries.join(';') : null; - step( - function getConnection() { - self.pgConnection.getConnection(self.user, this); - }, - function getAffectedTables(err, connection) { - assert.ifError(err); - QueryTables.getAffectedTablesFromQuery(connection, sql, this); - }, - this - ); - }, - function finish(err, affectedTables) { + if (!sql) { + return callback(); + } + + this.pgConnection.getConnection(this.user, (err, connection) => { if (err) { return callback(err); } - self.affectedTablesCache.set(dbname, token, affectedTables); + QueryTables.getAffectedTablesFromQuery(connection, sql, (err, affectedTables) => { + if (err) { + return callback(err); + } - return callback(err, affectedTables); - } - ); + this.affectedTablesCache.set(dbname, token, affectedTables); + + callback(err, affectedTables); + }); + }); + }); }; diff --git a/lib/cartodb/models/mapconfig/provider/named-map-provider.js b/lib/cartodb/models/mapconfig/provider/named-map-provider.js index 0fa003e2..50612065 100644 --- a/lib/cartodb/models/mapconfig/provider/named-map-provider.js +++ b/lib/cartodb/models/mapconfig/provider/named-map-provider.js @@ -263,66 +263,50 @@ NamedMapMapConfigProvider.prototype.getTemplateName = function() { }; NamedMapMapConfigProvider.prototype.getAffectedTables = function(callback) { - var self = this; + this.getMapConfig((err, mapConfig) => { + if (err) { + return callback(err); + } - let dbname = null; - let token = null; + const { dbname } = this.rendererParams; + const token = mapConfig.id(); - step( - function getMapConfig() { - self.getMapConfig(this); - }, - function getSql(err, mapConfig) { - assert.ifError(err); + if (this.affectedTablesCache.hasAffectedTables(dbname, token)) { + const affectedTables = this.affectedTablesCache.get(dbname, token); + return callback(null, affectedTables); + } - dbname = self.rendererParams; - token = mapConfig.id(); + const queries = []; - if (self.affectedTablesCache.hasAffectedTables(dbname, token)) { - const affectedTables = self.affectedTablesCache.get(dbname, token); - return callback(null, affectedTables); + mapConfig.getLayers().forEach(layer => { + queries.push(layer.options.sql); + if (layer.options.affected_tables) { + layer.options.affected_tables.map(table => { + queries.push(`SELECT * FROM ${table} LIMIT 0`); + }); } + }); - const queries = []; + const sql = queries.length ? queries.join(';') : null; - mapConfig.getLayers().forEach(layer => { - queries.push(layer.options.sql); - if (layer.options.affected_tables) { - layer.options.affected_tables.map(table => { - queries.push(`SELECT * FROM ${table} LIMIT 0`); - }); - } - }); + if (!sql) { + return callback(); + } - const sql = queries.length ? queries.join(';') : null; - - if (!sql) { - return callback(); - } - - return sql; - }, - function getAffectedTables(err, sql) { - assert.ifError(err); - step( - function getConnection() { - self.pgConnection.getConnection(self.owner, this); - }, - function getAffectedTables(err, connection) { - assert.ifError(err); - QueryTables.getAffectedTablesFromQuery(connection, sql, this); - }, - this - ); - }, - function finish(err, affectedTables) { + this.pgConnection.getConnection(this.owner, (err, connection) => { if (err) { return callback(err); } - self.affectedTablesCache.set(dbname, token, affectedTables); + QueryTables.getAffectedTablesFromQuery(connection, sql, (err, affectedTables) => { + if (err) { + return callback(err); + } - return callback(err, affectedTables); - } - ); + this.affectedTablesCache.set(dbname, token, affectedTables); + + callback(err, affectedTables); + }); + }); + }); };