/** @license MIT License (c) copyright B Cavalier & J Hann */

/**
 * plugins
 * Licensed under the MIT License at:
 * http://www.opensource.org/licenses/mit-license.php
 * @author: brian@hovercraftstudios.com
 */
(function(define) {
define(function(require) {

	var when, array, object, priority, nsKey, nsSeparator;

	when = require('when');
	array = require('../array');
	object = require('../object');
	priority = require('./priority');

	nsKey = '$ns';
	nsSeparator = ':';

	function PluginRegistry(parent) {
		if(!parent) {
			parent = {};
		}

		this.plugins = [];
		this._namespaces = {};

		this.contextListeners = [];

		this.listeners = array.delegate(parent.listeners);
		this.proxiers =  array.delegate(parent.proxiers);
		this.resolvers = object.inherit(parent.resolvers);
		this.factories = object.inherit(parent.factories);
		this.facets =    object.inherit(parent.facets);
	}

	PluginRegistry.prototype = {
		scanModule: function (module, spec, namespace) {
			var self, pluginFactory;

			pluginFactory = discoverPlugin(module);

			if (!allowPlugin(pluginFactory, this.plugins)) {
				return when.resolve();
			}

			// Add to singleton plugins list to only allow one instance
			// of this plugin in the current context.
			this.plugins.push(pluginFactory);

			// Initialize the plugin for this context
			self = this;
			return when(pluginFactory(spec),
				function (plugin) {
					plugin && self.registerPlugin(plugin, namespace || getNamespace(spec));
				}
			).yield();
		},

		registerPlugin: function (plugin, namespace) {
			addNamespace(namespace, this._namespaces);

			addPlugin(plugin.resolvers, this.resolvers, namespace);
			addPlugin(plugin.factories, this.factories, namespace);
			addPlugin(plugin.facets, this.facets, namespace);

			this.listeners.push(plugin);
			if(plugin.context) {
				this.contextListeners.push(plugin.context);
			}

			this._registerProxies(plugin.proxies);
		},

		_registerProxies: function (proxiesToAdd) {
			if (!proxiesToAdd) {
				return;
			}

			this.proxiers = priority.sortReverse(array.union(this.proxiers, proxiesToAdd));
		}
	};

	return PluginRegistry;

	function discoverPlugin(module) {
		var plugin;

		if(typeof module.wire$plugin === 'function') {
			plugin = module.wire$plugin;
		} else if(typeof module === 'function') {
			plugin = module;
		}

		return plugin;
	}

	function getNamespace(spec) {
		var namespace;
		if(typeof spec === 'object' && nsKey in spec) {
			// A namespace was provided
			namespace = spec[nsKey];
		}

		return namespace;
	}

	function addNamespace(namespace, namespaces) {
		if(namespace && namespace in namespaces) {
			throw new Error('plugin namespace already in use: ' + namespace);
		} else {
			namespaces[namespace] = 1;
		}
	}

	function allowPlugin(plugin, existing) {
		return typeof plugin === 'function' && existing.indexOf(plugin) === -1;
	}

	function addPlugin(src, registry, namespace) {
		var newPluginName, namespacedName;
		for (newPluginName in src) {
			namespacedName = makeNamespace(newPluginName, namespace);
			if (object.hasOwn(registry, namespacedName)) {
				throw new Error("Two plugins for same type in scope: " + namespacedName);
			}

			registry[namespacedName] = src[newPluginName];
		}
	}

	function makeNamespace(pluginName, namespace) {
		return namespace ? (namespace + nsSeparator + pluginName) : pluginName;
	}
});
}(typeof define === 'function' ? define : function(factory) { module.exports = factory(require); }));

@@ -501,54 +501,11 @@
 try(
-parent) %7B%0A%09%09if(!parent) %7B%0A%09%09%09parent = %7B%7D;%0A%09%09%7D%0A
+) %7B
 %0A%09%09t
@@ -578,17 +578,16 @@
 s = %5B%5D;%0A
-%0A
 %09%09this.l
@@ -601,248 +601,102 @@
 s = 
-array.delegate(parent.listeners);%0A%09%09this.proxiers =  array.delegate(parent.proxiers);%0A%09%09this.resolvers = object.inherit(parent.resolvers);%0A%09%09this.factories = object.inherit(parent.factories);%0A%09%09this.facets =    object.inherit(parent.facets)
+%5B%5D;%0A%09%09this.proxiers =  %5B%5D;%0A%09%09this.resolvers = %7B%7D;%0A%09%09this.factories = %7B%7D;%0A%09%09this.facets =    %7B%7D
 ;%0A%09%7D

Remove inheritance handling in plugin registry