From 983e8a95aa105013c9a64c6d303044b6f6aa2922 Mon Sep 17 00:00:00 2001 From: Dominic Tarr Date: Thu, 29 Sep 2011 15:23:18 +1000 Subject: [PATCH] add defered --- functions.js | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- objects.js | 6 +++++- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/functions.js b/functions.js index b45462e..069cbc9 100644 --- a/functions.js +++ b/functions.js @@ -57,7 +57,6 @@ exports.deepCurry = function () { return function () { var _args = [].slice.call(arguments) - console.error(args, _args) return funx.apply(this, objects.deepMerge(args, _args)) } } @@ -71,6 +70,25 @@ the 'before' function returns a function that calls a shim function before a giv the 'shim' function is passed the args of the returned function, and may alter them before the given function is called. +hmm, thats about twice as much english than javascript. + +use it like this: + +function (x,p,z) {return whatever(z, p, x)} + +before(whatever, function (args) { return args.reverse() }) + +hmm, thats more verbose than the straight forward way... +maybe that function is not such a great idea. + +what about beforeCallback? + +function (opts, callback) { request (opts, function (err, res, body) { callback(err, body) } } + +beforeCallback(request, function (args) { return [args[0], args[2]] }) + +ah, that is better. + */ var fName = function (f) { @@ -83,6 +101,7 @@ var before = exports.before = function (given, shim) { return given.apply(this, shim([].slice.call(arguments))) } } +//before(whatever, function (a) { return a.reverse() }) var beforeCallback = exports.beforeCallback = function (async, shim) { @@ -90,3 +109,33 @@ var beforeCallback = args.push(before(args.pop(), shim)); return args }) } + + +/* + prevent a function from being called intil some thing important has happened. + (useful for delaying something until a connection is made.) +*/ + +var defer = + exports.defer = function (deferred) { + var buffer = [] + , buffering = true + + function deferrer () { + var args = [].slice.call(arguments) + if(buffering) + buffer.push(args) + else deferred.apply(null, args) + } + deferrer.flush = function () { + buffering = false + while(!buffering && buffer.length) { + deferred.apply(null, buffer.shift()) + } + } + deferrer.buffer = function () { + buffering = true + } + + return deferrer +} diff --git a/objects.js b/objects.js index 1c8cc0b..b697dce 100644 --- a/objects.js +++ b/objects.js @@ -17,7 +17,6 @@ var merge = exports.merge = function (a, b) { return merge.apply(null, [].slice.call(arguments, 1)) if(b != null) each(b, function (v,k){ - console.error(b, v, k) a[k] = v }) return merge.apply(null, [a].concat([].slice.call(arguments, 2))) @@ -82,6 +81,11 @@ var map = exports.map = function (obj, iterator){ return r } +var reduce = exports.reduce = function (obj, iterator, initial) { + + +} + //this will make instanceof work in the repl var filter = exports.filter = function (obj, iterator){