Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Fetch API in browsers that support it #7371

Merged
merged 3 commits into from
Oct 9, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/source/geojson_worker_source.js
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ import type Actor from '../util/actor';
import type StyleLayerIndex from '../style/style_layer_index';

import type {LoadVectorDataCallback} from './vector_tile_worker_source';
import type {RequestParameters} from '../util/ajax';
import type { RequestParameters, ResponseCallback } from '../util/ajax';
import type { Callback } from '../types/callback';
import type {GeoJSONFeature} from '@mapbox/geojson-types';

@@ -33,7 +33,7 @@ export type LoadGeoJSONParameters = {
geojsonVtOptions?: Object
};

export type LoadGeoJSON = (params: LoadGeoJSONParameters, callback: Callback<mixed>) => void;
export type LoadGeoJSON = (params: LoadGeoJSONParameters, callback: ResponseCallback<Object>) => void;

export interface GeoJSONIndex {
getTile(z: number, x: number, y: number): Object;
@@ -161,7 +161,7 @@ class GeoJSONWorkerSource extends VectorTileWorkerSource {
const perf = (params && params.request && params.request.collectResourceTiming) ?
new performance.Performance(params.request) : false;

this.loadGeoJSON(params, (err, data) => {
this.loadGeoJSON(params, (err: ?Error, data: ?Object) => {
if (err || !data) {
return callback(err);
} else if (typeof data !== 'object') {
@@ -254,7 +254,7 @@ class GeoJSONWorkerSource extends VectorTileWorkerSource {
* @param [params.url] A URL to the remote GeoJSON data.
* @param [params.data] Literal GeoJSON data. Must be provided if `params.url` is not.
*/
loadGeoJSON(params: LoadGeoJSONParameters, callback: Callback<mixed>) {
loadGeoJSON(params: LoadGeoJSONParameters, callback: ResponseCallback<Object>) {
// Because of same origin issues, urls must either include an explicit
// origin or absolute path.
// ie: /foo/bar.json or http://example.com/bar.json
2 changes: 1 addition & 1 deletion src/source/load_tilejson.js
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ import type {TileJSON} from '../types/tilejson';
import type {Cancelable} from '../types/cancelable';

export default function(options: any, requestTransformFn: RequestTransformFunction, callback: Callback<TileJSON>): Cancelable {
const loaded = function(err, tileJSON: any) {
const loaded = function(err: ?Error, tileJSON: ?Object) {
if (err) {
return callback(err);
} else if (tileJSON) {
14 changes: 7 additions & 7 deletions src/source/vector_tile_worker_source.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow

import {getArrayBuffer} from '../util/ajax';
import { getArrayBuffer } from '../util/ajax';

import vt from '@mapbox/vector-tile';
import Protobuf from 'pbf';
@@ -43,15 +43,15 @@ export type LoadVectorData = (params: WorkerTileParameters, callback: LoadVector
* @private
*/
function loadVectorTile(params: WorkerTileParameters, callback: LoadVectorDataCallback) {
const request = getArrayBuffer(params.request, (err, response) => {
const request = getArrayBuffer(params.request, (err: ?Error, data: ?ArrayBuffer, cacheControl: ?string, expires: ?string) => {
if (err) {
callback(err);
} else if (response) {
} else if (data) {
callback(null, {
vectorTile: new vt.VectorTile(new Protobuf(response.data)),
rawData: response.data,
cacheControl: response.cacheControl,
expires: response.expires
vectorTile: new vt.VectorTile(new Protobuf(data)),
rawData: data,
cacheControl: cacheControl,
expires: expires
});
}
});
7 changes: 6 additions & 1 deletion src/source/worker.js
Original file line number Diff line number Diff line change
@@ -32,6 +32,7 @@ export default class Worker {
workerSourceTypes: { [string]: Class<WorkerSource> };
workerSources: { [string]: { [string]: { [string]: WorkerSource } } };
demWorkerSources: { [string]: { [string]: RasterDEMTileWorkerSource } };
referrer: ?string;

constructor(self: WorkerGlobalScopeInterface) {
this.self = self;
@@ -65,6 +66,10 @@ export default class Worker {
};
}

setReferrer(mapID: string, referrer: string) {
this.referrer = referrer;
}

setLayers(mapId: string, layers: Array<LayerSpecification>, callback: WorkerTileCallback) {
this.getLayerIndex(mapId).replace(layers);
callback();
@@ -196,5 +201,5 @@ export default class Worker {
if (typeof WorkerGlobalScope !== 'undefined' &&
typeof self !== 'undefined' &&
self instanceof WorkerGlobalScope) {
new Worker(self);
self.worker = new Worker(self);
}
7 changes: 4 additions & 3 deletions src/style/load_glyph_range.js
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
import { normalizeGlyphsURL } from '../util/mapbox';

import { getArrayBuffer, ResourceType } from '../util/ajax';

import parseGlyphPBF from './parse_glyph_pbf';

import type {StyleGlyph} from './style_glyph';
@@ -23,13 +24,13 @@ export default function (fontstack: string,
.replace('{range}', `${begin}-${end}`),
ResourceType.Glyphs);

getArrayBuffer(request, (err, response) => {
getArrayBuffer(request, (err: ?Error, data: ?ArrayBuffer) => {
if (err) {
callback(err);
} else if (response) {
} else if (data) {
const glyphs = {};

for (const glyph of parseGlyphPBF(response.data)) {
for (const glyph of parseGlyphPBF(data)) {
glyphs[glyph.id] = glyph;
}

2 changes: 1 addition & 1 deletion src/style/load_sprite.js
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ export default function(baseURL: string,
let json: any, image, error;
const format = browser.devicePixelRatio > 1 ? '@2x' : '';

let jsonRequest = getJSON(transformRequestCallback(normalizeSpriteURL(baseURL, format, '.json'), ResourceType.SpriteJSON), (err, data) => {
let jsonRequest = getJSON(transformRequestCallback(normalizeSpriteURL(baseURL, format, '.json'), ResourceType.SpriteJSON), (err: ?Error, data: ?Object) => {
jsonRequest = null;
if (!error) {
error = err;
8 changes: 5 additions & 3 deletions src/style/style.js
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ import GlyphManager from '../render/glyph_manager';
import Light from './light';
import LineAtlas from '../render/line_atlas';
import { pick, clone, extend, deepEqual, filterObject, mapObject } from '../util/util';
import { getJSON, ResourceType } from '../util/ajax';
import { getJSON, getReferrer, ResourceType } from '../util/ajax';
import { isMapboxURL, normalizeStyleURL } from '../util/mapbox';
import browser from '../util/browser';
import Dispatcher from '../util/dispatcher';
@@ -144,6 +144,8 @@ class Style extends Evented {

this._resetUpdates();

this.dispatcher.broadcast('setReferrer', getReferrer());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are dispatching the referrer to the workers when we set up the Style object. On single page applications, the pathname may change, but we'll continue to use the initial pathname. Should we rebroadcast a new pathname?


const self = this;
this._rtlTextPluginCallback = Style.registerForPluginAvailability((args) => {
self.dispatcher.broadcast('loadRTLTextPlugin', args.pluginURL, args.completionCallback);
@@ -188,12 +190,12 @@ class Style extends Evented {
url = normalizeStyleURL(url, options.accessToken);
const request = this.map._transformRequest(url, ResourceType.Style);

this._request = getJSON(request, (error, json) => {
this._request = getJSON(request, (error: ?Error, json: ?Object) => {
this._request = null;
if (error) {
this.fire(new ErrorEvent(error));
} else if (json) {
this._load((json: any), validate);
this._load(json, validate);
}
});
}
3 changes: 3 additions & 0 deletions src/types/window.js
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@ export interface Window extends EventTarget, IDBEnvironment {
+isSecureContext: boolean;
+length: number;
+location: Location;
+origin: string;
name: string;
+navigator: Navigator;
offscreenBuffering: string | boolean;
@@ -131,6 +132,8 @@ export interface Window extends EventTarget, IDBEnvironment {
WheelEvent: typeof WheelEvent;
Worker: typeof Worker;
XMLHttpRequest: typeof XMLHttpRequest;
Request: typeof Request;
AbortController: any;

alert(message?: any): void;
blur(): void;
Loading