UNPKG

5.39 kBTypeScriptView Raw
1import { StateObject } from './stateObject';
2import { StateMatcher } from './stateMatcher';
3import { StateQueueManager } from './stateQueueManager';
4import { StateDeclaration, _StateDeclaration } from './interface';
5import { BuilderFunction } from './stateBuilder';
6import { StateOrName } from './interface';
7import { UIRouter } from '../router';
8/**
9 * The signature for the callback function provided to [[StateRegistry.onStatesChanged]].
10 *
11 * This callback receives two parameters:
12 *
13 * @param event a string; either "registered" or "deregistered"
14 * @param states the list of [[StateDeclaration]]s that were registered (or deregistered).
15 */
16export declare type StateRegistryListener = (event: 'registered' | 'deregistered', states: StateDeclaration[]) => void;
17/**
18 * A registry for all of the application's [[StateDeclaration]]s
19 *
20 * This API is found at `router.stateRegistry` ([[UIRouter.stateRegistry]])
21 */
22export declare class StateRegistry {
23 private router;
24 private _root;
25 private states;
26 /** @internal */
27 matcher: StateMatcher;
28 private builder;
29 /** @internal */
30 stateQueue: StateQueueManager;
31 /** @internal */
32 listeners: StateRegistryListener[];
33 /** @internal */
34 constructor(router: UIRouter);
35 /** @internal */
36 private _registerRoot;
37 /** @internal */
38 dispose(): void;
39 /**
40 * Listen for a State Registry events
41 *
42 * Adds a callback that is invoked when states are registered or deregistered with the StateRegistry.
43 *
44 * #### Example:
45 * ```js
46 * let allStates = registry.get();
47 *
48 * // Later, invoke deregisterFn() to remove the listener
49 * let deregisterFn = registry.onStatesChanged((event, states) => {
50 * switch(event) {
51 * case: 'registered':
52 * states.forEach(state => allStates.push(state));
53 * break;
54 * case: 'deregistered':
55 * states.forEach(state => {
56 * let idx = allStates.indexOf(state);
57 * if (idx !== -1) allStates.splice(idx, 1);
58 * });
59 * break;
60 * }
61 * });
62 * ```
63 *
64 * @param listener a callback function invoked when the registered states changes.
65 * The function receives two parameters, `event` and `state`.
66 * See [[StateRegistryListener]]
67 * @return a function that deregisters the listener
68 */
69 onStatesChanged(listener: StateRegistryListener): () => void;
70 /**
71 * Gets the implicit root state
72 *
73 * Gets the root of the state tree.
74 * The root state is implicitly created by UI-Router.
75 * Note: this returns the internal [[StateObject]] representation, not a [[StateDeclaration]]
76 *
77 * @return the root [[StateObject]]
78 */
79 root(): StateObject;
80 /**
81 * Adds a state to the registry
82 *
83 * Registers a [[StateDeclaration]] or queues it for registration.
84 *
85 * Note: a state will be queued if the state's parent isn't yet registered.
86 *
87 * @param stateDefinition the definition of the state to register.
88 * @returns the internal [[StateObject]] object.
89 * If the state was successfully registered, then the object is fully built (See: [[StateBuilder]]).
90 * If the state was only queued, then the object is not fully built.
91 */
92 register(stateDefinition: _StateDeclaration): StateObject;
93 /** @internal */
94 private _deregisterTree;
95 /**
96 * Removes a state from the registry
97 *
98 * This removes a state from the registry.
99 * If the state has children, they are are also removed from the registry.
100 *
101 * @param stateOrName the state's name or object representation
102 * @returns {StateObject[]} a list of removed states
103 */
104 deregister(stateOrName: StateOrName): StateObject[];
105 /**
106 * Gets all registered states
107 *
108 * Calling this method with no arguments will return a list of all the states that are currently registered.
109 * Note: this does not return states that are *queued* but not yet registered.
110 *
111 * @return a list of [[StateDeclaration]]s
112 */
113 get(): StateDeclaration[];
114 /**
115 * Gets a registered state
116 *
117 * Given a state or a name, finds and returns the [[StateDeclaration]] from the registry.
118 * Note: this does not return states that are *queued* but not yet registered.
119 *
120 * @param stateOrName either the name of a state, or a state object.
121 * @param base the base state to use when stateOrName is relative.
122 * @return a registered [[StateDeclaration]] that matched the `stateOrName`, or null if the state isn't registered.
123 */
124 get(stateOrName: StateOrName, base?: StateOrName): StateDeclaration;
125 /**
126 * Registers a [[BuilderFunction]] for a specific [[StateObject]] property (e.g., `parent`, `url`, or `path`).
127 * More than one BuilderFunction can be registered for a given property.
128 *
129 * The BuilderFunction(s) will be used to define the property on any subsequently built [[StateObject]] objects.
130 *
131 * @param property The name of the State property being registered for.
132 * @param builderFunction The BuilderFunction which will be used to build the State property
133 * @returns a function which deregisters the BuilderFunction
134 */
135 decorator(property: string, builderFunction: BuilderFunction): Function;
136}