6
6
7
7
/** @type {Map<string, FrameworkComponent> } */
8
8
const registry = new Map ( ) ;
9
+ const listeners = new Map ( ) ;
9
10
10
11
/**
11
12
* @param {{[key: string]: FrameworkComponent} } components
@@ -23,14 +24,24 @@ function updateProps(webComponent, props = {}) {
23
24
webComponent [ key ] = value ;
24
25
}
25
26
27
+ /**
28
+ * @param {HTMLElement } webComponent
29
+ */
30
+ function removeEvents ( webComponent , events = { } ) {
31
+ for ( const [ key ] of Object . entries ( events ) ) {
32
+ webComponent . removeEventListener ( key , listeners . get ( key ) ) ;
33
+ listeners . delete ( key )
34
+ }
35
+ }
36
+
26
37
/**
27
38
* @param {HTMLElement } webComponent
28
39
*/
29
40
function updateEvents ( webComponent , events = { } ) {
30
41
for ( const [ key , listener ] of Object . entries ( events ) ) {
31
- webComponent . addEventListener ( key , event =>
32
- listener ( /** @type { CustomEvent } */ ( event ) . detail )
33
- ) ;
42
+ const fn = event => listener ( /** @type { CustomEvent } */ ( event ) . detail ) ;
43
+ webComponent . addEventListener ( key , fn )
44
+ listeners . set ( key , fn )
34
45
}
35
46
}
36
47
@@ -81,7 +92,7 @@ function updateSlots(webComponent, slots = {}) {
81
92
/**
82
93
* @param {Component } component
83
94
*/
84
- function createComponent ( component ) {
95
+ function getComponent ( component ) {
85
96
let Component = registry . get ( component . type ) ;
86
97
if ( ! Component ) {
87
98
// Lookup by shorthand.
@@ -100,15 +111,28 @@ function createComponent(component) {
100
111
} . Following components are registered: ${ [ ...registry . keys ( ) ] } `
101
112
) ;
102
113
114
+ return Component ;
115
+ }
116
+
117
+ window . playwrightMount = async ( component , rootElement , hooksConfig ) => {
103
118
if ( component . kind !== 'object' )
104
119
throw new Error ( 'JSX mount notation is not supported' ) ;
105
120
121
+ const Component = getComponent ( component ) ;
122
+
106
123
const webComponent = new Component ( ) ;
107
124
updateProps ( webComponent , component . options ?. props ) ;
108
125
updateSlots ( webComponent , component . options ?. slots ) ;
109
126
updateEvents ( webComponent , component . options ?. on ) ;
110
- return webComponent ;
111
- }
127
+
128
+ for ( const hook of window [ '__pw_hooks_before_mount' ] || [ ] )
129
+ await hook ( { hooksConfig } ) ;
130
+
131
+ rootElement . appendChild ( webComponent ) ;
132
+
133
+ for ( const hook of window [ '__pw_hooks_after_mount' ] || [ ] )
134
+ await hook ( { hooksConfig } ) ;
135
+ } ;
112
136
113
137
window . playwrightUpdate = async ( rootElement , component ) => {
114
138
if ( component . kind === 'jsx' )
@@ -119,19 +143,10 @@ window.playwrightUpdate = async (rootElement, component) => {
119
143
120
144
updateProps ( webComponent , component . options ?. props ) ;
121
145
updateSlots ( webComponent , component . options ?. slots ) ;
146
+ removeEvents ( webComponent , component . options ?. on ) ;
122
147
updateEvents ( webComponent , component . options ?. on ) ;
123
148
} ;
124
149
125
150
window . playwrightUnmount = async ( rootElement ) => {
126
151
rootElement . replaceChildren ( ) ;
127
- } ;
128
-
129
- window . playwrightMount = async ( component , rootElement , hooksConfig ) => {
130
- for ( const hook of window [ '__pw_hooks_before_mount' ] || [ ] )
131
- await hook ( { hooksConfig } ) ;
132
-
133
- rootElement . appendChild ( createComponent ( component ) ) ;
134
-
135
- for ( const hook of window [ '__pw_hooks_after_mount' ] || [ ] )
136
- await hook ( { hooksConfig } ) ;
137
- } ;
152
+ } ;
0 commit comments