Skip to content

Commit

Permalink
Expose mapboxgl.getRTLTextPluginStatus method (#8864)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Hamley authored Oct 15, 2019
1 parent e894e7d commit 036ddc4
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 8 deletions.
36 changes: 36 additions & 0 deletions debug/rtl.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!DOCTYPE html>
<html>
<head>
<title>Mapbox GL JS debug page</title>
<meta charset='utf-8'>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<link rel='stylesheet' href='/dist/mapbox-gl.css' />
<style>
body { margin: 0; padding: 0; }
html, body, #map { height: 100%; }
</style>
</head>

<body>
<div id='map'></div>

<script src='/dist/mapbox-gl-dev.js'></script>
<script src='/debug/access_token_generated.js'></script>
<script>
mapboxgl.setRTLTextPlugin('https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-rtl-text/v0.2.0/mapbox-gl-rtl-text.js');
var map = window.map = new mapboxgl.Map({
container: 'map',
zoom: 16.5,
center: [44.355435, 33.258814],
style: 'mapbox://styles/mapbox/streets-v11',
hash: true
});

setTimeout(function() {
if (['loading', 'loaded'].indexOf(mapboxgl.getRTLTextPluginStatus()) === -1) {
mapboxgl.setRTLTextPlugin('https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-rtl-text/v0.2.0/mapbox-gl-rtl-text.js');
}
}, 2000);
</script>
</body>
</html>
13 changes: 12 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ import Point from '@mapbox/point-geometry';
import MercatorCoordinate from './geo/mercator_coordinate';
import {Evented} from './util/evented';
import config from './util/config';
import {setRTLTextPlugin} from './source/rtl_text_plugin';
import {setRTLTextPlugin, getRTLTextPluginStatus} from './source/rtl_text_plugin';
import WorkerPool from './util/worker_pool';
import {clearTileCache} from './util/tile_request_cache';

const exported = {
version,
supported,
setRTLTextPlugin,
getRTLTextPluginStatus,
Map,
NavigationControl,
GeolocateControl,
Expand Down Expand Up @@ -162,6 +163,16 @@ const exported = {
* @see [Add support for right-to-left scripts](https://www.mapbox.com/mapbox-gl-js/example/mapbox-gl-rtl-text/)
*/

/**
* Gets the map's [RTL text plugin](https://www.mapbox.com/mapbox-gl-js/plugins/#mapbox-gl-rtl-text) status.
* The status can be `unavailable` (i.e. not requested or removed), `loading`, `loaded` or `error`.
* If the status is `loaded` and the plugin is requested again, an error will be thrown.
*
* @function getRTLTextPluginStatus
* @example
* const pluginStatus = mapboxgl.getRTLTextPluginStatus();
*/

export default exported;

// canary assert: used to confirm that asserts have been removed from production build
Expand Down
24 changes: 17 additions & 7 deletions src/source/rtl_text_plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
import {Event, Evented} from '../util/evented';
import browser from '../util/browser';

let pluginRequested = false;
const status = {
unavailable: 'unavailable',
loading: 'loading',
loaded: 'loaded',
error: 'error'
};
let pluginStatus = status.unavailable;
let pluginURL = null;
let foregroundLoadComplete = false;

export const evented = new Evented();

Expand All @@ -14,6 +19,10 @@ type ErrorCallback = (error: Error) => void;

let _completionCallback;

export const getRTLTextPluginStatus = function () {
return pluginStatus;
};

export const registerForPluginAvailability = function(
callback: (args: {pluginURL: string, completionCallback: CompletionCallback}) => void
) {
Expand All @@ -26,26 +35,27 @@ export const registerForPluginAvailability = function(
};

export const clearRTLTextPlugin = function() {
pluginRequested = false;
pluginStatus = status.unavailable;
pluginURL = null;
};

export const setRTLTextPlugin = function(url: string, callback: ErrorCallback) {
if (pluginRequested) {
if (pluginStatus === status.loading || pluginStatus === status.loaded) {
throw new Error('setRTLTextPlugin cannot be called multiple times.');
}
pluginRequested = true;
pluginStatus = status.loading;
pluginURL = browser.resolveURL(url);
_completionCallback = (error?: Error) => {
if (error) {
// Clear loaded state to allow retries
clearRTLTextPlugin();
pluginStatus = status.error;
if (callback) {
callback(error);
}
} else {
// Called once for each worker
foregroundLoadComplete = true;
pluginStatus = status.loaded;
}
};
evented.fire(new Event('pluginAvailable', {pluginURL, completionCallback: _completionCallback}));
Expand All @@ -61,7 +71,7 @@ export const plugin: {
processBidirectionalText: null,
processStyledBidirectionalText: null,
isLoaded() {
return foregroundLoadComplete || // Foreground: loaded if the completion callback returned successfully
return pluginStatus === status.loaded || // Foreground: loaded if the completion callback returned successfully
plugin.applyArabicShaping != null; // Background: loaded if the plugin functions have been compiled
}
};

0 comments on commit 036ddc4

Please sign in to comment.