(info) This AI is still in early stages of development.

Dependency Checking

A dependency checking framework to make working with mutliple includes more reliable...

 

Step 1

Note: It's best to sandbox your code to avoid things leaking out on to the global scope.

Define a Self Descriptor Object:

foo.js - self descriptor object
void (function fooAPI() {
 
	var self = {
	  file: "foo.js",
	  ver : 0.2
	}

Step 2

Define dependencies then make sure they are provided or check individual requirements...

foo.js - dependency checking
// continued...
 
	var dependencies = {
	  "Util.js": 1.0,
	  "bar.js" : Check.ANY_VERSION,
	  "baz.js" : Check.NOT_LOADED
	}
 
	Check.required(dependencies, self); // throws error if something wrong
 
	if (Check.has("wibble.js")) {
	  // do stuff that relies on wibble.js (any version)
	} else {
	  // use a wibble-free alternative
	}

Because the code is being defined in a sandbox, if there are missing dependencies an error will be thrown and the whole sandbox will be blocked from further execution so you don't have to worry about stray bits of broken API being available.

Step 3

After your code is defined, mark it as provided:

foo.js - define your API and confirm it's been provided
// continued...
	// rest of your code
 
    global.addConst("Foo", yourAPI); // whatever your API exposes globally
 
	Check.provide(self); // indicate your API is available
 
})();

If we get to the end of the sandbox, it means no errors have been found so far, so by providing your API at the end of the sandbox other scripts can be fairly sure the API is working from an architectural perspective.

Step 4

Use include() to import files in the usual manner:

main.js - include libraries
include("path/to/Util.js");
include("path/to/bar.js");
include("path/to/baz.js");
include("path/to/foo.js"); // error: baz.js was loaded before foo.js

Or use autoloading by specifying some load paths and let Check load all your dependencies for you:

main.js - include libraries
include("path/to/Util.js"); // always need this - its where Check lives
Check.paths = ["path/to/"]; // one or more paths to look for dependencies in
include("path/to/foo.js");  // autoloads "path/to/bar.js" (dependency of foo.js)

And more...

You can also:

Availability

Requires:

Dependency Checking

Topics: