Skip to content
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
10 changes: 5 additions & 5 deletions packages/angular_devkit/schematics/src/rules/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.dev/license
*/

import { Observable, concat, map, mapTo, toArray } from 'rxjs';
import { concat, lastValueFrom, map, mapTo, toArray } from 'rxjs';
import { FileOperator, Rule, Source } from '../engine/interface';
import { SchematicsException } from '../exception/exception';
import { FilterHostTree, HostTree } from '../tree/host-tree';
Expand Down Expand Up @@ -34,18 +34,18 @@ export function empty(): Source {
*/
export function chain(rules: Iterable<Rule> | AsyncIterable<Rule>): Rule {
return async (initialTree, context) => {
let intermediateTree: Observable<Tree> | undefined;
let intermediateTree = initialTree;
if (Symbol.asyncIterator in rules) {
for await (const rule of rules) {
intermediateTree = callRule(rule, intermediateTree ?? initialTree, context);
intermediateTree = await lastValueFrom(callRule(rule, intermediateTree, context));
}
} else {
for (const rule of rules) {
intermediateTree = callRule(rule, intermediateTree ?? initialTree, context);
intermediateTree = await lastValueFrom(callRule(rule, intermediateTree, context));
}
}

return () => intermediateTree;
return intermediateTree;
};
}

Expand Down
13 changes: 13 additions & 0 deletions packages/angular_devkit/schematics/src/rules/base_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,19 @@ describe('chain', () => {
expect(rulesCalled[2]).toBe(tree2);
expect(result).toBe(tree3);
});

it('works with a large number of rules', async () => {
const tree0 = empty();
let callCount = 0;
const rules: Rule[] = Array.from({ length: 20_000 }, () => () => {
callCount++;
});

const result = await lastValueFrom(callRule(chain(rules), tree0, context));

expect(result).toBe(tree0);
expect(callCount).toBe(20_000);
});
});

describe('apply', () => {
Expand Down
Loading