Skip to content

Instantly share code, notes, and snippets.

@ambar
Last active December 15, 2015 05:59
Show Gist options
  • Save ambar/5213098 to your computer and use it in GitHub Desktop.
Save ambar/5213098 to your computer and use it in GitHub Desktop.
var makePile = function(count, onfilter, onvalue) {
var values = []
var id = function(value) {
// console.info('onvalue::', value)
return value
}
return function(value) {
values.push((onvalue || id).apply(this, arguments))
if (values.length === count) {
onfilter.apply(this, values)
values = []
}
}
}
var f = makePile(2, function(a, b) {
console.info('f onfilter::', arguments)
})
var g = makePile(3, function(a, b, c) {
console.info('g onfilter::', arguments)
})
var i = 0
while (i++ < 10) {
f(i)
g(i)
}
/* =>
f onfilter:: [1, 2]
g onfilter:: [1, 2, 3]
f onfilter:: [3, 4]
f onfilter:: [5, 6]
g onfilter:: [4, 5, 6]
f onfilter:: [7, 8]
g onfilter:: [7, 8, 9]
f onfilter:: [9, 10]
*/
var request = makePile(5, function() {
$.post('/api', { list: JSON.stringify([].slice.call(arguments)) })
})
request({a:1})
request({a:2})
request({a:3})
request({a:4})
request({a:5})
/*
list:[{"a":1},{"a":2},{"a":3},{"a":4},{"a":5}]
*/
var touchFactory = function(delay, action) {
var params = [], slice = [].slice
var touch = debounce(delay, function() {
if (params.length) {
action(params)
params = []
}
}, true)
return function() {
params.push(slice.call(arguments))
touch()
}
}
//
touchItem = touchFactory(550, function(params) {
$.post('/touch', { 'items': JSON.stringify(params) })
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment