upr.git  about / heads / tags
Upload Progress for Rack
blob 8b0236dbc0c602b5463ba14217ae849acfc667ea 2575 bytes (raw)
$ git show HEAD:examples/rails_app-2.3.4/public/javascripts/ajax_pull/ajax_pull.js	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
 
// ref: http://rubyforge.org/pipermail/mongrel-users/2007-July/003747.html
// ry dahl <ry@tinyclouds.org>
// Don't Poll. Pull!
// version 0.1
Ajax.Pull = Class.create();

Ajax.Pull.prototype = Object.extend(Ajax.Request.prototype, {
  initialize: function(url, options) {
    this.handler = options.handler;
    this.frequency = options.frequency || 0.5;
    options['method'] = options['method'] || 'get';
    this.setOptions(options);
    this.transport = Ajax.getTransport();

    this.request(url);
    this.startPuller();
  },

  startPuller: function() {
    this.pullPointer = 0;
    this.puller = new PeriodicalExecuter(this.pull.bind(this), this.frequency);
  },

  pull: function() {
    if(this.transport.readyState < 3) {
      return; // not receiving yet
    } else if (this._complete) {
      this.puller.stop(); // this is our last pull
    }

    var slice = this.transport.responseText.slice(this.pullPointer);

    (this.options.pullDebugger || Prototype.emptyFunction)(
        'slice: <code>' + slice + '</code>');

    slice.extractJSON().each((function(statement) {
      (this.options.pullDebugger || Prototype.emptyFunction)(
          'extracted statement: <code>' + statement + '</code>');
      this.handler(eval( '(' + statement + ')' ));
      this.pullPointer += statement.length + 1;
    }).bind(this));

  },

});


Object.extend(String.prototype, {
  extractJSON: function() {
    var insideString = false;
    var sBrackets = cBrackets = parens = 0;
    var statements = new Array();
    var start = i = 0;
    for(i = 0; i < this.length; i++) {
      if( cBrackets < 0 || sBrackets < 0 || parens < 0 ) {
        // raise syntax error?
      }
      if(insideString) {
        switch(this[i]) {
          case '\\': i++; break;
          case '"': insideString = false; break;
        }
      } else {
        switch(this[i]) {
          case '"': insideString = true; break;
          case '{': cBrackets++; break;
          case '}': cBrackets--; break;
          case '[': sBrackets++; break;
          case ']': sBrackets--; break;
          case '(': parens++; break;
          case ')': parens++; break;
          case ';':
            if(cBrackets == 0 && sBrackets == 0 && parens == 0) {
              statements.push(this.slice(start, i));
              start = i+1;
            }
        }
      }
      // if the last statement doesn't have a semicolon, it's okay
      // if(i != start && cBrackets == 0 && sBrackets == 0 && parens == 0)
      //         statements.push(this.slice(start));
    }
    return statements;
  }
});

git clone https://yhbt.net/upr.git