also resolve missing previous node

This commit is contained in:
Yanzhen Yu
2018-11-05 13:20:42 +08:00
parent f1930490b8
commit 4709c720cd
3 changed files with 36 additions and 25 deletions

View File

@@ -11,7 +11,9 @@ import {
playerConfig, playerConfig,
playerMetaData, playerMetaData,
viewportResizeDimention, viewportResizeDimention,
missingNextNodeMap, missingNodeMap,
addedNodeMutation,
missingNode,
} from '../types'; } from '../types';
import { mirror } from '../utils'; import { mirror } from '../utils';
@@ -218,7 +220,7 @@ export class Replayer {
} }
}); });
const missingNextNodeMap: missingNextNodeMap = {}; const missingNodeMap: missingNodeMap = {};
d.adds.forEach(mutation => { d.adds.forEach(mutation => {
const target = buildNodeWithSN( const target = buildNodeWithSN(
mutation.node, mutation.node,
@@ -236,8 +238,8 @@ export class Replayer {
next = mirror.getNode(mutation.nextId) as Node; next = mirror.getNode(mutation.nextId) as Node;
} }
if (mutation.nextId === -1) { if (mutation.previousId === -1 || mutation.nextId === -1) {
missingNextNodeMap[mutation.node.id] = { missingNodeMap[mutation.node.id] = {
node: target, node: target,
mutation, mutation,
}; };
@@ -252,16 +254,13 @@ export class Replayer {
parent.appendChild(target); parent.appendChild(target);
} }
if (mutation.previousId) { if (mutation.previousId || mutation.nextId) {
this.resolveMissingNode( this.resolveMissingNode(missingNodeMap, parent, target, mutation);
missingNextNodeMap,
parent,
target,
mutation.previousId,
);
} }
}); });
// TODO: assert missingNextNodeMap has no key after resolve if (Object.keys(missingNodeMap).length) {
console.warn('Found unresolved missing node map', missingNodeMap);
}
d.texts.forEach(mutation => { d.texts.forEach(mutation => {
const target = (mirror.getNode(mutation.id) as Node) as Text; const target = (mirror.getNode(mutation.id) as Node) as Text;
@@ -343,17 +342,24 @@ export class Replayer {
} }
private resolveMissingNode( private resolveMissingNode(
map: missingNextNodeMap, map: missingNodeMap,
parent: Node, parent: Node,
target: Node, target: Node,
previousId: number, targetMutation: addedNodeMutation,
) { ) {
if (map[previousId]) { const { previousId, nextId } = targetMutation;
const { node, mutation } = map[previousId]; const previousInMap = previousId && map[previousId];
parent.insertBefore(node, target); const nextInMap = nextId && map[nextId];
if (previousInMap || nextInMap) {
const { node, mutation } = (previousInMap || nextInMap) as missingNode;
if (previousInMap) {
parent.insertBefore(node, target);
} else {
parent.insertBefore(node, target.nextSibling);
}
delete map[mutation.node.id]; delete map[mutation.node.id];
if (mutation.previousId) { if (mutation.previousId || mutation.nextId) {
this.resolveMissingNode(map, parent, node as Node, mutation.previousId); this.resolveMissingNode(map, parent, node as Node, mutation);
} }
} }
} }

View File

@@ -232,9 +232,10 @@ export type playerMetaData = {
totalTime: number; totalTime: number;
}; };
export type missingNextNodeMap = { export type missingNode = {
[id: number]: { node: Node;
node: Node; mutation: addedNodeMutation;
mutation: addedNodeMutation; };
}; export type missingNodeMap = {
[id: number]: missingNode;
}; };

File diff suppressed because one or more lines are too long