1 | ;
|
2 |
|
3 | const _ = require('lodash');
|
4 | const Action = require('./actions');
|
5 |
|
6 | /**
|
7 | * Is a registry for all user interface components, stores, and actions
|
8 | * in the application.
|
9 | */
|
10 | class AppRegistry {
|
11 |
|
12 | /**
|
13 | * Instantiate the registry.
|
14 | */
|
15 | constructor() {
|
16 | this.actions = {};
|
17 | this.components = {};
|
18 | this.stores = {};
|
19 | }
|
20 |
|
21 | /**
|
22 | * Deregister an action.
|
23 | *
|
24 | * @param {String} name - The action to deregister.
|
25 | *
|
26 | * @returns {ComponentRegistry} This instance.
|
27 | */
|
28 | deregisterAction(name) {
|
29 | delete this.actions[name];
|
30 | Action.actionDeregistered(name);
|
31 | return this;
|
32 | }
|
33 |
|
34 | /**
|
35 | * Deregister a component.
|
36 | *
|
37 | * @param {String} name - The component to deregister.
|
38 | *
|
39 | * @returns {ComponentRegistry} This instance.
|
40 | */
|
41 | deregisterComponent(name) {
|
42 | delete this.components[name];
|
43 | Action.componentDeregistered(name);
|
44 | return this;
|
45 | }
|
46 |
|
47 | /**
|
48 | * Deregister a store.
|
49 | *
|
50 | * @param {String} name - The store to deregister.
|
51 | *
|
52 | * @returns {ComponentRegistry} This instance.
|
53 | */
|
54 | deregisterStore(name) {
|
55 | delete this.stores[name];
|
56 | Action.storeDeregistered(name);
|
57 | return this;
|
58 | }
|
59 |
|
60 | /**
|
61 | * Get an action for the name.
|
62 | *
|
63 | * @param {String} name - The action name.
|
64 | *
|
65 | * @returns {Action} The action.
|
66 | */
|
67 | getAction(name) {
|
68 | return this.actions[name];
|
69 | }
|
70 |
|
71 | /**
|
72 | * Get a component by name.
|
73 | *
|
74 | * @param {String} name - The component name.
|
75 | *
|
76 | * @returns {Component} The component.
|
77 | */
|
78 | getComponent(name) {
|
79 | return this.components[name];
|
80 | }
|
81 |
|
82 | /**
|
83 | * Get a store by name.
|
84 | *
|
85 | * @param {String} name - The store name.
|
86 | *
|
87 | * @returns {Store} The store.
|
88 | */
|
89 | getStore(name) {
|
90 | return this.stores[name];
|
91 | }
|
92 |
|
93 | /**
|
94 | * Register an action in the registry.
|
95 | *
|
96 | * @param {String} name - The name of the action.
|
97 | * @param {Action} action - The action.
|
98 | *
|
99 | * @returns {ComponentRegistry} This instance.
|
100 | */
|
101 | registerAction(name, action) {
|
102 | var overwrite = this.actions.hasOwnProperty(name);
|
103 | this.actions[name] = action;
|
104 | if (overwrite) {
|
105 | Action.actionOverridden(name);
|
106 | } else {
|
107 | Action.actionRegistered(name);
|
108 | }
|
109 | return this;
|
110 | }
|
111 |
|
112 | /**
|
113 | * Register a component in the registry.
|
114 | *
|
115 | * @param {String} name - The name of the component.
|
116 | * @param {Component} component - The React Component.
|
117 | *
|
118 | * @returns {ComponentRegistry} This instance.
|
119 | */
|
120 | registerComponent(name, component) {
|
121 | var overwrite = this.components.hasOwnProperty(name);
|
122 | this.components[name] = component;
|
123 | if (overwrite) {
|
124 | Action.componentOverridden(name);
|
125 | } else {
|
126 | Action.componentRegistered(name);
|
127 | }
|
128 | return this;
|
129 | }
|
130 |
|
131 | /**
|
132 | * Register a store in the registry.
|
133 | *
|
134 | * @param {String} name - The name of the store.
|
135 | * @param {Store} store - The Reflux store.
|
136 | *
|
137 | * @returns {ComponentRegistry} This instance.
|
138 | */
|
139 | registerStore(name, store) {
|
140 | var overwrite = this.stores.hasOwnProperty(name);
|
141 | this.stores[name] = store;
|
142 | if (overwrite) {
|
143 | Action.storeOverridden(name);
|
144 | } else {
|
145 | Action.storeRegistered(name);
|
146 | }
|
147 | return this;
|
148 | }
|
149 |
|
150 | }
|
151 |
|
152 | module.exports = AppRegistry;
|