Skip to content

Commit dc20948

Browse files
committed
refactor(@angular-devkit/schematics): reduce direct rxjs usage in filter host tree
All direct usage of rxjs has been removed from the FilterHostTree implementation. This removes the need to import rxjs within the containing file. The recursive logic has also been replaced with an iterative approach. (cherry picked from commit d087b5f)
1 parent 86b53c5 commit dc20948

File tree

1 file changed

+27
-28
lines changed

1 file changed

+27
-28
lines changed

packages/angular_devkit/schematics/src/tree/host-tree.ts

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ import {
1818
virtualFs,
1919
} from '@angular-devkit/core';
2020
import { ParseError, parse as jsoncParse, printParseErrorCode } from 'jsonc-parser';
21-
import { EMPTY, Observable, concatMap, map, mergeMap } from 'rxjs';
22-
import { TextDecoder } from 'util';
2321
import {
2422
ContentHasMutatedException,
2523
FileAlreadyExistException,
@@ -496,38 +494,39 @@ export class FilterHostTree extends HostTree {
496494
// cast to allow access
497495
const originalBackend = (tree as FilterHostTree)._backend;
498496

499-
const recurse: (base: Path) => Observable<void> = (base) => {
500-
return originalBackend.list(base).pipe(
501-
mergeMap((x) => x),
502-
map((path) => join(base, path)),
503-
concatMap((path) => {
504-
let isDirectory = false;
505-
originalBackend.isDirectory(path).subscribe((val) => (isDirectory = val));
506-
if (isDirectory) {
507-
return recurse(path);
508-
}
509-
510-
let isFile = false;
511-
originalBackend.isFile(path).subscribe((val) => (isFile = val));
512-
if (!isFile || !filter(path)) {
513-
return EMPTY;
514-
}
497+
// Walk the original backend and add files that match the filter to the new backend
498+
const pendingPaths: Path[] = ['/' as Path];
499+
while (pendingPaths.length > 0) {
500+
const currentPath = pendingPaths.pop();
501+
if (currentPath === undefined) {
502+
break;
503+
}
515504

516-
let content: ArrayBuffer | null = null;
517-
originalBackend.read(path).subscribe((val) => (content = val));
518-
if (!content) {
519-
return EMPTY;
520-
}
505+
let isDirectory = false;
506+
originalBackend.isDirectory(currentPath).subscribe((val) => (isDirectory = val));
507+
if (isDirectory) {
508+
originalBackend
509+
.list(currentPath)
510+
.subscribe((val) => pendingPaths.push(...val.map((p) => join(currentPath, p))));
511+
continue;
512+
}
521513

522-
return newBackend.write(path, content as {} as virtualFs.FileBuffer);
523-
}),
524-
);
525-
};
514+
let isFile = false;
515+
originalBackend.isFile(currentPath).subscribe((val) => (isFile = val));
516+
if (!isFile || !filter(currentPath)) {
517+
continue;
518+
}
526519

527-
recurse(normalize('/')).subscribe();
520+
let content = null;
521+
originalBackend.read(currentPath).subscribe((val) => (content = val));
522+
if (content !== null) {
523+
newBackend.write(currentPath, content).subscribe();
524+
}
525+
}
528526

529527
super(newBackend);
530528

529+
// Add actions that match the filter to new tree
531530
for (const action of tree.actions) {
532531
if (!filter(action.path)) {
533532
continue;

0 commit comments

Comments
 (0)