Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Pinjector injector - somewhat Angular 1.x-ish, but I was too lazy to support modules properly
'use strict'; // jshint ignore:line
var pinjector = {};
(function () {
pinjector.module = module;
var modules = {};
function module(name) {
var controllers = {};
var factories = {};
var module = modules[name];
if (!module) {
module = {
controller: controller,
factory: factory,
run: run,
getController: getController,
getFactory: getFactory
};
modules[name] = module;
}
return module;
function controller(constructorName, params) {
controllers[constructorName] = {
name: constructorName,
params: params,
controller: params[params.length -1]
};
}
function factory(constructorName, params) {
factories[constructorName] = {
name: constructorName,
params: params,
constructor: params[params.length -1]
};
}
function run(controllerName) {
resolve();
var controllerDefinition = controllers[controllerName];//module.getController(controllerName);
if (!controllerDefinition) {
throw 'Specified controller "' + controllerName + '" is not registered. Unable to run.';
}
var main = controllerDefinition.controller.main;
if (!main) {
throw 'Controller "' + controllerName + '" doesn\'t export a main function. Unable to run.';
}
main();
}
function getController(controllerName) {
return controllers[controllerName].controller;
}
function getFactory(factoryName) {
return resolveFactoryName(factoryName);
}
function resolve() {
for (var propName in factories) {
if (!factories.hasOwnProperty(propName)) {
continue;
}
resolveFactoryName(propName);
}
resolveControllerDependencies();
}
function resolveControllerDependencies() {
for (var propName in controllers) {
if (!controllers.hasOwnProperty(propName)) {
continue;
}
var controllerDefinition = controllers[propName];
resolveFactoryDefinition(controllerDefinition);
}
}
function resolveFactoryName(factoryName) {
var definition = factories[factoryName];
if (definition) {
return resolveFactoryDefinition(definition);
}
return null;
}
function resolveFactoryDefinition(factoryDefinition) {
if (factoryDefinition.factory) {
return factoryDefinition.factory;
}
var dependencies = [];
var size = factoryDefinition.params.length - 1;
for (var index = 0; index < size; ++index) {
var name = factoryDefinition.params[index];
var dependency = resolveFactoryName(name);
if (dependency) {
dependencies.push(dependency);
} else {
throw 'Unable to resolve dependency "' +
name +
'" for factory "' +
factoryDefinition.name +
'".';
}
}
var constructor = factoryDefinition.params[size];
var factory = constructor.apply(constructor, dependencies);
factoryDefinition.factory = factory;
return factory;
}
}
}());
/*
This code is licensed under the terms of the MIT License as described
in LICENSE.txt.
Copyright (c) 2015 - 2017 Bart Read, arcade.ly (https://arcade.ly),
and bartread.com Ltd (http://www.bartread.com/)
The pinjector is loosely based on a limited subset of the Angular 1.x
module API and injector that is used for service registration and
dependency injection by games at http://arcade.ly. It exists because
the first game was initially implemented using AngularJS until Bart
experience a moment of clarity in which he realised this was an
insane thing to use Angular for but couldn't be bothered with the
faff of switching everything over to a different module system and
API.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.