Manage task IDs and merging
masterTasks can be identified by an ID. By default, the queue looks for a task.id property. You can customize this behavior using the id option.
Merging Tasks: If tasks share the same ID, they can be merged using a merge function. This is useful for aggregating data (e.g., counters) before processing.
Replacing Tasks: By default, if tasks have the same ID, the new task replaces the previous one in the queue.
// Customizing ID lookup
var q = new Queue(fn, {
id: 'name', // use task.name instead of task.id
// OR
id: function (task, cb) {
cb(null, 'computed_id');
}
});
// Merging tasks with the same ID
var counter = new Queue(function (task, cb) {
console.log("I have %d %ss.", task.count, task.id);
cb();
}, {
merge: function (oldTask, newTask, cb) {
oldTask.count += newTask.count;
cb(null, oldTask);
}
});
counter.push({ id: 'apple', count: 2 });
counter.push({ id: 'apple', count: 1 });
// Result: I have 3 apples.