PageRenderTime 14ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/jsonrpc/public/services/phpolait/jsolait/lib/iter.js

http://pyjamas.googlecode.com/
JavaScript | 166 lines | 166 code | 0 blank | 0 comment | 12 complexity | 4d42d1e22ed0c2cb9d872f7eb672eb43 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. Module("iter", "0.0.2", function(mod){
  2. mod.StopIteration=Class(mod.Exception, function(publ, supr){
  3. publ.init=function(){
  4. supr(this).init("No more Items");
  5. }
  6. })
  7. mod.Iterator=Class(function(publ, supr){
  8. publ.init=function(){
  9. }
  10. publ.next=function(){
  11. throw new mod.StopIteration();
  12. }
  13. publ.iterator = function(){
  14. return this;
  15. }
  16. })
  17. mod.Range =Class(mod.Iterator, function(publ, supr){
  18. publ.init=function(start, end, step){
  19. this.current = null;
  20. switch(arguments.length){
  21. case 1:
  22. this.start = 0;
  23. this.end = start;
  24. this.step = 1;
  25. break;
  26. case 2:
  27. this.start = start;
  28. this.end = end;
  29. this.step =1;
  30. break;
  31. case 3:
  32. this.start = start;
  33. this.end = end;
  34. this.step = step;
  35. break;
  36. }
  37. this.current=this.start - this.step;
  38. }
  39. publ.next = function(){
  40. if(this.current + this.step > this.end){
  41. throw new mod.StopIteration();
  42. }else{
  43. this.current = this.current + this.step;
  44. return this.current;
  45. }
  46. }
  47. })
  48. Range = mod.Range;
  49. mod.ArrayItereator=Class(mod.Iterator, function(publ, supr){
  50. publ.init=function(array){
  51. this.array = array;
  52. this.index = -1;
  53. }
  54. publ.next = function(){
  55. this.index += 1;
  56. if(this.index >= this.array.length){
  57. throw new mod.StopIteration();
  58. }
  59. return this.array[this.index];
  60. }
  61. })
  62. Array.prototype.iterator = function(){
  63. return new mod.ArrayItereator(this);
  64. }
  65. mod.IterationCallback = function(item, iteration){};
  66. mod.Iteration = Class(function(publ, supr){
  67. publ.init=function(iteratable, callback){
  68. this.doStop = false;
  69. this.iterator = iteratable.iterator();
  70. this.callback = callback;
  71. }
  72. publ.resume = function(){
  73. this.doStop = false;
  74. while(!this.doStop){
  75. this.handleStep();
  76. }
  77. }
  78. publ.pause=function(){
  79. this.doStop = true;
  80. }
  81. publ.stop = function(){
  82. this.pause();
  83. }
  84. publ.start = function(){
  85. this.resume();
  86. }
  87. publ.handleStep = function(){
  88. try{
  89. var item=this.iterator.next();
  90. }catch(e){
  91. if(e.constructor != mod.StopIteration){
  92. throw e;
  93. }else{
  94. this.stop();
  95. return;
  96. }
  97. }
  98. this.callback(item, this);
  99. }
  100. })
  101. mod.AsyncIteration = Class(mod.Iteration, function(publ, supr){
  102. publ.init=function(iteratable, interval, callback){
  103. if(arguments.length == 2){
  104. callback = interval;
  105. interval = 0;
  106. }
  107. this.iterator = iteratable.iterator();
  108. this.interval = interval;
  109. this.callback = callback;
  110. this.isRunning = false;
  111. }
  112. publ.pause=function(){
  113. if(this.isRunning){
  114. this.isRunning = false;
  115. clearTimeout(this.timeout);
  116. delete fora.iterations[this.id];
  117. }
  118. }
  119. publ.resume = function(){
  120. if(this.isRunning == false){
  121. this.isRunning = true;
  122. var id=0;
  123. while(fora.iterations[id]){
  124. this.id++;
  125. }
  126. this.id = "" + id;
  127. fora.iterations[this.id] = this;
  128. this.timeout = setTimeout("fora.handleAsyncStep('" + this.id + "')", this.interval);
  129. }
  130. }
  131. publ.handleAsyncStep = function(){
  132. if(this.isRunning){
  133. this.handleStep();
  134. this.timeout = setTimeout("fora.handleAsyncStep('" + this.id + "')", this.interval);
  135. }
  136. }
  137. })
  138. fora = function(iteratable, interval, cb){
  139. if(arguments.length==2){
  140. var it = new mod.AsyncIteration(iteratable, interval);
  141. }else{
  142. var it = new mod.AsyncIteration(iteratable, interval, cb);
  143. }
  144. it.start();
  145. return it;
  146. }
  147. fora.handleAsyncStep = function(id){
  148. if(fora.iterations[id]){
  149. fora.iterations[id].handleAsyncStep();
  150. }
  151. }
  152. fora.iterations = new Object();
  153. forin = function(iteratable, cb){
  154. var it = new mod.Iteration(iteratable, cb)
  155. it.start();
  156. }
  157. mod.test=function(){
  158. forin(new mod.Range(10), function(item,i){
  159. print(item);
  160. })
  161. forin([1,2,3,4,5,6], function(item,i){
  162. print(item);
  163. print("---")
  164. })
  165. }
  166. })