-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrouter_base.ts
313 lines (277 loc) · 10.7 KB
/
router_base.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import {objKeys} from './utilities';
import {
IRouterDeclaration,
ISerializeOptions,
IRouterInitArgs,
RouterInstance,
ExtractCustomStateFromTemplate,
RouterCurrentState,
RouterHistoricalState,
IRouterTemplates,
NeighborsOfType,
NarrowRouterTypeName,
Childs,
Parent,
Root,
IInputSearch,
IInputLocation,
ValueOf,
AllTemplates,
LinkOptions
} from './types';
import {IRouterBase} from './types/router_base';
export interface IInternalState {
isActive?: boolean;
}
export default class RouterBase<
CustomTemplates extends IRouterTemplates,
RouterTypeName extends NarrowRouterTypeName<keyof AllTemplates<CustomTemplates>>,
InitArgs extends IRouterInitArgs<
CustomTemplates,
NarrowRouterTypeName<RouterTypeName>
> = IRouterInitArgs<CustomTemplates, NarrowRouterTypeName<RouterTypeName>>
> implements IRouterBase<CustomTemplates, RouterTypeName, InitArgs> {
public name: InitArgs['name'];
public type: InitArgs['type'];
public manager: InitArgs['manager'];
public parent?: Parent<CustomTemplates>;
public children: Childs<CustomTemplates>;
public root: Root<CustomTemplates>;
public getState?: InitArgs['getState'];
public subscribe?: InitArgs['subscribe'];
public config: InitArgs['config'];
public _EXPERIMENTAL_internal_state: IInternalState; // eslint-disable-line
constructor(init: InitArgs) {
const {
name,
config,
type,
manager,
parent,
children,
root,
getState,
subscribe,
actions
} = init;
// required
if (!name || !type || !manager || !config) {
throw new Error('Missing required kwargs: name, type, config, and/or manager');
}
this.name = name;
this.config = config;
this.type = type;
this.manager = manager;
// optional
this.parent = parent;
this.children = children || {};
this.root = root;
// methods customized for instance from manager
this.getState = getState;
this.subscribe = subscribe;
this._EXPERIMENTAL_internal_state = {}; // eslint-disable-line
// TODO fix test so empty array isn't needed
// TODO add tests for this
// Since actions come from the template and are decorated by the manager, we need to bind them
// to the router instance where they live
(actions || []).forEach(actionName => {
// eslint-disable-next-line
if ((this as Record<any, any>)[actionName]) {
// eslint-disable-next-line
(this as any)[actionName] = (this as any)[actionName].bind(this);
}
});
}
public link = (
actionName: string,
options?: LinkOptions<
ExtractCustomStateFromTemplate<AllTemplates<CustomTemplates>[RouterTypeName]>
>
): string => {
return this.manager.linkTo(this.name, actionName, options);
};
get lastDefinedParentsDisableChildCacheState(): boolean {
if (!this.parent) {
return false;
}
const parentState = this.parent.config.disableCaching;
if (parentState !== undefined) {
return parentState;
} else {
return this.parent.lastDefinedParentsDisableChildCacheState;
}
}
get routeKey(): string {
return this.config.routeKey || this.name;
}
get data(): ExtractCustomStateFromTemplate<AllTemplates<CustomTemplates>[RouterTypeName]> {
return this.state.data
? this.state.data
: this.manager.routerCache.cache[this.name]
? this.manager.routerCache.cache[this.name].data
: undefined;
}
get siblings(): RouterInstance<CustomTemplates, RouterTypeName>[] {
return this.parent.children[this.type].filter(r => r.name !== this.name);
}
/**
* Returns all neighbors of a certain router type. This could include the same router type of this router if desired.
*/
public getNeighborsByType<
DesiredType extends NarrowRouterTypeName<keyof AllTemplates<CustomTemplates>>
>(type: DesiredType): Array<RouterInstance<CustomTemplates, DesiredType>> {
if (this.parent && this.parent.children) {
return this.parent.children[type] || [];
}
return [];
}
/**
* Given a location object, returns location data for the router or undefined if none is found
*/
public getLocationDataFromLocationObject = (
location: IInputLocation
): ValueOf<IInputSearch> => {
return this.isPathRouter
? location.pathname[this.pathLocation]
: location.search[this.routeKey];
};
/**
* Returns all neighboring routers. That is, all routers that have the same parent but are not of this router type.
*/
public getNeighbors(): NeighborsOfType<
CustomTemplates,
NarrowRouterTypeName<Exclude<keyof AllTemplates<CustomTemplates>, RouterTypeName>>
> {
if (!this.parent) {
return [];
}
// eslint-disable-next-line
const flattened = (acc: any[], arr: any[]): any[] => acc.concat(...arr);
return objKeys(this.parent.children)
.filter(t => t !== this.type)
.map(t => this.parent.children[t])
.reduce(flattened, []);
}
get pathLocation(): number {
if (!this.parent) {
return -1;
}
return 1 + this.parent.pathLocation;
}
get isRootRouter(): boolean {
return !this.parent;
}
// private set EXPERIMENTAL_internal_state(internalState: Object) {
// this._EXPERIMENTAL_internal_state = internalState; // stateUpdateFn({...this._EXPERIMENTAL_internal_state});
// }
// eslint-disable-next-line
public EXPERIMENTAL_setInternalState(internalState: IInternalState): void {
this._EXPERIMENTAL_internal_state = {...internalState}; // eslint-disable-line
}
// eslint-disable-next-line
public get EXPERIMENTAL_internal_state(): IInternalState {
// eslint-disable-next-line
return this._EXPERIMENTAL_internal_state;
}
/**
* Return serialized information about this router
* and all of its children routers.
* Useful for debugging.
*
* Returns a router serialization object tree
*/
public serialize(
options: ISerializeOptions = {}
// eslint-disable-next-line
): IRouterDeclaration<AllTemplates<CustomTemplates>> & {[key: string]: any} {
// create router declaration object
// TODO clean up this mess
const serialized: IRouterDeclaration<AllTemplates<CustomTemplates>> & {
// eslint-disable-next-line
[key: string]: any;
} = {
name: this.name,
routeKey: options.alwaysShowRouteKey
? this.routeKey
: this.routeKey === this.name
? undefined
: this.routeKey,
type: options.showType ? this.type : undefined,
parentName: options.showParentName && this.parent ? this.parent.name : undefined,
isPathRouter: this.config.isPathRouter,
disableCaching: options.showDefaults ? this.config.disableCaching : undefined,
shouldInverselyActivate: options.showDefaults
? this.config.shouldInverselyActivate
: undefined,
defaultAction: options.showDefaults ? this.config.defaultAction : undefined
};
// recursively serialize child routers
const childRouterTypes = Object.keys(this.children);
const childRouters = childRouterTypes.reduce((acc, type) => {
// eslint-disable-next-line
acc[type] = this.children[type].map(childRouter => childRouter.serialize(options));
return acc;
}, {} as {[routerType: string]: IRouterDeclaration<AllTemplates<CustomTemplates>>[]});
if (childRouterTypes.length > 0) {
serialized.routers = childRouters;
}
Object.keys(serialized).forEach(key =>
serialized[key] === undefined ? delete serialized[key] : ''
);
return serialized;
}
get isPathRouter(): boolean {
// If there is no parent, we are at the root.
// The root is by default a path router since
// it represents the '/' in a pathname location
if (!this.parent) {
return true;
}
// If this router was explicitly set to be a path router during config, return true
if (this.config.isPathRouter && this.parent.isPathRouter) {
return true;
}
// If this router is a path router but its parent isn't we need to throw an error.
// It is impossible to construct a path if all the parents are also not path routers
if (this.config.isPathRouter) {
throw new Error(`${this.type} router: ${this.name} is explicitly set to modify the pathname
but one of its parent routers doesn't have this permission.
Make sure all parents have 'isPathRouter' attribute set to 'true' in the router config OR
Make sure all parents are of router type 'scene' or 'data'.
If the routers parents have siblings of both 'scene' and 'data' the 'scene' router will always be used for the pathname
`);
}
return false;
}
get state(): RouterCurrentState<
ExtractCustomStateFromTemplate<AllTemplates<CustomTemplates>[RouterTypeName]>
> {
return this._state();
}
protected _state = (): RouterCurrentState<
ExtractCustomStateFromTemplate<AllTemplates<CustomTemplates>[RouterTypeName]>
> & {isActive?: boolean} => {
if (!this.getState) {
throw new Error('no getState function specified by the manager');
}
const {current} = this.getState();
const newState = current || {};
return {...newState, ...this.EXPERIMENTAL_internal_state} as RouterCurrentState<
ExtractCustomStateFromTemplate<AllTemplates<CustomTemplates>[RouterTypeName]>
> & {isActive?: boolean};
};
public get history(): RouterHistoricalState<
ExtractCustomStateFromTemplate<AllTemplates<CustomTemplates>[RouterTypeName]>
> {
return this._history();
}
protected _history = (): RouterHistoricalState<
ExtractCustomStateFromTemplate<AllTemplates<CustomTemplates>[RouterTypeName]>
> => {
if (!this.getState) {
throw new Error('no getState function specified by the manager');
}
const {historical} = this.getState();
return historical || [];
};
}