diff options
author | Langston Smith <langston.smith@mapbox.com> | 2018-01-04 11:15:50 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-04 11:15:50 -0800 |
commit | 2ea955d2751ba6459f99a0695e53505c0a11702b (patch) | |
tree | f54450918b634a2eea1bd2c4ebc671bf1bb06106 /platform/node/index.js | |
parent | f2ec6ae326bad79fea2b06a21151a2835522572a (diff) | |
parent | c62b0af24fc76b4bb2eb34100611dd3ee9ee5536 (diff) | |
download | qtlocation-mapboxgl-upstream/ls-android-readme-tweaks.tar.gz |
Merge branch 'master' into ls-android-readme-tweaksupstream/ls-android-readme-tweaks
Diffstat (limited to 'platform/node/index.js')
-rw-r--r-- | platform/node/index.js | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/platform/node/index.js b/platform/node/index.js index 54ba5c0dc6..5944a0a27d 100644 --- a/platform/node/index.js +++ b/platform/node/index.js @@ -4,6 +4,7 @@ var mbgl = require('../../lib/mapbox_gl_native.node'); var constructor = mbgl.Map.prototype.constructor; +var process = require('process'); var Map = function(options) { if (!(options instanceof Object)) { @@ -18,9 +19,29 @@ var Map = function(options) { return new constructor(Object.assign(options, { request: function(req) { - request(req, function() { - req.respond.apply(req, arguments); - }); + // Protect against `request` implementations that call the callback synchronously, + // call it multiple times, or throw exceptions. + // http://blog.izs.me/post/59142742143/designing-apis-for-asynchrony + + var responded = false; + var callback = function() { + var args = arguments; + if (!responded) { + responded = true; + process.nextTick(function() { + req.respond.apply(req, args); + }); + } else { + console.warn('request function responded multiple times; it should call the callback only once'); + } + }; + + try { + request(req, callback); + } catch (e) { + console.warn('request function threw an exception; it should call the callback with an error instead'); + callback(e); + } } })); }; |