Created
December 16, 2019 18:35
-
-
Save Streeterxs/15176f42ecd8d3959b08a7829d1f698e 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
import { BehaviorSubject } from 'rxjs'; | |
import { performanceDecorator } from '../../_decorators/performanceDecorator'; | |
export class Arvore<T> { | |
private _left: Arvore<T>; | |
private _right: Arvore<T>; | |
value: T; | |
lista: T[]; | |
constructor(lista: T[], observable: BehaviorSubject<Arvore<T>> | null) { | |
this.lista = lista; | |
const antes = performance.now(); | |
if (typeof Worker !== 'undefined') { | |
// Create a new | |
const worker = new Worker('./arvore-binaria.worker.ts', { type: 'module' }); | |
worker.onmessage = ({ data }) => { | |
data.halfDown && (this._left = new Arvore<T>(data.halfDown, null)); | |
data.halfUp && (this._right = new Arvore<T>(data.halfUp, null)); | |
this.value = data.value; | |
if (observable) { | |
observable.next(this); | |
} | |
}; | |
worker.postMessage({lista}); | |
} else { | |
// Web Workers are not supported in this environment. | |
// You should add a fallback so that your program still executes correctly. | |
return; | |
} | |
const depois = performance.now(); | |
console.log(`Performance do constructor da criação das árvores ${depois - antes} ms`); | |
} | |
@performanceDecorator() | |
retornaTodosOsValoresDaArvore(bSubject: BehaviorSubject<T>) { | |
bSubject.next(this.value); | |
this._left && this._left.retornaTodosOsValoresDaArvore(bSubject); | |
this._right && this._right.retornaTodosOsValoresDaArvore(bSubject); | |
} | |
@performanceDecorator() | |
retornaValorEspecificoPelaRegra(regra: boolean, bSubject: BehaviorSubject<T>) { | |
if (regra) { | |
bSubject.next(this.value); | |
} else { | |
this._left.retornaValorEspecificoPelaRegra(regra, bSubject); | |
this._right.retornaValorEspecificoPelaRegra(regra, bSubject); | |
} | |
} | |
@performanceDecorator() | |
retornaLista(bSubject: BehaviorSubject<T>) { | |
this.lista.forEach(item => { | |
bSubject.next(item); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment