Skip to content

Commit a36c79f

Browse files
committed
tweak
1 parent b2135c5 commit a36c79f

File tree

1 file changed

+34
-36
lines changed

1 file changed

+34
-36
lines changed

src/directives/for.ts

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -72,52 +72,53 @@ export const _for = (el: Element, exp: string, ctx: Context) => {
7272
const map: KeyToIndexMap = new Map()
7373
const scopes: ChildScope[] = []
7474

75-
const createScope = (
76-
value: any,
77-
index: number,
78-
objKey?: string
79-
): ChildScope => {
80-
const data: any = {}
81-
if (destructureBindings) {
82-
destructureBindings.forEach(
83-
(b, i) => (data[b] = value[isArrayDestructure ? i : b])
84-
)
85-
} else {
86-
data[valueExp] = value
87-
}
88-
if (objKey) {
89-
indexExp && (data[indexExp] = objKey)
90-
objIndexExp && (data[objIndexExp] = index)
91-
} else {
92-
indexExp && (data[indexExp] = index)
93-
}
94-
const childCtx = createScopedContext(ctx, data)
95-
const key = keyExp ? evaluate(childCtx.scope, keyExp) : index
96-
map.set(key, index)
97-
return {
98-
ctx: childCtx,
99-
key
100-
}
101-
}
102-
10375
if (isArray(source)) {
10476
for (let i = 0; i < source.length; i++) {
105-
scopes.push(createScope(source[i], i))
77+
scopes.push(createScope(map, source[i], i))
10678
}
10779
} else if (typeof source === 'number') {
10880
for (let i = 0; i < source; i++) {
109-
scopes.push(createScope(i + 1, i))
81+
scopes.push(createScope(map, i + 1, i))
11082
}
11183
} else if (isObject(source)) {
11284
let i = 0
11385
for (const key in source) {
114-
scopes.push(createScope(source[key], i++, key))
86+
scopes.push(createScope(map, source[key], i++, key))
11587
}
11688
}
11789

11890
return [scopes, map]
11991
}
12092

93+
const createScope = (
94+
map: KeyToIndexMap,
95+
value: any,
96+
index: number,
97+
objKey?: string
98+
): ChildScope => {
99+
const data: any = {}
100+
if (destructureBindings) {
101+
destructureBindings.forEach(
102+
(b, i) => (data[b] = value[isArrayDestructure ? i : b])
103+
)
104+
} else {
105+
data[valueExp] = value
106+
}
107+
if (objKey) {
108+
indexExp && (data[indexExp] = objKey)
109+
objIndexExp && (data[objIndexExp] = index)
110+
} else {
111+
indexExp && (data[indexExp] = index)
112+
}
113+
const childCtx = createScopedContext(ctx, data)
114+
const key = keyExp ? evaluate(childCtx.scope, keyExp) : index
115+
map.set(key, index)
116+
return {
117+
ctx: childCtx,
118+
key
119+
}
120+
}
121+
121122
const mountBlock = ({ ctx, key }: ChildScope, ref: Node) => {
122123
const block = new Block(el, ctx)
123124
block.key = key
@@ -130,13 +131,10 @@ export const _for = (el: Element, exp: string, ctx: Context) => {
130131
const prevKeyToIndexMap = keyToIndexMap
131132
;[scopes, keyToIndexMap] = createChildScopes(source)
132133
if (!mounted) {
133-
blocks = []
134-
for (const scope of scopes) {
135-
blocks.push(mountBlock(scope, anchor))
136-
}
134+
blocks = scopes.map((s) => mountBlock(s, anchor))
137135
mounted = true
138136
} else {
139-
const nextBlocks: Block[] = new Array(scopes.length)
137+
const nextBlocks: Block[] = []
140138
for (let i = 0; i < blocks.length; i++) {
141139
if (!keyToIndexMap.has(blocks[i].key)) {
142140
blocks[i].remove()

0 commit comments

Comments
 (0)