From 1c0f56d45e116777cb8a28be46d7d9adbbdb5ab4 Mon Sep 17 00:00:00 2001 From: ryanhamley Date: Fri, 11 Oct 2019 10:50:36 -0700 Subject: [PATCH 1/9] Begin implementing rtl plugin status and event listener --- src/source/rtl_text_plugin.js | 17 +++++++++++++++++ src/util/evented.js | 7 +++++++ 2 files changed, 24 insertions(+) diff --git a/src/source/rtl_text_plugin.js b/src/source/rtl_text_plugin.js index e2838ae37b4..bd9f2c7b565 100644 --- a/src/source/rtl_text_plugin.js +++ b/src/source/rtl_text_plugin.js @@ -3,6 +3,13 @@ import {Event, Evented} from '../util/evented'; import browser from '../util/browser'; +let status = { + unavailable: 'unavailable', + loading: 'loading', + loaded: 'loaded', + error: 'error' +}; +let pluginStatus = status.unavailable; let pluginRequested = false; let pluginURL = null; let foregroundLoadComplete = false; @@ -14,6 +21,11 @@ type ErrorCallback = (error: Error) => void; let _completionCallback; +const updatePluginStatus = (status) => { + console.log('status', status); + evented.fire(new Event('pluginStatusUpdated', {status})); +}; + export const registerForPluginAvailability = function( callback: (args: {pluginURL: string, completionCallback: CompletionCallback}) => void ) { @@ -26,6 +38,7 @@ export const registerForPluginAvailability = function( }; export const clearRTLTextPlugin = function() { + updatePluginStatus(status.unavailable); pluginRequested = false; pluginURL = null; }; @@ -34,12 +47,15 @@ export const setRTLTextPlugin = function(url: string, callback: ErrorCallback) { if (pluginRequested) { throw new Error('setRTLTextPlugin cannot be called multiple times.'); } + updatePluginStatus(status.loading); pluginRequested = true; + pluginURL = browser.resolveURL(url); _completionCallback = (error?: Error) => { if (error) { // Clear loaded state to allow retries clearRTLTextPlugin(); + updatePluginStatus(status.error); if (callback) { callback(error); } @@ -48,6 +64,7 @@ export const setRTLTextPlugin = function(url: string, callback: ErrorCallback) { foregroundLoadComplete = true; } }; + updatePluginStatus(status.loaded); evented.fire(new Event('pluginAvailable', {pluginURL, completionCallback: _completionCallback})); }; diff --git a/src/util/evented.js b/src/util/evented.js index eeab1a88c79..e1db462abda 100644 --- a/src/util/evented.js +++ b/src/util/evented.js @@ -60,6 +60,9 @@ export class Evented { * @returns {Object} `this` */ on(type: *, listener: Listener): this { + if (type === 'pluginStatusUpdated') { + console.log('on', type, listener); + } this._listeners = this._listeners || {}; _addEventListener(type, listener, this._listeners); @@ -106,6 +109,10 @@ export class Evented { const type = event.type; + if (type === 'pluginStatusUpdated') { + console.log('fire', type, event); + } + if (this.listens(type)) { (event: any).target = this; From 100f48a6af400f7294c018d08c91fc7cf62be2ac Mon Sep 17 00:00:00 2001 From: ryanhamley Date: Fri, 11 Oct 2019 14:38:42 -0700 Subject: [PATCH 2/9] Add debug page --- debug/rtl.html | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 debug/rtl.html diff --git a/debug/rtl.html b/debug/rtl.html new file mode 100644 index 00000000000..c4f8dc40f8b --- /dev/null +++ b/debug/rtl.html @@ -0,0 +1,41 @@ + + + + Mapbox GL JS debug page + + + + + + + +
+ + + + + + From a0bec9b37164fda02e78b2cb2f8b1a258848a3a2 Mon Sep 17 00:00:00 2001 From: ryanhamley Date: Fri, 11 Oct 2019 16:28:07 -0700 Subject: [PATCH 3/9] Expose method to get plugin status --- debug/rtl.html | 16 +++++----------- src/index.js | 13 ++++++++++++- src/source/rtl_text_plugin.js | 15 +++++++-------- 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/debug/rtl.html b/debug/rtl.html index c4f8dc40f8b..9a7e1f167cf 100644 --- a/debug/rtl.html +++ b/debug/rtl.html @@ -17,7 +17,6 @@ diff --git a/src/index.js b/src/index.js index 21baf999762..0b497911c4f 100644 --- a/src/index.js +++ b/src/index.js @@ -19,7 +19,7 @@ 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'; @@ -27,6 +27,7 @@ const exported = { version, supported, setRTLTextPlugin, + getRTLTextPluginStatus, Map, NavigationControl, GeolocateControl, @@ -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 diff --git a/src/source/rtl_text_plugin.js b/src/source/rtl_text_plugin.js index bd9f2c7b565..4effab01ccf 100644 --- a/src/source/rtl_text_plugin.js +++ b/src/source/rtl_text_plugin.js @@ -21,10 +21,9 @@ type ErrorCallback = (error: Error) => void; let _completionCallback; -const updatePluginStatus = (status) => { - console.log('status', status); - evented.fire(new Event('pluginStatusUpdated', {status})); -}; +export const getRTLTextPluginStatus = function () { + return pluginStatus; +} export const registerForPluginAvailability = function( callback: (args: {pluginURL: string, completionCallback: CompletionCallback}) => void @@ -38,7 +37,7 @@ export const registerForPluginAvailability = function( }; export const clearRTLTextPlugin = function() { - updatePluginStatus(status.unavailable); + pluginStatus = status.unavailable; pluginRequested = false; pluginURL = null; }; @@ -47,7 +46,7 @@ export const setRTLTextPlugin = function(url: string, callback: ErrorCallback) { if (pluginRequested) { throw new Error('setRTLTextPlugin cannot be called multiple times.'); } - updatePluginStatus(status.loading); + pluginStatus = status.loading; pluginRequested = true; pluginURL = browser.resolveURL(url); @@ -55,7 +54,7 @@ export const setRTLTextPlugin = function(url: string, callback: ErrorCallback) { if (error) { // Clear loaded state to allow retries clearRTLTextPlugin(); - updatePluginStatus(status.error); + pluginStatus = status.error; if (callback) { callback(error); } @@ -64,7 +63,7 @@ export const setRTLTextPlugin = function(url: string, callback: ErrorCallback) { foregroundLoadComplete = true; } }; - updatePluginStatus(status.loaded); + pluginStatus = status.loaded; evented.fire(new Event('pluginAvailable', {pluginURL, completionCallback: _completionCallback})); }; From 2a6405e7eba6d1c99ad1ad13d8ad380d42fdf390 Mon Sep 17 00:00:00 2001 From: ryanhamley Date: Fri, 11 Oct 2019 16:42:23 -0700 Subject: [PATCH 4/9] Lint fix --- src/index.js | 2 +- src/source/rtl_text_plugin.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 0b497911c4f..bc9fe4387ab 100644 --- a/src/index.js +++ b/src/index.js @@ -163,7 +163,7 @@ 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. diff --git a/src/source/rtl_text_plugin.js b/src/source/rtl_text_plugin.js index 4effab01ccf..ae9d192f718 100644 --- a/src/source/rtl_text_plugin.js +++ b/src/source/rtl_text_plugin.js @@ -3,7 +3,7 @@ import {Event, Evented} from '../util/evented'; import browser from '../util/browser'; -let status = { +const status = { unavailable: 'unavailable', loading: 'loading', loaded: 'loaded', @@ -23,7 +23,7 @@ let _completionCallback; export const getRTLTextPluginStatus = function () { return pluginStatus; -} +}; export const registerForPluginAvailability = function( callback: (args: {pluginURL: string, completionCallback: CompletionCallback}) => void From 6e1c8831b8a9a96345102e5c2fd017323c42fb1f Mon Sep 17 00:00:00 2001 From: ryanhamley Date: Fri, 11 Oct 2019 16:43:44 -0700 Subject: [PATCH 5/9] Remove whitespace --- src/source/rtl_text_plugin.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/source/rtl_text_plugin.js b/src/source/rtl_text_plugin.js index ae9d192f718..ba308f668bc 100644 --- a/src/source/rtl_text_plugin.js +++ b/src/source/rtl_text_plugin.js @@ -48,7 +48,6 @@ export const setRTLTextPlugin = function(url: string, callback: ErrorCallback) { } pluginStatus = status.loading; pluginRequested = true; - pluginURL = browser.resolveURL(url); _completionCallback = (error?: Error) => { if (error) { From 56aec25325d1d743d0382004f7bddccfe4a13711 Mon Sep 17 00:00:00 2001 From: ryanhamley Date: Fri, 11 Oct 2019 16:44:41 -0700 Subject: [PATCH 6/9] Remove logs --- src/util/evented.js | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/util/evented.js b/src/util/evented.js index e1db462abda..eeab1a88c79 100644 --- a/src/util/evented.js +++ b/src/util/evented.js @@ -60,9 +60,6 @@ export class Evented { * @returns {Object} `this` */ on(type: *, listener: Listener): this { - if (type === 'pluginStatusUpdated') { - console.log('on', type, listener); - } this._listeners = this._listeners || {}; _addEventListener(type, listener, this._listeners); @@ -109,10 +106,6 @@ export class Evented { const type = event.type; - if (type === 'pluginStatusUpdated') { - console.log('fire', type, event); - } - if (this.listens(type)) { (event: any).target = this; From 300918531e4482a8b36b4b34aefaed93de1f33e6 Mon Sep 17 00:00:00 2001 From: ryanhamley Date: Mon, 14 Oct 2019 12:44:40 -0700 Subject: [PATCH 7/9] Remove pluginRequested --- src/source/rtl_text_plugin.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/source/rtl_text_plugin.js b/src/source/rtl_text_plugin.js index ba308f668bc..1222cd05310 100644 --- a/src/source/rtl_text_plugin.js +++ b/src/source/rtl_text_plugin.js @@ -10,7 +10,6 @@ const status = { error: 'error' }; let pluginStatus = status.unavailable; -let pluginRequested = false; let pluginURL = null; let foregroundLoadComplete = false; @@ -38,16 +37,14 @@ export const registerForPluginAvailability = function( export const clearRTLTextPlugin = function() { pluginStatus = status.unavailable; - pluginRequested = false; 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.'); } pluginStatus = status.loading; - pluginRequested = true; pluginURL = browser.resolveURL(url); _completionCallback = (error?: Error) => { if (error) { From a45ec50543e65b42a49159579ee99eac9bed3cdf Mon Sep 17 00:00:00 2001 From: ryanhamley Date: Mon, 14 Oct 2019 16:57:39 -0700 Subject: [PATCH 8/9] Update isLoaded function to use status --- src/source/rtl_text_plugin.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/source/rtl_text_plugin.js b/src/source/rtl_text_plugin.js index 1222cd05310..e27fff927a6 100644 --- a/src/source/rtl_text_plugin.js +++ b/src/source/rtl_text_plugin.js @@ -56,10 +56,9 @@ export const setRTLTextPlugin = function(url: string, callback: ErrorCallback) { } } else { // Called once for each worker - foregroundLoadComplete = true; + pluginStatus = status.loaded; } }; - pluginStatus = status.loaded; evented.fire(new Event('pluginAvailable', {pluginURL, completionCallback: _completionCallback})); }; @@ -73,7 +72,7 @@ export const plugin: { processBidirectionalText: null, processStyledBidirectionalText: null, isLoaded() { - return foregroundLoadComplete || // Foreground: loaded if the completion callback returned successfully + return pluginStatus.loaded || // Foreground: loaded if the completion callback returned successfully plugin.applyArabicShaping != null; // Background: loaded if the plugin functions have been compiled } }; From 9806c3c6979b6d905b1fa9698a74dfebafc0ece1 Mon Sep 17 00:00:00 2001 From: ryanhamley Date: Mon, 14 Oct 2019 17:30:17 -0700 Subject: [PATCH 9/9] Fix lint/flow errors and improper equality check --- debug/rtl.html | 1 + src/source/rtl_text_plugin.js | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/debug/rtl.html b/debug/rtl.html index 9a7e1f167cf..6257d484629 100644 --- a/debug/rtl.html +++ b/debug/rtl.html @@ -17,6 +17,7 @@