all files / src/ absync.js

43.82% Statements 39/89
19.51% Branches 8/41
37.5% Functions 6/16
43.82% Lines 39/89
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302                                            14×                   14×     14×   14×   14×     14×       14×   14×         14×         14×     14×                   28×   28×                                                 28×       28×                                                                                                       28×       28×     28×         28×       28×             28×                                                                                       56×     56×   56×           56×       56×                                                                                                     14×    
/* globals angular, io */
 
/**
 * Please make note of the following conventions:
 * 1. Function-scope local variables must be prefixed with a single underscore.
 *    This indicates a temporary variable.
 * 2. Private variables that are persisted onto publicly accessible entities must be prefixed with two underscores.
 *    This indicates a publicly visible, private variable.
 *    Hiding private variables, by using closures, is discouraged.
 *    Modifying these values from outside of absync is discouraged, but should be respected whenever possible.
 */
 
angular
	.module( "absync" )
	.provider( "absync", getAbsyncProvider );
 
/**
 * Retrieves the absync provider.
 * @param {angular.auto.IInjectorService|Object} $injector The $injector provider.
 * @param {angular.auto.IProvideService|Object} $provide The $provide provider
 * @param {Function} absyncCache The AbsyncCache service constructor.
 * @ngInject
 */
function getAbsyncProvider( $injector, $provide, absyncCache ) {
	return new AbsyncProvider( $injector, $provide, absyncCache );
}
 
/**
 * Retrieves the absync provider.
 * @param {angular.auto.IInjectorService|Object} $injector The $injector provider.
 * @param {angular.auto.IProvideService|Object} $provide The $provide provider.
 * @param {Function} absyncCache The AbsyncCache service constructor.
 * @constructor
 */
function AbsyncProvider( $injector, $provide, absyncCache ) {
	var self = this;
 
	// Store a reference to the inject provider.
	self.__injector    = $injector;
	// Store a reference to the provide provider.
	self.__provide     = $provide;
	// Store a reference to the cache service constructor.
	self.__absyncCache = absyncCache;
 
	// A reference to the socket.io instance we're using to receive updates from the server.
	self.__ioSocket = null;
	// We usually register event listeners on the socket.io instance right away.
	// If socket.io was not connected when a service was constructed, we put the registration request
	// into this array and register it as soon as socket.io is configured.
	self.__registerLater = [];
	// References to all registered event listeners.
	self.__listeners     = [];
 
	// The collections that absync provides.
	// The keys are the names of the collections, the value contains the constructor of
	// the respective cache service.
	self.__collections = {};
 
	// The entities that absync provides.
	// The keys are the names of the entities, the value contains the constructor of
	// the respective cache service.
	self.__entities = {};
 
	// Debug should either be set through a configure() call, or on instantiated services.
	self.debug = undefined;
}
 
/**
 * Register the configurator on the provider itself to allow early configuration during setup phase.
 * It is recommended to configure absync within a configuration phase of a module.
 * @param {Object} configuration The configuration for the absync provider.
 * Can have a member `socket`, pointing to the socket.io instance or constructor to use.
 * Can have a member `debug`, enabling debugging, if set to true.
 */
AbsyncProvider.prototype.configure = function AbsyncProvider$configure( configuration ) {
	var self = this;
 
	Iif( typeof configuration.socket !== "undefined" ) {
		var socket   = configuration.socket;
		// Determine if the socket is an io.Socket.
		var isSocket = typeof io !== "undefined" && io.Socket && socket instanceof io.Socket;
 
		if( typeof socket == "function" ) {
			// Expect the passed socket to be a constructor.
			self.__ioSocket = new socket();// jscs:ignore requireCapitalizedConstructors
 
		} else if( isSocket ) {
			// Expect the passed socket to be an io.Socket instance.
			self.__ioSocket = socket;
 
		} else {
			throw new Error( "configure() expects input to be a function or a socket.io Socket instance." );
		}
 
		// Check if services already tried to register listeners, if so, register them now.
		// This can happen when a service was constructed before absync was configured.
		if( self.__registerLater.length ) {
			angular.forEach( self.__registerLater, self.__registerListener.bind( self ) );
			self.__registerLater = [];
		}
	}
 
	Iif( typeof configuration.debug !== "undefined" ) {
		self.debug = configuration.debug || false;
	}
 
	Iif( self.debug ) {
		angular.forEach( self.__collections, function enableDebugging( collection ) {
			collection.configuration.debug = true;
		} );
		angular.forEach( self.__entities, function enableDebugging( entity ) {
			entity.configuration.debug = true;
		} );
	}
};
 
/**
 * Detaches absync from the websocket.
 * @param {Boolean} [disconnectSocket=false] Should the underlying socket.io connection be disconnected as well?
 */
AbsyncProvider.prototype.disconnect = function AbsyncProvider$disconnect( disconnectSocket ) {
	var self = this;
 
	disconnectSocket = disconnectSocket || false;
 
	angular.forEach( self.__listeners, function unregisterListener( listener ) {
		listener.unregister();
		delete listener.unregister;
		self.__registerLater.push( listener );
	} );
 
	self.__listeners = [];
 
	if( disconnectSocket ) {
		self.__ioSocket.disconnect();
		self.__ioSocket = null;
	}
};
 
/**
 * Register an event listener with socket.io.
 * @param {Object} listener
 * @private
 */
