Skip to content

Commit

Permalink
Typescript and Engines support (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseto authored and offirgolan committed Oct 31, 2018
1 parent 1b0782e commit 9f8d57b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
30 changes: 30 additions & 0 deletions addon/initializers/ember-parachute.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ParachuteEvent from '../-private/parachute-event';
import lookupController from '../utils/lookup-controller';

const {
RSVP,
run,
assign,
canInvoke,
Expand Down Expand Up @@ -72,6 +73,35 @@ export function initialize(/* application */) {
}
},

/**
* For Engines support. `transition.handlerInfos` is used to compute
* the query params that will be injected into a controller. In lazily
* loaded engines, handlerInfos may be promises that don't contain the required
* information. Resolve them here to guarantee parachute can properly function.
*
* @method deserialize
* @param {Object} params the parameters extracted from the URL
* @param {Transition} transition
* @returns {Promise<any>} The model for this route
*/
deserialize(params, transition) {
// Check if handlers have already been loaded.
// If so, don't return a promise as it will result in
// the loading screen/state flashing.
if (!transition.handlerInfos.find(x => !x.handler)) {
return this._super(params, transition);
}

// Save and bind the refence to the super here
// as this._super doesn't work in callbacks
// https://github.com/emberjs/ember.js/issues/15291
const actualSuper = this._super.bind(this);

return RSVP.all(
transition.handlerInfos.map(x => x.handlerPromise)
).then(() => actualSuper(params, transition));
},

/**
* Serialize query param value if a given query param has a `serialize`
* method.
Expand Down
43 changes: 43 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
declare module 'ember-parachute' {
interface QueryParamOption<T> {
as?: string;
defaultValue: T;
refresh?: boolean;
replace?: boolean;
scope?: 'controller';
serialize?(value: T): string;
deserialize?(value: string): T;
}

type QueryParamOptions < T > = {
[K in keyof T]: QueryParamOption<T[K]>;
};

type QueryParamsState < T > = {
[K in keyof T]: {
value: T[K];
default: T[K];
changed: boolean;
}
};

export interface ParachuteEvent<T> {
changes: T;
changed: T;
queryParams: T;
routeName: string;
shouldRefresh: boolean;
}

export class QueryParamMixin<T> {
queryParamsState: QueryParamsState<T>;
setup(queryParamsChangedEvent: ParachuteEvent<T>): void;
queryParamsDidChange(queryParamsChangedEvent: ParachuteEvent<T>): void;
reset(queryParamsChangedEvent: ParachuteEvent<T>, isExiting: boolean): void;
}

export default class QueryParams<T> {
constructor(...params: Array<QueryParamOptions<T>>);
Mixin: QueryParamMixin<T> & T;
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"doc": "doc",
"test": "tests"
},
"types": "index.d.ts",
"repository": "https://github.com/offirgolan/ember-parachute",
"bugs": "https://github.com/offirgolan/ember-parachute/issues",
"homepage": "https://github.com/offirgolan/ember-parachute",
Expand Down

0 comments on commit 9f8d57b

Please sign in to comment.