Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

first stab at async

  • Loading branch information...
commit c0ca34939f86fe8e45108e62026f98a309e5c541 1 parent f12992c
weepy@github.com authored

Showing 1 changed file with 112 additions and 0 deletions. Show diff stats Hide diff stats

  1. +112 0 lib/brequire-async.js
112 lib/brequire-async.js
... ... @@ -0,0 +1,112 @@
  1 +// Brequire - CommonJS support for the browser
  2 +function require(path) {
  3 + var module = require.modules[path];
  4 + if(!module) {
  5 + throw("couldn't find module for: " + path);
  6 + }
  7 + if(!module.exports) {
  8 + module.exports = {};
  9 + module.call(module.exports, module, module.exports, require.bind(path));
  10 + }
  11 + return module.exports;
  12 +}
  13 +
  14 +require.modules = {};
  15 +
  16 +require.bind = function(path) {
  17 + return function(p) {
  18 + var fullPath = path.split('/');
  19 + fullPath.pop();
  20 + var parts = p.split('/');
  21 + for (var i=0; i < parts.length; i++) {
  22 + var part = parts[i];
  23 + if (part == '..') fullPath.pop();
  24 + else if (part != '.') fullPath.push(part);
  25 + }
  26 + return require(fullPath.join('/'));
  27 + };
  28 +};
  29 +
  30 +require.module = function(path, fn) {
  31 + require.modules[path] = fn;
  32 +};
  33 +
  34 +var to_load = []
  35 +
  36 +// function normalize(rel, path) {
  37 +// var fullPath = path.split('/');
  38 +// fullPath.pop();
  39 +// var parts = p.split('/');
  40 +// for (var i=0; i < parts.length; i++) {
  41 +// var part = parts[i];
  42 +// if (part == '..') fullPath.pop();
  43 +// else if (part != '.') fullPath.push(part);
  44 +// }
  45 +// return require(fullPath.join('/'));
  46 +// }
  47 +
  48 +function join(cur, path) {
  49 + var parts = path.split('/');
  50 + var fullPath = cur.split("/")
  51 + for (var i=0; i < parts.length; i++) {
  52 + var part = parts[i];
  53 + if (part == '..') fullPath.pop();
  54 + else if (part != '.') fullPath.push(part);
  55 + }
  56 + return require(fullPath.join('/'));
  57 +}
  58 +
  59 +/*
  60 +module.exports = function chain(fnList, complete) {
  61 + var self = this
  62 + var results = []
  63 +
  64 + function run(i) {
  65 + if(i == fnList.length) return complete(results)
  66 + fnList[i].call(self, function(answer) {
  67 + results[i] = answer
  68 + run.call(self, i+1)
  69 + })
  70 + }
  71 + run.call(self, 0)
  72 +}
  73 +*/
  74 +
  75 +asyncLoadModules = function(rel, paths, complete) {
  76 + var jobs = [path]
  77 +
  78 + function run() {
  79 + var job = jobs.unshift()
  80 +
  81 + load(url, function(text) {
  82 +
  83 + })
  84 + }
  85 +
  86 + load(url, function(text) {
  87 + var r = /require *(['"]([^()])*['"])/
  88 + modules = []
  89 + while(m = r.exec(text)) {
  90 + jobs.push({
  91 + rel: rel
  92 + path: join(cur, m[1])
  93 + })
  94 + }
  95 + asyncLoadModules(rel, modules, function() {
  96 +
  97 + })
  98 + var moduleText = "require.module('" + url + "', function(module, exports, require) {\n " + text + "\n});"
  99 + })
  100 +}
  101 +
  102 +
  103 +function load(url, callback) {
  104 + var xhr = new (window.ActiveXObject || XMLHttpRequest)('Microsoft.XMLHTTP');
  105 + xhr.open('GET', url, true);
  106 + if ('overrideMimeType' in xhr) xhr.overrideMimeType('text/plain');
  107 + xhr.onreadystatechange = function() {
  108 + if (xhr.readyState == 4) callback(url, xhr.responseText)
  109 + }
  110 + try xhr.send(null)
  111 + catch(e) console.log("failed loading: " + url)
  112 +}

0 comments on commit c0ca349

Please sign in to comment.
Something went wrong with that request. Please try again.