@@ -16,31 +16,48 @@ limitations under the License.
1616
1717'use strict' ;
1818
19- var components = { } ;
20-
2119function load ( name ) {
2220 var module = require ( "../skins/base/views/" + name ) ;
23- components [ name ] = module ;
2421 return module ;
2522} ;
2623
27- module . exports = {
24+ var ComponentBroker = function ( ) {
25+ this . components = { } ;
26+ } ;
27+
28+ ComponentBroker . prototype = {
2829 get : function ( name ) {
29- if ( components [ name ] ) return components [ name ] ;
30+ if ( this . components [ name ] ) {
31+ return this . components [ name ] ;
32+ }
3033
31- components [ name ] = load ( name ) ;
32- return components [ name ] ;
34+ this . components [ name ] = load ( name ) ;
35+ return this . components [ name ] ;
3336 } ,
3437
3538 set : function ( name , module ) {
36- components [ name ] = module ;
39+ this . components [ name ] = module ;
3740 }
3841} ;
3942
40- // Statically require all the components we know about,
41- // otherwise browserify has no way of knowing what module to include
43+ // We define one Component Broker globally, because the intention is
44+ // very much that it is a singleton. Relying on there only being one
45+ // copy of the module can be dicey and not work as browserify's
46+ // behaviour with multiple copies of files etc. is erratic at best.
47+ // XXX: We can still end up with the same file twice in the resulting
48+ // JS bundle which is nonideal.
49+ if ( global . componentBroker === undefined ) {
50+ global . componentBroker = new ComponentBroker ( ) ;
51+ }
52+ module . exports = global . componentBroker ;
53+
54+ // We need to tell browserify to include all the components
55+ // by direct require syntax in here, but we don't want them
56+ // to be evaluated in this file because then we wouldn't be
57+ // able to override them. if (0) does this.
4258// Must be in this file (because the require is file-specific) and
4359// must be at the end because the components include this file.
60+ if ( 0 ) {
4461require ( '../skins/base/views/atoms/LogoutButton' ) ;
4562require ( '../skins/base/views/atoms/EnableNotificationsButton' ) ;
4663require ( '../skins/base/views/atoms/MessageTimestamp' ) ;
@@ -62,3 +79,4 @@ require('../skins/base/views/organisms/RoomList');
6279require ( '../skins/base/views/organisms/RoomView' ) ;
6380require ( '../skins/base/views/templates/Login' ) ;
6481require ( '../skins/base/views/organisms/Notifier' ) ;
82+ }
0 commit comments