@@ -14,14 +14,20 @@ See the License for the specific language governing permissions and
14
14
limitations under the License.
15
15
*/
16
16
17
+ import EventEmitter from 'events' ;
18
+
19
+ import MatrixClientPeg from '../MatrixClientPeg' ;
20
+ import sdk from '../index' ;
21
+
17
22
/**
18
23
* Stores information about the widgets active in the app right now:
19
24
* * What widget is set to remain always-on-screen, if any
20
25
* Only one widget may be 'always on screen' at any one time.
21
26
* * Negotiated capabilities for active apps
22
27
*/
23
- class ActiveWidgetStore {
28
+ class ActiveWidgetStore extends EventEmitter {
24
29
constructor ( ) {
30
+ super ( ) ;
25
31
this . _persistentWidgetId = null ;
26
32
27
33
// A list of negotiated capabilities for each widget, by ID
@@ -35,6 +41,46 @@ class ActiveWidgetStore {
35
41
36
42
// What room ID each widget is associated with (if it's a room widget)
37
43
this . _roomIdByWidgetId = { } ;
44
+
45
+ this . onRoomStateEvents = this . onRoomStateEvents . bind ( this ) ;
46
+
47
+ this . dispatcherRef = null ;
48
+ }
49
+
50
+ start ( ) {
51
+ MatrixClientPeg . get ( ) . on ( 'RoomState.events' , this . onRoomStateEvents ) ;
52
+ }
53
+
54
+ stop ( ) {
55
+ if ( MatrixClientPeg . get ( ) ) {
56
+ MatrixClientPeg . get ( ) . removeListener ( 'RoomState.events' , this . onRoomStateEvents ) ;
57
+ }
58
+ this . _capsByWidgetId = { } ;
59
+ this . _widgetMessagingByWidgetId = { } ;
60
+ this . _roomIdByWidgetId = { } ;
61
+ }
62
+
63
+ onRoomStateEvents ( ev , state ) {
64
+ // XXX: This listens for state events in order to remove the active widget.
65
+ // Everything else relies on views listening for events and calling setters
66
+ // on this class which is terrible. This store should just listen for events
67
+ // and keep itself up to date.
68
+ if ( ev . getType ( ) !== 'im.vector.modular.widgets' ) return ;
69
+
70
+ if ( ev . getStateKey ( ) === this . _persistentWidgetId ) {
71
+ this . destroyPersistentWidget ( ) ;
72
+ }
73
+ }
74
+
75
+ destroyPersistentWidget ( ) {
76
+ const toDeleteId = this . _persistentWidgetId ;
77
+
78
+ const PersistedElement = sdk . getComponent ( "elements.PersistedElement" ) ;
79
+ PersistedElement . destroyElement ( 'widget_' + toDeleteId ) ;
80
+ this . setWidgetPersistence ( toDeleteId , false ) ;
81
+ this . delWidgetMessaging ( toDeleteId ) ;
82
+ this . delWidgetCapabilities ( toDeleteId ) ;
83
+ this . delRoomId ( toDeleteId ) ;
38
84
}
39
85
40
86
setWidgetPersistence ( widgetId , val ) {
@@ -43,6 +89,7 @@ class ActiveWidgetStore {
43
89
} else if ( this . _persistentWidgetId !== widgetId && val ) {
44
90
this . _persistentWidgetId = widgetId ;
45
91
}
92
+ this . emit ( 'update' ) ;
46
93
}
47
94
48
95
getWidgetPersistence ( widgetId ) {
@@ -55,6 +102,7 @@ class ActiveWidgetStore {
55
102
56
103
setWidgetCapabilities ( widgetId , caps ) {
57
104
this . _capsByWidgetId [ widgetId ] = caps ;
105
+ this . emit ( 'update' ) ;
58
106
}
59
107
60
108
widgetHasCapability ( widgetId , cap ) {
@@ -63,10 +111,12 @@ class ActiveWidgetStore {
63
111
64
112
delWidgetCapabilities ( widgetId ) {
65
113
delete this . _capsByWidgetId [ widgetId ] ;
114
+ this . emit ( 'update' ) ;
66
115
}
67
116
68
117
setWidgetMessaging ( widgetId , wm ) {
69
118
this . _widgetMessagingByWidgetId [ widgetId ] = wm ;
119
+ this . emit ( 'update' ) ;
70
120
}
71
121
72
122
getWidgetMessaging ( widgetId ) {
@@ -81,6 +131,7 @@ class ActiveWidgetStore {
81
131
console . error ( 'Failed to stop listening for widgetMessaging events' , e . message ) ;
82
132
}
83
133
delete this . _widgetMessagingByWidgetId [ widgetId ] ;
134
+ this . emit ( 'update' ) ;
84
135
}
85
136
}
86
137
@@ -90,10 +141,12 @@ class ActiveWidgetStore {
90
141
91
142
setRoomId ( widgetId , roomId ) {
92
143
this . _roomIdByWidgetId [ widgetId ] = roomId ;
144
+ this . emit ( 'update' ) ;
93
145
}
94
146
95
147
delRoomId ( widgetId ) {
96
148
delete this . _roomIdByWidgetId [ widgetId ] ;
149
+ this . emit ( 'update' ) ;
97
150
}
98
151
}
99
152
0 commit comments