all files / src/ hotswap.js

29.41% Statements 5/17
0% Branches 0/6
0% Functions 0/3
29.41% Lines 5/17
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                                                         
"use strict";
 
var HotSwap = {},
	winston = require('winston'),
	stack;
 
HotSwap.prepare = function(app) {
	stack = app._router.stack;
};
 
HotSwap.find = function(id) {
	if (stack) {
		for(var x=0,numEntries=stack.length;x<numEntries;x++) {
			if (stack[x].handle.hotswapId === id) {
				return x;
			}
		}
	} else {
		winston.error('[hotswap] HotSwap module has not been prepared!');
	}
};
 
HotSwap.replace = function(id, router) {
	var idx = HotSwap.find(id);
	if (idx) {
		delete stack[idx].handle;	// Destroy the old router
		stack[idx].handle = router;	// Replace with the new one
		winston.verbose('[hotswap] Router with id `' + id + '` replaced successfully');
	} else {
		winston.warn('[hotswap] Could not find router in stack with hotswapId `' + id + '`');
	}
};
 
module.exports = HotSwap;