UNPKG

903 BJavaScriptView Raw
1import * as is from '../is';
2import * as util from '../util';
3
4let cache = function( fn, name ){
5 return function traversalCache( arg1, arg2, arg3, arg4 ){
6 let selectorOrEles = arg1;
7 let eles = this;
8 let key;
9
10 if( selectorOrEles == null ){
11 key = '';
12 } else if( is.elementOrCollection( selectorOrEles ) && selectorOrEles.length === 1 ){
13 key = selectorOrEles.id();
14 }
15
16 if( eles.length === 1 && key ){
17 let _p = eles[0]._private;
18 let tch = _p.traversalCache = _p.traversalCache || {};
19 let ch = tch[ name ] = tch[ name ] || [];
20 let hash = util.hashString( key );
21 let cacheHit = ch[ hash ];
22
23 if( cacheHit ){
24 return cacheHit;
25 } else {
26 return ( ch[ hash ] = fn.call( eles, arg1, arg2, arg3, arg4 ) );
27 }
28 } else {
29 return fn.call( eles, arg1, arg2, arg3, arg4 );
30 }
31 };
32};
33
34export default cache;