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