Alex Eagle 208f3d4c65 fix(typings): repair broken type-checking for StringMap
Note that the previous type of StringMap was overly permissive and didn't catch errors.
Also we have to explicitly type empty objects, which is explained here:
https://github.com/Microsoft/TypeScript/issues/5089

Closes #4487
2015-10-03 01:09:42 +00:00

59 lines
1.6 KiB
TypeScript

import {isBlank, isPresent, CONST, Type} from 'angular2/src/core/facade/lang';
import {BaseException, WrappedException} from 'angular2/src/core/facade/exceptions';
import {StringMapWrapper} from 'angular2/src/core/facade/collection';
import {
Injectable,
OptionalMetadata,
SkipSelfMetadata,
Binding,
Injector,
bind
} from 'angular2/src/core/di';
import {PipeBinding} from './pipe_binding';
import * as cd from 'angular2/src/core/change_detection/pipes';
export class ProtoPipes {
static fromBindings(bindings: PipeBinding[]): ProtoPipes {
var config: {[key: string]: PipeBinding} = {};
bindings.forEach(b => config[b.name] = b);
return new ProtoPipes(config);
}
constructor(
/**
* Map of {@link PipeMetadata} names to {@link PipeMetadata} implementations.
*/
public config: {[key: string]: PipeBinding}) {
this.config = config;
}
get(name: string): PipeBinding {
var binding = this.config[name];
if (isBlank(binding)) throw new BaseException(`Cannot find pipe '${name}'.`);
return binding;
}
}
export class Pipes implements cd.Pipes {
_config: {[key: string]: cd.SelectedPipe} = {};
constructor(public proto: ProtoPipes, public injector: Injector) {}
get(name: string): cd.SelectedPipe {
var cached = StringMapWrapper.get(this._config, name);
if (isPresent(cached)) return cached;
var p = this.proto.get(name);
var transform = this.injector.instantiateResolved(p);
var res = new cd.SelectedPipe(transform, p.pure);
if (p.pure) {
StringMapWrapper.set(this._config, name, res);
}
return res;
}
}