Rewrite into TypeScript

This commit is contained in:
Wessel T
2019-08-01 16:49:14 +02:00
parent 9e7df46abb
commit 496cd5641c
29 changed files with 863 additions and 174 deletions

48
lib/Master.ts Normal file
View File

@@ -0,0 +1,48 @@
import { EventEmitter } from 'events';
import { SnowflakeWorker } from './types';
export default class SnowflakeMaster extends EventEmitter {
public workers: any[]
constructor() {
super();
this.setMaxListeners(1000);
this.workers = [];
}
addWorkers(...workers: SnowflakeWorker[]): void {
for (const worker of workers) {
this.workers.push(worker);
}
return this.refresh();
}
listWorkers(): SnowflakeWorker[] {
return this.workers;
}
removeWorkers(...identities: string[] | number[]): { 'removed': number } {
let found = 0;
for (const identity of identities) {
for (let i in this.workers) {
const worker = this.workers[i];
if (worker.options.name === identity || worker.options.workerId === identity) {
found++;
this.workers.splice(parseInt(i), 1);
}
}
}
return { removed: found }
}
refresh(): void {
for (let worker of this.workers) {
worker.on('newSnowflake', (...args) => this.emit('newSnowflake', ...args));
worker.on('deconstructedFlake', (...args) => this.emit('deconstructedFlake', ...args));
}
}
}