-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Add option to load rtl-text-plugin lazily #8865
Merged
Merged
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f285c5f
Autoload rtlTextPlugin using rtlTextPluginURL map option
7b55f59
fix flow errors
ad15029
Skip rendering rtl text if plugin is not loaded
5bb6ff6
Merge branch 'master' of github.com:mapbox/mapbox-gl-js into rtl-requ…
51158d9
diferred laoding of plugin while not rendering text
8e03389
Rearchitected rtlTextPlugin -> worker messaging.
4fd3a66
Fix lint issue
c3688f6
Remove rtlTextPlugin Map option
ffe7dea
fancier rtl-text plugin demo page
05db065
Lint
89b3bcb
Apply comments change suggestions from code review
247226e
Switch to assert instead of throwing error
edc94ad
Remove webkitURL and switch to using deferred
767c9dd
Fix unit test
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,143 @@ | ||
// @flow | ||
|
||
import {Event, Evented} from '../util/evented'; | ||
import {getArrayBuffer} from '../util/ajax'; | ||
import browser from '../util/browser'; | ||
import window from '../util/window'; | ||
import assert from 'assert'; | ||
import {isWorker} from '../util/util'; | ||
|
||
const status = { | ||
unavailable: 'unavailable', | ||
loading: 'loading', | ||
unavailable: 'unavailable', // Not loaded | ||
deferred: 'deferred', // The plugin URL has been specified, but loading has been deferred | ||
loading: 'loading', // request in-flight | ||
loaded: 'loaded', | ||
error: 'error' | ||
}; | ||
|
||
export type PluginState = { | ||
pluginStatus: $Values<typeof status>; | ||
pluginURL: ?string, | ||
pluginBlobURL: ?string | ||
}; | ||
|
||
type ErrorCallback = (error: ?Error) => void; | ||
type PluginStateSyncCallback = (state: PluginState) => void; | ||
let _completionCallback = null; | ||
|
||
//Variables defining the current state of the plugin | ||
let pluginStatus = status.unavailable; | ||
let pluginURL = null; | ||
let pluginBlobURL = null; | ||
|
||
export const evented = new Evented(); | ||
export const triggerPluginCompletionEvent = function(error: ?Error) { | ||
if (_completionCallback) { | ||
_completionCallback(error); | ||
} | ||
}; | ||
|
||
type CompletionCallback = (error?: Error) => void; | ||
type ErrorCallback = (error: Error) => void; | ||
function sendPluginStateToWorker() { | ||
evented.fire(new Event('pluginStateChange', {pluginStatus, pluginURL, pluginBlobURL})); | ||
} | ||
|
||
let _completionCallback; | ||
export const evented = new Evented(); | ||
|
||
export const getRTLTextPluginStatus = function () { | ||
return pluginStatus; | ||
}; | ||
|
||
export const registerForPluginAvailability = function( | ||
callback: (args: {pluginURL: string, completionCallback: CompletionCallback}) => void | ||
) { | ||
if (pluginURL) { | ||
callback({pluginURL, completionCallback: _completionCallback}); | ||
} else { | ||
evented.once('pluginAvailable', callback); | ||
} | ||
export const registerForPluginStateChange = function(callback: PluginStateSyncCallback) { | ||
// Do an initial sync of the state | ||
callback({pluginStatus, pluginURL, pluginBlobURL}); | ||
// Listen for all future state changes | ||
evented.on('pluginStateChange', callback); | ||
return callback; | ||
}; | ||
|
||
export const clearRTLTextPlugin = function() { | ||
pluginStatus = status.unavailable; | ||
pluginURL = null; | ||
if (pluginBlobURL) { | ||
window.URL.revokeObjectURL(pluginBlobURL); | ||
} | ||
pluginBlobURL = null; | ||
}; | ||
|
||
export const setRTLTextPlugin = function(url: string, callback: ErrorCallback) { | ||
if (pluginStatus === status.loading || pluginStatus === status.loaded) { | ||
export const setRTLTextPlugin = function(url: string, callback: ?ErrorCallback, deferred: boolean = false) { | ||
if (pluginStatus === status.deferred || pluginStatus === status.loading || pluginStatus === status.loaded) { | ||
throw new Error('setRTLTextPlugin cannot be called multiple times.'); | ||
} | ||
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); | ||
pluginStatus = status.deferred; | ||
_completionCallback = callback; | ||
sendPluginStateToWorker(); | ||
|
||
//Start downloading the plugin immediately if not intending to lazy-load | ||
if (!deferred) { | ||
downloadRTLTextPlugin(); | ||
} | ||
}; | ||
|
||
export const downloadRTLTextPlugin = function() { | ||
if (pluginStatus !== status.deferred || !pluginURL) { | ||
throw new Error('rtl-text-plugin cannot be downloaded unless a pluginURL is specified'); | ||
} | ||
pluginStatus = status.loading; | ||
sendPluginStateToWorker(); | ||
if (pluginURL) { | ||
getArrayBuffer({url: pluginURL}, (error, data) => { | ||
if (error) { | ||
triggerPluginCompletionEvent(error); | ||
} else { | ||
const rtlBlob = new window.Blob([data], {type: 'application/javascript'}); | ||
pluginBlobURL = window.URL.createObjectURL(rtlBlob); | ||
pluginStatus = status.loaded; | ||
sendPluginStateToWorker(); | ||
} | ||
} else { | ||
// Called once for each worker | ||
pluginStatus = status.loaded; | ||
} | ||
}; | ||
evented.fire(new Event('pluginAvailable', {pluginURL, completionCallback: _completionCallback})); | ||
}); | ||
} | ||
}; | ||
|
||
export const plugin: { | ||
applyArabicShaping: ?Function, | ||
processBidirectionalText: ?(string, Array<number>) => Array<string>, | ||
processStyledBidirectionalText: ?(string, Array<number>, Array<number>) => Array<[string, Array<number>]>, | ||
isLoaded: () => boolean | ||
isLoaded: () => boolean, | ||
isLoading: () => boolean, | ||
setState: (state: PluginState) => void, | ||
isParsed: () => boolean, | ||
getURLs: () => { blob: ?string, host: ?string } | ||
} = { | ||
applyArabicShaping: null, | ||
processBidirectionalText: null, | ||
processStyledBidirectionalText: null, | ||
isLoaded() { | ||
return pluginStatus === status.loaded || // Foreground: loaded if the completion callback returned successfully | ||
plugin.applyArabicShaping != null; // Background: loaded if the plugin functions have been compiled | ||
return pluginStatus === status.loaded || // Main Thread: loaded if the completion callback returned successfully | ||
plugin.applyArabicShaping != null; // Web-worker: loaded if the plugin functions have been compiled | ||
}, | ||
isLoading() { // Main Thread Only: query the loading status, this function does not return the correct value in the worker context. | ||
return pluginStatus === status.loading; | ||
}, | ||
setState(state: PluginState) { // Worker thread only: this tells the worker threads that the plugin is available on the Main thread | ||
assert(isWorker(), 'Cannot set the state of the rtl-text-plugin when not in the web-worker context'); | ||
|
||
pluginStatus = state.pluginStatus; | ||
pluginURL = state.pluginURL; | ||
pluginBlobURL = state.pluginBlobURL; | ||
}, | ||
isParsed(): boolean { | ||
assert(isWorker(), 'rtl-text-plugin is only parsed on the worker-threads'); | ||
|
||
return plugin.applyArabicShaping != null && | ||
plugin.processBidirectionalText != null && | ||
plugin.processStyledBidirectionalText != null; | ||
}, | ||
getURLs(): { blob: ?string, host: ?string } { | ||
assert(isWorker(), 'rtl-text-plugin urls can only be queried from the worker threads'); | ||
|
||
return { | ||
blob: pluginBlobURL, | ||
host: pluginURL, | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should there be a warning emitted here so that customers don't just see a map with no labels and wonder what the heck is going on?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've changed the behavior now so that only when lazy loading, does it skip rendering the text.