Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support pushing large number of items in tree iterators #12172

Merged
merged 2 commits into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/core/src/browser/tree/tree-iterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,25 +202,25 @@ export namespace Iterators {
* Generator for depth first, pre-order tree traversal iteration.
*/
export function* depthFirst<T>(root: T, children: (node: T) => T[] | undefined, include: (node: T) => boolean = () => true): IterableIterator<T> {
const stack: T[] = [];
let stack: T[] = [];
stack.push(root);
while (stack.length > 0) {
const top = stack.pop()!;
yield top;
stack.push(...(children(top) || []).filter(include).reverse());
stack = stack.concat((children(top) || []).filter(include).reverse());
}
}

/**
* Generator for breadth first tree traversal iteration.
*/
export function* breadthFirst<T>(root: T, children: (node: T) => T[] | undefined, include: (node: T) => boolean = () => true): IterableIterator<T> {
const queue: T[] = [];
let queue: T[] = [];
queue.push(root);
while (queue.length > 0) {
const head = queue.shift()!;
yield head;
queue.push(...(children(head) || []).filter(include));
queue = queue.concat((children(head) || []).filter(include));
}
}

Expand Down
31 changes: 29 additions & 2 deletions packages/core/src/browser/tree/tree-selection-state.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

import { expect } from 'chai';
import { MockTreeModel } from './test/mock-tree-model';
import { TreeSelectionState } from './tree-selection-state';
import { createTreeTestContainer } from './test/tree-test-container';
import { SelectableTreeNode, TreeSelection } from './tree-selection';
import { TreeModel } from './tree-model';
import { SelectableTreeNode, TreeSelection } from './tree-selection';
import { TreeSelectionState } from './tree-selection-state';

namespace TreeSelectionState {

Expand All @@ -34,6 +34,16 @@ namespace TreeSelectionState {

}

const LARGE_FLAT_MOCK_ROOT = (length = 250000) => {
const children = Array.from({ length }, (_, idx) => ({ 'id': (idx + 1).toString() }));
return MockTreeModel.Node.toTreeNode({
'id': 'ROOT',
'children': [
...children
]
});
};

describe('tree-selection-state', () => {

const model = createTreeModel();
Expand Down Expand Up @@ -391,6 +401,23 @@ describe('tree-selection-state', () => {
});
});

it('should be able to handle range selection on large tree', () => {
model.root = LARGE_FLAT_MOCK_ROOT();
expect(model.selectedNodes).to.be.empty;

const start = 10;
const end = 20;
newState()
.nextState('toggle', start.toString(), {
focus: start.toString(),
selection: [start.toString()]
})
.nextState('range', end.toString(), {
focus: start.toString(),
selection: Array.from({ length: end - start + 1 }, (_, idx) => (start + idx).toString())
});
});

function newState(): TreeSelectionState.Assert {
return nextState(new TreeSelectionState(model));
}
Expand Down