1 | 'use strict';
|
2 |
|
3 | var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
|
4 |
|
5 | Object.defineProperty(exports, "__esModule", {
|
6 | value: true
|
7 | });
|
8 | exports.routeReducer = routeReducer;
|
9 | exports.syncHistory = syncHistory;
|
10 |
|
11 |
|
12 | var TRANSITION = exports.TRANSITION = '@@router/TRANSITION';
|
13 | var UPDATE_LOCATION = exports.UPDATE_LOCATION = '@@router/UPDATE_LOCATION';
|
14 |
|
15 | var SELECT_STATE = function SELECT_STATE(state) {
|
16 | return state.routing;
|
17 | };
|
18 |
|
19 | function transition(method) {
|
20 | return function (arg) {
|
21 | return {
|
22 | type: TRANSITION,
|
23 | method: method, arg: arg
|
24 | };
|
25 | };
|
26 | }
|
27 |
|
28 | var routeActions = exports.routeActions = {
|
29 | push: transition('push'),
|
30 | replace: transition('replace'),
|
31 | go: transition('go'),
|
32 | goBack: transition('goBack'),
|
33 | goForward: transition('goForward')
|
34 | };
|
35 |
|
36 | function updateLocation(location) {
|
37 | return {
|
38 | type: UPDATE_LOCATION,
|
39 | location: location
|
40 | };
|
41 | }
|
42 |
|
43 |
|
44 |
|
45 | var initialState = {
|
46 | location: undefined
|
47 | };
|
48 |
|
49 | function routeReducer() {
|
50 | var state = arguments.length <= 0 || arguments[0] === undefined ? initialState : arguments[0];
|
51 | var _ref = arguments[1];
|
52 | var type = _ref.type;
|
53 | var location = _ref.location;
|
54 |
|
55 | if (type !== UPDATE_LOCATION) {
|
56 | return state;
|
57 | }
|
58 |
|
59 | return _extends({}, state, { location: location });
|
60 | }
|
61 |
|
62 |
|
63 |
|
64 | function syncHistory(history) {
|
65 | var unsubscribeHistory = undefined,
|
66 | currentKey = undefined,
|
67 | unsubscribeStore = undefined;
|
68 | var connected = false,
|
69 | syncing = false;
|
70 |
|
71 | function middleware(store) {
|
72 | unsubscribeHistory = history.listen(function (location) {
|
73 | currentKey = location.key;
|
74 | if (syncing) {
|
75 |
|
76 | return;
|
77 | }
|
78 |
|
79 | store.dispatch(updateLocation(location));
|
80 | });
|
81 |
|
82 | connected = true;
|
83 |
|
84 | return function (next) {
|
85 | return function (action) {
|
86 | if (action.type !== TRANSITION || !connected) {
|
87 | return next(action);
|
88 | }
|
89 |
|
90 | var method = action.method;
|
91 | var arg = action.arg;
|
92 |
|
93 | history[method](arg);
|
94 | };
|
95 | };
|
96 | }
|
97 |
|
98 | middleware.listenForReplays = function (store) {
|
99 | var selectRouterState = arguments.length <= 1 || arguments[1] === undefined ? SELECT_STATE : arguments[1];
|
100 |
|
101 | var getRouterState = function getRouterState() {
|
102 | return selectRouterState(store.getState());
|
103 | };
|
104 |
|
105 | var _getRouterState = getRouterState();
|
106 |
|
107 | var initialLocation = _getRouterState.location;
|
108 |
|
109 | unsubscribeStore = store.subscribe(function () {
|
110 | var _getRouterState2 = getRouterState();
|
111 |
|
112 | var location = _getRouterState2.location;
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | if (!location) {
|
119 | history.transitionTo(initialLocation);
|
120 | return;
|
121 | }
|
122 |
|
123 |
|
124 |
|
125 |
|
126 | if (location.key !== currentKey) {
|
127 | syncing = true;
|
128 | history.transitionTo(location);
|
129 | syncing = false;
|
130 | }
|
131 | });
|
132 | };
|
133 |
|
134 | middleware.unsubscribe = function () {
|
135 | unsubscribeHistory();
|
136 | if (unsubscribeStore) {
|
137 | unsubscribeStore();
|
138 | }
|
139 |
|
140 | connected = false;
|
141 | };
|
142 |
|
143 | return middleware;
|
144 | }
|