kusto.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. declare global {
  2. interface Window {
  3. __monacoKustoResolvePromise: (value: unknown) => void;
  4. __grafana_public_path__: string;
  5. }
  6. }
  7. const monacoPath = (window.__grafana_public_path__ ?? 'public/') + 'lib/monaco/min/vs';
  8. const scripts = [
  9. [`${monacoPath}/language/kusto/bridge.min.js`],
  10. [
  11. `${monacoPath}/language/kusto/kusto.javascript.client.min.js`,
  12. `${monacoPath}/language/kusto/newtonsoft.json.min.js`,
  13. `${monacoPath}/language/kusto/Kusto.Language.Bridge.min.js`,
  14. ],
  15. ];
  16. function loadScript(script: HTMLScriptElement | string): Promise<void> {
  17. return new Promise((resolve, reject) => {
  18. let scriptEl: HTMLScriptElement;
  19. if (typeof script === 'string') {
  20. scriptEl = document.createElement('script');
  21. scriptEl.src = script;
  22. } else {
  23. scriptEl = script;
  24. }
  25. scriptEl.onload = () => resolve();
  26. scriptEl.onerror = (err) => reject(err);
  27. document.body.appendChild(scriptEl);
  28. });
  29. }
  30. const loadMonacoKusto = () => {
  31. return new Promise((resolve) => {
  32. window.__monacoKustoResolvePromise = resolve;
  33. const script = document.createElement('script');
  34. script.innerHTML = `require(['vs/language/kusto/monaco.contribution'], function() {
  35. window.__monacoKustoResolvePromise();
  36. });`;
  37. return document.body.appendChild(script);
  38. });
  39. };
  40. export default async function loadKusto() {
  41. let promise = Promise.resolve();
  42. for (const parallelScripts of scripts) {
  43. await promise;
  44. // Load all these scripts in parallel, then wait for them all to finish before continuing
  45. // to the next iteration
  46. const allPromises = parallelScripts
  47. .filter((src) => !document.querySelector(`script[src="${src}"]`))
  48. .map((src) => loadScript(src));
  49. await Promise.all(allPromises);
  50. }
  51. await loadMonacoKusto();
  52. }