| 
									
										
										
										
											2016-06-05 04:11:20 +03:00
										 |  |  | /********************************************************************** | 
					
						
							|  |  |  | *  | 
					
						
							|  |  |  | * | 
					
						
							|  |  |  | * | 
					
						
							|  |  |  | **********************************************************************/ | 
					
						
							| 
									
										
										
										
											2016-08-21 02:19:24 +03:00
										 |  |  | ((typeof define)[0]=='u'?function(f){module.exports=f(require)}:define) | 
					
						
							|  |  |  | (function(require){ var module={} // make module AMD/node compatible...
 | 
					
						
							| 
									
										
										
										
											2016-08-20 22:49:36 +03:00
										 |  |  | /*********************************************************************/ | 
					
						
							| 
									
										
										
										
											2016-06-05 04:11:20 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 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...
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							| 
									
										
										
										
											2017-03-08 20:22:40 +03:00
										 |  |  | 	// This will return a promise that will resolve if load is successful 
 | 
					
						
							|  |  |  | 	// and fail if load fails, passing the 'recovered' if recovery was
 | 
					
						
							|  |  |  | 	// successful and the error if not...
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							| 
									
										
										
										
											2016-06-05 04:11:20 +03:00
										 |  |  | 	// 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){ | 
					
						
							| 
									
										
										
										
											2017-03-08 20:05:49 +03:00
										 |  |  | 			var that = this | 
					
						
							|  |  |  | 			return new Promise(function(resolve, reject){ | 
					
						
							|  |  |  | 				// this is the critical section, after this point we
 | 
					
						
							|  |  |  | 				// are doing the actual loading....
 | 
					
						
							|  |  |  | 				try { | 
					
						
							| 
									
										
										
										
											2016-06-05 04:11:20 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-08 20:05:49 +03:00
										 |  |  | 					that.load(data) | 
					
						
							| 
									
										
										
										
											2016-06-05 04:11:20 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-08 20:05:49 +03:00
										 |  |  | 					resolve(data) | 
					
						
							| 
									
										
										
										
											2016-06-05 04:11:20 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-08 20:05:49 +03:00
										 |  |  | 				// something bad happened, clear and handle it...
 | 
					
						
							|  |  |  | 				} catch(err){ | 
					
						
							|  |  |  | 					that.clear() | 
					
						
							| 
									
										
										
										
											2016-06-05 04:11:20 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-08 20:05:49 +03:00
										 |  |  | 					console.error(err) | 
					
						
							| 
									
										
										
										
											2016-06-05 04:11:20 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-08 20:05:49 +03:00
										 |  |  | 					// recover to last location...
 | 
					
						
							|  |  |  | 					if(that.__recover){ | 
					
						
							|  |  |  | 						that.recover() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						reject('recovered') | 
					
						
							| 
									
										
										
										
											2016-06-05 04:11:20 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 					// fail...
 | 
					
						
							| 
									
										
										
										
											2017-03-08 20:05:49 +03:00
										 |  |  | 					} else { | 
					
						
							|  |  |  | 						// clear the recovery data...
 | 
					
						
							|  |  |  | 						delete that.__recover | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						// fail...
 | 
					
						
							|  |  |  | 						//throw err
 | 
					
						
							|  |  |  | 						reject(err) | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2016-06-05 04:11:20 +03:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2017-03-08 20:05:49 +03:00
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2016-06-05 04:11:20 +03:00
										 |  |  | 		}], | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// 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, | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /********************************************************************** | 
					
						
							| 
									
										
										
										
											2016-08-20 22:49:36 +03:00
										 |  |  | * vim:set ts=4 sw=4 :                               */ return module }) |