UNPKG

3.6 kBJavaScriptView Raw
1var __assign = (this && this.__assign) || Object.assign || function(t) {
2 for (var s, i = 1, n = arguments.length; i < n; i++) {
3 s = arguments[i];
4 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
5 t[p] = s[p];
6 }
7 return t;
8};
9import { createStore, compose as reduxCompose, applyMiddleware, combineReducers, } from 'redux';
10import { data } from './data/store';
11import { optimistic, getDataWithOptimisticResults, } from './optimistic-data/store';
12export { getDataWithOptimisticResults };
13import { isQueryResultAction, isMutationResultAction, isSubscriptionResultAction, } from './actions';
14var crashReporter = function (store) { return function (next) { return function (action) {
15 try {
16 return next(action);
17 }
18 catch (err) {
19 console.error('Caught an exception!', err);
20 console.error(err.stack);
21 throw err;
22 }
23}; }; };
24var createReducerError = function (error, action) {
25 var reducerError = { error: error };
26 if (isQueryResultAction(action)) {
27 reducerError.queryId = action.queryId;
28 }
29 else if (isSubscriptionResultAction(action)) {
30 reducerError.subscriptionId = action.subscriptionId;
31 }
32 else if (isMutationResultAction(action)) {
33 reducerError.mutationId = action.mutationId;
34 }
35 return reducerError;
36};
37export function createApolloReducer(config) {
38 return function apolloReducer(state, action) {
39 if (state === void 0) { state = {}; }
40 try {
41 var newState = {
42 data: data(state.data, action, config),
43 optimistic: [],
44 reducerError: null,
45 };
46 newState.optimistic = optimistic(state.optimistic, action, newState, config);
47 if (state.data === newState.data &&
48 state.optimistic === newState.optimistic &&
49 state.reducerError === newState.reducerError) {
50 return state;
51 }
52 return newState;
53 }
54 catch (reducerError) {
55 return __assign({}, state, { reducerError: createReducerError(reducerError, action) });
56 }
57 };
58}
59export function createApolloStore(_a) {
60 var _b = _a === void 0 ? {} : _a, _c = _b.reduxRootKey, reduxRootKey = _c === void 0 ? 'apollo' : _c, initialState = _b.initialState, _d = _b.config, config = _d === void 0 ? {} : _d, _e = _b.reportCrashes, reportCrashes = _e === void 0 ? true : _e, logger = _b.logger;
61 var enhancers = [];
62 var middlewares = [];
63 if (reportCrashes) {
64 middlewares.push(crashReporter);
65 }
66 if (logger) {
67 middlewares.push(logger);
68 }
69 if (middlewares.length > 0) {
70 enhancers.push(applyMiddleware.apply(void 0, middlewares));
71 }
72 if (typeof window !== 'undefined') {
73 var anyWindow = window;
74 if (anyWindow.devToolsExtension) {
75 enhancers.push(anyWindow.devToolsExtension());
76 }
77 }
78 var compose = reduxCompose;
79 if (initialState &&
80 initialState[reduxRootKey] &&
81 initialState[reduxRootKey]['queries']) {
82 throw new Error('Apollo initial state may not contain queries, only data');
83 }
84 if (initialState &&
85 initialState[reduxRootKey] &&
86 initialState[reduxRootKey]['mutations']) {
87 throw new Error('Apollo initial state may not contain mutations, only data');
88 }
89 return createStore(combineReducers((_f = {}, _f[reduxRootKey] = createApolloReducer(config), _f)), initialState, compose.apply(void 0, enhancers));
90 var _f;
91}
92//# sourceMappingURL=store.js.map
\No newline at end of file