@@ -28,21 +28,68 @@ export class PluginReadySetupEvent {
28
28
}
29
29
}
30
30
31
- class App {
32
- private _previousTime : number = 0
33
- private readonly _registry : Registry
34
- private readonly _eventBus : EventBus
35
- private readonly _appEventBus : EventBus
31
+ export interface VeloxiApp {
32
+ addPlugin <
33
+ TConfig extends PluginConfig = PluginConfig ,
34
+ TPluginApi extends PluginApi = PluginApi
35
+ > (
36
+ pluginFactory : PluginFactory < TConfig , TPluginApi > ,
37
+ config ?: TConfig
38
+ ) : void
39
+
40
+ updatePlugin <
41
+ TConfig extends PluginConfig = PluginConfig ,
42
+ TPluginApi extends PluginApi = PluginApi
43
+ > (
44
+ pluginFactory : PluginFactory < TConfig , TPluginApi > ,
45
+ config ?: TConfig
46
+ ) : void
47
+
48
+ reset ( pluginName ?: string , callback ?: ( ) => void ) : void
49
+
50
+ destroy ( pluginName ?: string , callback ?: ( ) => void ) : void
51
+
52
+ getPlugin < TPluginApi extends PluginApi > (
53
+ pluginFactory : PluginFactory < PluginConfig > | string ,
54
+ pluginKey ?: string
55
+ ) : TPluginApi
56
+
57
+ getPlugins < TPluginApi extends PluginApi > (
58
+ pluginFactory : PluginFactory < PluginConfig > | string ,
59
+ pluginKey ?: string
60
+ ) : TPluginApi [ ]
61
+
62
+ onPluginEvent < TPlugin extends PluginFactory < any , any > , TEvent > (
63
+ pluginFactory : TPlugin ,
64
+ EventCtor : new ( eventData : TEvent ) => TEvent ,
65
+ listener : ( eventData : TEvent ) => void ,
66
+ pluginKey ?: string
67
+ ) : void
68
+
69
+ removePluginEventListener < TEvent > (
70
+ pluginFactory : PluginFactory ,
71
+ EventCtor : new ( eventData : TEvent ) => TEvent ,
72
+ listener : ( eventData : TEvent ) => void
73
+ ) : void
74
+
75
+ run ( ) : void
76
+ }
77
+
78
+ class App implements VeloxiApp {
79
+ private previousTime : number = 0
80
+ private readonly registry : Registry
81
+ private readonly eventBus : EventBus
82
+ private readonly appEventBus : EventBus
36
83
37
84
static create ( ) {
38
85
return new App ( )
39
86
}
40
87
41
88
constructor ( ) {
42
- this . _eventBus = new EventBus ( )
43
- this . _appEventBus = new EventBus ( )
44
- this . _registry = new Registry ( this . _appEventBus , this . _eventBus )
45
- new DomObserver ( this . _eventBus )
89
+ this . eventBus = new EventBus ( )
90
+ this . appEventBus = new EventBus ( )
91
+ this . registry = new Registry ( this . appEventBus , this . eventBus )
92
+ new DomObserver ( this . eventBus )
46
93
}
47
94
48
95
addPlugin <
@@ -52,8 +99,8 @@ class App {
52
99
pluginFactory : PluginFactory < TConfig , TPluginApi > ,
53
100
config : TConfig = { } as TConfig
54
101
) : void {
55
- if ( ! this . _registry . hasPlugin ( pluginFactory ) ) {
56
- this . _registry . createPlugin ( pluginFactory , this . _eventBus , config )
102
+ if ( ! this . registry . hasPlugin ( pluginFactory ) ) {
103
+ this . registry . createPlugin ( pluginFactory , this . eventBus , config )
57
104
}
58
105
}
59
106
@@ -64,17 +111,17 @@ class App {
64
111
pluginFactory : PluginFactory < TConfig , TPluginApi > ,
65
112
config : TConfig = { } as TConfig
66
113
) : void {
67
- if ( this . _registry . hasPlugin ( pluginFactory ) ) {
68
- this . _registry . updatePlugin ( pluginFactory , this . _eventBus , config )
114
+ if ( this . registry . hasPlugin ( pluginFactory ) ) {
115
+ this . registry . updatePlugin ( pluginFactory , this . eventBus , config )
69
116
}
70
117
}
71
118
72
119
reset ( pluginName ?: string , callback ?: ( ) => void ) {
73
- this . _registry . reset ( pluginName , callback )
120
+ this . registry . reset ( pluginName , callback )
74
121
}
75
122
76
123
destroy ( pluginName ?: string , callback ?: ( ) => void ) {
77
- this . _registry . destroy ( pluginName , callback )
124
+ this . registry . destroy ( pluginName , callback )
78
125
}
79
126
80
127
getPlugin < TPluginApi extends PluginApi > (
@@ -85,7 +132,7 @@ class App {
85
132
typeof pluginFactory === 'string'
86
133
? pluginFactory
87
134
: pluginFactory . pluginName
88
- const plugin = this . _registry . getPluginByName ( pluginName , pluginKey )
135
+ const plugin = this . registry . getPluginByName ( pluginName , pluginKey )
89
136
if ( ! plugin ) {
90
137
throw new Error (
91
138
`You can\'t call getPlugin for ${ pluginName } with key: ${ pluginKey } because it does not exist in your app`
@@ -102,7 +149,7 @@ class App {
102
149
typeof pluginFactory === 'string'
103
150
? pluginFactory
104
151
: pluginFactory . pluginName
105
- const plugins = this . _registry . getPluginsByName ( pluginName , pluginKey )
152
+ const plugins = this . registry . getPluginsByName ( pluginName , pluginKey )
106
153
if ( plugins . length === 0 ) {
107
154
throw new Error (
108
155
`You can\'t call getPlugins for ${ pluginName } with key: ${ pluginKey } because they don\'t exist in your app`
@@ -117,7 +164,7 @@ class App {
117
164
listener : ( eventData : TEvent ) => void ,
118
165
pluginKey ?: string
119
166
) {
120
- const plugin = this . _registry . getPluginByName (
167
+ const plugin = this . registry . getPluginByName (
121
168
pluginFactory . pluginName ! ,
122
169
pluginKey
123
170
)
@@ -132,7 +179,7 @@ class App {
132
179
EventCtor : new ( eventData : TEvent ) => TEvent ,
133
180
listener : ( eventData : TEvent ) => void
134
181
) {
135
- const plugin = this . _registry . getPluginByName ( pluginFactory . pluginName ! )
182
+ const plugin = this . registry . getPluginByName ( pluginFactory . pluginName ! )
136
183
137
184
if ( plugin ) {
138
185
plugin . removeListener ( EventCtor , listener )
@@ -141,148 +188,138 @@ class App {
141
188
142
189
run ( ) {
143
190
if ( document . readyState === 'loading' ) {
144
- document . addEventListener ( 'DOMContentLoaded' , this . _start . bind ( this ) )
191
+ document . addEventListener ( 'DOMContentLoaded' , this . start . bind ( this ) )
145
192
} else {
146
- this . _start ( )
193
+ this . start ( )
147
194
}
148
195
}
149
196
150
- ready < TPluginApi extends PluginApi > (
151
- pluginName : string ,
152
- callback : ReadyCallback < TPluginApi >
153
- ) : void {
154
- this . _appEventBus . subscribeToPluginReadyEvent ( callback , pluginName )
155
- }
156
-
157
- private _start ( ) {
158
- this . _setup ( )
159
- requestAnimationFrame ( this . _tick . bind ( this ) )
197
+ private start ( ) {
198
+ this . setup ( )
199
+ requestAnimationFrame ( this . tick . bind ( this ) )
160
200
}
161
201
162
- private _setup ( ) {
163
- this . _listenToNativeEvents ( )
164
- this . _subscribeToEvents ( )
202
+ private setup ( ) {
203
+ this . listenToNativeEvents ( )
204
+ this . subscribeToEvents ( )
165
205
}
166
206
167
- private _listenToNativeEvents ( ) {
207
+ private listenToNativeEvents ( ) {
168
208
document . addEventListener ( 'click' , ( e ) => {
169
- this . _eventBus . emitEvent ( PointerClickEvent , {
209
+ this . eventBus . emitEvent ( PointerClickEvent , {
170
210
x : e . clientX ,
171
211
y : e . clientY ,
172
212
target : e . target
173
213
} )
174
214
} )
175
215
document . addEventListener ( 'pointermove' , ( e ) => {
176
- this . _eventBus . emitEvent ( PointerMoveEvent , {
216
+ this . eventBus . emitEvent ( PointerMoveEvent , {
177
217
x : e . clientX ,
178
218
y : e . clientY ,
179
219
target : e . target
180
220
} )
181
221
} )
182
222
document . addEventListener ( 'pointerdown' , ( e ) => {
183
- this . _eventBus . emitEvent ( PointerDownEvent , {
223
+ this . eventBus . emitEvent ( PointerDownEvent , {
184
224
x : e . clientX ,
185
225
y : e . clientY ,
186
226
target : e . target
187
227
} )
188
228
} )
189
229
document . addEventListener ( 'pointerup' , ( e ) => {
190
- this . _eventBus . emitEvent ( PointerUpEvent , {
230
+ this . eventBus . emitEvent ( PointerUpEvent , {
191
231
x : e . clientX ,
192
232
y : e . clientY ,
193
233
target : e . target
194
234
} )
195
235
} )
196
236
}
197
237
198
- private _tick ( ts : number ) {
199
- let dt = ( ts - this . _previousTime ) / 1000
238
+ private tick ( ts : number ) {
239
+ let dt = ( ts - this . previousTime ) / 1000
200
240
if ( dt > 0.016 ) {
201
241
dt = 1 / 60
202
242
}
203
- this . _previousTime = ts
243
+ this . previousTime = ts
204
244
205
- this . _eventBus . reset ( )
206
- this . _subscribeToEvents ( )
207
- this . _read ( )
208
- this . _update ( ts , dt )
209
- this . _render ( )
245
+ this . eventBus . reset ( )
246
+ this . subscribeToEvents ( )
247
+ this . read ( )
248
+ this . update ( ts , dt )
249
+ this . render ( )
210
250
211
- requestAnimationFrame ( this . _tick . bind ( this ) )
251
+ requestAnimationFrame ( this . tick . bind ( this ) )
212
252
}
213
253
214
- private _subscribeToEvents ( ) {
215
- this . _eventBus . subscribeToEvent (
216
- NodeAddedEvent ,
217
- this . _onNodeAdded . bind ( this )
218
- )
254
+ private subscribeToEvents ( ) {
255
+ this . eventBus . subscribeToEvent ( NodeAddedEvent , this . onNodeAdded . bind ( this ) )
219
256
220
- this . _eventBus . subscribeToEvent (
257
+ this . eventBus . subscribeToEvent (
221
258
NodeRemovedEvent ,
222
- this . _onNodeRemoved . bind ( this )
259
+ this . onNodeRemoved . bind ( this )
223
260
)
224
261
225
- this . _eventBus . subscribeToEvent (
262
+ this . eventBus . subscribeToEvent (
226
263
DataChangedEvent ,
227
- this . _onDataChanged . bind ( this )
264
+ this . onDataChanged . bind ( this )
228
265
)
229
266
230
- this . _registry . getPlugins ( ) . forEach ( ( plugin ) => {
231
- plugin . subscribeToEvents ( this . _eventBus )
267
+ this . registry . getPlugins ( ) . forEach ( ( plugin ) => {
268
+ plugin . subscribeToEvents ( this . eventBus )
232
269
} )
233
270
}
234
271
235
- private _onNodeAdded ( { node } : NodeAddedEvent ) {
236
- this . _registry . queueNodeToBeCreated ( node )
272
+ private onNodeAdded ( { node } : NodeAddedEvent ) {
273
+ this . registry . queueNodeToBeCreated ( node )
237
274
}
238
275
239
- private _onNodeRemoved ( { node } : NodeRemovedEvent ) {
240
- this . _registry . queueNodeToBeRemoved ( node )
276
+ private onNodeRemoved ( { node } : NodeRemovedEvent ) {
277
+ this . registry . queueNodeToBeRemoved ( node )
241
278
}
242
279
243
- private _onDataChanged ( event : DataChangedEvent ) {
244
- this . _registry . notifyPluginAboutDataChange ( event )
280
+ private onDataChanged ( event : DataChangedEvent ) {
281
+ this . registry . notifyPluginAboutDataChange ( event )
245
282
}
246
283
247
- private _read ( ) {
248
- this . _registry . getViews ( ) . forEach ( ( view ) => {
284
+ private read ( ) {
285
+ this . registry . getViews ( ) . forEach ( ( view ) => {
249
286
view . read ( )
250
287
} )
251
288
}
252
289
253
- private _update ( ts : number , dt : number ) {
254
- this . _registry . update ( )
255
- this . _registry
290
+ private update ( ts : number , dt : number ) {
291
+ this . registry . update ( )
292
+ this . registry
256
293
. getPlugins ( )
257
294
. slice ( )
258
295
. reverse ( )
259
296
. forEach ( ( plugin ) => {
260
297
plugin . init ( )
261
298
} )
262
- this . _registry . getRenderablePlugins ( ) . forEach ( ( plugin ) => {
299
+ this . registry . getRenderablePlugins ( ) . forEach ( ( plugin ) => {
263
300
plugin . update ( ts , dt )
264
301
} )
265
- this . _registry . getViews ( ) . forEach ( ( view ) => {
302
+ this . registry . getViews ( ) . forEach ( ( view ) => {
266
303
view . update ( ts , dt )
267
304
} )
268
- this . _registry . getViews ( ) . forEach ( ( view ) => {
305
+ this . registry . getViews ( ) . forEach ( ( view ) => {
269
306
// Update previous rect after all views have been updated.
270
307
// This is needed to ensure that we are using the same
271
308
// previous rect across all view props.
272
309
view . _updatePreviousRect ( )
273
310
} )
274
311
}
275
312
276
- private _render ( ) {
277
- this . _registry . getRenderablePlugins ( ) . forEach ( ( plugin ) => {
313
+ private render ( ) {
314
+ this . registry . getRenderablePlugins ( ) . forEach ( ( plugin ) => {
278
315
plugin . render ( )
279
316
} )
280
- this . _registry . getViews ( ) . forEach ( ( view ) => {
317
+ this . registry . getViews ( ) . forEach ( ( view ) => {
281
318
view . render ( )
282
319
} )
283
320
}
284
321
}
285
322
286
- export function createApp ( ) {
323
+ export function createApp ( ) : VeloxiApp {
287
324
return App . create ( )
288
325
}
0 commit comments