mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-26 23:05:00 +08:00
[Navigator] A method subtract to compute the diff between stack mutation.
Summary: When mutation of a stack happens, we'd like to compute the diff of the stacks (before and after) so that we can know which routes are removed in the new stack. This diff adds a new method `substract` which does what we need.
This commit is contained in:
@@ -11,7 +11,7 @@ var invariant = require('invariant');
|
||||
|
||||
type IterationCallback = (route: any, index: number, key: string) => void;
|
||||
|
||||
var {List} = immutable;
|
||||
var {List, Set} = immutable;
|
||||
|
||||
function isRouteEmpty(route: any): boolean {
|
||||
return (route === undefined || route === null || route === '') || false;
|
||||
@@ -32,6 +32,12 @@ class RouteNode {
|
||||
}
|
||||
}
|
||||
|
||||
var StackDiffRecord = immutable.Record({
|
||||
key: null,
|
||||
route: null,
|
||||
index: null,
|
||||
});
|
||||
|
||||
/**
|
||||
* The immutable route stack.
|
||||
*/
|
||||
@@ -201,6 +207,25 @@ class RouteStack {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a Set excluding any routes contained within the stack given.
|
||||
*/
|
||||
subtract(stack: RouteStack): Set<StackDiffRecord> {
|
||||
var items = [];
|
||||
this._routeNodes.forEach((node: RouteNode, index: number) => {
|
||||
if (!stack._routeNodes.contains(node)) {
|
||||
items.push(
|
||||
new StackDiffRecord({
|
||||
route: node.value,
|
||||
index: index,
|
||||
key: node.key,
|
||||
})
|
||||
);
|
||||
}
|
||||
});
|
||||
return new Set(items);
|
||||
}
|
||||
|
||||
_update(index: number, routeNodes: List): RouteStack {
|
||||
if (this._index === index && this._routeNodes === routeNodes) {
|
||||
return this;
|
||||
|
||||
Reference in New Issue
Block a user