Skip to content
Browse files

Adding examples and compiled library.

  • Loading branch information...
0 parents commit 888e7ec371bb0e4f2aeae528386fbe696dc4f620 @stevekrenzel committed Feb 28, 2012
2 bin/saq.js
@@ -0,0 +1,2 @@
+(function(){var i,j,a;i=function(b,d){return function(){if(1>--b)return d.apply(null,arguments)}};j=function(b){return"[object Function]"===toString.call(b)};a=function(b){var d,a,f,g,e;e=[];g=0;a=function(h,c){var a;null==c&&(a=[1,h],h=a[0],c=a[1]);return e.splice(g++,0,[h,c])};d=function(){var a,c;g=0;if(0<e.length)return c=e.shift(),a=c[0],b=c[1],b(i(a,d))};f=b({async:a,sync:function(b){return a(function(a){b();return a()})}});j(f)||d();return function(){f.apply(null,arguments);return d()}};null!=
+this.window?this.saq=a:exports.saq=a}).call(this);
BIN bin/saq.js.gz
Binary file not shown.
22 examples/grouped.coffee
@@ -0,0 +1,22 @@
+{saq} = require '../saq'
+
+task = (i, j) -> (next) ->
+ seconds = 1000 * Math.random()
+ execute = (-> console.log "Computation #{i} #{j}"; next();)
+ setTimeout execute, seconds
+
+
+grouped = saq (queue) -> (groups, tasks, cb) ->
+ queue.async groups, (next) ->
+ for i in [0...groups]
+ saq (queue) ->
+ for j in [0...tasks]
+ queue.async task i, j
+ queue.sync next
+ queue.sync cb
+
+
+groups = parseInt process.argv[2]
+tasks = parseInt process.argv[3]
+grouped groups, tasks, ->
+ console.log 'Done!'
17 examples/sayhi.coffee
@@ -0,0 +1,17 @@
+{saq} = require '../saq'
+
+
+asyncHi = saq (queue) -> (name, cb) ->
+ queue.sync ->
+ console.log 'Hello'
+
+ queue.async (next) ->
+ console.log name
+ setTimeout next, 500
+
+ queue.sync cb
+
+
+name = process.argv.splice(2).join(' ')
+asyncHi name, ->
+ console.log 'Done!'
18 examples/twitter/index.html
@@ -0,0 +1,18 @@
+<html>
+ <head>
+ <title>Twitter Search Example</title>
+ </head>
+ <body>
+ <form class="search">
+ <input type="text" class="terms" placeholder="search for multiple keywords"/>
+ <input type="submit" value="Search">
+ </form>
+ <table>
+ <tr class="header"></tr>
+ <tr class="results"></tr>
+ </table>
+ <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
+ <script src="../../bin/saq.js"></script>
+ <script src="./twitter.js"></script>
+ </body>
+</html>
29 examples/twitter/twitter.coffee
@@ -0,0 +1,29 @@
+search = (keyword, cb) ->
+ host = "http://search.twitter.com/"
+ url = "#{host}/search.json?q=#{keyword}&callback=?"
+ $.getJSON url, (json) -> cb json.results
+
+
+parallelSearch = saq (queue) -> (keywords, cb) ->
+ out = []
+ queue.async keywords.length, (next) ->
+ keywords.map (k, i) ->
+ search k, (json) -> out[i] = json; next()
+ queue.sync -> cb out
+
+
+serialSearch = saq (queue) -> (keywords, cb) ->
+ out = []
+ keywords.map (k, i) ->
+ queue.async (next) ->
+ search k, (json) -> out[i] = json; next()
+ queue.sync -> cb out
+
+
+$('.search').submit ->
+ terms = (term.trim() for term in $('.terms').val().split(','))
+ serialSearch terms, (results) ->
+ results.map (tweets) ->
+ tweets.map (tweet) ->
+ $('.results').append "<div>#{tweet.text}</div>"
+ false
71 examples/twitter/twitter.js
@@ -0,0 +1,71 @@
+(function() {
+ var parallelSearch, search, serialSearch;
+
+ search = function(keyword, cb) {
+ var host, url;
+ host = "http://search.twitter.com/";
+ url = "" + host + "/search.json?q=" + keyword + "&callback=?";
+ return $.getJSON(url, function(json) {
+ return cb(json.results);
+ });
+ };
+
+ parallelSearch = saq(function(queue) {
+ return function(keywords, cb) {
+ var out;
+ out = [];
+ queue.async(keywords.length, function(next) {
+ return keywords.map(function(k, i) {
+ return search(k, function(json) {
+ out[i] = json;
+ return next();
+ });
+ });
+ });
+ return queue.sync(function() {
+ return cb(out);
+ });
+ };
+ });
+
+ serialSearch = saq(function(queue) {
+ return function(keywords, cb) {
+ var out;
+ out = [];
+ keywords.map(function(k, i) {
+ return queue.async(function(next) {
+ return search(k, function(json) {
+ out[i] = json;
+ return next();
+ });
+ });
+ });
+ return queue.sync(function() {
+ return cb(out);
+ });
+ };
+ });
+
+ $('.search').submit(function() {
+ var term, terms;
+ terms = (function() {
+ var _i, _len, _ref, _results;
+ _ref = $('.terms').val().split(',');
+ _results = [];
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ term = _ref[_i];
+ _results.push(term.trim());
+ }
+ return _results;
+ })();
+ serialSearch(terms, function(results) {
+ return results.map(function(tweets) {
+ return tweets.map(function(tweet) {
+ return $('.results').append("<div>" + tweet.text + "</div>");
+ });
+ });
+ });
+ return false;
+ });
+
+}).call(this);
42 examples/windowed.coffee
@@ -0,0 +1,42 @@
+{saq} = require '../saq'
+
+
+active_tasks = 0
+
+
+task = (i, cb) ->
+ active_tasks += 1
+ console.log "Active: #{active_tasks}. Starting task #{i}"
+ seconds = 2000 * Math.random()
+ execute = ->
+ console.log "Active: #{active_tasks}. Stopping tasks #{i}"
+ active_tasks -= 1
+ cb i * i
+ setTimeout execute, seconds
+
+
+windowed = saq (queue) -> (tasks, window, cb) ->
+ window = Math.min(window, tasks)
+ count = 0
+ squares = []
+
+ schedule = (next) ->
+ task count, (i) ->
+ squares.push i
+ schedule next if count < tasks
+ next()
+ count += 1
+
+ queue.async tasks, (next) ->
+ schedule next for i in [0...window]
+
+ queue.sync ->
+ sum = 0
+ sum += square for square in squares
+ cb sum
+
+
+tasks = parseInt process.argv[2]
+window = parseInt process.argv[3]
+windowed tasks, window, (sum) ->
+ console.log sum
37 saq.coffee
@@ -0,0 +1,37 @@
+after = (times, fn) -> ->
+ fn arguments... if --times < 1
+
+isFunction = (obj) ->
+ toString.call(obj) == '[object Function]'
+
+saq = (fn) ->
+ queue = []
+ index = 0
+
+ enqueue = (n, fn) ->
+ [n, fn] = [1, n] unless fn?
+ queue.splice index++, 0, [n, fn]
+
+ dequeue = ->
+ index = 0
+ if queue.length > 0
+ [n, fn] = queue.shift()
+ fn after n, dequeue
+
+ gn = fn
+ async: enqueue
+ sync: (fn) ->
+ enqueue (next) ->
+ fn()
+ next()
+
+ dequeue() unless isFunction gn
+
+ ->
+ gn arguments...
+ dequeue()
+
+if @window?
+ @saq = saq
+else
+ exports.saq = saq

0 comments on commit 888e7ec

Please sign in to comment.
Something went wrong with that request. Please try again.