@@ -51,10 +51,11 @@ type PropConfig =
51
51
type Ctrl = {
52
52
getName ( ) : string
53
53
getHost ( ) : HTMLElement
54
- isInitialized ( ) : boolean
54
+ hasRendered ( ) : boolean
55
55
isMounted ( ) : boolean
56
56
hasUpdated ( ) : boolean
57
57
refresh ( ) : void
58
+ onceBeforeMount ( task : ( ) => void ) : void
58
59
beforeMount ( taks : ( ) => void ) : void
59
60
afterMount ( task : ( ) => void ) : void
60
61
onceBeforeUpdate ( task : ( ) => void ) : void
@@ -194,11 +195,11 @@ function intercept(point: 'init' | 'render', fn: InterceptFn) {
194
195
195
196
class BaseElement extends HTMLElement {
196
197
private __ctrl ! : Ctrl
197
- private __hasAddedPropHandling = false
198
198
199
199
constructor ( ) {
200
200
super ( )
201
201
202
+ const self : any = this
202
203
const elemConfig = elemConfigByClass . get ( this . constructor ) !
203
204
const { init, patch } = elemConfig . impl
204
205
let styles = elemConfig . styles
@@ -232,29 +233,30 @@ class BaseElement extends HTMLElement {
232
233
//contentElement.append(document.createElement('span'))
233
234
this . shadowRoot ! . append ( stylesElement , contentElement )
234
235
235
- let initialized = false
236
+ let rendered = false
236
237
let mounted = false
237
238
let updated = false
238
239
let shallCommit = false
239
240
let getContent : ( ) => any // TODO
240
241
242
+ const onceBeforeMountNotifier = createNotifier ( )
243
+ const onceBeforeUpdateNotifier = createNotifier ( )
241
244
const beforeMountNotifier = createNotifier ( )
242
245
const afterMountNotifier = createNotifier ( )
243
246
const beforeUpdateNotifier = createNotifier ( )
244
247
const afterUpdateNotifier = createNotifier ( )
245
248
const beforeUnmountNotifier = createNotifier ( )
246
- const onceBeforeUpdateActions : ( ( ) => void ) [ ] = [ ]
247
249
248
250
const ctrl : Ctrl = {
249
251
getName : ( ) => this . localName ,
250
252
getHost : ( ) => this ,
251
- isInitialized : ( ) => initialized ,
253
+ hasRendered : ( ) => rendered ,
252
254
isMounted : ( ) => mounted ,
253
255
hasUpdated : ( ) => updated ,
256
+ onceBeforeMount : onceBeforeMountNotifier . subscribe ,
254
257
beforeMount : beforeMountNotifier . subscribe ,
255
258
afterMount : afterMountNotifier . subscribe ,
256
- onceBeforeUpdate : ( task : ( ) => void ) =>
257
- onceBeforeUpdateActions . push ( task ) ,
259
+ onceBeforeUpdate : onceBeforeUpdateNotifier . subscribe ,
258
260
beforeUpdate : beforeUpdateNotifier . subscribe ,
259
261
afterUpdate : afterUpdateNotifier . subscribe ,
260
262
beforeUnmount : beforeUnmountNotifier . subscribe ,
@@ -275,12 +277,10 @@ class BaseElement extends HTMLElement {
275
277
276
278
const commit = ( ) => {
277
279
if ( mounted ) {
278
- if ( onceBeforeUpdateActions . length ) {
279
- try {
280
- onceBeforeUpdateActions . forEach ( ( action ) => action ( ) )
281
- } finally {
282
- onceBeforeUpdateActions . length = 0
283
- }
280
+ try {
281
+ onceBeforeUpdateNotifier . notify ( )
282
+ } finally {
283
+ onceBeforeMountNotifier . clear ( )
284
284
}
285
285
286
286
beforeUpdateNotifier . notify ( )
@@ -301,7 +301,7 @@ class BaseElement extends HTMLElement {
301
301
interceptions . render
302
302
)
303
303
304
- initialized = true
304
+ rendered = true
305
305
306
306
if ( ! mounted ) {
307
307
mounted = true
@@ -312,27 +312,25 @@ class BaseElement extends HTMLElement {
312
312
}
313
313
}
314
314
315
- ; ( this as any ) . connectedCallback = ( ) => {
316
- if ( ! this . __hasAddedPropHandling ) {
317
- addPropHandling ( this )
318
- this . __hasAddedPropHandling = true
319
- }
315
+ runIntercepted (
316
+ ( ) => {
317
+ getContent = init ( this , ctrl )
318
+ } ,
319
+ ctrl ,
320
+ interceptions . init
321
+ )
320
322
321
- if ( ! initialized ) {
322
- runIntercepted (
323
- ( ) => {
324
- getContent = init ( this , ctrl )
325
- } ,
326
- ctrl ,
327
- interceptions . init
328
- )
323
+ self . connectedCallback = ( ) => {
324
+ if ( ! rendered ) {
325
+ addPropHandling ( this )
326
+ onceBeforeMountNotifier . notify ( )
329
327
}
330
328
331
329
beforeMountNotifier . notify ( )
332
-
333
330
commit ( )
334
331
}
335
- ; ( this as any ) . disconnectedCallback = ( ) => {
332
+
333
+ self . disconnectedCallback = ( ) => {
336
334
beforeUnmountNotifier . notify ( )
337
335
contentElement . innerHTML = ''
338
336
}
@@ -381,11 +379,6 @@ function addAttributeHandling(
381
379
oldValue : string | null ,
382
380
value : string | null
383
381
) {
384
- if ( ! this . __hasAddedPropHandling ) {
385
- addPropHandling ( this )
386
- this . __hasAddedPropHandling = true
387
- }
388
-
389
382
if ( ! ignoreAttributeChange ) {
390
383
const { propName, mapAttrToProp } = propConfigByAttrName . get (
391
384
attrName
@@ -409,6 +402,8 @@ function addPropHandling(obj: any) {
409
402
let propValue = obj [ propName ]
410
403
411
404
Object . defineProperty ( obj , propName , {
405
+ configurable : true , // TODO!!!!!!!!!!!!!!!!!!!!!!!!!!
406
+
412
407
get ( ) {
413
408
return propValue
414
409
} ,
@@ -466,7 +461,9 @@ function createNotifier() {
466
461
467
462
return {
468
463
subscribe : ( subscriber : ( ) => void ) => void subscribers . push ( subscriber ) ,
469
- notify : ( ) => void ( subscribers . length && subscribers . forEach ( ( it ) => it ( ) ) )
464
+ notify : ( ) =>
465
+ void ( subscribers . length && subscribers . forEach ( ( it ) => it ( ) ) ) ,
466
+ clear : ( ) => ( subscribers . length = 0 )
470
467
}
471
468
}
472
469
0 commit comments