UNPKG

11.8 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.TransitionService = exports.defaultTransOpts = void 0;
4var interface_1 = require("./interface");
5var transition_1 = require("./transition");
6var hookRegistry_1 = require("./hookRegistry");
7var coreResolvables_1 = require("../hooks/coreResolvables");
8var redirectTo_1 = require("../hooks/redirectTo");
9var onEnterExitRetain_1 = require("../hooks/onEnterExitRetain");
10var resolve_1 = require("../hooks/resolve");
11var views_1 = require("../hooks/views");
12var updateGlobals_1 = require("../hooks/updateGlobals");
13var url_1 = require("../hooks/url");
14var lazyLoad_1 = require("../hooks/lazyLoad");
15var transitionEventType_1 = require("./transitionEventType");
16var transitionHook_1 = require("./transitionHook");
17var predicates_1 = require("../common/predicates");
18var common_1 = require("../common/common");
19var hof_1 = require("../common/hof");
20var ignoredTransition_1 = require("../hooks/ignoredTransition");
21var invalidTransition_1 = require("../hooks/invalidTransition");
22/**
23 * The default [[Transition]] options.
24 *
25 * Include this object when applying custom defaults:
26 * let reloadOpts = { reload: true, notify: true }
27 * let options = defaults(theirOpts, customDefaults, defaultOptions);
28 */
29exports.defaultTransOpts = {
30 location: true,
31 relative: null,
32 inherit: false,
33 notify: true,
34 reload: false,
35 supercede: true,
36 custom: {},
37 current: function () { return null; },
38 source: 'unknown',
39};
40/**
41 * This class provides services related to Transitions.
42 *
43 * - Most importantly, it allows global Transition Hooks to be registered.
44 * - It allows the default transition error handler to be set.
45 * - It also has a factory function for creating new [[Transition]] objects, (used internally by the [[StateService]]).
46 *
47 * At bootstrap, [[UIRouter]] creates a single instance (singleton) of this class.
48 *
49 * This API is located at `router.transitionService` ([[UIRouter.transitionService]])
50 */
51var TransitionService = /** @class */ (function () {
52 /** @internal */
53 function TransitionService(_router) {
54 /** @internal */
55 this._transitionCount = 0;
56 /** The transition hook types, such as `onEnter`, `onStart`, etc */
57 this._eventTypes = [];
58 /** @internal The registered transition hooks */
59 this._registeredHooks = {};
60 /** The paths on a criteria object */
61 this._criteriaPaths = {};
62 this._router = _router;
63 this.$view = _router.viewService;
64 this._deregisterHookFns = {};
65 this._pluginapi = (common_1.createProxyFunctions(hof_1.val(this), {}, hof_1.val(this), [
66 '_definePathType',
67 '_defineEvent',
68 '_getPathTypes',
69 '_getEvents',
70 'getHooks',
71 ]));
72 this._defineCorePaths();
73 this._defineCoreEvents();
74 this._registerCoreTransitionHooks();
75 _router.globals.successfulTransitions.onEvict(coreResolvables_1.treeChangesCleanup);
76 }
77 /**
78 * Registers a [[TransitionHookFn]], called *while a transition is being constructed*.
79 *
80 * Registers a transition lifecycle hook, which is invoked during transition construction.
81 *
82 * This low level hook should only be used by plugins.
83 * This can be a useful time for plugins to add resolves or mutate the transition as needed.
84 * The Sticky States plugin uses this hook to modify the treechanges.
85 *
86 * ### Lifecycle
87 *
88 * `onCreate` hooks are invoked *while a transition is being constructed*.
89 *
90 * ### Return value
91 *
92 * The hook's return value is ignored
93 *
94 * @internal
95 * @param criteria defines which Transitions the Hook should be invoked for.
96 * @param callback the hook function which will be invoked.
97 * @param options the registration options
98 * @returns a function which deregisters the hook.
99 */
100 TransitionService.prototype.onCreate = function (criteria, callback, options) {
101 return;
102 };
103 /** @inheritdoc */
104 TransitionService.prototype.onBefore = function (criteria, callback, options) {
105 return;
106 };
107 /** @inheritdoc */
108 TransitionService.prototype.onStart = function (criteria, callback, options) {
109 return;
110 };
111 /** @inheritdoc */
112 TransitionService.prototype.onExit = function (criteria, callback, options) {
113 return;
114 };
115 /** @inheritdoc */
116 TransitionService.prototype.onRetain = function (criteria, callback, options) {
117 return;
118 };
119 /** @inheritdoc */
120 TransitionService.prototype.onEnter = function (criteria, callback, options) {
121 return;
122 };
123 /** @inheritdoc */
124 TransitionService.prototype.onFinish = function (criteria, callback, options) {
125 return;
126 };
127 /** @inheritdoc */
128 TransitionService.prototype.onSuccess = function (criteria, callback, options) {
129 return;
130 };
131 /** @inheritdoc */
132 TransitionService.prototype.onError = function (criteria, callback, options) {
133 return;
134 };
135 /**
136 * dispose
137 * @internal
138 */
139 TransitionService.prototype.dispose = function (router) {
140 common_1.values(this._registeredHooks).forEach(function (hooksArray) {
141 return hooksArray.forEach(function (hook) {
142 hook._deregistered = true;
143 common_1.removeFrom(hooksArray, hook);
144 });
145 });
146 };
147 /**
148 * Creates a new [[Transition]] object
149 *
150 * This is a factory function for creating new Transition objects.
151 * It is used internally by the [[StateService]] and should generally not be called by application code.
152 *
153 * @internal
154 * @param fromPath the path to the current state (the from state)
155 * @param targetState the target state (destination)
156 * @returns a Transition
157 */
158 TransitionService.prototype.create = function (fromPath, targetState) {
159 return new transition_1.Transition(fromPath, targetState, this._router);
160 };
161 /** @internal */
162 TransitionService.prototype._defineCoreEvents = function () {
163 var Phase = interface_1.TransitionHookPhase;
164 var TH = transitionHook_1.TransitionHook;
165 var paths = this._criteriaPaths;
166 var NORMAL_SORT = false, REVERSE_SORT = true;
167 var SYNCHRONOUS = true;
168 this._defineEvent('onCreate', Phase.CREATE, 0, paths.to, NORMAL_SORT, TH.LOG_REJECTED_RESULT, TH.THROW_ERROR, SYNCHRONOUS);
169 this._defineEvent('onBefore', Phase.BEFORE, 0, paths.to);
170 this._defineEvent('onStart', Phase.RUN, 0, paths.to);
171 this._defineEvent('onExit', Phase.RUN, 100, paths.exiting, REVERSE_SORT);
172 this._defineEvent('onRetain', Phase.RUN, 200, paths.retained);
173 this._defineEvent('onEnter', Phase.RUN, 300, paths.entering);
174 this._defineEvent('onFinish', Phase.RUN, 400, paths.to);
175 this._defineEvent('onSuccess', Phase.SUCCESS, 0, paths.to, NORMAL_SORT, TH.LOG_REJECTED_RESULT, TH.LOG_ERROR, SYNCHRONOUS);
176 this._defineEvent('onError', Phase.ERROR, 0, paths.to, NORMAL_SORT, TH.LOG_REJECTED_RESULT, TH.LOG_ERROR, SYNCHRONOUS);
177 };
178 /** @internal */
179 TransitionService.prototype._defineCorePaths = function () {
180 var STATE = interface_1.TransitionHookScope.STATE, TRANSITION = interface_1.TransitionHookScope.TRANSITION;
181 this._definePathType('to', TRANSITION);
182 this._definePathType('from', TRANSITION);
183 this._definePathType('exiting', STATE);
184 this._definePathType('retained', STATE);
185 this._definePathType('entering', STATE);
186 };
187 /** @internal */
188 TransitionService.prototype._defineEvent = function (name, hookPhase, hookOrder, criteriaMatchPath, reverseSort, getResultHandler, getErrorHandler, synchronous) {
189 if (reverseSort === void 0) { reverseSort = false; }
190 if (getResultHandler === void 0) { getResultHandler = transitionHook_1.TransitionHook.HANDLE_RESULT; }
191 if (getErrorHandler === void 0) { getErrorHandler = transitionHook_1.TransitionHook.REJECT_ERROR; }
192 if (synchronous === void 0) { synchronous = false; }
193 var eventType = new transitionEventType_1.TransitionEventType(name, hookPhase, hookOrder, criteriaMatchPath, reverseSort, getResultHandler, getErrorHandler, synchronous);
194 this._eventTypes.push(eventType);
195 hookRegistry_1.makeEvent(this, this, eventType);
196 };
197 /** @internal */
198 TransitionService.prototype._getEvents = function (phase) {
199 var transitionHookTypes = predicates_1.isDefined(phase)
200 ? this._eventTypes.filter(function (type) { return type.hookPhase === phase; })
201 : this._eventTypes.slice();
202 return transitionHookTypes.sort(function (l, r) {
203 var cmpByPhase = l.hookPhase - r.hookPhase;
204 return cmpByPhase === 0 ? l.hookOrder - r.hookOrder : cmpByPhase;
205 });
206 };
207 /**
208 * Adds a Path to be used as a criterion against a TreeChanges path
209 *
210 * For example: the `exiting` path in [[HookMatchCriteria]] is a STATE scoped path.
211 * It was defined by calling `defineTreeChangesCriterion('exiting', TransitionHookScope.STATE)`
212 * Each state in the exiting path is checked against the criteria and returned as part of the match.
213 *
214 * Another example: the `to` path in [[HookMatchCriteria]] is a TRANSITION scoped path.
215 * It was defined by calling `defineTreeChangesCriterion('to', TransitionHookScope.TRANSITION)`
216 * Only the tail of the `to` path is checked against the criteria and returned as part of the match.
217 *
218 * @internal
219 */
220 TransitionService.prototype._definePathType = function (name, hookScope) {
221 this._criteriaPaths[name] = { name: name, scope: hookScope };
222 };
223 /** @internal */
224 // tslint:disable-next-line
225 TransitionService.prototype._getPathTypes = function () {
226 return this._criteriaPaths;
227 };
228 /** @internal */
229 TransitionService.prototype.getHooks = function (hookName) {
230 return this._registeredHooks[hookName];
231 };
232 /** @internal */
233 TransitionService.prototype._registerCoreTransitionHooks = function () {
234 var fns = this._deregisterHookFns;
235 fns.addCoreResolves = coreResolvables_1.registerAddCoreResolvables(this);
236 fns.ignored = ignoredTransition_1.registerIgnoredTransitionHook(this);
237 fns.invalid = invalidTransition_1.registerInvalidTransitionHook(this);
238 // Wire up redirectTo hook
239 fns.redirectTo = redirectTo_1.registerRedirectToHook(this);
240 // Wire up onExit/Retain/Enter state hooks
241 fns.onExit = onEnterExitRetain_1.registerOnExitHook(this);
242 fns.onRetain = onEnterExitRetain_1.registerOnRetainHook(this);
243 fns.onEnter = onEnterExitRetain_1.registerOnEnterHook(this);
244 // Wire up Resolve hooks
245 fns.eagerResolve = resolve_1.registerEagerResolvePath(this);
246 fns.lazyResolve = resolve_1.registerLazyResolveState(this);
247 fns.resolveAll = resolve_1.registerResolveRemaining(this);
248 // Wire up the View management hooks
249 fns.loadViews = views_1.registerLoadEnteringViews(this);
250 fns.activateViews = views_1.registerActivateViews(this);
251 // Updates global state after a transition
252 fns.updateGlobals = updateGlobals_1.registerUpdateGlobalState(this);
253 // After globals.current is updated at priority: 10000
254 fns.updateUrl = url_1.registerUpdateUrl(this);
255 // Lazy load state trees
256 fns.lazyLoad = lazyLoad_1.registerLazyLoadHook(this);
257 };
258 return TransitionService;
259}());
260exports.TransitionService = TransitionService;
261//# sourceMappingURL=transitionService.js.map
\No newline at end of file