コード例 #1
0
ファイル: gate.js プロジェクト: DruRly/deferred
module.exports = function (cLimit, qLimit) {
	var fn, count, decrement, unload, queue, run, result;
	fn = callable(this);
	cLimit = max(toUint(cLimit), 1);
	qLimit = ((qLimit == null) || isNaN(qLimit)) ? Infinity : toUint(qLimit);
	count = 0;
	queue = [];

	run = function (thisArg, args, def) {
		var r;
		try {
			r = apply.call(fn, thisArg, args);
		} catch (e) {
			r = e;
		}
		if (isPromise(r)) {
			if (def) eeUnify(def.promise, r);
			if (!r.resolved) {
				++count;
				if (def) def.resolve(r);
				return r.finally(decrement);
			}
			r = r.value;
		}
		if (!def) return deferred(r);
		def.resolve(r);
		unload();
	};

	decrement = function () {
		--count;
		unload();
	};

	unload = function () {
		var data;
		if ((data = queue.shift())) run.apply(null, data);
	};

	result = function () {
		var def;
		if (count >= cLimit) {
			if (queue.length < qLimit) {
				def = deferred();
				queue.push([this, arguments, def]);
				return def.promise;
			}
			return reject();
		}
		return run(this, arguments);
	};
	result.returnsPromise = true;
	return result;
};
コード例 #2
0
ファイル: _base.js プロジェクト: statico/memoize
	return function self(fn/*, options */) {
		var options, length, get, clear, conf;

		callable(fn);
		options = Object(arguments[1]);

		// Do not memoize already memoized function
		if (fn.memoized && !options.force) return fn;

		if (ext.method && (options.method != null)) {
			return ext.method(options.method, options, fn, self);
		}

		conf = ee({ memoize: self, fn: fn });

		// Normalize length
		if (isNaN(options.length)) {
			length = fn.length;
			// Special case
			if (options.async && ext.async) --length;
		} else {
			length = (options.length === false) ? false : (options.length >>> 0);
		}

		core(conf, length, options);

		forEach(ext, function (fn, name) {
			if (fn.force) fn(conf, options);
			else if (options[name]) fn(options[name], conf, options);
		});

		fn = conf.fn;
		get = conf.get;
		clear = conf.clear;

		conf.memoized.clear = function () { clear(get(arguments)); };
		conf.memoized.clearAll = function () {
			conf.emit('purgeall');
			conf.clearAll();
		};
		conf.memoized.memoized = true;
		conf.emit('ready');
		return conf.memoized;
	};
コード例 #3
0
ファイル: promisify.js プロジェクト: allex/deferred
module.exports = function (length) {
	var fn, result;
	fn = callable(this);
	if (fn.returnsPromise) {
		return fn;
	}
	if (length != null) {
		length = length >>> 0;
	}
	result = function () {
		var args, def;
		args = processArguments(arguments, length);

		if (isPromise(args)) {
			if (args.failed) {
				return args;
			}
			def = deferred();
			args.end(function (args) {
				try {
					applyFn.call(this, fn, args, def.resolve);
				} catch (e) {
					def.resolve(e);
				}
			}.bind(this), def.resolve);
		} else {
			def = deferred();
			try {
				applyFn.call(this, fn, args, def.resolve);
			} catch (e) {
				def.resolve();
				throw e;
			}
		}

		return def.promise;
	};
	result.returnsPromise = true;
	return result;
};
コード例 #4
0
ファイル: dispose.js プロジェクト: 59023g/T
ext.dispose = function (dispose, conf, options) {
	var clear, async;

	callable(dispose);

	async = (options.async && ext.async);
	conf.on('purge' + (async ? 'async' : ''), clear =  async ? function (id) {
		var value = conf.async[id];
		delete conf.cache[id];
		dispose.apply(conf.memoized['_memoize:context_'], slice.call(value, 1));
	} : function (id) {
		var value = conf.cache[id];
		delete conf.cache[id];
		dispose.call(conf.memoized['_memoize:context_'], value);
	});

	if (!async) {
		conf.on('purgeall', function () {
			forEach(conf.cache, function (value, id) { clear(id); });
		});
	}
};
コード例 #5
0
ファイル: reduce.js プロジェクト: Wushaowei001/ALSBooks
module.exports = function (cb/*, initial*/) {
	value(this);
	(cb == null) || callable(cb);

	return new Reduce(this, cb, arguments[1], arguments.length > 1);
};
コード例 #6
0
ファイル: _ext.js プロジェクト: DruRly/deferred
	done: d(doneFn = function (win, fail) {
		((win == null) || callable(win));
		((fail == null) || callable(fail));
		if (!this.pending) this.pending = [];
		this.pending.push('done', arguments);
	}),
コード例 #7
0
ファイル: map.js プロジェクト: DruRly/deferred
module.exports = function (cb/*, thisArg*/) {
	value(this);
	((cb == null) || callable(cb));

	return new DMap(this, cb, arguments[1]);
};
コード例 #8
0
ファイル: finally.js プロジェクト: DruRly/deferred
}, function (cb) { cb(); }, function (cb) {
	callable(cb)();
	return this;
});
コード例 #9
0
ファイル: finally.js プロジェクト: DruRly/deferred
deferred.extend('finally', function (cb) {
	callable(cb);
	if (!this.pending) this.pending = [];
	this.pending.push('finally', arguments);
	return this;
}, function (cb) { cb(); }, function (cb) {
コード例 #10
0
ファイル: invoke-async.js プロジェクト: bwinton/foxograph
module.exports = function (obj, fn/*, …args*/) {
	value(obj);
	if (!isCallable(fn)) fn = callable(obj[fn]);
	return callAsync(fn, null, obj, slice.call(arguments, 2));
};