Comparing version 0.2.0 to 0.2.1
122
defer.js
@@ -5,3 +5,8 @@ /* | ||
var deferred = { | ||
var type = require("./type"), | ||
uid = require("./uid"), | ||
now = require("./date/now"), | ||
count = require("./object/count") | ||
var callbacks = { | ||
timeout: {}, | ||
@@ -12,48 +17,53 @@ frame: {}, | ||
var UID = 0 | ||
var push = function(collection, callback, context, defer){ | ||
var unique = uid() | ||
var _defer = function(type, callback, arg){ | ||
var iterator = function(){ | ||
iterate(collection) | ||
} | ||
var collection = deferred[type], | ||
method = functions[type] | ||
if (count(collection, 0)) defer(iterator) | ||
var empty = true | ||
for (var p in collection){ | ||
empty = false; | ||
break; | ||
collection[unique] = { | ||
callback: callback, | ||
context: context | ||
} | ||
var uid = (UID++).toString(36) | ||
if (empty) method(arg) | ||
collection[uid] = callback | ||
return function(){ | ||
delete collection[uid] | ||
delete collection[unique] | ||
} | ||
} | ||
var iterate = function(collection){ | ||
var time = new Date().getTime() | ||
var time = now() | ||
var exec = {}, p | ||
for (p in collection){ | ||
exec[p] = collection[p] | ||
delete collection[p] | ||
var exec = {}, key | ||
for (key in collection){ | ||
exec[key] = collection[key] | ||
delete collection[key] | ||
} | ||
for (p in exec) exec[p](time) | ||
for (key in exec){ | ||
var entry = exec[key] | ||
entry.callback.call(entry.context, time) | ||
} | ||
} | ||
var immediate = function(){ | ||
iterate(deferred.immediate) | ||
var defer = function(callback, argument, context){ | ||
return (type(argument) === "number") ? defer.timeout(callback, argument, context) : defer.immediate(callback, argument) | ||
} | ||
var functions = {} | ||
if (global.process && process.nextTick){ | ||
if (global.process && process.nextTick){ | ||
functions.immediate = function(){ | ||
process.nextTick(immediate) | ||
defer.immediate = function(callback, context){ | ||
return push(callbacks.immediate, callback, context, process.nextTick) | ||
} | ||
} else if (global.setImmediate){ | ||
functions.immediate = function(){ | ||
setImmediate(immediate) | ||
defer.immediate = function(callback, context){ | ||
return push(callbacks.immediate, callback, context, setImmediate) | ||
} | ||
} else if (global.postMessage && global.addEventListener){ | ||
@@ -64,14 +74,20 @@ | ||
event.stopPropagation() | ||
immediate() | ||
iterate(callbacks.immediate) | ||
} | ||
}, true) | ||
functions.immediate = function(){ | ||
postMessage("@deferred", "*") | ||
defer.immediate = function(callback, context){ | ||
return push(callbacks.immediate, callback, context, function(){ | ||
postMessage("@deferred", "*") | ||
}) | ||
} | ||
} else { | ||
functions.immediate = function(){ | ||
setTimeout(immediate, 0) | ||
defer.immediate = function(callback, context){ | ||
return push(callbacks.immediate, callback, context, function(iterator){ | ||
setTimeout(iterator, 0) | ||
}) | ||
} | ||
} | ||
@@ -88,37 +104,21 @@ | ||
functions.frame = function(){ | ||
requestAnimationFrame(function(){ | ||
iterate(deferred.frame) | ||
}) | ||
defer.frame = function(callback, context){ | ||
return push(callbacks.frame, callback, context, requestAnimationFrame) | ||
} | ||
functions.timeout = function(ms){ | ||
var timeout = deferred.timeout | ||
var collection = timeout[ms] || (timeout[ms] = {}) | ||
setTimeout(function(){ | ||
iterate(collection) | ||
}, ms) | ||
} | ||
var clear | ||
/* | ||
export! | ||
*/ | ||
defer.timeout = function(callback, ms, context){ | ||
var ct = callbacks.timeout | ||
var defer = function(callback, ms){ | ||
return (ms) ? _defer("timeout", callback, ms) : _defer("immediate", callback) | ||
} | ||
if (!clear) clear = defer.immediate(function(){ | ||
clear = null | ||
callbacks.timeout = {} | ||
}) | ||
defer.immediate = function(callback){ | ||
return _defer("immediate", callback) | ||
return push(ct[ms] || (ct[ms] = {}), callback, context, function(iterator){ | ||
setTimeout(iterator, ms) | ||
}) | ||
} | ||
defer.timeout = function(callback, ms){ | ||
if (!ms) ms = 0 | ||
return _defer("timeout", callback, ms) | ||
} | ||
defer.frame = function(callback){ | ||
return _defer("frame", callback) | ||
} | ||
module.exports = defer |
@@ -7,2 +7,3 @@ /* | ||
defer = require("./defer"), | ||
uid = require("./uid"), | ||
slice = require("./array/slice") | ||
@@ -20,3 +21,3 @@ | ||
events[(EID++).toString(36)] = fn | ||
events[uid()] = fn | ||
return this | ||
@@ -23,0 +24,0 @@ }, |
@@ -7,10 +7,10 @@ /* | ||
var count = function(self){ | ||
var count = function(self, n){ | ||
var length = 0 | ||
forIn(self, function(){ | ||
length++ | ||
if (++length === n || n === 0) return false | ||
}) | ||
return length | ||
return (n != null) ? n === length : length | ||
} | ||
module.exports = count |
{ | ||
"name": "prime", | ||
"homepage": "https://github.com/mootools/prime", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"main": "./index.js", | ||
@@ -6,0 +6,0 @@ "description": "prime, an OOP JavaScript library for node and the web.", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
31628
0
53
935