-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathui.ts
More file actions
112 lines (86 loc) · 2.99 KB
/
Copy pathui.ts
File metadata and controls
112 lines (86 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { EventEmitter as UiEventEmitter } from 'events'
import { IEventEmitterRegistryController } from '../../interfaces/eventEmitter'
import { IUiController, UiManager, View, isExtensionOverlayView } from '../../interfaces/ui'
import EventEmitter from '../eventEmitter/eventEmitter'
export class UiController extends EventEmitter implements IUiController {
uiEvent: UiEventEmitter
views: View[] = []
window: UiManager['window']
notification: UiManager['notification']
message: UiManager['message']
dispatchDappTabFocus?: UiManager['dispatchDappTabFocus']
constructor({
eventEmitterRegistry,
uiManager
}: {
eventEmitterRegistry?: IEventEmitterRegistryController
uiManager: UiManager
}) {
super(eventEmitterRegistry)
this.uiEvent = new UiEventEmitter()
this.window = uiManager.window
this.notification = uiManager.notification
this.message = uiManager.message
this.dispatchDappTabFocus = uiManager.dispatchDappTabFocus
}
addView(view: View) {
const existingOverlay = this.views.find((v) => isExtensionOverlayView(v))
// if an overlay view already exists, just update its id and stop here
if (isExtensionOverlayView(view) && existingOverlay) {
existingOverlay.id = view.id
existingOverlay.type = view.type
if (!existingOverlay.isReady) this.uiEvent.emit('addView', view)
this.emitUpdate()
return
}
// if the same view already exists, skip adding
if (this.views.some((v) => v.id === view.id)) return
this.views.push(view)
this.uiEvent.emit('addView', view)
this.emitUpdate()
}
updateView(
viewId: string,
updatedProps: Pick<View, 'currentRoute' | 'isReady' | 'searchParams'>
) {
const view = this.views.find((v) => v.id === viewId)
if (!view) return
// @ts-expect-error
const shouldUpdate = Object.entries(updatedProps).some(([key, value]) => view[key] !== value)
if (!shouldUpdate) return
let previousRoute = view.previousRoute
if (updatedProps.currentRoute && updatedProps.currentRoute !== view.currentRoute) {
previousRoute = view.currentRoute
}
Object.assign(view, updatedProps)
if (previousRoute) {
view.previousRoute = previousRoute
}
this.uiEvent.emit('updateView', view)
this.emitUpdate()
}
removeView(viewId: string) {
const view = this.views.find((v) => v.id === viewId)
if (!view) return
this.views = this.views.filter((v) => v.id !== viewId)
this.uiEvent.emit('removeView', view)
this.emitUpdate()
}
navigateView(viewId: string, route: string, params: { [key: string]: any }) {
const view = this.views.find((v) => v.id === viewId)
if (!view || view.currentRoute === route) return
view.currentRoute = route
this.message.sendNavigateMessage(viewId, route, params)
this.emitUpdate()
}
toJSON() {
return {
...this,
...super.toJSON(),
uiEvent: undefined,
window: undefined,
notification: undefined,
message: undefined
}
}
}