refactor(@angular-devkit/core): allow creation of a host from a non-observable source

This commit is contained in:
Charles Lyding 2020-08-18 12:07:43 -04:00 committed by Alan Agius
parent af5a1db467
commit 7045cee223
3 changed files with 97 additions and 0 deletions

View File

@ -229,6 +229,8 @@ export declare class CoreSchemaRegistry implements SchemaRegistry {
useXDeprecatedProvider(onUsage: (message: string) => void): void;
}
export declare function createSyncHost<StatsT extends object = {}>(handler: SyncHostHandler<StatsT>): Host<StatsT>;
export declare function createWorkspaceHost(host: virtualFs.Host): WorkspaceHost;
export interface CustomDimensionsAndMetricsOptions {
@ -1048,6 +1050,18 @@ export declare class SyncDelegateHost<T extends object = {}> {
write(path: Path, content: FileBufferLike): void;
}
export interface SyncHostHandler<StatsT extends object = {}> {
delete(path: Path): void;
exists(path: Path): boolean;
isDirectory(path: Path): boolean;
isFile(path: Path): boolean;
list(path: Path): PathFragment[];
read(path: Path): FileBuffer;
rename(from: Path, to: Path): void;
stat(path: Path): Stats<StatsT> | null;
write(path: Path, content: FileBufferLike): void;
}
export declare class SynchronousDelegateExpectedException extends BaseException {
constructor();
}

View File

@ -0,0 +1,82 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { Observable } from 'rxjs';
import { Path, PathFragment } from '../path';
import { FileBuffer, FileBufferLike, Host, HostCapabilities, Stats } from './interface';
export interface SyncHostHandler<StatsT extends object = {}> {
read(path: Path): FileBuffer;
list(path: Path): PathFragment[];
exists(path: Path): boolean;
isDirectory(path: Path): boolean;
isFile(path: Path): boolean;
stat(path: Path): Stats<StatsT> | null;
write(path: Path, content: FileBufferLike): void;
delete(path: Path): void;
rename(from: Path, to: Path): void;
}
function wrapAction<T>(action: () => T): Observable<T> {
return new Observable((subscriber) => {
subscriber.next(action());
subscriber.complete();
});
}
export function createSyncHost<StatsT extends object = {}>(
handler: SyncHostHandler<StatsT>,
): Host<StatsT> {
return new (class {
get capabilities(): HostCapabilities {
return { synchronous: true };
}
read(path: Path): Observable<FileBuffer> {
return wrapAction(() => handler.read(path));
}
list(path: Path): Observable<PathFragment[]> {
return wrapAction(() => handler.list(path));
}
exists(path: Path): Observable<boolean> {
return wrapAction(() => handler.exists(path));
}
isDirectory(path: Path): Observable<boolean> {
return wrapAction(() => handler.isDirectory(path));
}
isFile(path: Path): Observable<boolean> {
return wrapAction(() => handler.isFile(path));
}
stat(path: Path): Observable<Stats<StatsT> | null> {
return wrapAction(() => handler.stat(path));
}
write(path: Path, content: FileBufferLike): Observable<void> {
return wrapAction(() => handler.write(path, content));
}
delete(path: Path): Observable<void> {
return wrapAction(() => handler.delete(path));
}
rename(from: Path, to: Path): Observable<void> {
return wrapAction(() => handler.rename(from, to));
}
watch(): null {
return null;
}
})();
}

View File

@ -8,6 +8,7 @@
export * from './alias';
export * from './buffer';
export * from './create';
export * from './empty';
export * from './interface';
export * from './memory';