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.
32 lines
876 B
JavaScript
32 lines
876 B
JavaScript
"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 });
|