@thoughtbot/superglue 0.53.4 → 0.54.0

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/actions.js DELETED
@@ -1,17 +0,0 @@
1
- "use strict";
2
-
3
- exports.__esModule = true;
4
- exports.UPDATE_FRAGMENTS = exports.SUPERGLUE_ERROR = exports.SET_CSRF_TOKEN = exports.SAVE_RESPONSE = exports.REMOVE_PAGE = exports.HISTORY_CHANGE = exports.HANDLE_GRAFT = exports.GRAFTING_SUCCESS = exports.GRAFTING_ERROR = exports.COPY_PAGE = exports.BEFORE_VISIT = exports.BEFORE_REMOTE = exports.BEFORE_FETCH = void 0;
5
- var BEFORE_FETCH = exports.BEFORE_FETCH = '@@superglue/BEFORE_FETCH';
6
- var BEFORE_VISIT = exports.BEFORE_VISIT = '@@superglue/BEFORE_VISIT';
7
- var BEFORE_REMOTE = exports.BEFORE_REMOTE = '@@superglue/BEFORE_REMOTE';
8
- var SAVE_RESPONSE = exports.SAVE_RESPONSE = '@@superglue/SAVE_RESPONSE';
9
- var HANDLE_GRAFT = exports.HANDLE_GRAFT = '@@superglue/HANDLE_GRAFT';
10
- var SUPERGLUE_ERROR = exports.SUPERGLUE_ERROR = '@@superglue/ERROR';
11
- var GRAFTING_ERROR = exports.GRAFTING_ERROR = '@@superglue/GRAFTING_ERROR';
12
- var GRAFTING_SUCCESS = exports.GRAFTING_SUCCESS = '@@superglue/GRAFTING_SUCCESS';
13
- var HISTORY_CHANGE = exports.HISTORY_CHANGE = '@@superglue/HISTORY_CHANGE';
14
- var SET_CSRF_TOKEN = exports.SET_CSRF_TOKEN = '@@superglue/SET_CSRF_TOKEN';
15
- var REMOVE_PAGE = exports.REMOVE_PAGE = '@@superglue/REMOVE_PAGE';
16
- var COPY_PAGE = exports.COPY_PAGE = '@@superglue/COPY_PAGE';
17
- var UPDATE_FRAGMENTS = exports.UPDATE_FRAGMENTS = '@@superglue/UPDATE_FRAGMENTS';
package/components/Nav.js DELETED
@@ -1,211 +0,0 @@
1
- "use strict";
2
-
3
- exports.__esModule = true;
4
- exports["default"] = void 0;
5
- var _react = _interopRequireDefault(require("react"));
6
- var _utils = require("../utils");
7
- var _actions = require("../actions");
8
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
9
- function _extends() { _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; }; return _extends.apply(this, arguments); }
10
- function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
11
- function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
12
- function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
13
- var Nav = /*#__PURE__*/function (_React$Component) {
14
- _inheritsLoose(Nav, _React$Component);
15
- function Nav(props) {
16
- var _this;
17
- _this = _React$Component.call(this, props) || this;
18
- var _this$props = _this.props,
19
- history = _this$props.history,
20
- initialPageKey = _this$props.initialPageKey;
21
- _this.history = history;
22
- _this.navigateTo = _this.navigateTo.bind(_assertThisInitialized(_this));
23
- _this.scrollTo = _this.scrollTo.bind(_assertThisInitialized(_this));
24
- _this.onHistoryChange = _this.onHistoryChange.bind(_assertThisInitialized(_this));
25
- _this.state = {
26
- pageKey: initialPageKey,
27
- ownProps: {}
28
- };
29
- _this.hasWindow = typeof window !== 'undefined';
30
- return _this;
31
- }
32
- var _proto = Nav.prototype;
33
- _proto.componentDidMount = function componentDidMount() {
34
- this.unsubscribeHistory = this.history.listen(this.onHistoryChange);
35
- };
36
- _proto.componentWillUnmount = function componentWillUnmount() {
37
- this.unsubscribeHistory();
38
- };
39
- _proto.navigateTo = function navigateTo(path, _temp) {
40
- var _ref = _temp === void 0 ? {
41
- action: 'push',
42
- ownProps: {}
43
- } : _temp,
44
- action = _ref.action,
45
- ownProps = _ref.ownProps;
46
- if (action === 'none') {
47
- return false;
48
- }
49
- path = (0, _utils.pathWithoutBZParams)(path);
50
- var nextPageKey = (0, _utils.urlToPageKey)(path);
51
- var store = this.props.store;
52
- var hasPage = !!store.getState().pages[nextPageKey];
53
- if (hasPage) {
54
- var location = this.history.location;
55
- var prevPageKey = location.state.pageKey;
56
- var historyArgs = [path, {
57
- pageKey: nextPageKey,
58
- superglue: true,
59
- posY: 0,
60
- posX: 0
61
- }];
62
- if (action === 'push') {
63
- var _this$history;
64
- if (this.hasWindow) {
65
- this.history.replace({
66
- pathname: location.pathname,
67
- search: location.search,
68
- hash: location.hash
69
- }, Object.assign({}, location.state, {
70
- posY: window.pageYOffset,
71
- posX: window.pageXOffset
72
- }));
73
- }
74
- (_this$history = this.history).push.apply(_this$history, historyArgs);
75
- }
76
- if (action === 'replace') {
77
- var _this$history2;
78
- (_this$history2 = this.history).replace.apply(_this$history2, historyArgs);
79
- }
80
- this.setState({
81
- pageKey: nextPageKey,
82
- ownProps: ownProps
83
- });
84
- this.scrollTo(0, 0);
85
- if (action === 'replace' && prevPageKey && prevPageKey !== nextPageKey) {
86
- store.dispatch({
87
- type: _actions.REMOVE_PAGE,
88
- payload: {
89
- pageKey: prevPageKey
90
- }
91
- });
92
- }
93
- return true;
94
- } else {
95
- console.warn("`navigateTo` was called , but could not find.\n the pageKey in the store. This may happen when the wrong\n content_location was set in your non-get controller action.\n No navigation will take place");
96
- return false;
97
- }
98
- };
99
- _proto.scrollTo = function scrollTo(posX, posY) {
100
- this.hasWindow && window.scrollTo(posX, posY);
101
- };
102
- _proto.onHistoryChange = function onHistoryChange(_ref2) {
103
- var _this2 = this;
104
- var location = _ref2.location,
105
- action = _ref2.action;
106
- var _this$props2 = this.props,
107
- store = _this$props2.store,
108
- visit = _this$props2.visit;
109
- var pathname = location.pathname,
110
- search = location.search,
111
- hash = location.hash,
112
- state = location.state;
113
- if (state && state.superglue) {
114
- store.dispatch({
115
- type: _actions.HISTORY_CHANGE,
116
- payload: {
117
- pathname: pathname,
118
- search: search,
119
- hash: hash
120
- }
121
- });
122
- if (action !== 'POP') {
123
- return;
124
- }
125
- var pageKey = state.pageKey,
126
- posX = state.posX,
127
- posY = state.posY;
128
- var containsKey = !!store.getState().pages[pageKey];
129
- if (containsKey) {
130
- var restoreStrategy = store.getState().pages[pageKey].restoreStrategy;
131
- switch (restoreStrategy) {
132
- case 'fromCacheOnly':
133
- this.setState({
134
- pageKey: pageKey
135
- });
136
- this.scrollTo(posX, posY);
137
- break;
138
- case 'fromCacheAndRevisitInBackground':
139
- this.setState({
140
- pageKey: pageKey
141
- });
142
- this.scrollTo(posX, posY);
143
- visit(pageKey, {
144
- revisit: true
145
- });
146
- break;
147
- case 'revisitOnly':
148
- default:
149
- visit(pageKey, {
150
- revisit: true
151
- }).then(function (meta) {
152
- if (meta === undefined) {
153
- console.warn("scoll restoration was skipped. Your visit's then funtion\n should return the meta object it recieved if you want your\n application to restore the page's previous scroll.");
154
- }
155
- if (!!meta && meta.suggestedAction === 'none') {
156
- _this2.setState({
157
- pageKey: pageKey
158
- });
159
- _this2.scrollTo(posX, posY);
160
- }
161
- });
162
- }
163
- } else {
164
- visit(pageKey, {
165
- revisit: true
166
- }).then(function (meta) {
167
- if (meta === undefined) {
168
- console.warn("scoll restoration was skipped. Your visit's then funtion\n should return the meta object it recieved if you want your\n application to restore the page's previous scroll.");
169
- }
170
- if (!!meta && meta.suggestedAction === 'none') {
171
- _this2.setState({
172
- pageKey: pageKey
173
- });
174
- _this2.scrollTo(posX, posY);
175
- }
176
- });
177
- }
178
- }
179
- };
180
- _proto.notFound = function notFound(identifier) {
181
- var reminder = '';
182
- if (!identifier) {
183
- reminder = 'Did you forget to add `json.componentIdentifier` in your application.json.props layout?';
184
- }
185
- var error = new Error("Superglue Nav component was looking for " + identifier + " but could not find it in your mapping. " + reminder);
186
- throw error;
187
- };
188
- _proto.render = function render() {
189
- var _this$props3 = this.props,
190
- store = _this$props3.store,
191
- visit = _this$props3.visit,
192
- remote = _this$props3.remote;
193
- var _this$state = this.state,
194
- pageKey = _this$state.pageKey,
195
- ownProps = _this$state.ownProps;
196
- var componentIdentifier = store.getState().pages[pageKey].componentIdentifier;
197
- var Component = this.props.mapping[componentIdentifier];
198
- if (Component) {
199
- return /*#__PURE__*/_react["default"].createElement(Component, _extends({
200
- pageKey: pageKey,
201
- navigateTo: this.navigateTo,
202
- visit: visit,
203
- remote: remote
204
- }, ownProps));
205
- } else {
206
- this.notFound(componentIdentifier);
207
- }
208
- };
209
- return Nav;
210
- }(_react["default"].Component);
211
- var _default = exports["default"] = Nav;
package/config.js DELETED
@@ -1,8 +0,0 @@
1
- "use strict";
2
-
3
- exports.__esModule = true;
4
- exports.config = void 0;
5
- var config = exports.config = {
6
- baseUrl: '',
7
- maxPages: 20
8
- };
package/index.js DELETED
@@ -1,215 +0,0 @@
1
- "use strict";
2
-
3
- exports.__esModule = true;
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;
5
- var _react = _interopRequireDefault(require("react"));
6
- var _urlParse = _interopRequireDefault(require("url-parse"));
7
- var _reducers = require("./reducers");
8
- exports.rootReducer = _reducers.rootReducer;
9
- exports.superglueReducer = _reducers.superglueReducer;
10
- exports.pageReducer = _reducers.pageReducer;
11
- var _config = require("./config");
12
- var _utils = require("./utils");
13
- exports.urlToPageKey = _utils.urlToPageKey;
14
- var _action_creators = require("./action_creators");
15
- var _actions = require("./actions");
16
- exports.HISTORY_CHANGE = _actions.HISTORY_CHANGE;
17
- exports.BEFORE_FETCH = _actions.BEFORE_FETCH;
18
- exports.BEFORE_VISIT = _actions.BEFORE_VISIT;
19
- exports.BEFORE_REMOTE = _actions.BEFORE_REMOTE;
20
- exports.SAVE_RESPONSE = _actions.SAVE_RESPONSE;
21
- exports.UPDATE_FRAGMENTS = _actions.UPDATE_FRAGMENTS;
22
- exports.COPY_PAGE = _actions.COPY_PAGE;
23
- exports.REMOVE_PAGE = _actions.REMOVE_PAGE;
24
- exports.GRAFTING_ERROR = _actions.GRAFTING_ERROR;
25
- exports.GRAFTING_SUCCESS = _actions.GRAFTING_SUCCESS;
26
- var _redux = require("redux");
27
- var _reduxThunk = _interopRequireDefault(require("redux-thunk"));
28
- var _reactRedux = require("react-redux");
29
- var _history = require("history");
30
- var _Nav = _interopRequireDefault(require("./components/Nav"));
31
- var _react2 = require("./utils/react");
32
- exports.mapStateToProps = _react2.mapStateToProps;
33
- exports.mapDispatchToProps = _react2.mapDispatchToProps;
34
- exports.mapDispatchToPropsIncludingVisitAndRemote = _react2.mapDispatchToPropsIncludingVisitAndRemote;
35
- var _middleware = require("./middleware");
36
- exports.fragmentMiddleware = _middleware.fragmentMiddleware;
37
- var _immutability = require("./utils/immutability");
38
- exports.getIn = _immutability.getIn;
39
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
40
- function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
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); }
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); }
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; } }
44
- function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; }
45
- function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
46
- function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
47
- function pageToInitialState(key, page) {
48
- var _pages;
49
- var slices = page.slices || {};
50
- return Object.assign({
51
- pages: (_pages = {}, _pages[key] = page, _pages)
52
- }, slices);
53
- }
54
- function start(_ref) {
55
- var initialPage = _ref.initialPage,
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;
61
- var initialPageKey = (0, _utils.urlToPageKey)((0, _urlParse["default"])(path).href);
62
- var csrfToken = initialPage.csrfToken;
63
- var location = (0, _urlParse["default"])(path);
64
- _config.config.baseUrl = baseUrl;
65
- _config.config.maxPages = maxPages;
66
- return {
67
- reducer: _reducers.rootReducer,
68
- prepareStore: function prepareStore(store) {
69
- store.dispatch({
70
- type: _actions.HISTORY_CHANGE,
71
- payload: {
72
- pathname: location.pathname,
73
- search: location.query,
74
- hash: location.hash
75
- }
76
- });
77
- store.dispatch((0, _action_creators.saveAndProcessPage)(initialPageKey, initialPage));
78
- store.dispatch({
79
- type: _actions.SET_CSRF_TOKEN,
80
- payload: {
81
- csrfToken: csrfToken
82
- }
83
- });
84
- },
85
- initialState: pageToInitialState(initialPageKey, initialPage),
86
- initialPageKey: initialPageKey
87
- };
88
- }
89
- var NotImplementedError = /*#__PURE__*/function (_Error) {
90
- _inheritsLoose(NotImplementedError, _Error);
91
- function NotImplementedError(message) {
92
- var _this;
93
- _this = _Error.call(this, message) || this;
94
- _this.name = _this.constructor.name;
95
- return _this;
96
- }
97
- return NotImplementedError;
98
- }( /*#__PURE__*/_wrapNativeSuper(Error));
99
- var ApplicationBase = exports.ApplicationBase = /*#__PURE__*/function (_React$Component) {
100
- _inheritsLoose(ApplicationBase, _React$Component);
101
- function ApplicationBase(props) {
102
- var _this2$history;
103
- var _this2;
104
- _this2 = _React$Component.call(this, props) || this;
105
- _this2.hasWindow = typeof window !== 'undefined';
106
-
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();
110
-
111
- // Retrieve initial values and methods to prepare the store.
112
- var _start = start({
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
126
- // your reducers located at `application_reducer.js`
127
- _this2.store = _this2.buildStore(initialState, reducer);
128
-
129
- // Fire initial events and populate the store
130
- prepareStore(_this2.store);
131
-
132
- // Build history
133
- _this2.history = _this2.createHistory();
134
- (_this2$history = _this2.history).replace.apply(_this2$history, (0, _utils.argsForHistory)(_this2.props.path));
135
- var nextMapping = Object.assign({}, _this2.mapping());
136
- for (var key in nextMapping) {
137
- var component = nextMapping[key];
138
- nextMapping[key] = (0, _reactRedux.connect)(_react2.mapStateToProps, _react2.mapDispatchToProps)(component);
139
- }
140
- _this2.connectedMapping = nextMapping;
141
-
142
- // Build visit and remote thunks
143
- // Your modified `visit` and `remote` will get passed below to the
144
- // Nav component then to your components
145
- //
146
- // You can access them via `this.props.visit` or `this.props.remote`. In
147
- // your page components
148
- var _this2$visitAndRemote = _this2.visitAndRemote(_this2.navigatorRef, _this2.store),
149
- visit = _this2$visitAndRemote.visit,
150
- remote = _this2$visitAndRemote.remote;
151
- _this2.visit = visit;
152
- _this2.remote = remote;
153
- return _this2;
154
- }
155
- var _proto = ApplicationBase.prototype;
156
- _proto.visitAndRemote = function visitAndRemote() {
157
- throw new NotImplementedError('Implement this');
158
- };
159
- _proto.componentDidMount = function componentDidMount() {
160
- var appEl = this.props.appEl;
161
- // Create the ujs event handlers. You can change the ujsAttributePrefix
162
- // in the event the data attribute conflicts with another.
163
- this.ujsHandlers = (0, _utils.ujsHandlers)({
164
- visit: this.visit,
165
- remote: this.remote,
166
- store: this.store,
167
- ujsAttributePrefix: 'data-sg'
168
- });
169
- var _this$ujsHandlers = this.ujsHandlers,
170
- onClick = _this$ujsHandlers.onClick,
171
- onSubmit = _this$ujsHandlers.onSubmit;
172
- appEl.addEventListener('click', onClick);
173
- appEl.addEventListener('submit', onSubmit);
174
- };
175
- _proto.componentWillUnmount = function componentWillUnmount() {
176
- var appEl = this.props.appEl;
177
- var _this$ujsHandlers2 = this.ujsHandlers,
178
- onClick = _this$ujsHandlers2.onClick,
179
- onSubmit = _this$ujsHandlers2.onSubmit;
180
- appEl.removeEventListener('click', onClick);
181
- appEl.removeEventListener('submit', onSubmit);
182
- };
183
- _proto.buildStore = function buildStore(initialState, reducer) {
184
- var store = (0, _redux.createStore)((0, _redux.combineReducers)(reducer), initialState, (0, _redux.compose)((0, _redux.applyMiddleware)(_reduxThunk["default"])));
185
- return store;
186
- };
187
- _proto.createHistory = function createHistory() {
188
- if (this.hasWindow) {
189
- // This is used for client side rendering
190
- return (0, _history.createBrowserHistory)({});
191
- } else {
192
- // This is used for server side rendering
193
- return (0, _history.createMemoryHistory)({});
194
- }
195
- };
196
- _proto.mapping = function mapping() {
197
- throw new NotImplementedError('Implement this');
198
- };
199
- _proto.render = function render() {
200
- // The Nav component is pretty bare and can be inherited from for custom
201
- // behavior or replaced with your own.
202
- return /*#__PURE__*/_react["default"].createElement(_reactRedux.Provider, {
203
- store: this.store
204
- }, /*#__PURE__*/_react["default"].createElement(_Nav["default"], {
205
- store: this.store,
206
- ref: this.navigatorRef,
207
- visit: this.visit,
208
- remote: this.remote,
209
- mapping: this.connectedMapping,
210
- history: this.history,
211
- initialPageKey: this.initialPageKey
212
- }));
213
- };
214
- return ApplicationBase;
215
- }(_react["default"].Component);
package/middleware.js DELETED
@@ -1,63 +0,0 @@
1
- "use strict";
2
-
3
- exports.__esModule = true;
4
- exports.fragmentMiddleware = void 0;
5
- var actions = _interopRequireWildcard(require("./actions"));
6
- var _immutability = require("./utils/immutability");
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); }
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; }
9
- var actionValues = Object.values(actions);
10
- var fragmentMiddleware = exports.fragmentMiddleware = function fragmentMiddleware(store) {
11
- return function (next) {
12
- return function (action) {
13
- var prevState = store.getState();
14
- var nextAction = next(action);
15
- var nextState = store.getState();
16
- if (actionValues.includes(action.type)) {
17
- return nextAction;
18
- }
19
- if (prevState.pages === nextState.pages) {
20
- return nextAction;
21
- }
22
- var changedFragments = {};
23
- var changedKeys = Object.keys(nextState.pages).filter(function (key) {
24
- return prevState.pages[key] !== nextState.pages[key];
25
- });
26
- if (changedKeys.length === 0) {
27
- return nextAction;
28
- }
29
- changedKeys.forEach(function (key) {
30
- nextState.pages[key].fragments.forEach(function (fragment) {
31
- var type = fragment.type,
32
- path = fragment.path;
33
- var nextPage = nextState.pages[key];
34
- var prevPage = prevState.pages[key];
35
- var nextFragment, prevFragment;
36
- try {
37
- prevFragment = (0, _immutability.getIn)(prevPage, path);
38
- nextFragment = (0, _immutability.getIn)(nextPage, path);
39
- } catch (err) {
40
- if (err.name == 'KeyPathError') {
41
- console.warn(err.message);
42
- } else {
43
- throw err;
44
- }
45
- }
46
- if (nextFragment !== undefined && prevFragment !== undefined && nextFragment !== prevFragment && nextFragment) {
47
- changedFragments[type] = nextFragment;
48
- }
49
- });
50
- });
51
- if (Object.keys(changedFragments).length === 0) {
52
- return nextAction;
53
- }
54
- store.dispatch({
55
- type: actions.UPDATE_FRAGMENTS,
56
- payload: {
57
- changedFragments: changedFragments
58
- }
59
- });
60
- return nextAction;
61
- };
62
- };
63
- };