mirror of
				https://github.com/flynx/ImageGrid.git
				synced 2025-10-30 19:00:09 +00:00 
			
		
		
		
	updated the cache mechanics a bit more + don't show progress if things go fast enough...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
		
							parent
							
								
									8b2a135b9d
								
							
						
					
					
						commit
						9f1fe87509
					
				| @ -271,9 +271,10 @@ function(func){ | |||||||
| var IntrospectionActions = actions.Actions({ | var IntrospectionActions = actions.Actions({ | ||||||
| 	// user-callable actions...
 | 	// user-callable actions...
 | ||||||
| 	get useractions(){ | 	get useractions(){ | ||||||
| 		return this.cache('useractions', function(){ | 		return this.cache('useractions', function(d){ | ||||||
| 			return this.actions.filter(this.isUserCallable.bind(this)) }) }, | 			return d instanceof Array ?  | ||||||
| 		//return this.actions.filter(this.isUserCallable.bind(this)) },
 | 				d.slice()  | ||||||
|  | 				: this.actions.filter(this.isUserCallable.bind(this)) }) }, | ||||||
| 
 | 
 | ||||||
| 	// check if action is callable by user...
 | 	// check if action is callable by user...
 | ||||||
| 	isUserCallable: ['- System/', | 	isUserCallable: ['- System/', | ||||||
| @ -571,9 +572,9 @@ var CacheActions = actions.Actions({ | |||||||
| 		// Control pre-caching...
 | 		// Control pre-caching...
 | ||||||
| 		//
 | 		//
 | ||||||
| 		// This can be:
 | 		// This can be:
 | ||||||
| 		// 		true	- sync pre-cache
 | 		// 		true	- sync pre-cache (recursion)
 | ||||||
| 		// 		0		- semi-sync pre-cache
 | 		// 		0		- semi-sync pre-cache
 | ||||||
| 		// 		number	- delay pre-caching by number milliseconds
 | 		// 		number	- delay in milliseconds between pre-cache chunks
 | ||||||
| 		// 		false	- pre-caching disabled
 | 		// 		false	- pre-caching disabled
 | ||||||
| 		'pre-cache': 0, | 		'pre-cache': 0, | ||||||
| 
 | 
 | ||||||
| @ -582,66 +583,114 @@ var CacheActions = actions.Actions({ | |||||||
| 		// Caching is done in a series of chunks set by this separated by 
 | 		// Caching is done in a series of chunks set by this separated by 
 | ||||||
| 		// timeouts set by .config['pre-cache'] to let other stuff run...
 | 		// timeouts set by .config['pre-cache'] to let other stuff run...
 | ||||||
| 		'pre-cache-chunk': 8, | 		'pre-cache-chunk': 8, | ||||||
|  | 
 | ||||||
|  | 		// Control pre-cache progress display...
 | ||||||
|  | 		//
 | ||||||
|  | 		// This can be:
 | ||||||
|  | 		// 	false		- never show progress
 | ||||||
|  | 		// 	true		- always show progress
 | ||||||
|  | 		// 	number		- show progress if number of milliseconds has 
 | ||||||
|  | 		// 					passed and we are not done yet...
 | ||||||
|  | 		//
 | ||||||
|  | 		// NOTE: progress will only be displayed if .showProgress(..) 
 | ||||||
|  | 		// 		action is available...
 | ||||||
|  | 		'pre-cache-progress': 3000, | ||||||
| 	}, | 	}, | ||||||
| 
 | 
 | ||||||
| 	// XXX should these be actions??? 
 | 	// Cache utility method...
 | ||||||
|  | 	//
 | ||||||
|  | 	// Example use:
 | ||||||
|  | 	// 	someAction: [
 | ||||||
|  | 	// 		function(){
 | ||||||
|  | 	// 			return this.cache('someAction', 
 | ||||||
|  | 	// 				function(data){
 | ||||||
|  | 	// 					if(data){
 | ||||||
|  | 	// 						// clone/update the data...
 | ||||||
|  | 	// 						// NOTE: this should be faster than the construction
 | ||||||
|  | 	// 						//		branch below or this will defeat the purpose 
 | ||||||
|  | 	// 						//		of caching...
 | ||||||
|  | 	// 						...
 | ||||||
|  | 	//
 | ||||||
|  | 	// 					} else {
 | ||||||
|  | 	// 						// get the data...
 | ||||||
|  | 	// 						...
 | ||||||
|  | 	// 					}
 | ||||||
|  | 	// 					return data
 | ||||||
|  | 	// 				}) }],
 | ||||||
|  | 	//
 | ||||||
| 	cache: function(title, lister){ | 	cache: function(title, lister){ | ||||||
| 		if(!this.config.cache){ | 		if(!this.config.cache){ | ||||||
| 			return lister.call(this) | 			return lister.call(this) | ||||||
| 		} | 		} | ||||||
| 		var cache = this.__cache = this.__cache || {} | 		var cache = this.__cache = this.__cache || {} | ||||||
| 		var l = cache[title] = cache[title] || lister.call(this) | 		return (cache[title] =  | ||||||
| 		return l | 			title in cache ?  | ||||||
|  | 				// pass the cached data for cloning/update to the lister...
 | ||||||
|  | 				lister.call(this, cache[title]) | ||||||
|  | 				: lister.call(this)) | ||||||
| 	}, | 	}, | ||||||
| 	// XXX revise naming...
 | 
 | ||||||
| 	// XXX is this too broad??
 | 	preCache: ['System/Run pre-cache', | ||||||
| 	preCache: function(){ | 		doc`Run pre-cache...
 | ||||||
| 		if(this.config.cache){ | 
 | ||||||
| 			for(k in this){ this[k] } }  | 			Do an async pre-cache... | ||||||
| 		return this | 			.preCache() | ||||||
| 	}, | 
 | ||||||
| 	_preCache: function(t){ | 			Do a sync pre-cache... | ||||||
| 		if(this.config.cache){ | 			.preCache(true) | ||||||
| 			var t = t || this.config['pre-cache'] || 0 | 
 | ||||||
| 			var c = this.config['pre-cache-chunk'] || 8 | 		NOTE: both "modes" of doing a pre-cache run in the main thread, | ||||||
| 			var done = 0 | 			the difference is that the "async" version lets JS run frames | ||||||
| 			var attrs = [] | 			between processing sync chunks... | ||||||
| 			for(var k in this){ | 		`,
 | ||||||
| 				attrs.push(k) | 		function(t){ | ||||||
|  | 			if(this.config.cache){ | ||||||
|  | 				var t = t || this.config['pre-cache'] || 0 | ||||||
|  | 				var c = this.config['pre-cache-chunk'] || 8 | ||||||
|  | 				var done = 0 | ||||||
|  | 				var attrs = [] | ||||||
|  | 				for(var k in this){ | ||||||
|  | 					attrs.push(k) | ||||||
|  | 				} | ||||||
|  | 				var l = attrs.length | ||||||
|  | 
 | ||||||
|  | 				var started = Date.now() | ||||||
|  | 				var show = this.config['pre-cache-progress'] | ||||||
|  | 
 | ||||||
|  | 				var tick = function(){ | ||||||
|  | 					var a = Date.now() | ||||||
|  | 					var b = a | ||||||
|  | 					if(attrs.length == 0){ | ||||||
|  | 						return | ||||||
|  | 					} | ||||||
|  | 
 | ||||||
|  | 					while(b - a < c){ | ||||||
|  | 						this[attrs.pop()] | ||||||
|  | 						b = Date.now() | ||||||
|  | 						done += 1 | ||||||
|  | 
 | ||||||
|  | 						this.showProgress | ||||||
|  | 							&& (show === true || (show && b - started > show)) | ||||||
|  | 							&& this.showProgress('Caching', done, l) | ||||||
|  | 					} | ||||||
|  | 
 | ||||||
|  | 					t === true ? | ||||||
|  | 						tick() | ||||||
|  | 						: setTimeout(tick, t) | ||||||
|  | 				}.bind(this) | ||||||
|  | 
 | ||||||
|  | 				tick() | ||||||
| 			} | 			} | ||||||
| 			var l = attrs.length | 		}], | ||||||
|  | 	clearCache: ['System/Clear cache', | ||||||
|  | 		function(title){ | ||||||
|  | 			if(title){ | ||||||
|  | 				delete (this.__cache|| {})[title] | ||||||
| 
 | 
 | ||||||
| 			var tick = function(){ | 			} else { | ||||||
| 				var a = Date.now() | 				delete this.__cache | ||||||
| 				var b = a | 			} | ||||||
| 				if(attrs.length == 0){ | 		}], | ||||||
| 					return |  | ||||||
| 				} |  | ||||||
| 
 |  | ||||||
| 				while(b - a < c){ |  | ||||||
| 					this[attrs.pop()] |  | ||||||
| 					b = Date.now() |  | ||||||
| 
 |  | ||||||
| 					// XXX is this the right way to go???
 |  | ||||||
| 					this.showProgress |  | ||||||
| 						&& this.showProgress('Caching', done++, l) |  | ||||||
| 				} |  | ||||||
| 
 |  | ||||||
| 				setTimeout(tick, t) |  | ||||||
| 			}.bind(this) |  | ||||||
| 
 |  | ||||||
| 			tick() |  | ||||||
| 		} |  | ||||||
| 		return this |  | ||||||
| 	}, |  | ||||||
| 	clearCache: function(title){ |  | ||||||
| 		if(title){ |  | ||||||
| 			delete (this.__cache|| {})[title] |  | ||||||
| 
 |  | ||||||
| 		} else { |  | ||||||
| 			delete this.__cache |  | ||||||
| 		} |  | ||||||
| 	}, |  | ||||||
| }) | }) | ||||||
| 
 | 
 | ||||||
| var Cache =  | var Cache =  | ||||||
| @ -650,6 +699,9 @@ module.Cache = ImageGridFeatures.Feature({ | |||||||
| 	doc: '', | 	doc: '', | ||||||
| 
 | 
 | ||||||
| 	tag: 'cache', | 	tag: 'cache', | ||||||
|  | 	// NOTE: we use .showProgress(..) of 'ui-progress' but we do not 
 | ||||||
|  | 	// 		need it to work, thus we do not declare it as a dependency...
 | ||||||
|  | 	//depends: [],
 | ||||||
| 
 | 
 | ||||||
| 	actions: CacheActions, | 	actions: CacheActions, | ||||||
| 
 | 
 | ||||||
| @ -658,9 +710,9 @@ module.Cache = ImageGridFeatures.Feature({ | |||||||
| 			function(){  | 			function(){  | ||||||
| 				var t = this.config['pre-cache'] | 				var t = this.config['pre-cache'] | ||||||
| 				t === true ? | 				t === true ? | ||||||
| 					this.preCache()  | 					this.preCache('now')  | ||||||
| 				: t >= 0 ? | 				: t >= 0 ? | ||||||
| 					this._preCache()  | 					this.preCache()  | ||||||
| 				: false | 				: false | ||||||
| 			}], | 			}], | ||||||
| 	], | 	], | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user