Last active
August 29, 2015 14:00
-
-
Save dfkaye/11094033 to your computer and use it in GitHub Desktop.
monadic namespace loader sketch...
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
var registry = {}; | |
function define(id) { | |
function define(param) { | |
var self = define.namespace, type = typeof param; | |
// handle invalid type? then... | |
type == 'function' && exec(param, self); | |
return type == 'string' && string(param, define); | |
} | |
// resolve id filename (); then registry, attach and away... | |
registry[id] || (registry[id] = new namespace(id)); | |
define.namespace = registry[id]; | |
return define; | |
} | |
function error(message) { | |
throw new Error(message); | |
} | |
function namespace(filename) { | |
this.id = filename; | |
this.module = module.parent; | |
this.filename = filename | |
this.__filename = __filename; | |
this.__dirname = __dirname; | |
this.require = parentRequire; | |
} | |
function parentRequire(path) { | |
return module.parent.require(path); | |
} | |
parentRequire.cache = module.constructor._cache; | |
function string(path, define) { | |
var self = define.namespace; | |
// parse, load, and away... | |
return define; | |
} | |
function exec(fn, namespace) { | |
// guard clauses | |
namespace.module || error('module is not defined'); | |
make(fn, namespace)(namespace); // or f = make(fn, namespace); f(namespace); | |
return namespace.module.exports | |
} | |
function make(fn, namespace) { | |
// make context from namespace; fn toString; | |
return new function | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment