Browse Source

Custom loader functional for initial load

Steven Silvester 8 years ago
parent
commit
8eb614f847
2 changed files with 34 additions and 32 deletions
  1. 6 11
      jupyterlab/lab.html
  2. 28 21
      jupyterlab/loader.js

+ 6 - 11
jupyterlab/lab.html

@@ -14,7 +14,7 @@ Distributed under the terms of the Modified BSD License.
     <script type="text/javascript" src="{{mathjax_url}}?config={{mathjax_config}}&amp;delayStartupUntil=configured" charset="utf-8"></script>
     {% endif %}
     <link href="{{static_prefix}}/jupyterlab.css" rel="stylesheet">
-    <script src="{{static_url("components/requirejs/require.js") }}" type="text/javascript" charset="utf-8"></script>
+    
 </head>
 
 
@@ -26,16 +26,11 @@ Distributed under the terms of the Modified BSD License.
   "wsUrl": "{{ws_url| urlencode}}",
   "notebookPath": "{{notebook_path | urlencode}}"
 }</script>
-
-<script type="text/javascript">
-requirejs.config({
-    waitSeconds: 20,
-});
-require(['lab/custom.bundle', 'lab/loader.bundle'], function() {
-  console.log('Bundles loaded');
-  require(['jupyterlab-extension@0.1.0/index.js'], function() {
-    console.log('page loaded');
-  });
+<script src="{{static_prefix}}/loader.bundle.js" type="text/javascript" charset="utf-8"></script>
+<script>
+jupyterEnsure('lab/custom.jupyterlab.bundle.js', function(requireFunc) {
+  console.log('bundle loaded', requireFunc);
+  jupyterRequire("jupyterlab-extension@0.1.0/index.js");
 });
 </script>
 </body>

+ 28 - 21
jupyterlab/loader.js

@@ -25,7 +25,7 @@ function jupyterDefine(name, callback) {
 }
 
 
-// Require a jupyter module.
+// Require a jupyter module that has already been loaded.
 function jupyterRequire(moduleRequest) {
   // Check if module is in cache
   var moduleId = findModuleId(moduleRequest)
@@ -53,30 +53,38 @@ function jupyterRequire(moduleRequest) {
 // Ensure a jupyter bundle is loaded on a page.
 function jupyterEnsure(path, callback) {
   // "0" is the signal for "already loaded"
-  if(installedChunks[path] === 0)
+  if (installedChunks[path] === 0) {
     return callback.call(null, jupyterRequire);
+  }
 
   // an array means "currently loading".
-  if(installedChunks[path] !== undefined) {
+  if (Array.isArray(installedChunks[path])) {
     installedChunks[path].push(callback);
-  } else {
-    // start chunk loading
-    installedChunks[path] = [callback];
-    var head = document.getElementsByTagName('head')[0];
-    var script = document.createElement('script');
-    script.type = 'text/javascript';
-    script.charset = 'utf-8';
-    script.async = true;
-    script.src = path;
-    head.appendChild(script);
+    return;
   }
+
+  // start chunk loading
+  installedChunks[path] = [callback];
+  var head = document.getElementsByTagName('head')[0];
+  var script = document.createElement('script');
+  script.type = 'text/javascript';
+  script.charset = 'utf-8';
+  script.async = true;
+  script.onload = function() {
+    var callbacks = installedChunks[path];
+    while(callbacks.length)
+      callbacks.shift().call(null, jupyterRequire);
+    installedChunks[path] = 0;
+  }
+  head.appendChild(script);
+  script.src = path;
 }
 
 
 // Find a module matching a given module request.
-function findModuleId(moduleRequest) {
-  if (lookupCache[moduleRequest]) {
-    return lookupCache[moduleRequest];
+function findModuleId(name) {
+  if (lookupCache[name]) {
+    return lookupCache[name];
   }
   var modules = Object.keys(registered);
   // Get the package name, semver string, and module name.
@@ -113,14 +121,13 @@ function findModuleId(moduleRequest) {
     }
     index = versions.indexOf(best);
   }
-  lookupCache[moduleRequest] = matches[index];
+  lookupCache[name] = matches[index];
   return matches[index];
 }
 
-
 // Add the functions to window.
 jupyterRequire.e = jupyterEnsure;
 
-exports.jupyterDefine = jupyterDefine;
-exports.jupyterRequire = jupyterRequire;
-exports.jupyterEnsure = jupyterEnsure;
+window.jupyterDefine = jupyterDefine;
+window.jupyterRequire = jupyterRequire;
+window.jupyterEnsure = jupyterEnsure;