/
create-history-multiplexers.ts
48 lines (41 loc) · 1.49 KB
/
create-history-multiplexers.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
import * as history from 'history';
import {RootLocationTransformer} from '../create-root-location-transformer';
import {HistoryMultiplexer} from './history-multiplexer';
import {HistoryServiceContext} from './history-service-context';
import {StaticRootHistory} from './static-root-history';
export interface HistoryMultiplexers {
readonly browserHistoryMultiplexer: HistoryMultiplexer;
readonly staticHistoryMultiplexer: HistoryMultiplexer;
}
export function createHistoryMultiplexers(
context: HistoryServiceContext,
rootLocationTransformer: RootLocationTransformer
): HistoryMultiplexers {
let browserHistoryMultiplexer: HistoryMultiplexer | undefined;
let staticHistoryMultiplexer: HistoryMultiplexer | undefined;
return {
get browserHistoryMultiplexer(): HistoryMultiplexer {
if (!browserHistoryMultiplexer) {
browserHistoryMultiplexer = new HistoryMultiplexer(
history.createBrowserHistory(),
rootLocationTransformer
);
}
return browserHistoryMultiplexer;
},
get staticHistoryMultiplexer(): HistoryMultiplexer {
if (!staticHistoryMultiplexer) {
if (!context.serverRequest) {
throw new Error(
'Static history can not be created without a server request.'
);
}
staticHistoryMultiplexer = new HistoryMultiplexer(
new StaticRootHistory(context.serverRequest),
rootLocationTransformer
);
}
return staticHistoryMultiplexer;
}
};
}