@thoughtbot/superglue 0.53.3 → 0.53.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/action_creators/index.js +9 -25
- package/action_creators/requests.js +42 -71
- package/components/Nav.js +20 -67
- package/index.js +38 -88
- package/middleware.js +1 -16
- package/package.json +4 -1
- package/reducers/index.js +19 -57
- package/utils/helpers.js +2 -7
- package/utils/immutability.js +1 -43
- package/utils/index.js +0 -14
- package/utils/react.js +5 -12
- package/utils/request.js +15 -37
- package/utils/ujs.js +6 -29
- package/utils/url.js +5 -19
- package/utils/window.js +0 -1
package/index.js
CHANGED
|
@@ -1,29 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
exports.__esModule = true;
|
|
4
|
-
exports.
|
|
5
|
-
|
|
4
|
+
exports.superglueReducer = exports.pageReducer = exports.mapDispatchToPropsIncludingVisitAndRemote = exports.getIn = exports.fragmentMiddleware = exports.UPDATE_FRAGMENTS = exports.SAVE_RESPONSE = exports.REMOVE_PAGE = exports.GRAFTING_SUCCESS = exports.GRAFTING_ERROR = exports.COPY_PAGE = exports.BEFORE_VISIT = exports.BEFORE_REMOTE = exports.BEFORE_FETCH = exports.ApplicationBase = void 0;
|
|
6
5
|
var _react = _interopRequireDefault(require("react"));
|
|
7
|
-
|
|
8
6
|
var _urlParse = _interopRequireDefault(require("url-parse"));
|
|
9
|
-
|
|
10
7
|
var _reducers = require("./reducers");
|
|
11
|
-
|
|
12
8
|
exports.rootReducer = _reducers.rootReducer;
|
|
13
9
|
exports.superglueReducer = _reducers.superglueReducer;
|
|
14
10
|
exports.pageReducer = _reducers.pageReducer;
|
|
15
|
-
exports.updateFragments = _reducers.updateFragments;
|
|
16
|
-
|
|
17
11
|
var _config = require("./config");
|
|
18
|
-
|
|
19
12
|
var _utils = require("./utils");
|
|
20
|
-
|
|
21
13
|
exports.urlToPageKey = _utils.urlToPageKey;
|
|
22
|
-
|
|
23
14
|
var _action_creators = require("./action_creators");
|
|
24
|
-
|
|
25
15
|
var _actions = require("./actions");
|
|
26
|
-
|
|
27
16
|
exports.HISTORY_CHANGE = _actions.HISTORY_CHANGE;
|
|
28
17
|
exports.BEFORE_FETCH = _actions.BEFORE_FETCH;
|
|
29
18
|
exports.BEFORE_VISIT = _actions.BEFORE_VISIT;
|
|
@@ -34,63 +23,41 @@ exports.COPY_PAGE = _actions.COPY_PAGE;
|
|
|
34
23
|
exports.REMOVE_PAGE = _actions.REMOVE_PAGE;
|
|
35
24
|
exports.GRAFTING_ERROR = _actions.GRAFTING_ERROR;
|
|
36
25
|
exports.GRAFTING_SUCCESS = _actions.GRAFTING_SUCCESS;
|
|
37
|
-
|
|
38
26
|
var _redux = require("redux");
|
|
39
|
-
|
|
40
27
|
var _reduxThunk = _interopRequireDefault(require("redux-thunk"));
|
|
41
|
-
|
|
42
28
|
var _reactRedux = require("react-redux");
|
|
43
|
-
|
|
44
29
|
var _history = require("history");
|
|
45
|
-
|
|
46
30
|
var _Nav = _interopRequireDefault(require("./components/Nav"));
|
|
47
|
-
|
|
48
31
|
var _react2 = require("./utils/react");
|
|
49
|
-
|
|
50
32
|
exports.mapStateToProps = _react2.mapStateToProps;
|
|
51
33
|
exports.mapDispatchToProps = _react2.mapDispatchToProps;
|
|
52
34
|
exports.mapDispatchToPropsIncludingVisitAndRemote = _react2.mapDispatchToPropsIncludingVisitAndRemote;
|
|
53
|
-
|
|
54
35
|
var _middleware = require("./middleware");
|
|
55
|
-
|
|
56
36
|
exports.fragmentMiddleware = _middleware.fragmentMiddleware;
|
|
57
|
-
|
|
58
37
|
var _immutability = require("./utils/immutability");
|
|
59
|
-
|
|
60
38
|
exports.getIn = _immutability.getIn;
|
|
61
|
-
|
|
62
39
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
63
|
-
|
|
64
40
|
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
|
|
65
|
-
|
|
66
41
|
function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); }
|
|
67
|
-
|
|
68
42
|
function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct; } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
|
|
69
|
-
|
|
70
43
|
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
|
71
|
-
|
|
72
44
|
function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; }
|
|
73
|
-
|
|
74
45
|
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
|
75
|
-
|
|
76
46
|
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
|
77
|
-
|
|
78
47
|
function pageToInitialState(key, page) {
|
|
79
48
|
var _pages;
|
|
80
|
-
|
|
81
49
|
var slices = page.slices || {};
|
|
82
50
|
return Object.assign({
|
|
83
51
|
pages: (_pages = {}, _pages[key] = page, _pages)
|
|
84
52
|
}, slices);
|
|
85
53
|
}
|
|
86
|
-
|
|
87
54
|
function start(_ref) {
|
|
88
55
|
var initialPage = _ref.initialPage,
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
56
|
+
_ref$baseUrl = _ref.baseUrl,
|
|
57
|
+
baseUrl = _ref$baseUrl === void 0 ? _config.config.baseUrl : _ref$baseUrl,
|
|
58
|
+
_ref$maxPages = _ref.maxPages,
|
|
59
|
+
maxPages = _ref$maxPages === void 0 ? _config.config.maxPages : _ref$maxPages,
|
|
60
|
+
path = _ref.path;
|
|
94
61
|
var initialPageKey = (0, _utils.urlToPageKey)((0, _urlParse["default"])(path).href);
|
|
95
62
|
var csrfToken = initialPage.csrfToken;
|
|
96
63
|
var location = (0, _urlParse["default"])(path);
|
|
@@ -119,91 +86,80 @@ function start(_ref) {
|
|
|
119
86
|
initialPageKey: initialPageKey
|
|
120
87
|
};
|
|
121
88
|
}
|
|
122
|
-
|
|
123
89
|
var NotImplementedError = /*#__PURE__*/function (_Error) {
|
|
124
90
|
_inheritsLoose(NotImplementedError, _Error);
|
|
125
|
-
|
|
126
91
|
function NotImplementedError(message) {
|
|
127
92
|
var _this;
|
|
128
|
-
|
|
129
93
|
_this = _Error.call(this, message) || this;
|
|
130
94
|
_this.name = _this.constructor.name;
|
|
131
95
|
return _this;
|
|
132
96
|
}
|
|
133
|
-
|
|
134
97
|
return NotImplementedError;
|
|
135
98
|
}( /*#__PURE__*/_wrapNativeSuper(Error));
|
|
136
|
-
|
|
137
99
|
var ApplicationBase = exports.ApplicationBase = /*#__PURE__*/function (_React$Component) {
|
|
138
100
|
_inheritsLoose(ApplicationBase, _React$Component);
|
|
139
|
-
|
|
140
101
|
function ApplicationBase(props) {
|
|
141
102
|
var _this2$history;
|
|
142
|
-
|
|
143
103
|
var _this2;
|
|
144
|
-
|
|
145
104
|
_this2 = _React$Component.call(this, props) || this;
|
|
146
|
-
_this2.hasWindow = typeof window !== 'undefined';
|
|
147
|
-
// and `visit` thunks
|
|
105
|
+
_this2.hasWindow = typeof window !== 'undefined';
|
|
148
106
|
|
|
149
|
-
|
|
107
|
+
// Create a navigator Ref for UJS attributes and to enhance the base `visit`
|
|
108
|
+
// and `visit` thunks
|
|
109
|
+
_this2.navigatorRef = /*#__PURE__*/_react["default"].createRef();
|
|
150
110
|
|
|
111
|
+
// Retrieve initial values and methods to prepare the store.
|
|
151
112
|
var _start = start({
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
113
|
+
initialPage: _this2.props.initialPage,
|
|
114
|
+
baseUrl: _this2.props.baseUrl,
|
|
115
|
+
path: _this2.props.path
|
|
116
|
+
// The max number of pages to keep in the store. Default is 20
|
|
117
|
+
// maxPages: 20
|
|
118
|
+
}),
|
|
119
|
+
prepareStore = _start.prepareStore,
|
|
120
|
+
initialState = _start.initialState,
|
|
121
|
+
initialPageKey = _start.initialPageKey,
|
|
122
|
+
reducer = _start.reducer;
|
|
123
|
+
_this2.initialPageKey = initialPageKey;
|
|
124
|
+
|
|
125
|
+
// Build the store and pass Superglue's provided reducer to be combined with
|
|
164
126
|
// your reducers located at `application_reducer.js`
|
|
127
|
+
_this2.store = _this2.buildStore(initialState, reducer);
|
|
165
128
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
prepareStore(_this2.store); // Build history
|
|
129
|
+
// Fire initial events and populate the store
|
|
130
|
+
prepareStore(_this2.store);
|
|
169
131
|
|
|
132
|
+
// Build history
|
|
170
133
|
_this2.history = _this2.createHistory();
|
|
171
|
-
|
|
172
134
|
(_this2$history = _this2.history).replace.apply(_this2$history, (0, _utils.argsForHistory)(_this2.props.path));
|
|
173
|
-
|
|
174
135
|
var nextMapping = Object.assign({}, _this2.mapping());
|
|
175
|
-
|
|
176
136
|
for (var key in nextMapping) {
|
|
177
137
|
var component = nextMapping[key];
|
|
178
138
|
nextMapping[key] = (0, _reactRedux.connect)(_react2.mapStateToProps, _react2.mapDispatchToProps)(component);
|
|
179
139
|
}
|
|
140
|
+
_this2.connectedMapping = nextMapping;
|
|
180
141
|
|
|
181
|
-
|
|
142
|
+
// Build visit and remote thunks
|
|
182
143
|
// Your modified `visit` and `remote` will get passed below to the
|
|
183
144
|
// Nav component then to your components
|
|
184
145
|
//
|
|
185
146
|
// You can access them via `this.props.visit` or `this.props.remote`. In
|
|
186
147
|
// your page components
|
|
187
|
-
|
|
188
148
|
var _this2$visitAndRemote = _this2.visitAndRemote(_this2.navigatorRef, _this2.store),
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
149
|
+
visit = _this2$visitAndRemote.visit,
|
|
150
|
+
remote = _this2$visitAndRemote.remote;
|
|
192
151
|
_this2.visit = visit;
|
|
193
152
|
_this2.remote = remote;
|
|
194
153
|
return _this2;
|
|
195
154
|
}
|
|
196
|
-
|
|
197
155
|
var _proto = ApplicationBase.prototype;
|
|
198
|
-
|
|
199
156
|
_proto.visitAndRemote = function visitAndRemote() {
|
|
200
157
|
throw new NotImplementedError('Implement this');
|
|
201
158
|
};
|
|
202
|
-
|
|
203
159
|
_proto.componentDidMount = function componentDidMount() {
|
|
204
|
-
var appEl = this.props.appEl;
|
|
160
|
+
var appEl = this.props.appEl;
|
|
161
|
+
// Create the ujs event handlers. You can change the ujsAttributePrefix
|
|
205
162
|
// in the event the data attribute conflicts with another.
|
|
206
|
-
|
|
207
163
|
this.ujsHandlers = (0, _utils.ujsHandlers)({
|
|
208
164
|
visit: this.visit,
|
|
209
165
|
remote: this.remote,
|
|
@@ -211,26 +167,23 @@ var ApplicationBase = exports.ApplicationBase = /*#__PURE__*/function (_React$Co
|
|
|
211
167
|
ujsAttributePrefix: 'data-sg'
|
|
212
168
|
});
|
|
213
169
|
var _this$ujsHandlers = this.ujsHandlers,
|
|
214
|
-
|
|
215
|
-
|
|
170
|
+
onClick = _this$ujsHandlers.onClick,
|
|
171
|
+
onSubmit = _this$ujsHandlers.onSubmit;
|
|
216
172
|
appEl.addEventListener('click', onClick);
|
|
217
173
|
appEl.addEventListener('submit', onSubmit);
|
|
218
174
|
};
|
|
219
|
-
|
|
220
175
|
_proto.componentWillUnmount = function componentWillUnmount() {
|
|
221
176
|
var appEl = this.props.appEl;
|
|
222
177
|
var _this$ujsHandlers2 = this.ujsHandlers,
|
|
223
|
-
|
|
224
|
-
|
|
178
|
+
onClick = _this$ujsHandlers2.onClick,
|
|
179
|
+
onSubmit = _this$ujsHandlers2.onSubmit;
|
|
225
180
|
appEl.removeEventListener('click', onClick);
|
|
226
181
|
appEl.removeEventListener('submit', onSubmit);
|
|
227
182
|
};
|
|
228
|
-
|
|
229
183
|
_proto.buildStore = function buildStore(initialState, reducer) {
|
|
230
184
|
var store = (0, _redux.createStore)((0, _redux.combineReducers)(reducer), initialState, (0, _redux.compose)((0, _redux.applyMiddleware)(_reduxThunk["default"])));
|
|
231
185
|
return store;
|
|
232
186
|
};
|
|
233
|
-
|
|
234
187
|
_proto.createHistory = function createHistory() {
|
|
235
188
|
if (this.hasWindow) {
|
|
236
189
|
// This is used for client side rendering
|
|
@@ -240,11 +193,9 @@ var ApplicationBase = exports.ApplicationBase = /*#__PURE__*/function (_React$Co
|
|
|
240
193
|
return (0, _history.createMemoryHistory)({});
|
|
241
194
|
}
|
|
242
195
|
};
|
|
243
|
-
|
|
244
196
|
_proto.mapping = function mapping() {
|
|
245
197
|
throw new NotImplementedError('Implement this');
|
|
246
198
|
};
|
|
247
|
-
|
|
248
199
|
_proto.render = function render() {
|
|
249
200
|
// The Nav component is pretty bare and can be inherited from for custom
|
|
250
201
|
// behavior or replaced with your own.
|
|
@@ -260,6 +211,5 @@ var ApplicationBase = exports.ApplicationBase = /*#__PURE__*/function (_React$Co
|
|
|
260
211
|
initialPageKey: this.initialPageKey
|
|
261
212
|
}));
|
|
262
213
|
};
|
|
263
|
-
|
|
264
214
|
return ApplicationBase;
|
|
265
215
|
}(_react["default"].Component);
|
package/middleware.js
CHANGED
|
@@ -2,49 +2,37 @@
|
|
|
2
2
|
|
|
3
3
|
exports.__esModule = true;
|
|
4
4
|
exports.fragmentMiddleware = void 0;
|
|
5
|
-
|
|
6
5
|
var actions = _interopRequireWildcard(require("./actions"));
|
|
7
|
-
|
|
8
6
|
var _immutability = require("./utils/immutability");
|
|
9
|
-
|
|
10
7
|
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
11
|
-
|
|
12
8
|
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
13
|
-
|
|
14
9
|
var actionValues = Object.values(actions);
|
|
15
|
-
|
|
16
10
|
var fragmentMiddleware = exports.fragmentMiddleware = function fragmentMiddleware(store) {
|
|
17
11
|
return function (next) {
|
|
18
12
|
return function (action) {
|
|
19
13
|
var prevState = store.getState();
|
|
20
14
|
var nextAction = next(action);
|
|
21
15
|
var nextState = store.getState();
|
|
22
|
-
|
|
23
16
|
if (actionValues.includes(action.type)) {
|
|
24
17
|
return nextAction;
|
|
25
18
|
}
|
|
26
|
-
|
|
27
19
|
if (prevState.pages === nextState.pages) {
|
|
28
20
|
return nextAction;
|
|
29
21
|
}
|
|
30
|
-
|
|
31
22
|
var changedFragments = {};
|
|
32
23
|
var changedKeys = Object.keys(nextState.pages).filter(function (key) {
|
|
33
24
|
return prevState.pages[key] !== nextState.pages[key];
|
|
34
25
|
});
|
|
35
|
-
|
|
36
26
|
if (changedKeys.length === 0) {
|
|
37
27
|
return nextAction;
|
|
38
28
|
}
|
|
39
|
-
|
|
40
29
|
changedKeys.forEach(function (key) {
|
|
41
30
|
nextState.pages[key].fragments.forEach(function (fragment) {
|
|
42
31
|
var type = fragment.type,
|
|
43
|
-
|
|
32
|
+
path = fragment.path;
|
|
44
33
|
var nextPage = nextState.pages[key];
|
|
45
34
|
var prevPage = prevState.pages[key];
|
|
46
35
|
var nextFragment, prevFragment;
|
|
47
|
-
|
|
48
36
|
try {
|
|
49
37
|
prevFragment = (0, _immutability.getIn)(prevPage, path);
|
|
50
38
|
nextFragment = (0, _immutability.getIn)(nextPage, path);
|
|
@@ -55,17 +43,14 @@ var fragmentMiddleware = exports.fragmentMiddleware = function fragmentMiddlewar
|
|
|
55
43
|
throw err;
|
|
56
44
|
}
|
|
57
45
|
}
|
|
58
|
-
|
|
59
46
|
if (nextFragment !== undefined && prevFragment !== undefined && nextFragment !== prevFragment && nextFragment) {
|
|
60
47
|
changedFragments[type] = nextFragment;
|
|
61
48
|
}
|
|
62
49
|
});
|
|
63
50
|
});
|
|
64
|
-
|
|
65
51
|
if (Object.keys(changedFragments).length === 0) {
|
|
66
52
|
return nextAction;
|
|
67
53
|
}
|
|
68
|
-
|
|
69
54
|
store.dispatch({
|
|
70
55
|
type: actions.UPDATE_FRAGMENTS,
|
|
71
56
|
payload: {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thoughtbot/superglue",
|
|
3
|
-
"version": "0.53.
|
|
3
|
+
"version": "0.53.4",
|
|
4
4
|
"description": "Use a vanilla Rails with React and Redux",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -17,6 +17,9 @@
|
|
|
17
17
|
"@babel/core": "^7.14.3",
|
|
18
18
|
"@babel/preset-env": "^7.14.4",
|
|
19
19
|
"@babel/preset-react": "^7.13.13",
|
|
20
|
+
"@babel/preset-typescript": "^7.24.6",
|
|
21
|
+
"@typescript-eslint/eslint-plugin": "^4.28.5",
|
|
22
|
+
"@typescript-eslint/parser": "^4.28.5",
|
|
20
23
|
"core-js": "^2.6.12",
|
|
21
24
|
"enzyme": "^3.11.0",
|
|
22
25
|
"enzyme-adapter-react-16": "^1.15.6",
|
package/reducers/index.js
CHANGED
|
@@ -8,16 +8,12 @@ exports.metaReducer = metaReducer;
|
|
|
8
8
|
exports.pageReducer = pageReducer;
|
|
9
9
|
exports.rootReducer = void 0;
|
|
10
10
|
exports.superglueReducer = superglueReducer;
|
|
11
|
-
|
|
12
11
|
var _utils = require("../utils");
|
|
13
|
-
|
|
14
12
|
var _actions = require("../actions");
|
|
15
|
-
|
|
16
13
|
var _config = require("../config");
|
|
17
|
-
|
|
18
14
|
function addPlaceholdersToDeferredNodes(existingPage, page) {
|
|
19
15
|
var _existingPage$defers = existingPage.defers,
|
|
20
|
-
|
|
16
|
+
defers = _existingPage$defers === void 0 ? [] : _existingPage$defers;
|
|
21
17
|
var prevDefers = defers.map(function (_ref) {
|
|
22
18
|
var path = _ref.path;
|
|
23
19
|
var node = (0, _utils.getIn)(existingPage, path);
|
|
@@ -26,11 +22,10 @@ function addPlaceholdersToDeferredNodes(existingPage, page) {
|
|
|
26
22
|
});
|
|
27
23
|
return prevDefers.reduce(function (memo, _ref2) {
|
|
28
24
|
var path = _ref2[0],
|
|
29
|
-
|
|
25
|
+
node = _ref2[1];
|
|
30
26
|
return (0, _utils.setIn)(page, path, node);
|
|
31
27
|
}, page);
|
|
32
28
|
}
|
|
33
|
-
|
|
34
29
|
function constrainPagesSize(state) {
|
|
35
30
|
var maxPages = _config.config.maxPages;
|
|
36
31
|
var allPageKeys = Object.keys(state);
|
|
@@ -39,16 +34,13 @@ function constrainPagesSize(state) {
|
|
|
39
34
|
}).sort(function (a, b) {
|
|
40
35
|
return b - a;
|
|
41
36
|
});
|
|
42
|
-
|
|
43
37
|
for (var _i = 0, _Array$from = Array.from(allPageKeys); _i < _Array$from.length; _i++) {
|
|
44
38
|
var key = _Array$from[_i];
|
|
45
|
-
|
|
46
39
|
if (state[key].savedAt <= cacheTimesRecentFirst[maxPages - 1]) {
|
|
47
40
|
delete state[key];
|
|
48
41
|
}
|
|
49
42
|
}
|
|
50
43
|
}
|
|
51
|
-
|
|
52
44
|
function saveResponse(state, pageKey, page) {
|
|
53
45
|
state = Object.assign({}, state);
|
|
54
46
|
page = Object.assign({
|
|
@@ -58,28 +50,23 @@ function saveResponse(state, pageKey, page) {
|
|
|
58
50
|
savedAt: Date.now()
|
|
59
51
|
});
|
|
60
52
|
var existingPage = state[pageKey];
|
|
61
|
-
|
|
62
53
|
if (existingPage) {
|
|
63
54
|
page = addPlaceholdersToDeferredNodes(existingPage, page);
|
|
64
55
|
}
|
|
65
|
-
|
|
66
56
|
constrainPagesSize(state);
|
|
67
57
|
state[pageKey] = page;
|
|
68
58
|
return state;
|
|
69
59
|
}
|
|
70
|
-
|
|
71
60
|
function appendReceivedFragmentsOntoPage(state, pageKey, receivedFragments) {
|
|
72
61
|
if (!pageKey) {
|
|
73
62
|
return state;
|
|
74
63
|
}
|
|
75
|
-
|
|
76
64
|
if (receivedFragments.length === 0) {
|
|
77
65
|
return state;
|
|
78
66
|
}
|
|
79
|
-
|
|
80
67
|
var currentPage = state[pageKey];
|
|
81
68
|
var _currentPage$fragment = currentPage.fragments,
|
|
82
|
-
|
|
69
|
+
prevFragments = _currentPage$fragment === void 0 ? [] : _currentPage$fragment;
|
|
83
70
|
var nextFragments = [].concat(prevFragments);
|
|
84
71
|
var existingKeys = {};
|
|
85
72
|
prevFragments.forEach(function (frag) {
|
|
@@ -97,33 +84,27 @@ function appendReceivedFragmentsOntoPage(state, pageKey, receivedFragments) {
|
|
|
97
84
|
nextState[pageKey] = nextPage;
|
|
98
85
|
return nextState;
|
|
99
86
|
}
|
|
100
|
-
|
|
101
87
|
function graftNodeOntoPage(state, pageKey, node, pathToNode) {
|
|
102
88
|
if (!node) {
|
|
103
89
|
console.warn('There was no node returned in the response. Do you have the correct key path in your props_at?');
|
|
104
90
|
return state;
|
|
105
91
|
}
|
|
106
|
-
|
|
107
92
|
if (!pathToNode || !pageKey) {
|
|
108
93
|
return state;
|
|
109
94
|
}
|
|
110
|
-
|
|
111
95
|
var fullPathToNode = [pageKey, pathToNode].join('.');
|
|
112
96
|
return (0, _utils.setIn)(state, fullPathToNode, node);
|
|
113
97
|
}
|
|
114
|
-
|
|
115
98
|
function handleGraft(state, pageKey, page) {
|
|
116
99
|
var currentPage = state[pageKey];
|
|
117
|
-
|
|
118
100
|
if (!currentPage) {
|
|
119
101
|
var error = new Error("Superglue was looking for " + pageKey + " in your state, but could not find it in your mapping. Did you forget to pass in a valid pageKey to this.props.remote or this.props.visit?");
|
|
120
102
|
throw error;
|
|
121
103
|
}
|
|
122
|
-
|
|
123
104
|
var receivedNode = page.data,
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
105
|
+
pathToNode = page.path,
|
|
106
|
+
_page$fragments = page.fragments,
|
|
107
|
+
receivedFragments = _page$fragments === void 0 ? [] : _page$fragments;
|
|
127
108
|
return [function (nextState) {
|
|
128
109
|
return graftNodeOntoPage(nextState, pageKey, receivedNode, pathToNode);
|
|
129
110
|
}, function (nextState) {
|
|
@@ -132,42 +113,37 @@ function handleGraft(state, pageKey, page) {
|
|
|
132
113
|
return fn(memo);
|
|
133
114
|
}, state);
|
|
134
115
|
}
|
|
135
|
-
|
|
136
116
|
function pageReducer(state, action) {
|
|
137
117
|
if (state === void 0) {
|
|
138
118
|
state = {};
|
|
139
119
|
}
|
|
140
|
-
|
|
141
120
|
switch (action.type) {
|
|
142
121
|
case _actions.SAVE_RESPONSE:
|
|
143
122
|
{
|
|
144
123
|
var _action$payload = action.payload,
|
|
145
|
-
|
|
146
|
-
|
|
124
|
+
pageKey = _action$payload.pageKey,
|
|
125
|
+
page = _action$payload.page;
|
|
147
126
|
return saveResponse(state, pageKey, page);
|
|
148
127
|
}
|
|
149
|
-
|
|
150
128
|
case _actions.HANDLE_GRAFT:
|
|
151
129
|
{
|
|
152
130
|
var _action$payload2 = action.payload,
|
|
153
|
-
|
|
154
|
-
|
|
131
|
+
_pageKey = _action$payload2.pageKey,
|
|
132
|
+
_page = _action$payload2.page;
|
|
155
133
|
return handleGraft(state, _pageKey, _page);
|
|
156
134
|
}
|
|
157
|
-
|
|
158
135
|
case _actions.UPDATE_FRAGMENTS:
|
|
159
136
|
{
|
|
160
137
|
var changedFragments = action.payload.changedFragments;
|
|
161
138
|
var nextState = state;
|
|
162
139
|
Object.entries(state).forEach(function (_ref3) {
|
|
163
140
|
var pageKey = _ref3[0],
|
|
164
|
-
|
|
141
|
+
page = _ref3[1];
|
|
165
142
|
page.fragments.forEach(function (fragment) {
|
|
166
143
|
var type = fragment.type,
|
|
167
|
-
|
|
144
|
+
path = fragment.path;
|
|
168
145
|
var changedNode = changedFragments[type];
|
|
169
146
|
var currentNode = (0, _utils.getIn)(nextState, pageKey + "." + path);
|
|
170
|
-
|
|
171
147
|
if (type in changedFragments && changedNode !== currentNode) {
|
|
172
148
|
var nextNode = JSON.parse(JSON.stringify(changedNode));
|
|
173
149
|
nextState = (0, _utils.setIn)(nextState, pageKey + "." + path, nextNode);
|
|
@@ -176,45 +152,37 @@ function pageReducer(state, action) {
|
|
|
176
152
|
});
|
|
177
153
|
return nextState;
|
|
178
154
|
}
|
|
179
|
-
|
|
180
155
|
case _actions.COPY_PAGE:
|
|
181
156
|
{
|
|
182
157
|
var _nextState = Object.assign({}, state);
|
|
183
|
-
|
|
184
158
|
var _action$payload3 = action.payload,
|
|
185
|
-
|
|
186
|
-
|
|
159
|
+
from = _action$payload3.from,
|
|
160
|
+
to = _action$payload3.to;
|
|
187
161
|
_nextState[(0, _utils.urlToPageKey)(to)] = JSON.parse(JSON.stringify(_nextState[from]));
|
|
188
162
|
return _nextState;
|
|
189
163
|
}
|
|
190
|
-
|
|
191
164
|
case _actions.REMOVE_PAGE:
|
|
192
165
|
{
|
|
193
166
|
var _pageKey2 = action.payload.pageKey;
|
|
194
|
-
|
|
195
167
|
var _nextState2 = Object.assign({}, state);
|
|
196
|
-
|
|
197
168
|
delete _nextState2[_pageKey2];
|
|
198
169
|
return _nextState2;
|
|
199
170
|
}
|
|
200
|
-
|
|
201
171
|
default:
|
|
202
172
|
return state;
|
|
203
173
|
}
|
|
204
174
|
}
|
|
205
|
-
|
|
206
175
|
function metaReducer(state, action) {
|
|
207
176
|
if (state === void 0) {
|
|
208
177
|
state = {};
|
|
209
178
|
}
|
|
210
|
-
|
|
211
179
|
switch (action.type) {
|
|
212
180
|
case _actions.HISTORY_CHANGE:
|
|
213
181
|
{
|
|
214
182
|
var _action$payload4 = action.payload,
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
183
|
+
pathname = _action$payload4.pathname,
|
|
184
|
+
search = _action$payload4.search,
|
|
185
|
+
hash = _action$payload4.hash;
|
|
218
186
|
var currentPageKey = (0, _utils.urlToPageKey)(pathname + search);
|
|
219
187
|
return Object.assign({}, state, {
|
|
220
188
|
currentPageKey: currentPageKey,
|
|
@@ -223,18 +191,16 @@ function metaReducer(state, action) {
|
|
|
223
191
|
hash: hash
|
|
224
192
|
});
|
|
225
193
|
}
|
|
226
|
-
|
|
227
194
|
case _actions.SAVE_RESPONSE:
|
|
228
195
|
{
|
|
229
196
|
var _action$payload$page = action.payload.page,
|
|
230
|
-
|
|
231
|
-
|
|
197
|
+
csrfToken = _action$payload$page.csrfToken,
|
|
198
|
+
assets = _action$payload$page.assets;
|
|
232
199
|
return Object.assign({}, state, {
|
|
233
200
|
csrfToken: csrfToken,
|
|
234
201
|
assets: assets
|
|
235
202
|
});
|
|
236
203
|
}
|
|
237
|
-
|
|
238
204
|
case _actions.SET_CSRF_TOKEN:
|
|
239
205
|
{
|
|
240
206
|
var _csrfToken = action.payload.csrfToken;
|
|
@@ -242,21 +208,17 @@ function metaReducer(state, action) {
|
|
|
242
208
|
csrfToken: _csrfToken
|
|
243
209
|
});
|
|
244
210
|
}
|
|
245
|
-
|
|
246
211
|
default:
|
|
247
212
|
return state;
|
|
248
213
|
}
|
|
249
214
|
}
|
|
250
|
-
|
|
251
215
|
function superglueReducer(state, action) {
|
|
252
216
|
if (state === void 0) {
|
|
253
217
|
state = {};
|
|
254
218
|
}
|
|
255
|
-
|
|
256
219
|
var meta = metaReducer(state, action);
|
|
257
220
|
return Object.assign({}, meta);
|
|
258
221
|
}
|
|
259
|
-
|
|
260
222
|
var rootReducer = exports.rootReducer = {
|
|
261
223
|
superglue: superglueReducer,
|
|
262
224
|
pages: pageReducer
|
package/utils/helpers.js
CHANGED
|
@@ -4,18 +4,14 @@ exports.__esModule = true;
|
|
|
4
4
|
exports.argsForHistory = argsForHistory;
|
|
5
5
|
exports.extractNodeAndPath = extractNodeAndPath;
|
|
6
6
|
exports.isGraft = isGraft;
|
|
7
|
-
|
|
8
7
|
var _url = require("./url");
|
|
9
|
-
|
|
10
8
|
function isGraft(page) {
|
|
11
9
|
return page.action === 'graft';
|
|
12
10
|
}
|
|
13
|
-
|
|
14
11
|
function extractNodeAndPath(page) {
|
|
15
12
|
var node = page.data,
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
13
|
+
action = page.action,
|
|
14
|
+
pathToNode = page.path;
|
|
19
15
|
if (action === 'graft') {
|
|
20
16
|
return {
|
|
21
17
|
node: node,
|
|
@@ -26,7 +22,6 @@ function extractNodeAndPath(page) {
|
|
|
26
22
|
throw new Error(errMsg);
|
|
27
23
|
}
|
|
28
24
|
}
|
|
29
|
-
|
|
30
25
|
function argsForHistory(path) {
|
|
31
26
|
var pageKey = (0, _url.urlToPageKey)(path);
|
|
32
27
|
return [path, {
|