Skip to content

Commit 3f0d460

Browse files
committed
refactor App
1 parent 00b316d commit 3f0d460

File tree

1 file changed

+114
-77
lines changed

1 file changed

+114
-77
lines changed

src/core/App.ts

+114-77
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,68 @@ export class PluginReadySetupEvent {
2828
}
2929
}
3030

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
3683

3784
static create() {
3885
return new App()
3986
}
4087

4188
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)
4693
}
4794

4895
addPlugin<
@@ -52,8 +99,8 @@ class App {
5299
pluginFactory: PluginFactory<TConfig, TPluginApi>,
53100
config: TConfig = {} as TConfig
54101
): 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)
57104
}
58105
}
59106

@@ -64,17 +111,17 @@ class App {
64111
pluginFactory: PluginFactory<TConfig, TPluginApi>,
65112
config: TConfig = {} as TConfig
66113
): 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)
69116
}
70117
}
71118

72119
reset(pluginName?: string, callback?: () => void) {
73-
this._registry.reset(pluginName, callback)
120+
this.registry.reset(pluginName, callback)
74121
}
75122

76123
destroy(pluginName?: string, callback?: () => void) {
77-
this._registry.destroy(pluginName, callback)
124+
this.registry.destroy(pluginName, callback)
78125
}
79126

