-
Notifications
You must be signed in to change notification settings - Fork 497
/
RoutingManager.ts
215 lines (175 loc) · 5.85 KB
/
RoutingManager.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
import algoliasearchHelper from 'algoliasearch-helper';
import { isEqual } from './utils';
import {
InstantSearch,
UiState,
SearchParameters,
Router,
StateMapping,
Widget,
} from '../types';
type RoutingManagerProps = {
instantSearchInstance: InstantSearch;
router: Router;
stateMapping: StateMapping;
};
class RoutingManager implements Widget {
private readonly instantSearchInstance: InstantSearch;
private readonly router: Router;
private readonly stateMapping: StateMapping;
private isFirstRender: boolean = true;
private currentUiState: UiState;
private initState?: UiState;
private renderURLFromState?: (searchParameters: SearchParameters) => void;
public constructor({
router,
stateMapping,
instantSearchInstance,
}: RoutingManagerProps) {
this.router = router;
this.stateMapping = stateMapping;
this.instantSearchInstance = instantSearchInstance;
this.currentUiState = this.stateMapping.routeToState(this.router.read());
}
private getAllSearchParameters({
currentSearchParameters,
uiState,
}): Partial<SearchParameters> {
const widgets = this.instantSearchInstance.widgets;
return widgets.reduce((parameters, widget) => {
if (!widget.getWidgetSearchParameters) {
return parameters;
}
return widget.getWidgetSearchParameters(parameters, {
uiState,
});
}, currentSearchParameters);
}
private getAllUiStates({
searchParameters,
}: {
searchParameters: SearchParameters;
}): UiState {
const widgets = this.instantSearchInstance.widgets;
const helper = this.instantSearchInstance.helper!;
return widgets.reduce<UiState>((state, widget) => {
if (!widget.getWidgetState) {
return state;
}
return widget.getWidgetState(state, {
helper,
searchParameters,
});
}, {});
}
private setupRouting(state: SearchParameters): void {
const helper = this.instantSearchInstance.helper!;
this.router.onUpdate(route => {
const nextUiState = this.stateMapping.routeToState(route);
const widgetsUiState = this.getAllUiStates({
searchParameters: helper.state,
});
if (isEqual(nextUiState, widgetsUiState)) {
return;
}
this.currentUiState = nextUiState;
const searchParameters = this.getAllSearchParameters({
currentSearchParameters: state,
uiState: this.currentUiState,
});
helper
.overrideStateWithoutTriggeringChangeEvent(searchParameters)
.search();
});
this.renderURLFromState = searchParameters => {
this.currentUiState = this.getAllUiStates({
searchParameters,
});
const route = this.stateMapping.stateToRoute(this.currentUiState);
this.router.write(route);
};
helper.on('change', this.renderURLFromState);
// Compare initial state and first render state to see if the query has been
// changed by the `searchFunction`. It's required because the helper of the
// `searchFunction` does not trigger change event (not the same instance).
const firstRenderState = this.getAllUiStates({
searchParameters: state,
});
if (!isEqual(this.initState, firstRenderState)) {
// Force update the URL, if the state has changed since the initial read.
// We do this in order to make the URL update when there is `searchFunction`
// that prevents the search of the initial rendering.
// See: https://github.com/algolia/instantsearch.js/issues/2523#issuecomment-339356157
this.currentUiState = firstRenderState;
const route = this.stateMapping.stateToRoute(this.currentUiState);
this.router.write(route);
}
}
public getConfiguration(
currentConfiguration: Partial<SearchParameters>
): Partial<SearchParameters> {
// We have to create a `SearchParameters` because `getAllSearchParameters`
// expects an instance of `SearchParameters` and not a plain object.
const currentSearchParameters = algoliasearchHelper.SearchParameters.make(
currentConfiguration
);
return {
...this.getAllSearchParameters({
uiState: this.currentUiState,
currentSearchParameters,
}),
};
}
public init({ state }: { state: SearchParameters }): void {
// Store the initial state from the storage to compare it with the state on next renders
// in case the `searchFunction` has modified it.
this.initState = this.getAllUiStates({
searchParameters: state,
});
}
public render({ state }: { state: SearchParameters }): void {
if (this.isFirstRender) {
this.isFirstRender = false;
this.setupRouting(state);
}
}
public dispose({ helper, state }): void {
if (this.renderURLFromState) {
this.instantSearchInstance.helper!.removeListener(
'change',
this.renderURLFromState
);
}
if (this.router.dispose) {
this.router.dispose({ helper, state });
}
}
public createURL(state: SearchParameters): string {
const uiState = this.getAllUiStates({
searchParameters: state,
});
const route = this.stateMapping.stateToRoute(uiState);
return this.router.createURL(route);
}
public onHistoryChange(
callback: (state: Partial<SearchParameters>) => void
): void {
const helper = this.instantSearchInstance.helper!;
this.router.onUpdate(route => {
const nextUiState = this.stateMapping.routeToState(route);
const widgetsUiState = this.getAllUiStates({
searchParameters: helper.state,
});
if (isEqual(nextUiState, widgetsUiState)) {
return;
}
this.currentUiState = nextUiState;
const searchParameters = this.getAllSearchParameters({
currentSearchParameters: helper.state,
uiState: this.currentUiState,
});
callback({ ...searchParameters });
});
}
}
export default RoutingManager;