node_modules/wire/lib/plugin/registry.js

Maintainability

70.02

Lines of code

129

Created with Raphaël 2.1.002550751002015-1-52014-12-42014-12-3

2015-5-18
Maintainability: 70.02

Created with Raphaël 2.1.00501001502002015-1-52014-12-42014-12-3

2015-5-18
Lines of Code: 129

Difficulty

39.50

Estimated Errors

0.91

Function weight

By Complexity

Created with Raphaël 2.1.0discoverPlugin3

By SLOC

Created with Raphaël 2.1.0<anonymous>129
1
/** @license MIT License (c) copyright B Cavalier & J Hann */
2
 
3
/**
4
 * plugins
5
 * Licensed under the MIT License at:
6
 * http://www.opensource.org/licenses/mit-license.php
7
 * @author: brian@hovercraftstudios.com
8
 */
9
(function(define) {
10
define(function(require) {
11
 
12
    var when, array, object, priority, instantiate, nsKey, nsSeparator;
13
 
14
    when = require('when');
15
    array = require('../array');
16
    object = require('../object');
17
    priority = require('./priority');
18
    instantiate = require('../instantiate');
19
 
20
    nsKey = '$ns';
21
    nsSeparator = ':';
22
 
23
    function PluginRegistry() {
24
        this.plugins = [];
25
        this._namespaces = {};
26
 
27
        this.contextListeners = [];
28
        this.listeners = [];
29
        this.proxiers =  [];
30
        this.resolvers = {};
31
        this.factories = {};
32
        this.facets =    {};
33
    }
34
 
35
    PluginRegistry.prototype = {
36
        scanModule: function (module, spec, namespace) {
37
            var self, pluginFactory;
38
 
39
            pluginFactory = discoverPlugin(module);
40
 
41
            if (!allowPlugin(pluginFactory, this.plugins)) {
42
                return when.resolve();
43
            }
44
 
45
            // Add to singleton plugins list to only allow one instance
46
            // of this plugin in the current context.
47
            this.plugins.push(pluginFactory);
48
 
49
            // Initialize the plugin for this context
50
            self = this;
51
            return when(instantiate(pluginFactory, [spec]),
52
                function (plugin) {
53
                    plugin && self.registerPlugin(plugin, namespace || getNamespace(spec));
Column: 90 "Expected an assignment or function call and instead saw an expression."
54
                }
55
            ).yield();
56
        },
57
 
58
        registerPlugin: function (plugin, namespace) {
59
            addNamespace(namespace, this._namespaces);
60
 
61
            addPlugin(plugin.resolvers, this.resolvers, namespace);
62
            addPlugin(plugin.factories, this.factories, namespace);
63
            addPlugin(plugin.facets, this.facets, namespace);
64
 
65
            this.listeners.push(plugin);
66
            if(plugin.context) {
67
                this.contextListeners.push(plugin.context);
68
            }
69
 
70
            this._registerProxies(plugin.proxies);
71
        },
72
 
73
        _registerProxies: function (proxiesToAdd) {
74
            if (!proxiesToAdd) {
75
                return;
76
            }
77
 
78
            this.proxiers = priority.sortReverse(array.union(this.proxiers, proxiesToAdd));
79
        }
80
    };
81
 
82
    return PluginRegistry;
83
 
84
    function discoverPlugin(module) {
85
        var plugin;
86
 
87
        // Prefer deprecated legacy wire$plugin format over newer
88
        // plain function format.
89
        // TODO: Remove support for wire$plugin
90
        if(typeof module.wire$plugin === 'function') {
91
            plugin = module.wire$plugin;
92
        } else if(typeof module === 'function') {
93
            plugin = module;
94
        }
95
 
96
        return plugin;
97
    }
98
 
99
    function getNamespace(spec) {
100
        var namespace;
101
        if(typeof spec === 'object' && nsKey in spec) {
102
            // A namespace was provided
103
            namespace = spec[nsKey];
104
        }
105
 
106
        return namespace;
107
    }
108
 
109
    function addNamespace(namespace, namespaces) {
110
        if(namespace && namespace in namespaces) {
111
            throw new Error('plugin namespace already in use: ' + namespace);
112
        } else {
113
            namespaces[namespace] = 1;
114
        }
115
    }
116
 
117
    function allowPlugin(plugin, existing) {
118
        return typeof plugin === 'function' && existing.indexOf(plugin) === -1;
119
    }
120
 
121
    function addPlugin(src, registry, namespace) {
122
        var newPluginName, namespacedName;
123
        for (newPluginName in src) {
124
            namespacedName = makeNamespace(newPluginName, namespace);
125
            if (object.hasOwn(registry, namespacedName)) {
126
                throw new Error('Two plugins for same type in scope: ' + namespacedName);
127
            }
128
 
129
            registry[namespacedName] = src[newPluginName];
130
        }
131
    }
132
 
133
    function makeNamespace(pluginName, namespace) {
134
        return namespace ? (namespace + nsSeparator + pluginName) : pluginName;
135
    }
136
});
137
}(typeof define === 'function' ? define : function(factory) { module.exports = factory(require); }));