angular-docs-cn/modules/@angular/compiler/testing/pipe_resolver_mock.ts

46 lines
1.3 KiB
TypeScript
Raw Normal View History

2016-07-28 09:40:50 -04:00
/**
* @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 {PipeResolver} from '@angular/compiler';
2016-07-28 09:40:50 -04:00
import {Compiler, Injectable, Injector, PipeMetadata, Type} from '@angular/core';
import {Map} from './facade/collection';
2016-07-28 09:40:50 -04:00
@Injectable()
export class MockPipeResolver extends PipeResolver {
private _pipes = new Map<Type<any>, PipeMetadata>();
2016-07-28 09:40:50 -04:00
constructor(private _injector: Injector) { super(); }
private get _compiler(): Compiler { return this._injector.get(Compiler); }
private _clearCacheFor(pipe: Type<any>) { this._compiler.clearCacheFor(pipe); }
2016-07-28 09:40:50 -04:00
/**
* Overrides the {@link PipeMetadata} for a pipe.
*/
setPipe(type: Type<any>, metadata: PipeMetadata): void {
2016-07-28 09:40:50 -04:00
this._pipes.set(type, metadata);
this._clearCacheFor(type);
}
/**
* Returns the {@link PipeMetadata} for a pipe:
* - Set the {@link PipeMetadata} to the overridden view when it exists or fallback to the
* default
* `PipeResolver`, see `setPipe`.
*/
resolve(type: Type<any>, throwIfNotFound = true): PipeMetadata {
2016-07-28 09:40:50 -04:00
var metadata = this._pipes.get(type);
if (!metadata) {
metadata = super.resolve(type, throwIfNotFound);
}
return metadata;
}
}