Created
          July 23, 2022 23:50 
        
      - 
      
- 
        Save Akifcan/e24b461e65d5e7039d8b6e366d4234dd to your computer and use it in GitHub Desktop. 
    My task scheduling implementation
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | const schedules = [{ | |
| id: 'a', | |
| fun() { | |
| console.log('a dependency run!') | |
| }, | |
| depends_on: ['b', 'c'] | |
| }, | |
| { | |
| id: 'b', | |
| fun() { | |
| console.log('b dependency run!') | |
| }, | |
| depends_on: [] | |
| }, | |
| { | |
| id: 'c', | |
| fun() { | |
| console.log('c dependency run!') | |
| }, | |
| depends_on: [] | |
| }, | |
| { | |
| id: 'd', | |
| fun() { | |
| console.log('d dependency run!') | |
| }, | |
| depends_on: ['a', 'c'] | |
| }, | |
| { | |
| id: 'x', | |
| fun() { | |
| console.log('x dependency run!') | |
| }, | |
| depends_on: ['a', 'c', 'b', 'f'] | |
| }, | |
| { | |
| id: 'j', | |
| fun() { | |
| console.log('j dependency run!') | |
| }, | |
| depends_on: ['a', 'c', 'b', 'f', 'e'] | |
| }, | |
| { | |
| id: 'e', | |
| fun() { | |
| console.log('e dependency run!') | |
| }, | |
| depends_on: ['f', 'b'] | |
| }, | |
| { | |
| id: 'f', | |
| fun() { | |
| console.log('f dependency run!') | |
| }, | |
| depends_on: ['d'] | |
| }, | |
| ] | |
| const upDependencies = [] | |
| function runDependencies() { | |
| schedules.forEach(event => { | |
| if (event.depends_on.map(dependency => upDependencies.includes(dependency)).every(item => item === true) && !upDependencies.includes(event.id)) { | |
| event.fun() | |
| upDependencies.push(event.id) | |
| } | |
| }) | |
| } | |
| while(upDependencies.length !== schedules.length){ | |
| runDependencies() | |
| } | |
| console.log(upDependencies.length) | |
| /* const x = ['b', 'c'] | |
| const y = ['b'] | |
| console.log(y.map(z => x.includes(z)).every(j => j == true)) */ | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment