Tue 27 Aug 00:14:56 CEST 2024
This commit is contained in:
		
							parent
							
								
									d33efa35e4
								
							
						
					
					
						commit
						42bfed827f
					
				
							
								
								
									
										41
									
								
								test/producer-consumer.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								test/producer-consumer.js
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,41 @@
 | 
			
		|||
function foo (options) {
 | 
			
		||||
  this.producer=options.producer;
 | 
			
		||||
  this.data=null;
 | 
			
		||||
  this.act = {
 | 
			
		||||
    main : function () {
 | 
			
		||||
      log('starting',this.producer)
 | 
			
		||||
    },
 | 
			
		||||
    produce : function () {
 | 
			
		||||
      this.data={number:random(1,100)}
 | 
			
		||||
      out(['SENSOR',this.data])
 | 
			
		||||
      this.data.number++;
 | 
			
		||||
      out(['SENSOR',this.data])
 | 
			
		||||
    },
 | 
			
		||||
    forkme : function () {
 | 
			
		||||
      fork({
 | 
			
		||||
        producer:false,
 | 
			
		||||
        data:null
 | 
			
		||||
      });
 | 
			
		||||
    },
 | 
			
		||||
    consume : function () {
 | 
			
		||||
      inp.try(3000, ['SENSOR',_],function (t) {
 | 
			
		||||
        this.data=t;
 | 
			
		||||
      },true);
 | 
			
		||||
    },
 | 
			
		||||
    end : function () {
 | 
			
		||||
      log('terminate',this.data);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  this.trans = {
 | 
			
		||||
    main : produce,
 | 
			
		||||
    produce : forkme,
 | 
			
		||||
    forkme : function () {
 | 
			
		||||
      return this.producer?end:consume
 | 
			
		||||
    },
 | 
			
		||||
    consume : end,
 | 
			
		||||
  }
 | 
			
		||||
  this.next=main;
 | 
			
		||||
}
 | 
			
		||||
compile(foo,{verbose:1})
 | 
			
		||||
create('foo',{producer:true})
 | 
			
		||||
start()
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user