var Contacts = Futures.anywhereify({
// Providers must be promisables
all: function(params) {
var p = Futures.promise();
$.ajaxSetup({ error: p.smash });
$.getJSON('http://graph.facebook.com/me/friends', params, p.fulfill);
$.ajaxSetup({ error: undefined });
return p.passable();
},
one: function(id, params) {
var p = Futures.promise();
$.ajaxSetup({ error: p.smash });
$.getJSON('http://graph.facebook.com/' + id, params, p.fulfill);
$.ajaxSetup({ error: undefined });
return p.passable();
}
},{
// Consumers will be called in synchronous order
// with the `lastResult` of the previous provider or consumer.
// They should return either lastResult or a promise
randomize: function(data, params) {
data.sort(function(){ return Math.round(Math.random())-0.5); // Underscore.js
return Futures.promise(data);
},
limit: function(data, n, params) {
data = data.first(n);
return Futures.promise(data);
},
display: function(data, params) {
$('#friend-area').render(directive, data); // jQuery+PURE
// always return the data, even if you don't modify it!
// otherwise your results could be unexpected
return Futures.promise(data);
}
}, context, params);
Contacts.all(params).randomize().limit(10).display();
Contacts.all(params).randomize().limit(10).display();
// much later...
Contacts.randomize().limit(20).display();
--
Please use prefixes: [Pony] Feature requests, [TestCase] Test cases, [Bug] if you're lazy. If you have contract work or position for FuturesJS devs, prefix [Bounty] or [Job].
-----
To post to this group, send email to futures-j...@googlegroups.com
To unsubscribe from this group, send email to
futures-javascr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/futures-javascript?hl=en
1. What are the arguments 'context' and 'params' on the anywhereify() method? The first two arguments are for providers and subscribers, but what do the other two do?
2. What is in the params argument in the subscribers functions? It isn't used in randomize(), limit(), or display().
3. Is there any kind of data caching is going on behind the scenes? Can I call just a subscriber function later on without first doing a provider function? For instance:Contacts.randomize().limit(20).display();
Contacts.all(params).randomize().limit(10).display(); // much later...
4. If I do the above, would I be randomizing just the 10 data items that were the result of the first command, so my output would still be 10 items, just in a different order? Or do I still have access to all the data, and would get an output of 20 random items from the original all() call? Or must I first call a provider function before a subscriber function? Perhaps there should be a jquery-like end() method to set a chain back to the original state?
var queue = Contacts.all(params).randomize().limit(10).display();
// much later...
queue.randomize().limit(20).display();
5. Is the point of modifying $.ajaxSetup() so that you can have an error handler with $.getJSON commands? Since $.ajaxSetup is a global setting, couldn't that cause conflicts if multiple async events are going on at the same time?
5. Is the point of modifying $.ajaxSetup() so that you can have an error handler with $.getJSON commands? Since $.ajaxSetup is a global setting, couldn't that cause conflicts if multiple async events are going on at the same time?In JavaScript there is only 1 thing ever going on at a time. The call stack must return before the next event loop occurs - at which time the next callback or setTimeout or setInterval, or User event in the stack is called. Since I'm setting the error handler back to `undefined` before the function returns, this is a non-issue.
In future versions, Node will be able to fork new processes (using the Web Workers API ) which fits well into the current design.