UNPKG

3.57 kBJavaScriptView Raw
1'use strict';
2
3var _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
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.routeReducer = routeReducer;
9exports.syncHistory = syncHistory;
10// Constants
11
12var TRANSITION = exports.TRANSITION = '@@router/TRANSITION';
13var UPDATE_LOCATION = exports.UPDATE_LOCATION = '@@router/UPDATE_LOCATION';
14
15var SELECT_STATE = function SELECT_STATE(state) {
16 return state.routing;
17};
18
19function transition(method) {
20 return function (arg) {
21 return {
22 type: TRANSITION,
23 method: method, arg: arg
24 };
25 };
26}
27
28var 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
36function updateLocation(location) {
37 return {
38 type: UPDATE_LOCATION,
39 location: location
40 };
41}
42
43// Reducer
44
45var initialState = {
46 location: undefined
47};
48
49function 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// Syncing
63
64function 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 // Don't dispatch a new action if we're replaying location.
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 // If we're resetting to the beginning, use the saved initial value. We
115 // need to dispatch a new action at this point to populate the store
116 // appropriately.
117
118 if (!location) {
119 history.transitionTo(initialLocation);
120 return;
121 }
122
123 // Otherwise, if we need to update the history location, do so without
124 // dispatching a new action, as we're just bringing history in sync
125 // with the store.
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}