Sha256: 627bce426b50d8c50df13aa5a89b637d549f507f4947b7e7b1b8f82e042f3a99

Contents?: true

Size: 1.86 KB

Versions: 46

Compression:

Stored size: 1.86 KB

Contents

'use strict';

var callable, byObserver;

callable = function (fn) {
	if (typeof fn !== 'function') throw new TypeError(fn + " is not a function");
	return fn;
};

byObserver = function (Observer) {
	var node = document.createTextNode(''), queue, currentQueue, i = 0;
	new Observer(function () {
		var callback;
		if (!queue) {
			if (!currentQueue) return;
			queue = currentQueue;
		} else if (currentQueue) {
			queue = currentQueue.concat(queue);
		}
		currentQueue = queue;
		queue = null;
		if (typeof currentQueue === 'function') {
			callback = currentQueue;
			currentQueue = null;
			callback();
			return;
		}
		node.data = (i = ++i % 2); // Invoke other batch, to handle leftover callbacks in case of crash
		while (currentQueue) {
			callback = currentQueue.shift();
			if (!currentQueue.length) currentQueue = null;
			callback();
		}
	}).observe(node, { characterData: true });
	return function (fn) {
		callable(fn);
		if (queue) {
			if (typeof queue === 'function') queue = [queue, fn];
			else queue.push(fn);
			return;
		}
		queue = fn;
		node.data = (i = ++i % 2);
	};
};

module.exports = (function () {
	// Node.js
	if ((typeof process === 'object') && process && (typeof process.nextTick === 'function')) {
		return process.nextTick;
	}

	// MutationObserver
	if ((typeof document === 'object') && document) {
		if (typeof MutationObserver === 'function') return byObserver(MutationObserver);
		if (typeof WebKitMutationObserver === 'function') return byObserver(WebKitMutationObserver);
	}

	// W3C Draft
	// http://dvcs.w3.org/hg/webperf/raw-file/tip/specs/setImmediate/Overview.html
	if (typeof setImmediate === 'function') {
		return function (cb) { setImmediate(callable(cb)); };
	}

	// Wide available standard
	if ((typeof setTimeout === 'function') || (typeof setTimeout === 'object')) {
		return function (cb) { setTimeout(callable(cb), 0); };
	}

	return null;
}());

Version data entries

46 entries across 46 versions & 2 rubygems

Version Path
optimacms-0.1.61 spec/dummy/node_modules/next-tick/index.js
govuk_publishing_components-18.0.0 node_modules/next-tick/index.js
govuk_publishing_components-17.21.0 node_modules/next-tick/index.js
govuk_publishing_components-17.20.0 node_modules/next-tick/index.js
govuk_publishing_components-17.19.1 node_modules/next-tick/index.js
govuk_publishing_components-17.19.0 node_modules/next-tick/index.js
govuk_publishing_components-17.18.0 node_modules/next-tick/index.js
govuk_publishing_components-17.17.0 node_modules/next-tick/index.js
govuk_publishing_components-17.16.0 node_modules/next-tick/index.js
govuk_publishing_components-17.15.0 node_modules/next-tick/index.js
govuk_publishing_components-17.14.0 node_modules/next-tick/index.js
govuk_publishing_components-17.13.0 node_modules/next-tick/index.js
govuk_publishing_components-17.12.2 node_modules/next-tick/index.js
govuk_publishing_components-17.12.1 node_modules/next-tick/index.js
govuk_publishing_components-17.12.0 node_modules/next-tick/index.js
govuk_publishing_components-17.11.0 node_modules/next-tick/index.js
govuk_publishing_components-17.10.0 node_modules/next-tick/index.js
govuk_publishing_components-17.9.0 node_modules/next-tick/index.js
govuk_publishing_components-17.8.0 node_modules/next-tick/index.js
govuk_publishing_components-17.7.0 node_modules/next-tick/index.js