Created
August 20, 2020 17:38
-
-
Save saiumesh535/039de77c1b88bbc7c70edff3013d2423 to your computer and use it in GitHub Desktop.
This file contains 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
// let's code some emitters | |
function Emitter(){ | |
this.events = {}; | |
} | |
export const emtr = new Emitter(); | |
// on function to push events in array | |
Emitter.prototype.on = function(type,listener){ | |
this.events[type] = this.events[type] || []; | |
this.events[type].push(listener); | |
} | |
// when you want to emit | |
Emitter.prototype.emit = function(type, data){ | |
if(this.events[type]){ | |
this.events[type].forEach(listener => { | |
listener(data); | |
}); | |
} | |
} | |
enum Tooltype { | |
Workflow = 1, | |
Tooltip | |
} | |
type PublishingStatus = 'PUBLISHED' | 'OUT_DATED'; | |
interface ToolWorker { | |
type: Tooltype; | |
id: number; | |
status: PublishingStatus; | |
} | |
type ToolInput = Array<ToolWorker>; | |
type Executors = Array<ToolInput>; | |
const executors: Executors = [ | |
[{ | |
id: 1, | |
type: Tooltype.Workflow, | |
status: 'OUT_DATED', | |
}, | |
{ | |
id: 2, | |
type: Tooltype.Workflow, | |
status: 'PUBLISHED' | |
} | |
], | |
[{ | |
id: 1, | |
type: Tooltype.Tooltip, | |
status: 'PUBLISHED' | |
}] | |
] | |
function worker(timer: number) { | |
return new Promise<number>((res) => { | |
setTimeout(() => { | |
res(1) | |
}, timer) | |
}) | |
} | |
// this will allow us to run each row of code asynchronously and | |
// send events whenever it finishes. so, we don't have to wait for all to finish | |
// like we have for Promise.All | |
function doPublish() { | |
return new Promise((res) => { | |
let count = executors.length; | |
emtr.on('result', (haha: any) => { | |
count = count - 1; | |
if(count === 0) { | |
res() | |
} | |
}) | |
executors.forEach(async (e, index) => { | |
worker(1000).then((s) => { | |
emtr.emit('result', index); | |
}) | |
}) | |
}) | |
} | |
async function work() { | |
console.time('hello'); | |
await doPublish(); | |
console.timeEnd('hello'); | |
} | |
work(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment