jitsi-meet/react/features/base/util/loadScript.native.js
Lyubomir Marinov d55e0f70d9 Import jitsi/jitsi-meet-react#2f23d98
As an intermediate step on the path to merging jitsi-meet and
jitsi-meet-react, import the whole source code of jitsi-meet-react as it
stands at
2f23d98424
i.e. the lastest master at the time of this import. No modifications are
applied to the imported source code in order to preserve a complete
snapshot of it in the repository of jitsi-meet and, thus, facilitate
comparison later on. Consequently, the source code of jitsi-meet and/or
jitsi-meet-react may not work. For example, jitsi-meet's jshint may be
unable to parse jitsi-meet-react's source code.
2016-10-12 10:31:52 -05:00

50 lines
1.7 KiB
JavaScript

/**
* Loads a script from a specific source. React Native cannot load a JS
* file/resource/URL via a <script> HTML element, so the implementation
* fetches the specified src as plain text (e.g. via XMLHttpRequest) and then
* evaluates the fetched string as JavaScript code (i.e. via the {@link eval}
* function).
*
* @param {string} src - The source from the which the script is to be
* (down)loaded. Only absolute URLs are supported.
* @param {Object} options - Additional options.
* @param {boolean} options.async=true - True to asynchronously load the script
* or false to synchronously load the script.
* @returns {void}
*/
export function loadScript(
src,
options = {
async: true
}) {
return new Promise((resolve, reject) => {
// XXX We are using XMLHttpRequest instead of Fetch API only in order
// to be able to do 'sync' requests. If this not needed, this can be
// replaced with much simpler and readable fetch().
const xhr = new XMLHttpRequest();
xhr.open('GET', src, options.async);
xhr.responseType = 'text';
xhr.onload = () => {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
try {
// eslint-disable-next-line no-eval
eval.call(window, xhr.responseText);
resolve();
} catch (e) {
reject(e);
}
} else {
reject(xhr.statusText);
}
}
};
xhr.onerror = () => reject(xhr.statusText);
xhr.send();
});
}