-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy path_registry.js
93 lines (83 loc) · 2.97 KB
/
_registry.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import _ from 'lodash';
import { IndexedArray } from 'ui/indexed_array';
const notPropsOptNames = IndexedArray.OPT_NAMES.concat('constructor', 'invokeProviders');
/**
* Create a registry, which is just a Private module provider.
*
* The registry allows modifying the values it will provide
* using the #register method.
*
* To access these modules, pass the registry to the Private
* module loader.
*
* # Examples
*
* + register a module
* ```js
* let registry = require('ui/registry/vis_types');
* registry.add(function InjectablePrivateModule($http, Promise) {
* ...
* })
* ```
*
* + get all registered modules
* ```js
* let visTypes = Private(RegistryVisTypesProvider);
* ```
*
*
* @param {object} [spec] - an object describing the properties of
* the registry to create. Any property specified
* that is not listed below will be mixed into the
* final IndexedArray object.
*
* # init
* @param {Function} [spec.constructor] - an injectable function that is called when
* the registry is first instanciated by the app.
*
* # IndexedArray params
* @param {array[String]} [spec.index] - passed to the IndexedArray constructor
* @param {array[String]} [spec.group] - passed to the IndexedArray constructor
* @param {array[String]} [spec.order] - passed to the IndexedArray constructor
* @param {array[String]} [spec.initialSet] - passed to the IndexedArray constructor
* @param {array[String]} [spec.immutable] - passed to the IndexedArray constructor
*
* @return {[type]} [description]
*/
export function uiRegistry(spec) {
spec = spec || {};
const constructor = _.has(spec, 'constructor') && spec.constructor;
const invokeProviders = _.has(spec, 'invokeProviders') && spec.invokeProviders;
const iaOpts = _.defaults(_.pick(spec, IndexedArray.OPT_NAMES), { index: ['name'] });
const props = _.omit(spec, notPropsOptNames);
const providers = [];
/**
* This is the Private module that will be instantiated by
*
* @tag:PrivateModule
* @return {IndexedArray} - an indexed array containing the values
* that were registered, the registry spec
* defines how things will be indexed.
*/
const registry = function (Private, $injector) {
// call the registered providers to get their values
iaOpts.initialSet = invokeProviders
? $injector.invoke(invokeProviders, undefined, { providers })
: providers.map(Private);
// index all of the modules
let modules = new IndexedArray(iaOpts);
// mixin other props
_.assign(modules, props);
// construct
if (constructor) {
modules = $injector.invoke(constructor, modules) || modules;
}
return modules;
};
registry.displayName = '[registry ' + props.name + ']';
registry.register = function (privateModule) {
providers.push(privateModule);
return registry;
};
return registry;
}