summaryrefslogtreecommitdiff
path: root/platform/node/index.js
diff options
context:
space:
mode:
authorThiago Marcos P. Santos <thiago@mapbox.com>2016-06-29 21:35:24 +0300
committerMike Morris <mikemorris@users.noreply.github.com>2016-09-06 18:14:22 -0400
commitedec56e6c55bdf992fb2d3f92d770db57c737e3e (patch)
tree51fd82feb9e7bffda6a653e15ecd331ee6095593 /platform/node/index.js
parentd6f667a5e762ce1faec80bee774b805fe7ef5e11 (diff)
downloadqtlocation-mapboxgl-edec56e6c55bdf992fb2d3f92d770db57c737e3e.tar.gz
[node] switch to NodeRequest member fn callback
For (hopefully) better performance than creating a new v8::Context to wrap each callback while still avoiding leaking memory with v8::FunctionTemplate. Adds a JavaScript shim in front of module.exports.Map to wrap the req.respond API internally and preserve the public callback-passing API, while still exporting the correct prototype.
Diffstat (limited to 'platform/node/index.js')
-rw-r--r--platform/node/index.js31
1 files changed, 31 insertions, 0 deletions
diff --git a/platform/node/index.js b/platform/node/index.js
new file mode 100644
index 0000000000..8c59023847
--- /dev/null
+++ b/platform/node/index.js
@@ -0,0 +1,31 @@
+"use strict";
+
+// Shim to wrap req.respond while preserving callback-passing API
+
+var mbgl = require('../../lib/mapbox-gl-native.node');
+var constructor = mbgl.Map.prototype.constructor;
+
+var Map = function(options) {
+ if (!(options instanceof Object)) {
+ throw TypeError("Requires an options object as first argument");
+ }
+
+ if (!options.hasOwnProperty('request') || !(options.request instanceof Function)) {
+ throw TypeError("Options object must have a 'request' method");
+ }
+
+ var request = options.request;
+
+ return new constructor(Object.assign(options, {
+ request: function(req) {
+ request(req, function() {
+ req.respond.apply(req, arguments);
+ });
+ }
+ }));
+};
+
+Map.prototype = mbgl.Map.prototype;
+Map.prototype.constructor = Map;
+
+module.exports = Object.assign(mbgl, { Map: Map });