Skip to content

Commit 3c6ebf4

Browse files
authored
Merge pull request #259 from wechat-miniprogram/fix-lazy-relation
Fx relation when anthor component is lazily loaded
2 parents 7a0494b + 2732fad commit 3c6ebf4

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

glass-easel/src/relation.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export const normalizeRelation = <TOut extends { [key: string]: any }>(
5959
let target:
6060
| GeneralBehavior
6161
| TraitBehavior<{ [key: string]: unknown }, { [key: string]: unknown }>
62+
| string
6263
| null = null
6364
if (relation.target instanceof ComponentDefinition) {
6465
target = relation.target.behavior as GeneralBehavior
@@ -73,6 +74,8 @@ export const normalizeRelation = <TOut extends { [key: string]: any }>(
7374
const globalTarget = ownerSpace.getGlobalUsingComponent(path)
7475
if (typeof globalTarget === 'object' && globalTarget !== null) {
7576
target = globalTarget.behavior
77+
} else {
78+
target = path
7679
}
7780
}
7881
}
@@ -110,7 +113,10 @@ export type RelationListener = (target: unknown) => void
110113
export type RelationFailedListener = () => void
111114

112115
export type RelationDefinition = {
113-
target: GeneralBehavior | TraitBehavior<{ [x: string]: unknown }, { [x: string]: unknown }>
116+
target:
117+
| GeneralBehavior
118+
| TraitBehavior<{ [x: string]: unknown }, { [x: string]: unknown }>
119+
| string
114120
type: RelationType
115121
linked: RelationListener | null
116122
linkChanged: RelationListener | null
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/* eslint-disable no-restricted-syntax */
2+
import { domBackend } from '../base/env'
3+
import * as glassEasel from '../../src'
4+
5+
const componentSpace = new glassEasel.ComponentSpace()
6+
componentSpace.updateComponentOptions({
7+
writeFieldsToNode: true,
8+
})
9+
componentSpace.defineComponent({
10+
is: '',
11+
})
12+
13+
describe('Relation', () => {
14+
test('should link when another component is lazily loaded', () => {
15+
const childLinkedList: ['linked' | 'unlinked', glassEasel.GeneralComponent][] = []
16+
const childDef = componentSpace.defineComponent({
17+
is: 'child',
18+
relations: {
19+
parent: {
20+
type: 'ancestor',
21+
linked(target) {
22+
childLinkedList.push(['linked', target])
23+
},
24+
unlinked(target) {
25+
childLinkedList.push(['unlinked', target])
26+
},
27+
},
28+
},
29+
})
30+
const child = glassEasel.Component.createWithContext('child', childDef, domBackend)
31+
32+
expect(childLinkedList).toEqual([])
33+
34+
const parentLinkedList: ['linked' | 'unlinked', glassEasel.GeneralComponent][] = []
35+
const parentDef = componentSpace.defineComponent({
36+
is: 'parent',
37+
relations: {
38+
child: {
39+
type: 'descendant',
40+
linked(target) {
41+
parentLinkedList.push(['linked', target])
42+
},
43+
unlinked(target) {
44+
parentLinkedList.push(['unlinked', target])
45+
},
46+
},
47+
},
48+
})
49+
const parent = glassEasel.Component.createWithContext('parent', parentDef, domBackend)
50+
glassEasel.Element.pretendAttached(parent)
51+
52+
expect(childLinkedList).toStrictEqual([])
53+
expect(parentLinkedList).toStrictEqual([])
54+
55+
parent.appendChild(child)
56+
57+
expect(childLinkedList).toStrictEqual([['linked', parent]])
58+
expect(parentLinkedList).toStrictEqual([['linked', child]])
59+
60+
parent.removeChild(child)
61+
62+
expect(childLinkedList).toStrictEqual([
63+
['linked', parent],
64+
['unlinked', parent],
65+
])
66+
expect(parentLinkedList).toStrictEqual([
67+
['linked', child],
68+
['unlinked', child],
69+
])
70+
})
71+
})

0 commit comments

Comments
 (0)