Skip to content

Commit 99004d6

Browse files
committed
Finished Algorithm Implementation for Detecting List Changes
1 parent 9692879 commit 99004d6

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed

Framework/framework-br.md

+36-36
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Embora a verificação suja ser ineficiente, ele consegue completar a tarefa sem
4242

4343
## Sequesto de dados
4444

45-
Vuew internamente usa `Obeject.defineProperty()` para implementar o two-way binding, do qual permite você escutar por eventos de `set` e `get`.
45+
Vue internamente usa `Obeject.defineProperty()` para implementar o two-way binding, do qual permite você escutar por eventos de `set` e `get`.
4646

4747
```js
4848
var data = { name: 'yck' }
@@ -414,7 +414,7 @@ function dfs(oldNode, newNode, index, patches) {
414414
// três casos
415415
// 1. não é novo nó, não faça nada
416416
// 2. novos nós tagName e `key` são diferentes dos antigos, substitua
417-
// 3. novos nós tagName e chave são o mesmo do antigo, comece a recursão
417+
// 3. novos nós tagName e key são o mesmo do antigo, comece a recursão
418418
if (!newNode) {
419419
} else if (newNode.tag === oldNode.tag && newNode.key === oldNode.key) {
420420
// verifique se as propriedades mudaram
@@ -437,20 +437,20 @@ function dfs(oldNode, newNode, index, patches) {
437437
}
438438
```
439439

440-
### checking property changes
440+
### verificando mudança das propriedades
441441

442-
We also have three steps for checking for property changes
442+
Nós temos também três passos para verificar por mudanças nas propriedades
443443

444-
1. iterate the old property list, check if the property still exists in the new property list.
445-
2. iterate the new property list, check if there are changes for properties existing in both lists.
446-
3. for the second step, also check if a property doesn't exist in the old property list.
444+
1. itere a lista de propriedades antiga, verifique se a propriedade ainda existe na nova lista de propriedade.
445+
2. itere a nova lista de propriedades, verifique se existe mudanças para propriedades existente nas duas listas.
446+
3. no segundo passo, também verifique se a propriedade não existe na lista de propriedades antiga.
447447

448448
```js
449449
function diffProps(oldProps, newProps) {
450-
// three steps for checking for props
451-
// iterate oldProps for removed properties
452-
// iterate newProps for chagned property values
453-
// lastly check if new properties are added
450+
// três passos para checar as props
451+
// itere oldProps para remover propriedades
452+
// itere newProps para mudar os valores das propriedades
453+
// por último verifique se novas propriedades foram adicionadas
454454
let change = []
455455
for (const key in oldProps) {
456456
if (oldProps.hasOwnProperty(key) && !newProps[key]) {
@@ -479,50 +479,50 @@ function diffProps(oldProps, newProps) {
479479
}
480480
```
481481

482-
### Algorithm Implementation for Detecting List Changes
482+
### Implementação do Algoritmo de detecção de mudanças na lista
483483

484-
This algorithm is the core of the Virtual Dom. Let's go down the list.
485-
The main steps are similar to checking property changes. There are also three steps.
484+
Esse algoritmo é o núcle do Virtual Dom. Vamos descer a lista.
485+
O passo principal é similar a verificação de mudanças nas propriedades. Também existe três passos.
486486

487-
1. iterate the old node list, check if the node still exists in the new list.
488-
2. iterate the new node list, check if there is any new node.
489-
3. for the second step, also check if a node moved.
487+
1. itere a antiga lista de nós, verifique se ao nó ainda existe na nova lista.
488+
2. itere a nova lista de nós, verifiquen se existe algum novo nó.
489+
3. para o seguindo passo, também verifique se o nó moveu.
490490

491-
PS: this algorithm only handles nodes with `key`s.
491+
PS: esse algoritmo apenas manipula nós com `key`s.
492492

493493
```js
494494
function listDiff(oldList, newList, index, patches) {
495-
// to make the iteration more convenient, first take all keys from both lists
495+
// para fazer a iteração mais conveniente, primeiro pegue todas as chaves de ambas as listas
496496
let oldKeys = getKeys(oldList)
497497
let newKeys = getKeys(newList)
498498
let changes = []
499499

500-
// for saving the node daa after changes
501-
// there are several advantages of using this array to save
502-
// 1. we can correctly obtain the index of the deleted node
503-
// 2. we only need to operate on the DOM once for interexchanged nodes
504-
// 3. we only need to iterate for the checking in the `diffChildren` function
505-
// we don't need to check again for nodes existing in both lists
500+
// para salvar o dado do nó depois das mudanças
501+
// existe varia vantagem de usar esse array para salvar
502+
// 1. nós conseguimos obter corretamente o index de nós deletados
503+
// 2. precisamos apenas opera no DOM uma vez para interexchanged os nós
504+
// 3. precisamos apenas iterar para verificar na função `diffChildren`
505+
// nós não precisamos verificar de novo para nós existente nas duas listas
506506
let list = []
507507
oldList &&
508508
oldList.forEach(item => {
509509
let key = item.key
510510
if (isString(item)) {
511511
key = item
512512
}
513-
// checking if the new children has the current node
514-
// if not then delete
513+
// verificando se o novo filho tem o nó atual
514+
// se não, então delete
515515
let index = newKeys.indexOf(key)
516516
if (index === -1) {
517517
list.push(null)
518518
} else list.push(key)
519519
})
520-
// array after iterative changes
520+
// array depois de alterações iterativas
521521
let length = list.length
522-
// since deleting array elements changes the indices
523-
// we remove from the back to make sure indices stay the same
522+
// uma vez deletando um array de elementos, o índice muda
523+
// removemos de trás para ter certeza que os índices permanecem o mesmo
524524
for (let i = length - 1; i >= 0; i--) {
525-
// check if the current element is null, if so then it means we need to remove it
525+
// verifica se o elemento atual é null, se sim então significa que precisamos remover ele
526526
if (!list[i]) {
527527
list.splice(i, 1)
528528
changes.push({
@@ -531,17 +531,17 @@ function listDiff(oldList, newList, index, patches) {
531531
})
532532
}
533533
}
534-
// iterate the new list, check if a node is added or moved
535-
// also add and move nodes for `list`
534+
// itere a nova lista, verificando se um nó é adicionado ou movido
535+
// também adicione ou mova nós para `list`
536536
newList &&
537537
newList.forEach((item, i) => {
538538
let key = item.key
539539
if (isString(item)) {
540540
key = item
541541
}
542-
// check if the old children has the current node
542+
// verifique se o filho antigo tem o nó atual
543543
let index = list.indexOf(key)
544-
// if not then we need to insert
544+
// se não então precisamos inserir
545545
if (index === -1 || key == null) {
546546
changes.push({
547547
type: StateEnums.Insert,
@@ -550,7 +550,7 @@ function listDiff(oldList, newList, index, patches) {
550550
})
551551
list.splice(i, 0, key)
552552
} else {
553-
// found the node, need to check if it needs to be moved.
553+
// encontrado o nó, precisamos verificar se ele precisar ser movido.
554554
if (index !== i) {
555555
changes.push({
556556
type: StateEnums.Move,

0 commit comments

Comments
 (0)