80127
getPlugin<TPluginApi extends PluginApi>(
@@ -85,7 +132,7 @@ class App {
85132
typeof pluginFactory === 'string'
86133
? pluginFactory
87134
: pluginFactory.pluginName
88-
const plugin = this._registry.getPluginByName(pluginName, pluginKey)
135+
const plugin = this.registry.getPluginByName(pluginName, pluginKey)
89136
if (!plugin) {
90137
throw new Error(
91138
`You can\'t call getPlugin for ${pluginName} with key: ${pluginKey} because it does not exist in your app`
@@ -102,7 +149,7 @@ class App {
102149
typeof pluginFactory === 'string'
103150
? pluginFactory
104151
: pluginFactory.pluginName
105-
const plugins = this._registry.getPluginsByName(pluginName, pluginKey)
152+
const plugins = this.registry.getPluginsByName(pluginName, pluginKey)
106153
if (plugins.length === 0) {
107154
throw new Error(
108155
`You can\'t call getPlugins for ${pluginName} with key: ${pluginKey} because they don\'t exist in your app`
@@ -117,7 +164,7 @@ class App {
117164
listener: (eventData: TEvent) => void,
118165
pluginKey?: string
119166
) {
120-
const plugin = this._registry.getPluginByName(
167+
const plugin = this.registry.getPluginByName(
121168
pluginFactory.pluginName!,
122169
pluginKey
123170
)
@@ -132,7 +179,7 @@ class App {
132179
EventCtor: new (eventData: TEvent) => TEvent,
133180
listener: (eventData: TEvent) => void
134181
) {
135-
const plugin = this._registry.getPluginByName(pluginFactory.pluginName!)
182+
const plugin = this.registry.getPluginByName(pluginFactory.pluginName!)
136183

137184
if (plugin) {
138185
plugin.removeListener(EventCtor, listener)
@@ -141,148 +188,138 @@ class App {
141188

142189
run() {
143190
if (document.readyState === 'loading') {
144-
document.addEventListener('DOMContentLoaded', this._start.bind(this))
191+
document.addEventListener('DOMContentLoaded', this.start.bind(this))
145192
} else {
146-
this._start()
193+
this.start()
147194
}
148195
}
149196

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))
160200
}
161201

162-
private _setup() {
163-
this._listenToNativeEvents()
164-
this._subscribeToEvents()
202+
private setup() {
203+
this.listenToNativeEvents()
204+
this.subscribeToEvents()
165205
}
166206

167-
private _listenToNativeEvents() {
207+
private listenToNativeEvents() {
168208
document.addEventListener('click', (e) => {
169-
this._eventBus.emitEvent(PointerClickEvent, {
209+
this.eventBus.emitEvent(PointerClickEvent, {
170210
x: e.clientX,
171211
y: e.clientY,
172212
target: e.target
173213
})
174214
})
175215
document.addEventListener('pointermove', (e) => {
176-
this._eventBus.emitEvent(PointerMoveEvent, {
216+
this.eventBus.emitEvent(PointerMoveEvent, {
177217
x: e.clientX,
178218
y: e.clientY,
179219
target: e.target
180220
})
181221
})
182222
document.addEventListener('pointerdown', (e) => {
183-
this._eventBus.emitEvent(PointerDownEvent, {
223+
this.eventBus.emitEvent(PointerDownEvent, {
184224
x: e.clientX,
185225
y: e.clientY,
186226
target: e.target
187227
})
188228
})
189229
document.addEventListener('pointerup', (e) => {
190-
this._eventBus.emitEvent(PointerUpEvent, {
230+
this.eventBus.emitEvent(PointerUpEvent, {
191231
x: e.clientX,
192232
y: e.clientY,
193233
target: e.target
194234
})
195235
})
196236
}
197237

198-
private _tick(ts: number) {
199-
let dt = (ts - this._previousTime) / 1000
238+
private tick(ts: number) {
239+
let dt = (ts - this.previousTime) / 1000
200240
if (dt > 0.016) {
201241
dt = 1 / 60
202242
}
203-
this._previousTime = ts
243+
this.previousTime = ts
204244

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()
210250

211-
requestAnimationFrame(this._tick.bind(this))
251+
requestAnimationFrame(this.tick.bind(this))
212252
}
213253

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))
219256

220-
this._eventBus.subscribeToEvent(
257+
this.eventBus.subscribeToEvent(
221258
NodeRemovedEvent,
222-
this._onNodeRemoved.bind(this)
259+
this.onNodeRemoved.bind(this)
223260
)
224261

225-
this._eventBus.subscribeToEvent(
262+
this.eventBus.subscribeToEvent(
226263
DataChangedEvent,
227-
this._onDataChanged.bind(this)
264+
this.onDataChanged.bind(this)
228265
)
229266

230-
this._registry.getPlugins().forEach((plugin) => {
231-
plugin.subscribeToEvents(this._eventBus)
267+
this.registry.getPlugins().forEach((plugin) => {
268+
plugin.subscribeToEvents(this.eventBus)
232269
})
233270
}
234271

235-
private _onNodeAdded({ node }: NodeAddedEvent) {
236-
this._registry.queueNodeToBeCreated(node)
272+
private onNodeAdded({ node }: NodeAddedEvent) {
273+
this.registry.queueNodeToBeCreated(node)
237274
}
238275

239-
private _onNodeRemoved({ node }: NodeRemovedEvent) {
240-
this._registry.queueNodeToBeRemoved(node)
276+
private onNodeRemoved({ node }: NodeRemovedEvent) {
277+
this.registry.queueNodeToBeRemoved(node)
241278
}
242279

243-
private _onDataChanged(event: DataChangedEvent) {
244-
this._registry.notifyPluginAboutDataChange(event)
280+
private onDataChanged(event: DataChangedEvent) {
281+
this.registry.notifyPluginAboutDataChange(event)
245282
}
246283

247-
private _read() {
248-
this._registry.getViews().forEach((view) => {
284+
private read() {
285+
this.registry.getViews().forEach((view) => {
249286
view.read()
250287
})
251288
}
252289

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
256293
.getPlugins()
257294
.slice()
258295
.reverse()
259296
.forEach((plugin) => {
260297
plugin.init()
261298
})
262-
this._registry.getRenderablePlugins().forEach((plugin) => {
299+
this.registry.getRenderablePlugins().forEach((plugin) => {
263300
plugin.update(ts, dt)
264301
})
265-
this._registry.getViews().forEach((view) => {
302+
this.registry.getViews().forEach((view) => {
266303
view.update(ts, dt)
267304
})
268-
this._registry.getViews().forEach((view) => {
305+
this.registry.getViews().forEach((view) => {
269306
// Update previous rect after all views have been updated.
270307
// This is needed to ensure that we are using the same
271308
// previous rect across all view props.
272309
view._updatePreviousRect()
273310
})
274311
}
275312

276-
private _render() {
277-
this._registry.getRenderablePlugins().forEach((plugin) => {
313+
private render() {
314+
this.registry.getRenderablePlugins().forEach((plugin) => {
278315
plugin.render()
279316
})
280-
this._registry.getViews().forEach((view) => {
317+
this.registry.getViews().forEach((view) => {
281318
view.render()
282319
})
283320
}
284321
}
285322

286-
export function createApp() {
323+
export function createApp(): VeloxiApp {
287324
return App.create()
288325
}

0 commit comments

Comments
 (0)