react-redux-cache 0.21.0 → 0.22.1

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