Skip to content

Commit e4d0174

Browse files
authored
Merge pull request #255 from wechat-miniprogram/feat-attach-status-for-element
feat: allow to observe attach status for native node
2 parents ddd99de + 329978f commit e4d0174

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

glass-easel/src/element.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -578,20 +578,23 @@ export class Element implements NodeCast {
578578
const callFunc = function callFunc(node: Node) {
579579
if (isElement(node) && !node._$attached) {
580580
node._$attached = true
581-
if (isComponent(node)) {
581+
const nodeIsComponent = isComponent(node)
582+
if (nodeIsComponent) {
582583
node.triggerLifetime('attached', [])
583584
if (node._$relation) {
584585
node._$relation.triggerLinkEvent(RelationType.ParentNonVirtualNode, false)
585586
node._$relation.triggerLinkEvent(RelationType.ParentComponent, false)
586587
node._$relation.triggerLinkEvent(RelationType.Ancestor, false)
587588
}
588-
if (node._$mutationObserverTarget) {
589-
MutationObserverTarget.callAttachObservers(node, {
590-
type: 'attachStatus',
591-
target: node,
592-
status: 'attached',
593-
})
594-
}
589+
}
590+
if (node._$mutationObserverTarget) {
591+
MutationObserverTarget.callAttachObservers(node, {
592+
type: 'attachStatus',
593+
target: node,
594+
status: 'attached',
595+
})
596+
}
597+
if (nodeIsComponent) {
595598
const shadowRoot = node.getShadowRoot()
596599
if (shadowRoot) callFunc(shadowRoot)
597600
}
@@ -622,16 +625,16 @@ export class Element implements NodeCast {
622625
node._$relation.triggerLinkEvent(RelationType.ParentComponent, true)
623626
node._$relation.triggerLinkEvent(RelationType.Ancestor, true)
624627
}
625-
if (node._$mutationObserverTarget) {
626-
MutationObserverTarget.callAttachObservers(node, {
627-
type: 'attachStatus',
628-
target: node,
629-
status: 'detached',
630-
})
631-
}
632628
} else {
633629
node._$attached = false
634630
}
631+
if (node._$mutationObserverTarget) {
632+
MutationObserverTarget.callAttachObservers(node, {
633+
type: 'attachStatus',
634+
target: node,
635+
status: 'detached',
636+
})
637+
}
635638
}
636639
}
637640
callFunc(node)

glass-easel/tests/core/mutation_observer.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,4 +409,26 @@ describe('MutationObserver', () => {
409409
textNode.textContent = 'def'
410410
expect(callEv.pop()).toBeUndefined()
411411
})
412+
413+
it('observer native node attach status', () => {
414+
const elem = glassEasel.Component.createWithContext('root', parentDef, domBackend)
415+
const shadowRoot = elem.getShadowRoot()!
416+
const child1 = shadowRoot.childNodes[0]!.asInstanceOf(childDef)!
417+
const div = child1.childNodes[1]!.asNativeNode()!
418+
const callEv = [] as glassEasel.mutationObserver.MutationObserverEvent[]
419+
const observer = glassEasel.MutationObserver.create((ev) => {
420+
callEv.push(ev)
421+
})
422+
observer.observe(div, { attachStatus: true })
423+
glassEasel.Element.pretendAttached(elem)
424+
expect(callEv.pop()).toMatchObject({
425+
type: 'attachStatus',
426+
status: 'attached'
427+
})
428+
glassEasel.Element.pretendDetached(elem)
429+
expect(callEv.pop()).toMatchObject({
430+
type: 'attachStatus',
431+
status: 'detached'
432+
})
433+
})
412434
})

0 commit comments

Comments
 (0)