Skip to content

Commit

Permalink
Activate strictBindCallApply and strictFunctionTypes compiler rules (P…
Browse files Browse the repository at this point in the history
…olymerLabs#2880)

* Fix firebase strictFunctionTypes violation
* Activate strictBindCallApply and strictFunctionTypes compiler rules
 - Improves types in api-channel and slot-consumer
 - Introduce option types for startRender and stopRender
 - Fix mismatched Map/object types in providedSlots
 - Use specific types in Literalizer
  • Loading branch information
lindner authored and Scott J Miles committed Apr 23, 2019
1 parent effb058 commit a008a56
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 17 deletions.
2 changes: 2 additions & 0 deletions config/tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"noImplicitThis": true,
"module": "es2015",
"moduleResolution": "Node",
"strictBindCallApply": true,
"strictFunctionTypes": true,

"allowJs": false,
"sourceMap": true,
Expand Down
26 changes: 18 additions & 8 deletions src/runtime/api-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ import {Arc} from './arc.js';
import {DevtoolsConnection} from './debug/devtools-connection.js';
import {OuterPortAttachment} from './debug/outer-port-attachment.js';
import {Handle} from './handle.js';
import {ParticleSpec} from './particle-spec.js';
import {ParticleSpec, SerializedParticleSpec} from './particle-spec.js';
import {Particle} from './particle.js';
import * as recipeHandle from './recipe/handle.js';
import * as recipeParticle from './recipe/particle.js';
import {StorageProxy} from './storage-proxy.js';
import {SerializedModelEntry} from './storage/crdt-collection-model.js';
import {StorageProviderBase} from './storage/storage-provider-base.js';
import {Type} from './type.js';
import {PropagatedException} from './arc-exceptions.js';
import {Type, TypeLiteral} from './type.js';
import {PropagatedException, SerializedPropagatedException} from './arc-exceptions.js';

enum MappingType {Mapped, LocalMapped, RemoteMapped, Direct, ObjectMap, List, ByLiteral}

Expand All @@ -32,15 +32,25 @@ interface MappingInfo {
redundant?: boolean;
value?: MappingInfo;
key?: MappingInfo;
converter?: Literalizer;
converter?: Literalizer | LiteralizerParticleSpec | LiteralizerPropagatedException;
identifier?: boolean;
ignore?: boolean;
}

// TODO(shans): are there better types that I can use for this?
interface Literalizer {
prototype: {toLiteral: () => {}};
fromLiteral: ({}) => {};
prototype: {toLiteral: () => TypeLiteral};
fromLiteral: (typeliteral: TypeLiteral) => Type;
}

interface LiteralizerParticleSpec {
prototype: {toLiteral: () => SerializedParticleSpec};
fromLiteral: (spec: SerializedParticleSpec) => ParticleSpec;
}

interface LiteralizerPropagatedException {
prototype: {toLiteral: () => SerializedPropagatedException};
fromLiteral: (exception: SerializedPropagatedException) => PropagatedException;
}

const targets = new Map<{}, Map<string, MappingInfo[]>>();
Expand All @@ -67,7 +77,7 @@ function Mapped(target: {}, propertyKey: string, parameterIndex: number) {
set(target.constructor, propertyKey, parameterIndex, {type: MappingType.Mapped});
}

function ByLiteral(constructor: Literalizer) {
function ByLiteral(constructor: Literalizer | LiteralizerParticleSpec | LiteralizerPropagatedException) {
return (target: {}, propertyKey: string, parameterIndex: number) => {
const info: MappingInfo = {type: MappingType.ByLiteral, converter: constructor};
set(target.constructor, propertyKey, parameterIndex, info);
Expand Down Expand Up @@ -415,7 +425,7 @@ export abstract class PECOuterPort extends APIPort {
UIEvent(@Mapped particle: recipeParticle.Particle, @Direct slotName: string, @Direct event: {}) {}
SimpleCallback(@RemoteMapped callback: number, @Direct data: {}) {}
AwaitIdle(@Direct version: number) {}
StartRender(@Mapped particle: recipeParticle.Particle, @Direct slotName: string, @ObjectMap(MappingType.Direct, MappingType.Direct) providedSlots: {[index: string]: string}, @List(MappingType.Direct) contentTypes: string[]) {}
StartRender(@Mapped particle: recipeParticle.Particle, @Direct slotName: string, @ObjectMap(MappingType.Direct, MappingType.Direct) providedSlots: Map<string, string>, @List(MappingType.Direct) contentTypes: string[]) {}
StopRender(@Mapped particle: recipeParticle.Particle, @Direct slotName: string) {}

abstract onRender(particle: recipeParticle.Particle, slotName: string, content: string);
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/arc-exceptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {Particle} from './particle';
* http://polymer.github.io/PATENTS.txt
*/

type SerializedPropagatedException = {
export type SerializedPropagatedException = {
exceptionType: string,
cause: {name: string, message: string, stack: string}, // Serialized Error.
method: string,
Expand Down
16 changes: 14 additions & 2 deletions src/runtime/particle-execution-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ import {SlotComposer} from './slot-composer.js';
import {StorageProviderBase} from './storage/storage-provider-base.js';
import {Type} from './type.js';

export type StartRenderOptions = {
particle: Particle;
slotName: string;
providedSlots: Map<string, string>;
contentTypes: string[];
};

export type StopRenderOptions = {
particle: Particle;
slotName: string;
};

export class ParticleExecutionHost {
private _apiPort : PECOuterPort;
close : () => void;
Expand Down Expand Up @@ -281,11 +293,11 @@ export class ParticleExecutionHost {
this._apiPort.InstantiateParticle(particle, particle.id.toString(), particle.spec, stores);
}

startRender({particle, slotName, providedSlots, contentTypes}: {particle: Particle, slotName: string, providedSlots: {[index: string]: string}, contentTypes: string[]}) {
startRender({particle, slotName, providedSlots, contentTypes}: StartRenderOptions): void {
this._apiPort.StartRender(particle, slotName, providedSlots, contentTypes);
}

stopRender({particle, slotName}: {particle: Particle, slotName: string}) {
stopRender({particle, slotName}: StopRenderOptions): void {
this._apiPort.StopRender(particle, slotName);
}

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/particle-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export class ProvideSlotConnectionSpec {
}
}

type SerializedParticleSpec = {
export type SerializedParticleSpec = {
name: string,
id?: string,
verbs: string[],
Expand Down
10 changes: 7 additions & 3 deletions src/runtime/slot-consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import {assert} from '../platform/assert-web.js';

import {Arc} from './arc.js';
import {Description} from './description.js';
import {Particle} from './recipe/particle.js';
import {SlotConnection} from './recipe/slot-connection.js';
import {HostedSlotContext, ProvidedSlotContext, SlotContext} from './slot-context.js';
import {StartRenderOptions, StopRenderOptions} from './particle-execution-host.js';

export interface Content {
templateName?: string | Map<string, string>;
Expand Down Expand Up @@ -41,8 +43,8 @@ export class SlotConsumer {
slotContext: SlotContext;
readonly directlyProvidedSlotContexts: ProvidedSlotContext[] = [];
readonly hostedSlotContexts: HostedSlotContext[] = [];
startRenderCallback: ({}) => void;
stopRenderCallback: ({}) => void;
startRenderCallback: (options: StartRenderOptions) => void;
stopRenderCallback: (options: StopRenderOptions) => void;
eventHandler: ({}) => void;
readonly containerKind?: string;
// Contains `container` and other modality specific rendering information
Expand Down Expand Up @@ -149,10 +151,12 @@ export class SlotConsumer {

startRender() {
if (this.consumeConn && this.startRenderCallback) {
const providedSlots = new Map(this.allProvidedSlotContexts.map(context => ([context.name, context.id] as [string, string])));

this.startRenderCallback({
particle: this.consumeConn.particle,
slotName: this.consumeConn.name,
providedSlots: new Map(this.allProvidedSlotContexts.map(context => ([context.name, context.id] as [string, string]))),
providedSlots,
contentTypes: this.constructRenderRequest()
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/storage/firebase/firebase-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ class FirebaseVariable extends FirebaseStorageProvider implements VariableStorag
private pendingWrites: {storageKey: string, value: {}}[] = [];
wasConnect: boolean; // for debugging
private resolveInitialized: () => void;
private readonly valueChangeCallback: ({}) => void;
private readonly valueChangeCallback: (dataSnapshot: firebase.database.DataSnapshot, s?: string) => void;

constructor(type, storageEngine, id, reference, firebaseKey, shouldExist) {
super(type, storageEngine, id, reference, firebaseKey);
Expand Down Expand Up @@ -651,7 +651,7 @@ class FirebaseCollection extends FirebaseStorageProvider implements CollectionSt
private pendingWrites: {value: {}, storageKey: string}[] = [];
private resolveInitialized: () => void;
private localKeyId = Date.now();
private readonly valueChangeCallback: ({}) => void;
private readonly valueChangeCallback: (dataSnapshot: firebase.database.DataSnapshot, s?: string) => void;

constructor(type, storageEngine, id, reference, firebaseKey) {
super(type, storageEngine, id, reference, firebaseKey);
Expand Down

0 comments on commit a008a56

Please sign in to comment.