Skip to content

Commit

Permalink
fix(all): ts strict (part 3)
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat committed Mar 20, 2018
1 parent 28215a3 commit 06ad60e
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export interface Animation {


export interface AnimationBuilder {
(Animation: Animation, baseEl?: HTMLElement, opts?: any): Promise<Animation>;
(Animation: Animation, baseEl: HTMLElement, opts?: any): Promise<Animation>;
}

export interface PlayOptions {
Expand Down
7 changes: 3 additions & 4 deletions core/src/components/nav/nav-util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ViewController, isViewController } from './view-controller';
import { Animation, FrameworkDelegate } from '../..';

export function convertToView(page: any, params: any): ViewController {
export function convertToView(page: any, params: any): ViewController|null {
if (!page) {
return null;
}
Expand All @@ -20,8 +20,7 @@ export function convertToViews(pages: any[]): ViewController[] {
return convertToView(page.page, page.params);
}
return convertToView(page, undefined);
})
.filter(v => v !== null);
}).filter(v => v !== null) as ViewController[];
}

export function isPresent(val: any): val is any {
Expand Down Expand Up @@ -80,7 +79,7 @@ export interface TransitionDoneFn {
}

export interface TransitionInstruction {
opts: NavOptions;
opts: NavOptions|undefined;
insertStart?: number;
insertViews?: any[];
removeView?: ViewController;
Expand Down
37 changes: 18 additions & 19 deletions core/src/components/nav/nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export class NavControllerBase implements NavOutlet {
popAll(): Promise<boolean[]> {
const promises: Promise<boolean>[] = [];
for (let i = this._views.length - 1; i >= 0; i--) {
promises.push(this.pop(null));
promises.push(this.pop(undefined));
}
return Promise.all(promises);
}
Expand Down Expand Up @@ -235,24 +235,24 @@ export class NavControllerBase implements NavOutlet {
}

@Method()
getRouteId(): RouteID|null {
getRouteId(): RouteID|undefined {
const active = this.getActive();
if (active) {
return {
id: active.element.tagName,
params: active.data
};
}
return null;
return undefined;
}

@Method()
getContainerEl(): HTMLElement {
getContainerEl(): HTMLElement|undefined {
const active = this.getActive();
if (active) {
return active.element;
}
return null;
return undefined;
}

@Method()
Expand All @@ -276,10 +276,10 @@ export class NavControllerBase implements NavOutlet {
}

@Method()
getPrevious(view = this.getActive()): ViewController {
getPrevious(view = this.getActive()): ViewController|undefined {
const views = this._views;
const index = views.indexOf(view);
return (index > 0) ? views[index - 1] : null;
return (index > 0) ? views[index - 1] : undefined;
}

@Method()
Expand All @@ -291,7 +291,7 @@ export class NavControllerBase implements NavOutlet {
* Return a view controller
*/
@Method()
getViewById(id: string): ViewController {
getViewById(id: string): ViewController|undefined {
return this._views.find(vc => vc.id === id);
}

Expand All @@ -313,7 +313,7 @@ export class NavControllerBase implements NavOutlet {
// 7. _transitionStart(): called once the transition actually starts, it initializes the Animation underneath.
// 8. _transitionFinish(): called once the transition finishes
// 9. _cleanup(): syncs the navigation internal state with the DOM. For example it removes the pages from the DOM or hides/show them.
private _queueTrns(ti: TransitionInstruction, done: TransitionDoneFn): Promise<boolean> {
private _queueTrns(ti: TransitionInstruction, done: TransitionDoneFn|undefined): Promise<boolean> {
const promise = new Promise<boolean>((resolve, reject) => {
ti.resolve = resolve;
ti.reject = reject;
Expand Down Expand Up @@ -349,7 +349,7 @@ export class NavControllerBase implements NavOutlet {
const isPop = result.direction === NavDirection.back;
if (this.useRouter) {
const router = document.querySelector('ion-router');
router.navChanged(isPop);
router && router.navChanged(isPop);
}

this.ionNavChanged.emit({isPop});
Expand Down Expand Up @@ -438,7 +438,7 @@ export class NavControllerBase implements NavOutlet {
private _prepareTI(ti: TransitionInstruction) {
const viewsLength = this._views.length;

if (isPresent(ti.removeView)) {
if (ti.removeView != null) {
assert(isPresent(ti.removeStart), 'removeView needs removeStart');
assert(isPresent(ti.removeCount), 'removeView needs removeCount');

Expand All @@ -448,7 +448,7 @@ export class NavControllerBase implements NavOutlet {
}
ti.removeStart += index;
}
if (isPresent(ti.removeStart)) {
if (ti.removeStart != null) {
if (ti.removeStart < 0) {
ti.removeStart = (viewsLength - 1);
}
Expand Down Expand Up @@ -523,7 +523,7 @@ export class NavControllerBase implements NavOutlet {
const insertViews = ti.insertViews;
const removeStart = ti.removeStart;
const removeCount = ti.removeCount;
let destroyQueue: ViewController[];
let destroyQueue: ViewController[] = undefined;

// there are views to remove
if (isPresent(removeStart)) {
Expand Down Expand Up @@ -597,7 +597,7 @@ export class NavControllerBase implements NavOutlet {
}
}

private _transition(enteringView: ViewController, leavingView: ViewController, ti: TransitionInstruction): Promise<NavResult> {
private async _transition(enteringView: ViewController, leavingView: ViewController, ti: TransitionInstruction): Promise<NavResult> {
if (!ti.requiresTransition) {
// transition is not required, so we are already done!
// they're inserting/removing the views somewhere in the middle or
Expand Down Expand Up @@ -645,12 +645,11 @@ export class NavControllerBase implements NavOutlet {
enteringEl,
leavingEl
};
return transition(animationOpts)
.then(trns => this._transitionFinish(trns, enteringView, leavingView, ti.opts));
const trns = await transition(animationOpts);
return this._transitionFinish(trns, enteringView, leavingView, ti.opts);
}

private _transitionFinish(transition: Animation, enteringView: ViewController, leavingView: ViewController, opts: NavOptions): NavResult {

private _transitionFinish(transition: Animation|void, enteringView: ViewController, leavingView: ViewController, opts: NavOptions): NavResult {
const hasCompleted = transition ? transition.hasCompleted : true;

if (hasCompleted) {
Expand Down Expand Up @@ -777,7 +776,7 @@ export class NavControllerBase implements NavOutlet {
removeStart: -1,
removeCount: 1,
opts: opts,
}, null);
}, undefined);
}

private swipeBackProgress(detail: GestureDetail) {
Expand Down
56 changes: 27 additions & 29 deletions core/src/components/nav/transition.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NavDirection } from './nav-util';
import { Animation, AnimationBuilder } from '../..';

export function transition(opts: AnimationOptions): Promise<Animation|undefined> {
export async function transition(opts: AnimationOptions): Promise<Animation|void> {
const enteringEl = opts.enteringEl;
const leavingEl = opts.leavingEl;

Expand All @@ -15,51 +15,49 @@ export function transition(opts: AnimationOptions): Promise<Animation|undefined>
}

// transition path
return waitDeepReady(opts)
.then(() => fireWillEvents(enteringEl, leavingEl))
.then(() => createTransition(opts))
.then((transition) => playTransition(transition, opts))
.then((transition) => {
if (transition.hasCompleted) {
fireDidEvents(enteringEl, leavingEl);
}
return transition;
});
await waitDeepReady(opts);
const transition = await createTransition(opts);
fireWillEvents(enteringEl, leavingEl);
await playTransition(transition, opts);
if (transition.hasCompleted) {
fireDidEvents(enteringEl, leavingEl);
}
return transition;
}

function notifyViewReady(viewIsReady: undefined | (() => Promise<any>)) {
async function notifyViewReady(viewIsReady: undefined | (() => Promise<any>)) {
if (viewIsReady) {
return viewIsReady();
await viewIsReady();
}
return Promise.resolve();
}

function noAnimation(opts: AnimationOptions) {
async function noAnimation(opts: AnimationOptions) {
const enteringEl = opts.enteringEl;
const leavingEl = opts.leavingEl;

enteringEl && enteringEl.classList.remove('hide-page');
leavingEl && leavingEl.classList.remove('hide-page');

return waitShallowReady(opts).then(() => {
fireWillEvents(enteringEl, leavingEl);
fireDidEvents(enteringEl, leavingEl);
return undefined;
});
await waitShallowReady(opts);

fireWillEvents(enteringEl, leavingEl);
fireDidEvents(enteringEl, leavingEl);
}

function waitDeepReady(opts: AnimationOptions) {
return Promise.all([
async function waitDeepReady(opts: AnimationOptions) {
await Promise.all([
deepReady(opts.enteringEl),
deepReady(opts.leavingEl)
]).then(() => notifyViewReady(opts.viewIsReady));
]);
await notifyViewReady(opts.viewIsReady);
}

function waitShallowReady(opts: AnimationOptions) {
return Promise.all([
async function waitShallowReady(opts: AnimationOptions) {
await Promise.all([
shallowReady(opts.enteringEl),
shallowReady(opts.leavingEl)
]).then(() => notifyViewReady(opts.viewIsReady));
]);
await notifyViewReady(opts.viewIsReady);
}

function showPages(enteringEl: HTMLElement, leavingEl: HTMLElement) {
Expand Down Expand Up @@ -140,14 +138,14 @@ export function lifecycle(el: HTMLElement, lifecycle: ViewLifecycle) {
}
}

function shallowReady(el: HTMLElement): Promise<any> {
function shallowReady(el: Element): Promise<any> {
if (el && (el as any).componentOnReady) {
return (el as any).componentOnReady();
}
return Promise.resolve();
}

function deepReady(el: HTMLElement): Promise<any> {
function deepReady(el: Element): Promise<any> {
if (!el) {
return Promise.resolve();
}
Expand All @@ -158,7 +156,7 @@ function deepReady(el: HTMLElement): Promise<any> {
}
}

export enum ViewLifecycle {
export const enum ViewLifecycle {
WillEnter = 'ionViewWillEnter',
DidEnter = 'ionViewDidEnter',
WillLeave = 'ionViewWillLeave',
Expand Down
19 changes: 11 additions & 8 deletions core/src/components/router/utils/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ export function writeNavState(root: HTMLElement, chain: RouteChain|null, index:
? writeNavState(nextEl, chain, index + 1, direction)
: Promise.resolve(direction === 0);

if (result.markVisible) {
return promise.then((c) => {
return promise.then((c) => {
if (result.markVisible) {
result.markVisible();
return c;
});
}
return promise;
}
return c;
});
});
}

export function readNavState(node: HTMLElement) {
export function readNavState(root: HTMLElement) {
const ids: RouteID[] = [];
let pivot: NavOutlet|null;
let node: HTMLElement|undefined = root;
while (true) {
pivot = searchNavNode(node);
if (pivot) {
Expand All @@ -53,7 +53,10 @@ export function readNavState(node: HTMLElement) {

const QUERY = ':not([no-router]) ion-nav,:not([no-router]) ion-tabs';

function searchNavNode(root: HTMLElement): NavOutletElement {
function searchNavNode(root: HTMLElement|undefined): NavOutletElement|null {
if (!root) {
return null;
}
if (root.matches(QUERY)) {
return root as NavOutletElement;
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/components/router/utils/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

export interface NavOutlet {
setRouteId(id: string, data: any, direction: number): Promise<RouteWrite>;
getRouteId(): RouteID|null;
getRouteId(): RouteID|undefined;

getContainerEl(): HTMLElement | null;
getContainerEl(): HTMLElement | undefined;
}

export interface RouterEventDetail {
Expand Down
6 changes: 3 additions & 3 deletions core/src/components/tabs/tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,13 @@ export class Tabs implements NavOutlet {
}

@Method()
getRouteId(): RouteID|null {
getRouteId(): RouteID|undefined {
const id = this.selectedTab && this.selectedTab.getTabId();
return id ? {id} : null;
return id ? {id} : undefined;
}

@Method()
getContainerEl(): HTMLElement {
getContainerEl(): HTMLElement|undefined {
return this.selectedTab;
}

Expand Down
8 changes: 4 additions & 4 deletions core/src/utils/overlays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,16 @@ export function attachComponent(delegate: FrameworkDelegate, container: Element,
export function eventMethod<T>(element: HTMLElement, eventName: string, callback?: (detail: T) => void): Promise<T> {
let resolve: Function;
const promise = new Promise<T>(r => resolve = r);
onceEvent(element, eventName, (event) => {
onceEvent(element, eventName, (event: any) => {
const detail = event.detail;
callback && callback(detail);
resolve(detail);
});
return promise;
}

export function onceEvent(element: HTMLElement, eventName: string, callback: (ev: CustomEvent) => void) {
const handler = (ev: CustomEvent) => {
export function onceEvent(element: HTMLElement, eventName: string, callback: (ev: Event) => void) {
const handler = (ev: Event) => {
element.removeEventListener(eventName, handler);
callback(ev);
};
Expand All @@ -178,7 +178,7 @@ function closeKeyboard() {
activeElement && activeElement.blur && activeElement.blur();
}

export function isCancel(role: string): boolean {
export function isCancel(role: string|undefined): boolean {
return role === 'cancel' || role === BACKDROP;
}

Expand Down
2 changes: 1 addition & 1 deletion core/stencil.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ exports.config = {
{ components: ['ion-popover', 'ion-popover-controller'] },
{ components: ['ion-radio', 'ion-radio-group'] },
{ components: ['ion-reorder', 'ion-reorder-group'] },
{ components: ['ion-route', 'ion-router'] },
{ components: ['ion-router', 'ion-route', 'ion-route-redirect'] },
{ components: ['ion-searchbar'] },
{ components: ['ion-segment', 'ion-segment-button'] },
{ components: ['ion-select', 'ion-select-option', 'ion-select-popover'] },
Expand Down
Loading

0 comments on commit 06ad60e

Please sign in to comment.