Пример #1
0
(function () {
	var galaxy = require('galaxy');
	var mysql  = require('mysql');

	require('galaxy-augment');

	function augmentConnection(connection) {
		connection.queryAsync = galaxy.star(connection.query, { returnObject: [ "rows", "fields" ] });

		['connect', 'ping', 'statistics', 'end'].forEach(function (obj) {
			connection[obj + 'Async'] = galaxy.star(connection[obj]);
		});

		return connection;
	}
	

	galaxy.augment(mysql, "createConnection", augmentConnection);

	galaxy.augment(mysql, "createPool", function (pool) {

		galaxy.augment(pool, "getConnection", null, augmentConnection);

		pool.getConnectionAsync = galaxy.star(pool.getConnection);
	});


	if (module.parent) {
		module.exports = mysql;
		return;
	}
	
	var connection = mysql.createConnection(
		JSON.parse(require('fs').readFileSync('mysql.json', 'utf8'))
	);

	galaxy.main(function *() {
		try {
			connection.connect();
			var res = yield connection.queryAsync('SELECT 1 + 1 AS solution');
			console.log(res.rows[0].solution);
			console.log(yield connection.statisticsAsync());
			connection.end();
		} catch (ex) {
			console.log(ex);
		}
	});
})();
Пример #2
0
}

var fs = galaxy.star(require('fs'));

function* fileSearch(q) {
	var t0 = new Date();
	var results = '';

	function* doDir(dir) {
		yield (yield fs.readdir(dir)).forEachStar(function*(file) {
			var f = dir + '/' + file;
			var stat = yield fs.stat(f);
			if (stat.isFile()) {
				(yield fs.readFile(f, 'utf8')).split('\n').forEach(function(line, i) {
					if (line.indexOf(q) >= 0) results += '<br/>' + f + ':' + i + ':' + line;
				});
			} else if (stat.isDirectory()) {
				yield doDir(f);
			}
		});
	}
	yield doDir(__dirname);
	return results + '<br/>completed in ' + (new Date() - t0) + ' ms';;
}

galaxy.main(function *() {
	yield server.listen(1337);
	console.log('Server running at http://127.0.0.1:1337/');
});

Пример #3
0
(function() {
	var galaxy = require('galaxy');

	function Guard(locked) {
		var callbacks = [];
		var locked = locked === true ? true : false;

		function lock() {
			locked = true;
		}

		function unlock() {
			locked = false;
			// clone array
			var tmp = callbacks.slice(0);
			// clear callbacks
			callbacks = [];
			for (var i = 0; i < tmp.length; i++) {
				tmp[i]();
			}
		}

		function isLocked() {
			return locked;
		}

		function callback(callback) {
			if (locked) {
				callbacks.push(callback);
			} else {
				callback();
			}
		}

		return {
			lock: lock,
			unlock: unlock,
			isLocked: isLocked,
			callback: callback,
			callbackAsync: galaxy.star(callback)
		};
	}

	if (module.parent) {
		module.exports = Guard;
		return;
	}

	var assert = require('assert');

	galaxy.main(function *() {
		var callbacks = 0;

		var guard = new Guard(false);

		assert(guard.isLocked() == false);

		guard.lock();

		assert(guard.isLocked() == true);

		guard.callback(function() {
			// First lock, so this one has to be executed first.
			assert(callbacks == 0);
			callbacks++;
		});

		setTimeout(function() {
			guard.unlock();
		});

		yield guard.callbackAsync();
		callbacks++;

		assert(callbacks == 2, "Not all callbacks where called");
	});
})();