UNPKG

3.85 kBTypeScriptView Raw
1import { TransitionHookOptions, HookResult } from './interface';
2import { Transition } from './transition';
3import { TransitionEventType } from './transitionEventType';
4import { RegisteredHook } from './hookRegistry';
5import { StateDeclaration } from '../state/interface';
6export type GetResultHandler = (hook: TransitionHook) => ResultHandler;
7export type GetErrorHandler = (hook: TransitionHook) => ErrorHandler;
8export type ResultHandler = (result: HookResult) => Promise<HookResult>;
9export type ErrorHandler = (error: any) => Promise<any>;
10export declare class TransitionHook {
11 private transition;
12 private stateContext;
13 private registeredHook;
14 private options;
15 type: TransitionEventType;
16 /**
17 * These GetResultHandler(s) are used by [[invokeHook]] below
18 * Each HookType chooses a GetResultHandler (See: [[TransitionService._defineCoreEvents]])
19 */
20 static HANDLE_RESULT: GetResultHandler;
21 /**
22 * If the result is a promise rejection, log it.
23 * Otherwise, ignore the result.
24 */
25 static LOG_REJECTED_RESULT: GetResultHandler;
26 /**
27 * These GetErrorHandler(s) are used by [[invokeHook]] below
28 * Each HookType chooses a GetErrorHandler (See: [[TransitionService._defineCoreEvents]])
29 */
30 static LOG_ERROR: GetErrorHandler;
31 static REJECT_ERROR: GetErrorHandler;
32 static THROW_ERROR: GetErrorHandler;
33 /**
34 * Chains together an array of TransitionHooks.
35 *
36 * Given a list of [[TransitionHook]] objects, chains them together.
37 * Each hook is invoked after the previous one completes.
38 *
39 * #### Example:
40 * ```js
41 * var hooks: TransitionHook[] = getHooks();
42 * let promise: Promise<any> = TransitionHook.chain(hooks);
43 *
44 * promise.then(handleSuccess, handleError);
45 * ```
46 *
47 * @param hooks the list of hooks to chain together
48 * @param waitFor if provided, the chain is `.then()`'ed off this promise
49 * @returns a `Promise` for sequentially invoking the hooks (in order)
50 */
51 static chain(hooks: TransitionHook[], waitFor?: Promise<any>): Promise<any>;
52 /**
53 * Invokes all the provided TransitionHooks, in order.
54 * Each hook's return value is checked.
55 * If any hook returns a promise, then the rest of the hooks are chained off that promise, and the promise is returned.
56 * If no hook returns a promise, then all hooks are processed synchronously.
57 *
58 * @param hooks the list of TransitionHooks to invoke
59 * @param doneCallback a callback that is invoked after all the hooks have successfully completed
60 *
61 * @returns a promise for the async result, or the result of the callback
62 */
63 static invokeHooks<T>(hooks: TransitionHook[], doneCallback: (result?: HookResult) => T): Promise<any> | T;
64 /**
65 * Run all TransitionHooks, ignoring their return value.
66 */
67 static runAllHooks(hooks: TransitionHook[]): void;
68 constructor(transition: Transition, stateContext: StateDeclaration, registeredHook: RegisteredHook, options: TransitionHookOptions);
69 private isSuperseded;
70 logError(err: any): any;
71 invokeHook(): Promise<HookResult> | void;
72 /**
73 * This method handles the return value of a Transition Hook.
74 *
75 * A hook can return false (cancel), a TargetState (redirect),
76 * or a promise (which may later resolve to false or a redirect)
77 *
78 * This also handles "transition superseded" -- when a new transition
79 * was started while the hook was still running
80 */
81 handleHookResult(result: HookResult): Promise<HookResult>;
82 /**
83 * Return a Rejection promise if the transition is no longer current due
84 * to a stopped router (disposed), or a new transition has started and superseded this one.
85 */
86 private getNotCurrentRejection;
87 toString(): string;
88}
89
\No newline at end of file