1
1
import { VTree } from './types' ;
2
2
3
3
/* virtual-dom diffing algorithm, applies patches as detected */
4
- export function diff ( currentObj : VTree , newObj : VTree , parent : Element ) {
4
+ export function diff ( currentObj : VTree , newObj : VTree , parent : Element ) : void {
5
5
if ( ! currentObj && ! newObj ) return ;
6
6
else if ( ! currentObj && newObj ) create ( newObj , parent ) ;
7
7
else if ( ! newObj ) destroy ( currentObj , parent ) ;
@@ -14,7 +14,7 @@ export function diff(currentObj: VTree, newObj: VTree, parent: Element) {
14
14
}
15
15
}
16
16
// replace everything function
17
- function replace ( c : VTree , n : VTree , parent : Element ) {
17
+ function replace ( c : VTree , n : VTree , parent : Element ) : void {
18
18
// step1 : prepare to delete, unmount things
19
19
callBeforeDestroyedRecursive ( c ) ;
20
20
// ^ this will unmount sub components before we replace the child
@@ -33,7 +33,7 @@ function replace (c: VTree, n: VTree, parent: Element) {
33
33
} ;
34
34
35
35
// destroy vtext, vnode, vcomp
36
- function destroy ( obj : VTree , parent : Element ) {
36
+ function destroy ( obj : VTree , parent : Element ) : void {
37
37
// step 1: invoke destroy pre-hooks on vnode and vcomp
38
38
callBeforeDestroyedRecursive ( obj ) ;
39
39
// step 2: destroy
@@ -42,7 +42,7 @@ function destroy (obj: VTree, parent: Element) {
42
42
callDestroyedRecursive ( obj ) ;
43
43
} ;
44
44
45
- function diffNodes ( c : VTree , n : VTree , parent : Element ) {
45
+ function diffNodes ( c : VTree , n : VTree , parent : Element ) : void {
46
46
// bail out on easy vtext case
47
47
if ( c [ 'type' ] === 'vtext' ) {
48
48
if ( c [ 'text' ] !== n [ 'text' ] ) c [ 'domRef' ] . textContent = n [ 'text' ] ;
@@ -64,36 +64,36 @@ function diffNodes (c: VTree, n: VTree, parent: Element) {
64
64
}
65
65
} ;
66
66
// ** recursive calls to hooks
67
- function callDestroyedRecursive ( obj : VTree ) {
67
+ function callDestroyedRecursive ( obj : VTree ) : void {
68
68
callDestroyed ( obj ) ;
69
69
for ( var i in obj [ 'children' ] ) {
70
70
callDestroyedRecursive ( obj [ 'children' ] [ i ] ) ;
71
71
}
72
72
} ;
73
73
74
- function callDestroyed ( obj : VTree ) {
74
+ function callDestroyed ( obj : VTree ) : void {
75
75
if ( obj [ 'onDestroyed' ] ) obj [ 'onDestroyed' ] ( ) ;
76
76
} ;
77
77
78
- function callBeforeDestroyed ( obj : any ) {
78
+ function callBeforeDestroyed ( obj : VTree ) : void {
79
79
if ( obj [ 'onBeforeDestroyed' ] ) obj [ 'onBeforeDestroyed' ] ( ) ;
80
80
if ( obj [ 'type' ] === 'vcomp' ) obj [ 'unmount' ] ( obj [ 'domRef' ] ) ;
81
81
} ;
82
82
83
- function callBeforeDestroyedRecursive ( obj : VTree ) {
83
+ function callBeforeDestroyedRecursive ( obj : VTree ) : void {
84
84
callBeforeDestroyed ( obj ) ;
85
85
for ( var i in obj [ 'children' ] ) {
86
86
callBeforeDestroyedRecursive ( obj [ 'children' ] [ i ] ) ;
87
87
}
88
88
} ;
89
89
90
90
// ** </> recursive calls to hooks
91
- export function callCreated ( obj : VTree ) {
91
+ export function callCreated ( obj : VTree ) : void {
92
92
if ( obj [ 'onCreated' ] ) obj [ 'onCreated' ] ( ) ;
93
93
if ( obj [ 'type' ] === 'vcomp' ) mountComponent ( obj ) ;
94
94
}
95
95
96
- export function populate ( c : VTree , n : VTree ) {
96
+ export function populate ( c : VTree , n : VTree ) : void {
97
97
if ( ! c ) {
98
98
c = {
99
99
props : null ,
@@ -125,7 +125,7 @@ function diffProps
125
125
, nProps : Map < string , string >
126
126
, node : Element
127
127
, isSvg : boolean
128
- ) {
128
+ ) : void {
129
129
var newProp ;
130
130
/* Is current prop in new prop list? */
131
131
for ( var c in cProps ) {
@@ -173,7 +173,11 @@ function diffProps
173
173
}
174
174
} ;
175
175
176
- function diffCss ( cCss : Map < string , string > , nCss : Map < string , string > , node : HTMLElement ) {
176
+ function diffCss
177
+ ( cCss : Map < string , string >
178
+ , nCss : Map < string , string >
179
+ , node : HTMLElement
180
+ ) : void {
177
181
var result ;
178
182
/* is current attribute in new attribute list? */
179
183
for ( var c in cCss ) {
@@ -192,7 +196,7 @@ function diffCss (cCss: Map<string,string>, nCss: Map<string,string>, node: HTML
192
196
}
193
197
} ;
194
198
195
- function hasKeys ( ns : Array < VTree > , cs : Array < VTree > ) {
199
+ function hasKeys ( ns : Array < VTree > , cs : Array < VTree > ) : boolean {
196
200
return (
197
201
ns . length > 0 &&
198
202
cs . length > 0 &&
@@ -201,7 +205,7 @@ function hasKeys (ns: Array<VTree>, cs: Array<VTree>) {
201
205
) ;
202
206
} ;
203
207
204
- function diffChildren ( cs : Array < VTree > , ns : Array < VTree > , parent : HTMLElement ) {
208
+ function diffChildren ( cs : Array < VTree > , ns : Array < VTree > , parent : HTMLElement ) : void {
205
209
var longest : number = ns . length > cs . length ? ns . length : cs . length ;
206
210
if ( hasKeys ( ns , cs ) ) {
207
211
syncChildren ( cs , ns , parent ) ;
@@ -212,7 +216,7 @@ function diffChildren (cs: Array<VTree>, ns: Array<VTree>, parent: HTMLElement)
212
216
}
213
217
} ;
214
218
215
- function populateDomRef ( obj : VTree ) {
219
+ function populateDomRef ( obj : VTree ) : void {
216
220
if ( obj [ 'ns' ] === 'svg' ) {
217
221
obj [ 'domRef' ] = document . createElementNS (
218
222
'http://www.w3.org/2000/svg' ,
@@ -228,15 +232,15 @@ function populateDomRef (obj: VTree) {
228
232
}
229
233
} ;
230
234
// dmj: refactor this, the callback function feels meh
231
- function createElement ( obj : VTree , cb : ( ( Element ) => void ) ) {
235
+ function createElement ( obj : VTree , cb : ( ( Element ) => void ) ) : void {
232
236
populateDomRef ( obj ) ;
233
237
cb ( obj [ 'domRef' ] ) ;
234
238
populate ( null , obj ) ;
235
239
callCreated ( obj ) ;
236
240
} ;
237
241
// mounts vcomp by calling into Haskell side.
238
242
// unmount is handled with pre-destroy recursive hooks
239
- function mountComponent ( obj : VTree ) {
243
+ function mountComponent ( obj : VTree ) : void {
240
244
var componentId = obj [ 'data-component-id' ] ,
241
245
nodeList = document . querySelectorAll (
242
246
"[data-component-id='" + componentId + "']" ,
@@ -262,7 +266,7 @@ function mountComponent (obj: VTree) {
262
266
} ) ;
263
267
} ;
264
268
// creates nodes on virtual and dom (vtext, vcomp, vnode)
265
- function create ( obj : VTree , parent : Element ) {
269
+ function create ( obj : VTree , parent : Element ) : void {
266
270
if ( obj . type === 'vtext' ) {
267
271
obj [ 'domRef' ] = document . createTextNode ( obj [ 'text' ] ) ;
268
272
parent . appendChild ( obj [ 'domRef' ] ) ;
@@ -273,7 +277,7 @@ function create (obj: VTree, parent: Element) {
273
277
}
274
278
} ;
275
279
/* Child reconciliation algorithm, inspired by kivi and Bobril */
276
- function syncChildren ( os : Array < VTree > , ns : Array < VTree > , parent : Element ) {
280
+ function syncChildren ( os : Array < VTree > , ns : Array < VTree > , parent : Element ) : void {
277
281
var oldFirstIndex : number = 0 ,
278
282
newFirstIndex : number = 0 ,
279
283
oldLastIndex : number = os . length - 1 ,
@@ -284,7 +288,7 @@ function syncChildren (os: Array<VTree>, ns: Array<VTree>, parent: Element) {
284
288
oLast : VTree ,
285
289
oFirst : VTree ,
286
290
found : boolean ,
287
- node : any ;
291
+ node : VTree ;
288
292
for ( ; ; ) {
289
293
/* check base case, first > last for both new and old
290
294
[ ] -- old children empty (fully-swapped)
@@ -333,7 +337,7 @@ function syncChildren (os: Array<VTree>, ns: Array<VTree>, parent: Element) {
333
337
-> [ c b a ] <- new children
334
338
*/
335
339
else if ( oFirst [ 'key' ] === nLast [ 'key' ] && nFirst [ 'key' ] === oLast [ 'key' ] ) {
336
- swapDOMRefs ( node , oLast [ 'domRef' ] , oFirst [ 'domRef' ] , parent ) ;
340
+ swapDOMRefs ( oLast [ 'domRef' ] , oFirst [ 'domRef' ] , parent ) ;
337
341
swap < VTree > ( os , oldFirstIndex , oldLastIndex ) ;
338
342
diff ( os [ oldFirstIndex ++ ] , ns [ newFirstIndex ++ ] , parent ) ;
339
343
diff ( os [ oldLastIndex -- ] , ns [ newLastIndex -- ] , parent ) ;
@@ -430,13 +434,13 @@ function syncChildren (os: Array<VTree>, ns: Array<VTree>, parent: Element) {
430
434
}
431
435
} ;
432
436
433
- function swapDOMRefs ( tmp : ChildNode , a : Element , b : Element , p : Element ) {
434
- tmp = a . nextSibling ;
437
+ function swapDOMRefs ( a : Element , b : Element , p : Element ) : void {
438
+ const tmp = a . nextSibling ;
435
439
p . insertBefore ( a , b ) ;
436
440
p . insertBefore ( b , tmp ) ;
437
441
} ;
438
442
439
- function swap < T > ( os : Array < T > , l : number , r : number ) {
443
+ function swap < T > ( os : Array < T > , l : number , r : number ) : void {
440
444
var k = os [ l ] ;
441
445
os [ l ] = os [ r ] ;
442
446
os [ r ] = k ;
0 commit comments