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

Disable using Transferables in Safari #9003

Merged
merged 9 commits into from
Nov 21, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 3 additions & 1 deletion src/data/feature_position_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ export default class FeaturePositionMap {

sort(ids, positions, 0, ids.length - 1);

transferables.push(ids.buffer, positions.buffer);
if (transferables) {
transferables.push(ids.buffer, positions.buffer);
}

return {ids, positions};
}
Expand Down
9 changes: 6 additions & 3 deletions src/util/actor.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow

import {bindAll, isWorker} from './util';
import {bindAll, isWorker, isSafari} from './util';
import window from './window';
import {serialize, deserialize} from './web_worker_transfer';
import ThrottledInvoker from './throttled_invoker';

Expand Down Expand Up @@ -28,6 +29,7 @@ class Actor {
taskQueue: Array<number>;
cancelCallbacks: { number: Cancelable };
invoker: ThrottledInvoker;
globalScope: any;

constructor(target: any, parent: any, mapId: ?number) {
this.target = target;
Expand All @@ -40,6 +42,7 @@ class Actor {
bindAll(['receive', 'process'], this);
this.invoker = new ThrottledInvoker(this.process);
this.target.addEventListener('message', this.receive, false);
this.globalScope = isWorker() ? target : window;
}

/**
Expand All @@ -59,7 +62,7 @@ class Actor {
if (callback) {
this.callbacks[id] = callback;
}
const buffers: Array<Transferable> = [];
const buffers: ?Array<Transferable> = isSafari(this.globalScope) ? undefined : [];
this.target.postMessage({
id,
type,
Expand Down Expand Up @@ -158,10 +161,10 @@ class Actor {
}
} else {
let completed = false;
const buffers: ?Array<Transferable> = isSafari(this.globalScope) ? undefined : [];
const done = task.hasCallback ? (err, data) => {
completed = true;
delete this.cancelCallbacks[id];
const buffers: Array<Transferable> = [];
this.target.postMessage({
id,
type: '<response>',
Expand Down
22 changes: 22 additions & 0 deletions src/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,28 @@ export function parseCacheControl(cacheControl: string): Object {
return header;
}

let _isSafari = null;

/**
* Retuns true when run in WebKit derived browsers.
* This is used as a workaround for a memory leak in Safari caused by using Transferable objects to
* transfer data between WebWorker's and the main thread.
* https://github.com/mapbox/mapbox-gl-js/issues/8771
*
* This should be removed once the underlying Safari issue is fixed.
*
* @private
* @returns {boolean}
*/
export function isSafari(scope: any): boolean {
if (_isSafari == null) {
const userAgent = scope.navigator ? scope.navigator.userAgent : null;
_isSafari = !!scope.safari ||
!!(userAgent && (/\b(iPad|iPhone|iPod)\b/.test(userAgent) || (!!userAgent.match('Safari') && !userAgent.match('Chrome'))));
}
return _isSafari;
}

export function storageAvailable(type: string): boolean {
try {
const storage = window[type];
Expand Down
2 changes: 1 addition & 1 deletion src/util/web_worker_transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ function isArrayBuffer(val: any): boolean {
*
* @private
*/
export function serialize(input: mixed, transferables?: Array<Transferable>): Serialized {
export function serialize(input: mixed, transferables: ?Array<Transferable>): Serialized {
if (input === null ||
input === undefined ||
typeof input === 'boolean' ||
Expand Down