khoi01

Example of Observer (waiting other services to completed.. then run final process)

Apr 14th, 2021
421
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. class JobServices {
  3.  
  4.     //All the objects will store here
  5.     static observers = [];
  6.  
  7.     //register objects
  8.     static async subscribe(service) {
  9.         this.observers.push(service);
  10.     }
  11.  
  12.     //begin start service
  13.     static async fire() {
  14.  
  15.  
  16.         this.observers.forEach(async (obj) =>{
  17.             console.log(`Begin Service observers[${obj.id}] name: ${obj.entity}`);
  18.             await obj.observeService();
  19.         });
  20.        
  21.     }
  22.  
  23.     //check if all the process is completed
  24.     static async check() {        
  25.         if(this.observers.every( obj => obj.isCompleted)){
  26.             console.log("completed process..");  
  27.         }
  28.     }
  29.  
  30.    
  31. }
  32.  
  33. //Service
  34. class Service  {
  35.  
  36.     //parameter : unique id , "Service name" ,"service timeout process"
  37.         constructor(id,entity,timeout){
  38.             this.id = id;
  39.             this.entity = entity;
  40.             this.isCompleted = false;
  41.             this.timeout = timeout;
  42.         }
  43.    
  44.  
  45.       observeService = async function() {
  46.         setTimeout(() => {
  47.             //after process done isCompleted flag change to true(boolean) value
  48.             console.log(`${this.entity} is Trigger:Service`);
  49.             this.isCompleted = true;
  50.             JobServices.check(); //check if other services and current service comepleted process or not.
  51.         }, this.timeout);
  52.     }
  53. }
  54.  
  55. class Service1 extends Service{
  56.  
  57.     constructor(id,entity,timeout){
  58.         super(id,entity,timeout); //derived
  59.     }
  60.     observeService = async function() {
  61.         setTimeout(() => {
  62.             //after process done isCompleted flag change to true(boolean) value
  63.             console.log(`${this.entity} is Trigger:Service1`);
  64.             this.isCompleted = true;
  65.             JobServices.check(); //check if other services and current service comepleted process or not.
  66.         }, this.timeout);
  67.     }
  68. }
  69.  
  70. function start() {
  71.  
  72.  
  73.     //Register Services
  74.     JobServices.subscribe(new Service("1","Service 1",2000));
  75.     JobServices.subscribe(new Service1("2","Service 2",200));
  76.     JobServices.subscribe(new Service("3","Service 3",500));
  77.  
  78.     //Begin services
  79.     JobServices.fire();
  80.  
  81. }
  82.  
  83.  
  84. start();
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×