mirror of
				https://github.com/flynx/ImageGrid.git
				synced 2025-11-03 21:00:14 +00:00 
			
		
		
		
	
		
			
	
	
		
			130 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
		
		
			
		
	
	
			130 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| 
								 | 
							
								/**********************************************************************
							 | 
						||
| 
								 | 
							
								* 
							 | 
						||
| 
								 | 
							
								*
							 | 
						||
| 
								 | 
							
								*
							 | 
						||
| 
								 | 
							
								**********************************************************************/
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								define(function(require){ var module = {}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								var actions = require('lib/actions')
							 | 
						||
| 
								 | 
							
								var features = require('lib/features')
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								var core = require('features/core')
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/*********************************************************************/
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								var RecoverActions = actions.Actions({
							 | 
						||
| 
								 | 
							
									config: {
							 | 
						||
| 
								 | 
							
										'recover-load-errors-to-previous-location': true,
							 | 
						||
| 
								 | 
							
									},
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									// Load index with recovery...
							 | 
						||
| 
								 | 
							
									//
							 | 
						||
| 
								 | 
							
									// This will:
							 | 
						||
| 
								 | 
							
									// 	- save recovery data (.__recover)
							 | 
						||
| 
								 | 
							
									// 	- load
							 | 
						||
| 
								 | 
							
									// 	- clear recovery data (if load successful)
							 | 
						||
| 
								 | 
							
									//
							 | 
						||
| 
								 | 
							
									// NOTE: for more info on the load protocol see: base.BaseActions.load 
							 | 
						||
| 
								 | 
							
									load: [
							 | 
						||
| 
								 | 
							
										function(data){
							 | 
						||
| 
								 | 
							
											// prepare to recover, just in case...
							 | 
						||
| 
								 | 
							
											this.__recover = (this.__recover !== false 
							 | 
						||
| 
								 | 
							
													&& this.config['recover-load-errors-to-previous-location']) ? 
							 | 
						||
| 
								 | 
							
												this.location
							 | 
						||
| 
								 | 
							
												: false
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
											return function(){
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
												// all went well clear the recovery data...
							 | 
						||
| 
								 | 
							
												delete this.__recover
							 | 
						||
| 
								 | 
							
											}
							 | 
						||
| 
								 | 
							
										}],
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									// Load data and recover on error... 
							 | 
						||
| 
								 | 
							
									//
							 | 
						||
| 
								 | 
							
									// This is the same as .load(..) but will monitor for load errors 
							 | 
						||
| 
								 | 
							
									// and attempt to recover if it fails...
							 | 
						||
| 
								 | 
							
									//
							 | 
						||
| 
								 | 
							
									// NOTE: this avoids load loops by attempting to recover only once...
							 | 
						||
| 
								 | 
							
									// NOTE: this is done as a wrapper because we can't catch errors in
							 | 
						||
| 
								 | 
							
									// 		parent actions at this point...
							 | 
						||
| 
								 | 
							
									loadOrRecover: ['- Location/',
							 | 
						||
| 
								 | 
							
										function(data){
							 | 
						||
| 
								 | 
							
											// this is the critical section, after this point we
							 | 
						||
| 
								 | 
							
											// are doing the actual loading....
							 | 
						||
| 
								 | 
							
											try {
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
												this.load(data)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
											// something bad happened, clear and handle it...
							 | 
						||
| 
								 | 
							
											} catch(err){
							 | 
						||
| 
								 | 
							
												this.clear()
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
												console.error(err)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
												// recover to last location...
							 | 
						||
| 
								 | 
							
												if(this.__recover){
							 | 
						||
| 
								 | 
							
													this.recover()
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
												// fail...
							 | 
						||
| 
								 | 
							
												} else {
							 | 
						||
| 
								 | 
							
													// clear the recovery data...
							 | 
						||
| 
								 | 
							
													delete this.__recover
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
													// fail...
							 | 
						||
| 
								 | 
							
													throw err
							 | 
						||
| 
								 | 
							
												}
							 | 
						||
| 
								 | 
							
											}
							 | 
						||
| 
								 | 
							
										}],
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									// Recover from load error...
							 | 
						||
| 
								 | 
							
									//
							 | 
						||
| 
								 | 
							
									// This will:
							 | 
						||
| 
								 | 
							
									//	- get recovery data if present
							 | 
						||
| 
								 | 
							
									//	- load recovery data
							 | 
						||
| 
								 | 
							
									//	- clear recovery data
							 | 
						||
| 
								 | 
							
									//
							 | 
						||
| 
								 | 
							
									// NOTE: if no recovery data present (.__recover) this will do nothing.
							 | 
						||
| 
								 | 
							
									recover: ['- File/Recover from load error',
							 | 
						||
| 
								 | 
							
										function(){
							 | 
						||
| 
								 | 
							
											var l = this.__recover
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
											// nothing to recover...
							 | 
						||
| 
								 | 
							
											if(!l){
							 | 
						||
| 
								 | 
							
												delete this.__recover
							 | 
						||
| 
								 | 
							
												return
							 | 
						||
| 
								 | 
							
											}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
											// NOTE: this will prevent us from entering
							 | 
						||
| 
								 | 
							
											// 		a recover attempt loop...
							 | 
						||
| 
								 | 
							
											// 		...if the recovery fails we will just
							 | 
						||
| 
								 | 
							
											// 		clear and stop.
							 | 
						||
| 
								 | 
							
											this.__recover = false
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
											// do the loading...
							 | 
						||
| 
								 | 
							
											this.location = l
							 | 
						||
| 
								 | 
							
										}],
							 | 
						||
| 
								 | 
							
								})
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								module.Recovery = core.ImageGridFeatures.Feature({
							 | 
						||
| 
								 | 
							
									title: '',
							 | 
						||
| 
								 | 
							
									doc: '',
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									tag: 'recover',
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									depends: [
							 | 
						||
| 
								 | 
							
										'location',
							 | 
						||
| 
								 | 
							
									],
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									actions: RecoverActions,
							 | 
						||
| 
								 | 
							
								})
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**********************************************************************
							 | 
						||
| 
								 | 
							
								* vim:set ts=4 sw=4 :                                                */
							 | 
						||
| 
								 | 
							
								return module })
							 |