AbsyncProvider.prototype.__registerListener = function AbsyncProvider$registerListener( listener ) {
	var self = this;
 
	// Remember this listener.
	self.__listeners.push( listener );
 
	// Register the listener and remember the function to use when the listener should be unregistered.
	listener.unregister = self.__handleEntityEvent( listener.eventName, listener.callback );
};
 
/**
 * Request a new synchronized collection.
 * This only registers the intent to use that collection. It will be constructed when it is first used.
 * @param {String} name The name of the collection and service name.
 * @param {AbsyncServiceConfiguration|Object} configuration The configuration for this collection.
 */
AbsyncProvider.prototype.collection = function AbsyncProvider$collection( name, configuration ) {
	var self = this;
 
	// Collection/entity names (and, thus service names) have to be unique.
	// We can't create multiple services with the same name.
	Iif( self.__collections[ name ] ) {
		throw new Error( "A collection with the name '" + name + "' was already requested. Names for collections must be unique." );
	}
	Iif( self.__entities[ name ] ) {
		throw new Error( "An entity with the name '" + name + "' was already requested. Names for collections must be unique and can't be shared with entities." );
	}
 
	// If no debug flag was set, use the value from the core absync provider.
	configuration.debug = typeof configuration.debug === "undefined" ? self.debug : configuration.debug;
 
	// Register the service configuration.
	// __absyncCache will return a constructor for a service with the given configuration.
	self.__collections[ name ] = {
		constructor   : self.__absyncCache( name, configuration ),
		configuration : configuration
	};
 
	// Register the new service.
	// Yes, we want an Angular "service" here, because we want it constructed with "new".
	self.__provide.service( name, self.__collections[ name ].constructor );
};
 
/**
 * Request a new synchronized entity.
 * This only registers the intent to use that entity. It will be constructed when it is first used.
 * @param {String} name The name of the entity and service name.
 * @param {AbsyncServiceConfiguration|Object} configuration The configuration for this entity.
 */
AbsyncProvider.prototype.entity = function AbsyncProvider$entity( name, configuration ) {
	var self = this;
 
	// Collection/entity names (and, thus service names) have to be unique.
	// We can't create multiple services with the same name.
	if( self.__entities[ name ] ) {
		throw new Error( "An entity with the name '" + name + "' was already requested. Names for entities must be unique." );
	}
	if( self.__collections[ name ] ) {
		throw new Error( "A collection with the name '" + name + "' was already requested. Names for entities must be unique and can't be shared with collections." );
	}
 
	// If no debug flag was set, use the value from the core absync provider.
	configuration.debug = typeof configuration.debug === "undefined" ? self.debug : configuration.debug;
 
	// Register the service configuration.
	// __absyncCache will return a constructor for a service with the given configuration.
	self.__entities[ name ] = {
		constructor   : self.__absyncCache( name, configuration ),
		configuration : configuration
	};
 
	// Register the new service.
	// Yes, we want an Angular "service" here, because we want it constructed with "new".
	self.__provide.service( name, self.__entities[ name ].constructor );
};
 
 
/**
 * Register an event listener that is called when a specific entity is received on the websocket.
 * @param {String} eventName The event name, usually the name of the entity.
 * @param {Function} callback The function to call when the entity is received.
 * @return {Function|null} If the listener could be registered, it returns a function that, when called, removes
 * the event listener.
 * If the listener registration was delayed, null is returned.
 */
AbsyncProvider.prototype.on = function AbsyncProvider$on( eventName, callback ) {
	var self = this;
 
	// If we have no configured socket.io connection yet, remember to register it later.
	Eif( !self.__ioSocket ) {
 
		Iif( self.__registerLater.length > 8192 ) {
			// Be defensive, something is probably not right here.
			return null;
		}
 
		// TODO: Use promises here, so that we can always return the event listener removal function.
		self.__registerLater.push( {
			eventName : eventName,
			callback  : callback
		} );
		return null;
	}
 
	return self.__registerListener( {
		eventName : eventName,
		callback  : callback
	} );
};
 
/**
 * Register an event listener on the websocket.
 * @param {String} eventName The event name, usually the name of the entity.
 * @param {Function} callback The function to call when the entity is received.
 * @returns {Function}
 */
AbsyncProvider.prototype.__handleEntityEvent = function AbsyncProvider$handleEntityEvent( eventName, callback ) {
	var self = this;
 
	// Register the callback with socket.io.
	self.__ioSocket.on( eventName, callback );
 
	// Return a function that removes the listener.
	return function removeListener() {
		self.__ioSocket.removeListener( eventName, callback );
	};
};
 
/**
 * Convenience method to allow the user to emit() from the socket.io connection.
 * This is not utilized in absync internally.
 * @param {String} eventName
 * @param {*} data
 * @param {Function} [callback]
 */
AbsyncProvider.prototype.emit = function AbsyncProvider$emit( eventName, data, callback ) {
	var self = this;
 
	if( !self.__ioSocket ) {
		throw new Error( "socket.io is not initialized." );
	}
 
	self.__ioSocket.emit( eventName, data, function afterEmit() {
		if( callback ) {
			callback.apply( self.__ioSocket, arguments );
		}
	} );
};
 
/**
 * The service is just used as a convenience to access the provider.
 * @returns {AbsyncProvider}
 * @ngInject
 */
AbsyncProvider.prototype.$get = function AbsyncProvider$$get() {
	return this;
};