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