UNPKG

14.1 kBSource Map (JSON)View Raw
1{
2 "version": 3,
3 "file": "transitionHook.js",
4 "sourceRoot": "",
5 "sources": [
6 "@uirouter/core/transition/transitionHook.ts"
7 ],
8 "names": [],
9 "mappings": ";;;AAAA,yCAAqF;AACrF,2CAAmE;AACnE,6CAA0D;AAC1D,mDAAiD;AACjD,qCAA0C;AAC1C,yCAAwC;AACxC,uDAAkD;AAClD,iDAA4C;AAC5C,oDAAmD;AAMnD,IAAM,cAAc,GAA0B;IAC5C,OAAO,EAAE,aAAI;IACb,UAAU,EAAE,IAAI;IAChB,SAAS,EAAE,EAAE;IACb,IAAI,EAAE,IAAI;CACX,CAAC;AAQF;IAuFE,wBACU,UAAsB,EACtB,YAA8B,EAC9B,cAA8B,EAC9B,OAA8B;QAJxC,iBAQC;QAPS,eAAU,GAAV,UAAU,CAAY;QACtB,iBAAY,GAAZ,YAAY,CAAkB;QAC9B,mBAAc,GAAd,cAAc,CAAgB;QAC9B,YAAO,GAAP,OAAO,CAAuB;QAMhC,iBAAY,GAAG,cAAM,OAAA,KAAI,CAAC,IAAI,CAAC,SAAS,KAAK,+BAAmB,CAAC,GAAG,IAAI,CAAC,KAAI,CAAC,OAAO,CAAC,UAAU,CAAC,QAAQ,EAAE,EAAtF,CAAsF,CAAC;QAJlH,IAAI,CAAC,OAAO,GAAG,iBAAQ,CAAC,OAAO,EAAE,cAAc,CAAC,CAAC;QACjD,IAAI,CAAC,IAAI,GAAG,cAAc,CAAC,SAAS,CAAC;IACvC,CAAC;IAhED;;;;;;;;;;;;;;;;;OAiBG;IACI,oBAAK,GAAZ,UAAa,KAAuB,EAAE,OAAsB;QAC1D,uCAAuC;QACvC,IAAM,gBAAgB,GAAG,UAAC,IAAkB,EAAE,QAAwB,IAAK,OAAA,IAAI,CAAC,IAAI,CAAC,cAAM,OAAA,QAAQ,CAAC,UAAU,EAAE,EAArB,CAAqB,CAAC,EAAtC,CAAsC,CAAC;QAClH,OAAO,KAAK,CAAC,MAAM,CAA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IAAK,OAAA,UAAC,KAAU,IAAK,OAAA,wBAAe,CAAC,KAAK,CAAC,EAAtB,CAAsB,EAAtC,CAAsC,CAAC;IAEjG,0BAAW,GAAoB,UAAC,IAAoB,IAAK,OAAA,UAAC,KAAU;QACzE,MAAM,KAAK,CAAC;IACd,CAAC,EAF+D,CAE/D,CAAC;IAgLJ,qBAAC;CAAA,AA7MD,IA6MC;AA7MY,wCAAc",
10 "sourcesContent": [
11 "import { TransitionHookOptions, HookResult, TransitionHookPhase } from './interface';\nimport { defaults, noop, silentRejection } from '../common/common';\nimport { fnToString, maxLength } from '../common/strings';\nimport { isPromise } from '../common/predicates';\nimport { is, parse } from '../common/hof';\nimport { trace } from '../common/trace';\nimport { services } from '../common/coreservices';\nimport { Rejection } from './rejectFactory';\nimport { TargetState } from '../state/targetState';\nimport { Transition } from './transition';\nimport { TransitionEventType } from './transitionEventType';\nimport { RegisteredHook } from './hookRegistry';\nimport { StateDeclaration } from '../state/interface';\n\nconst defaultOptions: TransitionHookOptions = {\n current: noop,\n transition: null,\n traceData: {},\n bind: null,\n};\n\nexport type GetResultHandler = (hook: TransitionHook) => ResultHandler;\nexport type GetErrorHandler = (hook: TransitionHook) => ErrorHandler;\n\nexport type ResultHandler = (result: HookResult) => Promise<HookResult>;\nexport type ErrorHandler = (error: any) => Promise<any>;\n\nexport class TransitionHook {\n type: TransitionEventType;\n\n /**\n * These GetResultHandler(s) are used by [[invokeHook]] below\n * Each HookType chooses a GetResultHandler (See: [[TransitionService._defineCoreEvents]])\n */\n static HANDLE_RESULT: GetResultHandler = (hook: TransitionHook) => (result: HookResult) =>\n hook.handleHookResult(result);\n\n /**\n * If the result is a promise rejection, log it.\n * Otherwise, ignore the result.\n */\n static LOG_REJECTED_RESULT: GetResultHandler = (hook: TransitionHook) => (result: HookResult) => {\n isPromise(result) && result.catch((err) => hook.logError(Rejection.normalize(err)));\n return undefined;\n };\n\n /**\n * These GetErrorHandler(s) are used by [[invokeHook]] below\n * Each HookType chooses a GetErrorHandler (See: [[TransitionService._defineCoreEvents]])\n */\n static LOG_ERROR: GetErrorHandler = (hook: TransitionHook) => (error: any) => hook.logError(error);\n\n static REJECT_ERROR: GetErrorHandler = (hook: TransitionHook) => (error: any) => silentRejection(error);\n\n static THROW_ERROR: GetErrorHandler = (hook: TransitionHook) => (error: any) => {\n throw error;\n };\n\n /**\n * Chains together an array of TransitionHooks.\n *\n * Given a list of [[TransitionHook]] objects, chains them together.\n * Each hook is invoked after the previous one completes.\n *\n * #### Example:\n * ```js\n * var hooks: TransitionHook[] = getHooks();\n * let promise: Promise<any> = TransitionHook.chain(hooks);\n *\n * promise.then(handleSuccess, handleError);\n * ```\n *\n * @param hooks the list of hooks to chain together\n * @param waitFor if provided, the chain is `.then()`'ed off this promise\n * @returns a `Promise` for sequentially invoking the hooks (in order)\n */\n static chain(hooks: TransitionHook[], waitFor?: Promise<any>): Promise<any> {\n // Chain the next hook off the previous\n const createHookChainR = (prev: Promise<any>, nextHook: TransitionHook) => prev.then(() => nextHook.invokeHook());\n return hooks.reduce(createHookChainR, waitFor || services.$q.when());\n }\n\n /**\n * Invokes all the provided TransitionHooks, in order.\n * Each hook's return value is checked.\n * If any hook returns a promise, then the rest of the hooks are chained off that promise, and the promise is returned.\n * If no hook returns a promise, then all hooks are processed synchronously.\n *\n * @param hooks the list of TransitionHooks to invoke\n * @param doneCallback a callback that is invoked after all the hooks have successfully completed\n *\n * @returns a promise for the async result, or the result of the callback\n */\n static invokeHooks<T>(hooks: TransitionHook[], doneCallback: (result?: HookResult) => T): Promise<any> | T {\n for (let idx = 0; idx < hooks.length; idx++) {\n const hookResult = hooks[idx].invokeHook();\n\n if (isPromise(hookResult)) {\n const remainingHooks = hooks.slice(idx + 1);\n\n return TransitionHook.chain(remainingHooks, hookResult).then(doneCallback);\n }\n }\n\n return doneCallback();\n }\n\n /**\n * Run all TransitionHooks, ignoring their return value.\n */\n static runAllHooks(hooks: TransitionHook[]): void {\n hooks.forEach((hook) => hook.invokeHook());\n }\n\n constructor(\n private transition: Transition,\n private stateContext: StateDeclaration,\n private registeredHook: RegisteredHook,\n private options: TransitionHookOptions\n ) {\n this.options = defaults(options, defaultOptions);\n this.type = registeredHook.eventType;\n }\n\n private isSuperseded = () => this.type.hookPhase === TransitionHookPhase.RUN && !this.options.transition.isActive();\n\n logError(err): any {\n this.transition.router.stateService.defaultErrorHandler()(err);\n }\n\n invokeHook(): Promise<HookResult> | void {\n const hook = this.registeredHook;\n if (hook._deregistered) return;\n\n const notCurrent = this.getNotCurrentRejection();\n if (notCurrent) return notCurrent;\n\n const options = this.options;\n trace.traceHookInvocation(this, this.transition, options);\n\n const invokeCallback = () => hook.callback.call(options.bind, this.transition, this.stateContext);\n\n const normalizeErr = (err) => Rejection.normalize(err).toPromise();\n\n const handleError = (err) => hook.eventType.getErrorHandler(this)(err);\n\n const handleResult = (result) => hook.eventType.getResultHandler(this)(result);\n\n try {\n const result = invokeCallback();\n\n if (!this.type.synchronous && isPromise(result)) {\n return result.catch(normalizeErr).then(handleResult, handleError);\n } else {\n return handleResult(result);\n }\n } catch (err) {\n // If callback throws (synchronously)\n return handleError(Rejection.normalize(err));\n } finally {\n if (hook.invokeLimit && ++hook.invokeCount >= hook.invokeLimit) {\n hook.deregister();\n }\n }\n }\n\n /**\n * This method handles the return value of a Transition Hook.\n *\n * A hook can return false (cancel), a TargetState (redirect),\n * or a promise (which may later resolve to false or a redirect)\n *\n * This also handles \"transition superseded\" -- when a new transition\n * was started while the hook was still running\n */\n handleHookResult(result: HookResult): Promise<HookResult> {\n const notCurrent = this.getNotCurrentRejection();\n if (notCurrent) return notCurrent;\n\n // Hook returned a promise\n if (isPromise(result)) {\n // Wait for the promise, then reprocess with the resulting value\n return result.then((val) => this.handleHookResult(val));\n }\n\n trace.traceHookResult(result, this.transition, this.options);\n\n // Hook returned false\n if (result === false) {\n // Abort this Transition\n return Rejection.aborted('Hook aborted transition').toPromise();\n }\n\n const isTargetState = is(TargetState);\n // hook returned a TargetState\n if (isTargetState(result)) {\n // Halt the current Transition and redirect (a new Transition) to the TargetState.\n return Rejection.redirected(result).toPromise();\n }\n }\n\n /**\n * Return a Rejection promise if the transition is no longer current due\n * to a stopped router (disposed), or a new transition has started and superseded this one.\n */\n private getNotCurrentRejection() {\n const router = this.transition.router;\n\n // The router is stopped\n if (router._disposed) {\n return Rejection.aborted(`UIRouter instance #${router.$id} has been stopped (disposed)`).toPromise();\n }\n\n if (this.transition._aborted) {\n return Rejection.aborted().toPromise();\n }\n\n // This transition is no longer current.\n // Another transition started while this hook was still running.\n if (this.isSuperseded()) {\n // Abort this transition\n return Rejection.superseded(this.options.current()).toPromise();\n }\n }\n\n toString() {\n const { options, registeredHook } = this;\n const event = parse('traceData.hookType')(options) || 'internal',\n context = parse('traceData.context.state.name')(options) || parse('traceData.context')(options) || 'unknown',\n name = fnToString(registeredHook.callback);\n return `${event} context: ${context}, ${maxLength(200, name)}`;\n }\n}\n"
12 ]
13}
\No newline at end of file