Skip to content

Commit

Permalink
refactor: several code improvements
Browse files Browse the repository at this point in the history
- use Sets instead of Arrays in Nodes for faster lookups
- Use flatMap instead of flatten
  • Loading branch information
alan-agius4 committed May 5, 2021
1 parent 426d653 commit a312558
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/lib/graph/build-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class BuildGraph implements Traversable<Node> {
if (this.store.has(node.url)) {
// Clean up dependee references
const oldNode = this.store.get(node.url);
oldNode['_dependees'] = oldNode['_dependees'].filter(node => node !== oldNode);
oldNode['_dependees'].delete(oldNode);
}

this.store.set(node.url, node);
Expand Down
22 changes: 11 additions & 11 deletions src/lib/graph/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,40 @@ export class Node {
public state: NodeState = '';

public filter(by: (value: Node, index: number) => boolean): Node[] {
return this._dependents.filter(by);
return [...this._dependents].filter(by);
}

public find(by: (value: Node, index: number) => boolean): Node | undefined {
return this._dependents.find(by);
return [...this._dependents].find(by);
}

public some(by: (value: Node, index: number) => boolean): boolean {
return this._dependents.some(by);
return [...this._dependents].some(by);
}

public get dependents(): Node[] {
public get dependents(): Set<Node> {
return this._dependents;
}

public get dependees(): Node[] {
public get dependees(): Set<Node> {
return this._dependees;
}

private _dependents: Node[] = [];
private _dependees: Node[] = [];
private _dependents = new Set<Node>();
private _dependees = new Set<Node>();

/** @experimental DO NOT USE. For time being, dirty checking is for `type=entryPoint && state !== 'done'` (full rebuild of entry point). */
public dependsOn(dependent: Node | Node[]) {
const newDeps = dependent instanceof Array ? dependent : [dependent];
const newDeps = Array.isArray(dependent) ? dependent : [dependent];

for (const newDep of newDeps) {
if (newDep._dependees.some(x => x.url === this.url)) {
if (newDep._dependees.has(this)) {
// nodes already depends on each other
continue;
}

newDep._dependees.push(this);
this._dependents.push(newDep);
newDep._dependees.add(this);
this._dependents.add(newDep);
}
}
}
40 changes: 16 additions & 24 deletions src/lib/ng-package/package.transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import {
import { discoverPackages } from './discover-packages';
import { createFileWatch } from '../file-system/file-watcher';
import { NgPackagrOptions } from './options.di';
import { flatten } from '../utils/array';
import { ensureUnixPath } from '../utils/path';
import { FileCache } from '../file-system/file-cache';

Expand Down Expand Up @@ -90,7 +89,7 @@ export const packageTransformFactory = (
if (deleteDestPath) {
try {
await rmdir(dest, { recursive: true });
} catch { }
} catch {}
}
},
(graph, _) => graph,
Expand Down Expand Up @@ -143,14 +142,12 @@ const watchTransformFactory = (
return createFileWatch(data.src, [data.dest]).pipe(
tap(fileChange => {
const { filePath, event } = fileChange;
const { sourcesFileCache, ngccProcessingCache } = cache;
const { sourcesFileCache } = cache;
const cachedSourceFile = sourcesFileCache.get(filePath);
const { declarationFileName } = cachedSourceFile || {};
const uriToClean = [filePath, declarationFileName].map(x => fileUrl(ensureUnixPath(x)));
const nodesToClean = graph.filter(node => uriToClean.some(uri => uri === node.url));

ngccProcessingCache.clear();

if (!cachedSourceFile) {
if (event === 'unlink' || event === 'add') {
cache.globCache = regenerateGlobCache(sourcesFileCache);
Expand All @@ -161,25 +158,20 @@ const watchTransformFactory = (
}
}

const allUrlsToClean = new Set<string>(
flatten([
...nodesToClean.map(node => node.url),
// if a non ts file changes we need to clean up its direct dependees
// this is mainly done for resources such as html and css
...nodesToClean
.filter(node => !node.url.endsWith('.ts'))
.map(node => node.dependees.map(dependee => dependee.url)),
]),
);
const allNodesToClean = [
...nodesToClean,
// if a non ts file changes we need to clean up its direct dependees
// this is mainly done for resources such as html and css
...nodesToClean.filter(node => !node.url.endsWith('.ts')).flatMap(node => [...node.dependees]),
];

// delete node that changes
allUrlsToClean.forEach(url => {
for (const { url } of allNodesToClean) {
sourcesFileCache.delete(fileUrlPath(url));
});
}

const entryPoints: EntryPointNode[] = graph.filter(isEntryPoint);
entryPoints.forEach(entryPoint => {
const isDirty = entryPoint.dependents.some(dependent => allUrlsToClean.has(dependent.url));
for (const entryPoint of graph.filter(isEntryPoint)) {
const isDirty = [...allNodesToClean].some(dependent => entryPoint.dependents.has(dependent));
if (isDirty) {
entryPoint.state = 'dirty';
const { metadata } = entryPoint.data.destinationFiles;
Expand All @@ -189,7 +181,7 @@ const watchTransformFactory = (
entryPoint.cache.analysesSourcesFileCache.delete(fileUrlPath(url));
});
}
});
}

// Regenerate glob cache
if (event === 'unlink' || event === 'add') {
Expand Down Expand Up @@ -248,7 +240,7 @@ const writeNpmPackage = (pkgUri: string): Transform =>
let isFile = false;
try {
isFile = (await stat(srcFile)).isFile();
} catch { }
} catch {}

if (isFile) {
await copyFile(srcFile, path.join(data.dest, path.basename(srcFile)));
Expand All @@ -271,10 +263,10 @@ const scheduleEntryPoints = (epTransform: Transform): Transform =>
});

// The array index is the depth.
const groups = depthBuilder.build();
const groups = depthBuilder.build().flatMap(x => x);

// Build entry points with lower depth values first.
return from(flatten(groups)).pipe(
return from(groups).pipe(
map((epUrl: string): EntryPointNode => graph.find(byEntryPoint().and(ep => ep.url === epUrl))),
filter((entryPoint: EntryPointNode): boolean => entryPoint.state !== 'done'),
concatMap(ep =>
Expand Down
4 changes: 0 additions & 4 deletions src/lib/utils/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ export function toArray<T>(value: T | T[]): T[] {
return [].concat(value);
}

export function flatten<T>(value: Array<T | T[]>): T[] {
return [].concat.apply([], value);
}

export function unique<T>(value: T[]): T[] {
return [...new Set(value)];
}
4 changes: 2 additions & 2 deletions src/lib/utils/glob.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as glob from 'glob';
import { promisify } from 'util';
import { flatten, toArray } from './array';
import { toArray } from './array';

const globPromise = promisify(glob);

export async function globFiles(pattern: string | string[], options?: glob.IOptions): Promise<string[]> {
const files = await Promise.all<string[]>(toArray(pattern).map(p => globPromise(p, options)));

return flatten(files);
return files.flatMap(x => x);
}

0 comments on commit a312558

Please sign in to comment.