@@ -9,11 +9,6 @@ const forIteratorRE = /,([^,\}\]]*)(?:,([^,\}\]]*))?$/
9
9
const stripParensRE = / ^ \( | \) $ / g
10
10
const destructureRE = / ^ [ { [ ] \s * ( (?: [ \w _ $ ] + \s * , ? \s * ) + ) [ \] } ] $ /
11
11
12
- interface ChildScope {
13
- ctx : Context
14
- key : any
15
- }
16
-
17
12
type KeyToIndexMap = Map < any , number >
18
13
19
14
export const _for = ( el : Element , exp : string , ctx : Context ) => {
@@ -63,39 +58,37 @@ export const _for = (el: Element, exp: string, ctx: Context) => {
63
58
64
59
let mounted = false
65
60
let blocks : Block [ ]
66
- let scopes : ChildScope [ ]
61
+ let childCtxs : Context [ ]
67
62
let keyToIndexMap : Map < any , number >
68
63
69
- const createChildScopes = (
70
- source : unknown
71
- ) : [ ChildScope [ ] , KeyToIndexMap ] => {
64
+ const createChildContexts = ( source : unknown ) : [ Context [ ] , KeyToIndexMap ] => {
72
65
const map : KeyToIndexMap = new Map ( )
73
- const scopes : ChildScope [ ] = [ ]
66
+ const ctxs : Context [ ] = [ ]
74
67
75
68
if ( isArray ( source ) ) {
76
69
for ( let i = 0 ; i < source . length ; i ++ ) {
77
- scopes . push ( createScope ( map , source [ i ] , i ) )
70
+ ctxs . push ( createChildContext ( map , source [ i ] , i ) )
78
71
}
79
72
} else if ( typeof source === 'number' ) {
80
73
for ( let i = 0 ; i < source ; i ++ ) {
81
- scopes . push ( createScope ( map , i + 1 , i ) )
74
+ ctxs . push ( createChildContext ( map , i + 1 , i ) )
82
75
}
83
76
} else if ( isObject ( source ) ) {
84
77
let i = 0
85
78
for ( const key in source ) {
86
- scopes . push ( createScope ( map , source [ key ] , i ++ , key ) )
79
+ ctxs . push ( createChildContext ( map , source [ key ] , i ++ , key ) )
87
80
}
88
81
}
89
82
90
- return [ scopes , map ]
83
+ return [ ctxs , map ]
91
84
}
92
85
93
- const createScope = (
86
+ const createChildContext = (
94
87
map : KeyToIndexMap ,
95
88
value : any ,
96
89
index : number ,
97
90
objKey ?: string
98
- ) : ChildScope => {
91
+ ) : Context => {
99
92
const data : any = { }
100
93
if ( destructureBindings ) {
101
94
destructureBindings . forEach (
@@ -113,25 +106,23 @@ export const _for = (el: Element, exp: string, ctx: Context) => {
113
106
const childCtx = createScopedContext ( ctx , data )
114
107
const key = keyExp ? evaluate ( childCtx . scope , keyExp ) : index
115
108
map . set ( key , index )
116
- return {
117
- ctx : childCtx ,
118
- key
119
- }
109
+ childCtx . key = key
110
+ return childCtx
120
111
}
121
112
122
- const mountBlock = ( { ctx, key } : ChildScope , ref : Node ) => {
113
+ const mountBlock = ( ctx : Context , ref : Node ) => {
123
114
const block = new Block ( el , ctx )
124
- block . key = key
115
+ block . key = ctx . key
125
116
block . insert ( parent , ref )
126
117
return block
127
118
}
128
119
129
120
ctx . effect ( ( ) => {
130
121
const source = evaluate ( ctx . scope , sourceExp )
131
122
const prevKeyToIndexMap = keyToIndexMap
132
- ; [ scopes , keyToIndexMap ] = createChildScopes ( source )
123
+ ; [ childCtxs , keyToIndexMap ] = createChildContexts ( source )
133
124
if ( ! mounted ) {
134
- blocks = scopes . map ( ( s ) => mountBlock ( s , anchor ) )
125
+ blocks = childCtxs . map ( ( s ) => mountBlock ( s , anchor ) )
135
126
mounted = true
136
127
} else {
137
128
const nextBlocks : Block [ ] = [ ]
@@ -141,21 +132,24 @@ export const _for = (el: Element, exp: string, ctx: Context) => {
141
132
}
142
133
}
143
134
144
- let i = scopes . length
135
+ let i = childCtxs . length
145
136
while ( i -- ) {
146
- const scope = scopes [ i ]
147
- const oldIndex = prevKeyToIndexMap . get ( scope . key )
148
- const next = scopes [ i + 1 ]
137
+ const childCtx = childCtxs [ i ]
138
+ const oldIndex = prevKeyToIndexMap . get ( childCtx . key )
139
+ const next = childCtxs [ i + 1 ]
149
140
const nextBlockOldIndex = next && prevKeyToIndexMap . get ( next . key )
150
141
const nextBlock =
151
142
nextBlockOldIndex == null ? undefined : blocks [ nextBlockOldIndex ]
152
143
if ( oldIndex == null ) {
153
144
// new
154
- nextBlocks [ i ] = mountBlock ( scope , nextBlock ? nextBlock . el : anchor )
145
+ nextBlocks [ i ] = mountBlock (
146
+ childCtx ,
147
+ nextBlock ? nextBlock . el : anchor
148
+ )
155
149
} else {
156
150
// update
157
151
const block = ( nextBlocks [ i ] = blocks [ oldIndex ] )
158
- Object . assign ( block . ctx . scope , scope . ctx . scope )
152
+ Object . assign ( block . ctx . scope , childCtx . scope )
159
153
if ( oldIndex !== i ) {
160
154
// moved
161
155
if ( blocks [ oldIndex + 1 ] !== nextBlock ) {
0 commit comments