fix(platform-browser): Update types for TypeScript nullability support

Closes #15898
This commit is contained in:
Miško Hevery
2017-03-24 09:59:41 -07:00
committed by Tobias Bosch
parent 01d93f3af8
commit 728c9d0632
52 changed files with 169 additions and 154 deletions
@@ -44,7 +44,8 @@ export class ClientMessageBrokerFactory_ extends ClientMessageBrokerFactory {
* @experimental WebWorker support in Angular is experimental.
*/
export abstract class ClientMessageBroker {
abstract runOnService(args: UiArguments, returnType: Type<any>|SerializerTypes): Promise<any>;
abstract runOnService(args: UiArguments, returnType: Type<any>|SerializerTypes|null):
Promise<any>|null;
}
interface PromiseCompleter {
@@ -78,7 +79,7 @@ export class ClientMessageBroker_ extends ClientMessageBroker {
return id;
}
runOnService(args: UiArguments, returnType: Type<any>|SerializerTypes): Promise<any> {
runOnService(args: UiArguments, returnType: Type<any>|SerializerTypes): Promise<any>|null {
const fnArgs: any[] = [];
if (args.args) {
args.args.forEach(argument => {
@@ -90,10 +91,10 @@ export class ClientMessageBroker_ extends ClientMessageBroker {
});
}
let promise: Promise<any>;
let id: string = null;
let promise: Promise<any>|null;
let id: string|null = null;
if (returnType != null) {
let completer: PromiseCompleter;
let completer: PromiseCompleter = undefined !;
promise = new Promise((resolve, reject) => { completer = {resolve, reject}; });
id = this._generateMessageId(args.method);
this._pending.set(id, completer);
@@ -127,12 +128,12 @@ export class ClientMessageBroker_ extends ClientMessageBroker {
private _handleMessage(message: ResponseMessageData): void {
if (message.type === 'result' || message.type === 'error') {
const id = message.id;
const id = message.id !;
if (this._pending.has(id)) {
if (message.type === 'result') {
this._pending.get(id).resolve(message.value);
this._pending.get(id) !.resolve(message.value);
} else {
this._pending.get(id).reject(message.value);
this._pending.get(id) !.reject(message.value);
}
this._pending.delete(id);
}
@@ -34,5 +34,7 @@ export class RenderStore {
return this._lookupById.has(id) ? this._lookupById.get(id) : null;
}
serialize(obj: any): number { return obj == null ? null : this._lookupByObject.get(obj); }
serialize(obj: any): number|null|undefined {
return obj == null ? null : this._lookupByObject.get(obj);
}
}
@@ -33,7 +33,7 @@ export const PRIMITIVE = SerializerTypes.PRIMITIVE;
export class LocationType {
constructor(
public href: string, public protocol: string, public host: string, public hostname: string,
public port: string, public pathname: string, public search: string, public hash: string,
public port: string, public pathname: string|null, public search: string, public hash: string,
public origin: string) {}
}
@@ -49,7 +49,7 @@ export class Serializer {
return obj.map(v => this.serialize(v, type));
}
if (type === SerializerTypes.RENDER_STORE_OBJECT) {
return this._renderStore.serialize(obj);
return this._renderStore.serialize(obj) !;
}
if (type === RenderComponentType) {
return this._serializeRenderComponentType(obj);
@@ -47,7 +47,7 @@ export class ServiceMessageBrokerFactory_ extends ServiceMessageBrokerFactory {
*/
export abstract class ServiceMessageBroker {
abstract registerMethod(
methodName: string, signature: Array<Type<any>|SerializerTypes>, method: Function,
methodName: string, signature: Array<Type<any>|SerializerTypes>|null, method: Function,
returnType?: Type<any>|SerializerTypes): void;
}
@@ -83,7 +83,7 @@ export class ServiceMessageBroker_ extends ServiceMessageBroker {
private _handleMessage(message: ReceivedMessage): void {
if (this._methods.has(message.method)) {
this._methods.get(message.method)(message);
this._methods.get(message.method) !(message);
}
}
@@ -18,7 +18,7 @@ export class WebWorkerPlatformLocation extends PlatformLocation {
private _broker: ClientMessageBroker;
private _popStateListeners: Array<Function> = [];
private _hashChangeListeners: Array<Function> = [];
private _location: LocationType = null;
private _location: LocationType = null !;
private _channelSource: EventEmitter<Object>;
public initialized: Promise<any>;
private initializedResolve: () => void;
@@ -31,7 +31,7 @@ export class WebWorkerPlatformLocation extends PlatformLocation {
this._channelSource.subscribe({
next: (msg: {[key: string]: any}) => {
let listeners: Array<Function> = null;
let listeners: Array<Function>|null = null;
if (msg.hasOwnProperty('event')) {
const type: string = msg['event']['type'];
if (type === 'popstate') {
@@ -55,14 +55,13 @@ export class WebWorkerPlatformLocation extends PlatformLocation {
init(): Promise<boolean> {
const args: UiArguments = new UiArguments('getLocation');
return this._broker.runOnService(args, LocationType)
.then(
(val: LocationType) => {
this._location = val;
this.initializedResolve();
return true;
},
err => { throw new Error(err); });
return this._broker.runOnService(args, LocationType) !.then(
(val: LocationType) => {
this._location = val;
this.initializedResolve();
return true;
},
err => { throw new Error(err); });
}
getBaseHrefFromDOM(): string {
@@ -74,11 +73,11 @@ export class WebWorkerPlatformLocation extends PlatformLocation {
onHashChange(fn: LocationChangeListener): void { this._hashChangeListeners.push(fn); }
get pathname(): string { return this._location ? this._location.pathname : null; }
get pathname(): string { return this._location ? this._location.pathname ! : '<unknown>'; }
get search(): string { return this._location ? this._location.search : null; }
get search(): string { return this._location ? this._location.search : '<unknown>'; }
get hash(): string { return this._location ? this._location.hash : null; }
get hash(): string { return this._location ? this._location.hash : '<unknown>'; }
set pathname(newPath: string) {
if (this._location === null) {
@@ -67,7 +67,7 @@ export class WebWorkerRendererFactory2 implements RendererFactory2 {
source.subscribe({next: (message: any) => this._dispatchEvent(message)});
}
createRenderer(element: any, type: RendererType2): Renderer2 {
createRenderer(element: any, type: RendererType2|null): Renderer2 {
const renderer = new WebWorkerRenderer2(this);
const id = this.renderStore.allocateId();
@@ -277,9 +277,9 @@ export class WebWorkerRenderer2 implements Renderer2 {
listener: (event: any) => boolean): () => void {
const unlistenId = this._rendererFactory.allocateId();
const [targetEl, targetName, fullName]: [any, string, string] = typeof target === 'string' ?
[null, target, `${target}:${eventName}`] :
[target, null, null];
const [targetEl, targetName, fullName]: [any, string | null, string | null] =
typeof target === 'string' ? [null, target, `${target}:${eventName}`] :
[target, null, null];
if (fullName) {
this._rendererFactory.globalEvents.listen(fullName, listener);