react-redux-cache 0.22.1 → 0.22.2

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.
@@ -1,59 +1,67 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createSelectors = void 0;
4
- const utilsAndConstants_1 = require("./utilsAndConstants");
1
+ 'use strict'
2
+ Object.defineProperty(exports, '__esModule', {value: true})
3
+ exports.createSelectors = void 0
4
+ const utilsAndConstants_1 = require('./utilsAndConstants')
5
5
  const createSelectors = (cache) => {
6
- const selectEntityById = (state, id, typename) => {
7
- var _a;
8
- return id == null ? undefined : (_a = cache.cacheStateSelector(state).entities[typename]) === null || _a === void 0 ? void 0 : _a[id];
9
- };
10
- const selectQueryState = (state, query, cacheKey) => {
11
- var _a;
12
- return (_a = cache.cacheStateSelector(state).queries[query][cacheKey]) !== null && _a !== void 0 ? _a : utilsAndConstants_1.EMPTY_OBJECT;
13
- };
14
- const selectMutationState = (state, mutation) => {
15
- var _a;
16
- return (_a = cache.cacheStateSelector(state).mutations[mutation]) !== null && _a !== void 0 ? _a : utilsAndConstants_1.EMPTY_OBJECT;
17
- };
18
- return {
19
- selectEntityById,
20
- selectQueryState,
21
- selectQueryResult: (state, query, cacheKey) => {
22
- return selectQueryState(state, query, cacheKey).result;
23
- },
24
- selectQueryLoading: (state, query, cacheKey) => {
25
- var _a;
26
- return (_a = selectQueryState(state, query, cacheKey).loading) !== null && _a !== void 0 ? _a : false;
27
- },
28
- selectQueryError: (state, query, cacheKey) => {
29
- return selectQueryState(state, query, cacheKey).error;
30
- },
31
- selectQueryParams: (state, query, cacheKey) => {
32
- return selectQueryState(state, query, cacheKey).params;
33
- },
34
- selectQueryExpiresAt: (state, query, cacheKey) => {
35
- return selectQueryState(state, query, cacheKey).expiresAt;
36
- },
37
- selectMutationState,
38
- selectMutationResult: (state, mutation) => {
39
- return selectMutationState(state, mutation).result;
40
- },
41
- selectMutationLoading: (state, mutation) => {
42
- var _a;
43
- return (_a = selectMutationState(state, mutation).loading) !== null && _a !== void 0 ? _a : false;
44
- },
45
- selectMutationError: (state, mutation) => {
46
- return selectMutationState(state, mutation).error;
47
- },
48
- selectMutationParams: (state, mutation) => {
49
- return selectMutationState(state, mutation).params;
50
- },
51
- selectEntities: (state) => {
52
- return cache.cacheStateSelector(state).entities;
53
- },
54
- selectEntitiesByTypename: (state, typename) => {
55
- return cache.cacheStateSelector(state).entities[typename];
56
- },
57
- };
58
- };
59
- exports.createSelectors = createSelectors;
6
+ const selectEntityById = (state, id, typename) => {
7
+ var _a
8
+ return id == null
9
+ ? undefined
10
+ : (_a = cache.cacheStateSelector(state).entities[typename]) === null || _a === void 0
11
+ ? void 0
12
+ : _a[id]
13
+ }
14
+ const selectQueryState = (state, query, cacheKey) => {
15
+ var _a
16
+ return (_a = cache.cacheStateSelector(state).queries[query][cacheKey]) !== null && _a !== void 0
17
+ ? _a
18
+ : utilsAndConstants_1.EMPTY_OBJECT
19
+ }
20
+ const selectMutationState = (state, mutation) => {
21
+ var _a
22
+ return (_a = cache.cacheStateSelector(state).mutations[mutation]) !== null && _a !== void 0
23
+ ? _a
24
+ : utilsAndConstants_1.EMPTY_OBJECT
25
+ }
26
+ return {
27
+ selectEntityById,
28
+ selectQueryState,
29
+ selectQueryResult: (state, query, cacheKey) => {
30
+ return selectQueryState(state, query, cacheKey).result
31
+ },
32
+ selectQueryLoading: (state, query, cacheKey) => {
33
+ var _a
34
+ return (_a = selectQueryState(state, query, cacheKey).loading) !== null && _a !== void 0 ? _a : false
35
+ },
36
+ selectQueryError: (state, query, cacheKey) => {
37
+ return selectQueryState(state, query, cacheKey).error
38
+ },
39
+ selectQueryParams: (state, query, cacheKey) => {
40
+ return selectQueryState(state, query, cacheKey).params
41
+ },
42
+ selectQueryExpiresAt: (state, query, cacheKey) => {
43
+ return selectQueryState(state, query, cacheKey).expiresAt
44
+ },
45
+ selectMutationState,
46
+ selectMutationResult: (state, mutation) => {
47
+ return selectMutationState(state, mutation).result
48
+ },
49
+ selectMutationLoading: (state, mutation) => {
50
+ var _a
51
+ return (_a = selectMutationState(state, mutation).loading) !== null && _a !== void 0 ? _a : false
52
+ },
53
+ selectMutationError: (state, mutation) => {
54
+ return selectMutationState(state, mutation).error
55
+ },
56
+ selectMutationParams: (state, mutation) => {
57
+ return selectMutationState(state, mutation).params
58
+ },
59
+ selectEntities: (state) => {
60
+ return cache.cacheStateSelector(state).entities
61
+ },
62
+ selectEntitiesByTypename: (state, typename) => {
63
+ return cache.cacheStateSelector(state).entities[typename]
64
+ },
65
+ }
66
+ }
67
+ exports.createSelectors = createSelectors
package/dist/cjs/index.js CHANGED
@@ -1,27 +1,88 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
1
+ 'use strict'
2
+ var __createBinding =
3
+ (this && this.__createBinding) ||
4
+ (Object.create
5
+ ? function (o, m, k, k2) {
6
+ if (k2 === undefined) {
7
+ k2 = k
8
+ }
9
+ var desc = Object.getOwnPropertyDescriptor(m, k)
10
+ if (!desc || ('get' in desc ? !m.__esModule : desc.writable || desc.configurable)) {
11
+ desc = {
12
+ enumerable: true,
13
+ get() {
14
+ return m[k]
15
+ },
16
+ }
17
+ }
18
+ Object.defineProperty(o, k2, desc)
19
+ }
20
+ : function (o, m, k, k2) {
21
+ if (k2 === undefined) {
22
+ k2 = k
23
+ }
24
+ o[k2] = m[k]
25
+ })
26
+ var __exportStar =
27
+ (this && this.__exportStar) ||
28
+ function (m, exports) {
29
+ for (var p in m) {
30
+ if (p !== 'default' && !Object.prototype.hasOwnProperty.call(exports, p)) {
31
+ __createBinding(exports, m, p)
32
+ }
7
33
  }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.noop = exports.isEmptyObject = exports.FetchPolicy = exports.defaultGetCacheKey = exports.createStateComparer = exports.withTypenames = exports.createCache = void 0;
18
- var createCache_1 = require("./createCache");
19
- Object.defineProperty(exports, "createCache", { enumerable: true, get: function () { return createCache_1.createCache; } });
20
- Object.defineProperty(exports, "withTypenames", { enumerable: true, get: function () { return createCache_1.withTypenames; } });
21
- __exportStar(require("./types"), exports);
22
- var utilsAndConstants_1 = require("./utilsAndConstants");
23
- Object.defineProperty(exports, "createStateComparer", { enumerable: true, get: function () { return utilsAndConstants_1.createStateComparer; } });
24
- Object.defineProperty(exports, "defaultGetCacheKey", { enumerable: true, get: function () { return utilsAndConstants_1.defaultGetCacheKey; } });
25
- Object.defineProperty(exports, "FetchPolicy", { enumerable: true, get: function () { return utilsAndConstants_1.FetchPolicy; } });
26
- Object.defineProperty(exports, "isEmptyObject", { enumerable: true, get: function () { return utilsAndConstants_1.isEmptyObject; } });
27
- Object.defineProperty(exports, "noop", { enumerable: true, get: function () { return utilsAndConstants_1.noop; } });
34
+ }
35
+ Object.defineProperty(exports, '__esModule', {value: true})
36
+ exports.noop =
37
+ exports.isEmptyObject =
38
+ exports.FetchPolicy =
39
+ exports.defaultGetCacheKey =
40
+ exports.createStateComparer =
41
+ exports.withTypenames =
42
+ exports.createCache =
43
+ void 0
44
+ var createCache_1 = require('./createCache')
45
+ Object.defineProperty(exports, 'createCache', {
46
+ enumerable: true,
47
+ get() {
48
+ return createCache_1.createCache
49
+ },
50
+ })
51
+ Object.defineProperty(exports, 'withTypenames', {
52
+ enumerable: true,
53
+ get() {
54
+ return createCache_1.withTypenames
55
+ },
56
+ })
57
+ __exportStar(require('./types'), exports)
58
+ var utilsAndConstants_1 = require('./utilsAndConstants')
59
+ Object.defineProperty(exports, 'createStateComparer', {
60
+ enumerable: true,
61
+ get() {
62
+ return utilsAndConstants_1.createStateComparer
63
+ },
64
+ })
65
+ Object.defineProperty(exports, 'defaultGetCacheKey', {
66
+ enumerable: true,
67
+ get() {
68
+ return utilsAndConstants_1.defaultGetCacheKey
69
+ },
70
+ })
71
+ Object.defineProperty(exports, 'FetchPolicy', {
72
+ enumerable: true,
73
+ get() {
74
+ return utilsAndConstants_1.FetchPolicy
75
+ },
76
+ })
77
+ Object.defineProperty(exports, 'isEmptyObject', {
78
+ enumerable: true,
79
+ get() {
80
+ return utilsAndConstants_1.isEmptyObject
81
+ },
82
+ })
83
+ Object.defineProperty(exports, 'noop', {
84
+ enumerable: true,
85
+ get() {
86
+ return utilsAndConstants_1.noop
87
+ },
88
+ })
@@ -1,77 +1,160 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.mutate = void 0;
13
- const utilsAndConstants_1 = require("./utilsAndConstants");
14
- const mutate = (logTag_1, store_1, cache_1, actions_1, selectors_1, mutationKey_1, params_1, abortControllers_1, ...args_1) => __awaiter(void 0, [logTag_1, store_1, cache_1, actions_1, selectors_1, mutationKey_1, params_1, abortControllers_1, ...args_1], void 0, function* (logTag, store, cache, actions, selectors, mutationKey, params, abortControllers, onCompleted = cache.mutations[mutationKey].onCompleted, onSuccess = cache.mutations[mutationKey].onSuccess, onError = cache.mutations[mutationKey].onError) {
15
- var _a, _b;
16
- const { updateMutationStateAndEntities } = actions;
17
- let abortControllersOfStore = abortControllers.get(store);
18
- if (abortControllersOfStore === undefined) {
19
- abortControllersOfStore = {};
20
- abortControllers.set(store, abortControllersOfStore);
1
+ 'use strict'
2
+ var __awaiter =
3
+ (this && this.__awaiter) ||
4
+ function (thisArg, _arguments, P, generator) {
5
+ function adopt(value) {
6
+ return value instanceof P
7
+ ? value
8
+ : new P(function (resolve) {
9
+ resolve(value)
10
+ })
21
11
  }
22
- {
23
- const abortController = abortControllersOfStore[mutationKey];
12
+ return new (P || (P = Promise))(function (resolve, reject) {
13
+ function fulfilled(value) {
14
+ try {
15
+ step(generator.next(value))
16
+ } catch (e) {
17
+ reject(e)
18
+ }
19
+ }
20
+ function rejected(value) {
21
+ try {
22
+ step(generator['throw'](value))
23
+ } catch (e) {
24
+ reject(e)
25
+ }
26
+ }
27
+ function step(result) {
28
+ result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected)
29
+ }
30
+ step((generator = generator.apply(thisArg, _arguments || [])).next())
31
+ })
32
+ }
33
+ Object.defineProperty(exports, '__esModule', {value: true})
34
+ exports.mutate = void 0
35
+ const utilsAndConstants_1 = require('./utilsAndConstants')
36
+ const mutate = (
37
+ logTag_1,
38
+ store_1,
39
+ cache_1,
40
+ actions_1,
41
+ selectors_1,
42
+ mutationKey_1,
43
+ params_1,
44
+ abortControllers_1,
45
+ ...args_1
46
+ ) =>
47
+ __awaiter(
48
+ void 0,
49
+ [
50
+ logTag_1,
51
+ store_1,
52
+ cache_1,
53
+ actions_1,
54
+ selectors_1,
55
+ mutationKey_1,
56
+ params_1,
57
+ abortControllers_1,
58
+ ...args_1,
59
+ ],
60
+ void 0,
61
+ function* (
62
+ logTag,
63
+ store,
64
+ cache,
65
+ actions,
66
+ selectors,
67
+ mutationKey,
68
+ params,
69
+ abortControllers,
70
+ onCompleted = cache.mutations[mutationKey].onCompleted,
71
+ onSuccess = cache.mutations[mutationKey].onSuccess,
72
+ onError = cache.mutations[mutationKey].onError,
73
+ ) {
74
+ var _a, _b
75
+ const {updateMutationStateAndEntities} = actions
76
+ let abortControllersOfStore = abortControllers.get(store)
77
+ if (abortControllersOfStore === undefined) {
78
+ abortControllersOfStore = {}
79
+ abortControllers.set(store, abortControllersOfStore)
80
+ }
81
+ {
82
+ const abortController = abortControllersOfStore[mutationKey]
24
83
  cache.options.logsEnabled &&
25
- (0, utilsAndConstants_1.logDebug)(logTag, { mutationKey, params, previousAborted: abortController !== undefined });
84
+ (0, utilsAndConstants_1.logDebug)(logTag, {
85
+ mutationKey,
86
+ params,
87
+ previousAborted: abortController !== undefined,
88
+ })
26
89
  if (abortController !== undefined) {
27
- abortController.abort();
90
+ abortController.abort()
28
91
  }
29
- }
30
- const abortController = new AbortController();
31
- abortControllersOfStore[mutationKey] = abortController;
32
- const mutatePromise = cache.mutations[mutationKey].mutation(params, store, abortController.signal);
33
- store.dispatch(updateMutationStateAndEntities(mutationKey, {
34
- loading: mutatePromise,
35
- params,
36
- result: undefined,
37
- }));
38
- let response;
39
- let error;
40
- try {
41
- response = yield mutatePromise;
42
- }
43
- catch (e) {
44
- error = e;
45
- }
46
- cache.options.logsEnabled &&
47
- (0, utilsAndConstants_1.logDebug)(`${logTag} finished`, { response, error, aborted: abortController.signal.aborted });
48
- if (abortController.signal.aborted) {
49
- return ABORTED_RESULT;
50
- }
51
- delete abortControllersOfStore[mutationKey];
52
- if (error) {
53
- store.dispatch(updateMutationStateAndEntities(mutationKey, {
54
- error: error,
92
+ }
93
+ const abortController = new AbortController()
94
+ abortControllersOfStore[mutationKey] = abortController
95
+ const mutatePromise = cache.mutations[mutationKey].mutation(params, store, abortController.signal)
96
+ store.dispatch(
97
+ updateMutationStateAndEntities(mutationKey, {
98
+ loading: mutatePromise,
99
+ params,
100
+ result: undefined,
101
+ }),
102
+ )
103
+ let response
104
+ let error
105
+ try {
106
+ response = yield mutatePromise
107
+ } catch (e) {
108
+ error = e
109
+ }
110
+ cache.options.logsEnabled &&
111
+ (0, utilsAndConstants_1.logDebug)(`${logTag} finished`, {
112
+ response,
113
+ error,
114
+ aborted: abortController.signal.aborted,
115
+ })
116
+ if (abortController.signal.aborted) {
117
+ return ABORTED_RESULT
118
+ }
119
+ delete abortControllersOfStore[mutationKey]
120
+ if (error) {
121
+ store.dispatch(
122
+ updateMutationStateAndEntities(mutationKey, {
123
+ error,
55
124
  loading: undefined,
56
- }));
57
- if (!(onError === null || onError === void 0 ? void 0 : onError(error, params, store, actions, selectors))) {
58
- (_b = (_a = cache.globals).onError) === null || _b === void 0 ? void 0 : _b.call(_a, error, mutationKey, params, store, actions, selectors);
125
+ }),
126
+ )
127
+ if (
128
+ !(onError === null || onError === void 0
129
+ ? void 0
130
+ : onError(error, params, store, actions, selectors))
131
+ ) {
132
+ ;(_b = (_a = cache.globals).onError) === null || _b === void 0
133
+ ? void 0
134
+ : _b.call(_a, error, mutationKey, params, store, actions, selectors)
59
135
  }
60
- onCompleted === null || onCompleted === void 0 ? void 0 : onCompleted(response, error, params, store, actions, selectors);
61
- return { error };
62
- }
63
- if (response) {
136
+ onCompleted === null || onCompleted === void 0
137
+ ? void 0
138
+ : onCompleted(response, error, params, store, actions, selectors)
139
+ return {error}
140
+ }
141
+ if (response) {
64
142
  const newState = {
65
- error: undefined,
66
- loading: undefined,
67
- result: response.result,
68
- };
69
- store.dispatch(updateMutationStateAndEntities(mutationKey, newState, response));
70
- onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(response, params, store, actions, selectors);
71
- onCompleted === null || onCompleted === void 0 ? void 0 : onCompleted(response, error, params, store, actions, selectors);
72
- return { result: response.result };
73
- }
74
- throw new Error(`${logTag}: both error and response are not defined`);
75
- });
76
- exports.mutate = mutate;
77
- const ABORTED_RESULT = Object.freeze({ aborted: true });
143
+ error: undefined,
144
+ loading: undefined,
145
+ result: response.result,
146
+ }
147
+ store.dispatch(updateMutationStateAndEntities(mutationKey, newState, response))
148
+ onSuccess === null || onSuccess === void 0
149
+ ? void 0
150
+ : onSuccess(response, params, store, actions, selectors)
151
+ onCompleted === null || onCompleted === void 0
152
+ ? void 0
153
+ : onCompleted(response, error, params, store, actions, selectors)
154
+ return {result: response.result}
155
+ }
156
+ throw new Error(`${logTag}: both error and response are not defined`)
157
+ },
158
+ )
159
+ exports.mutate = mutate
160
+ const ABORTED_RESULT = Object.freeze({aborted: true})