react-redux-cache 0.19.5 → 0.20.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.
@@ -32,7 +32,7 @@ var __awaiter =
32
32
  import {useCallback, useEffect} from 'react'
33
33
 
34
34
  import {query as queryImpl} from './query'
35
- import {createStateComparer, defaultGetCacheKey, EMPTY_OBJECT, log} from './utilsAndConstants'
35
+ import {createStateComparer, defaultGetCacheKey, EMPTY_OBJECT, logDebug} from './utilsAndConstants'
36
36
 
37
37
  export const useQuery = (cache, actions, selectors, options) => {
38
38
  var _a, _b, _c, _d, _e
@@ -99,13 +99,13 @@ export const useQuery = (cache, actions, selectors, options) => {
99
99
  : EMPTY_OBJECT
100
100
  useEffect(() => {
101
101
  if (skipFetch) {
102
- logsEnabled && log('useQuery.useEffect skip fetch', {skipFetch, queryKey, cacheKey})
102
+ logsEnabled && logDebug('useQuery.useEffect skip fetch', {skipFetch, queryKey, cacheKey})
103
103
  return
104
104
  }
105
105
  const expired = queryState.expiresAt != null && queryState.expiresAt <= Date.now()
106
106
  if (!fetchPolicy(expired, params, queryState, store, selectors)) {
107
107
  logsEnabled &&
108
- log('useQuery.useEffect skip fetch due to fetch policy', {
108
+ logDebug('useQuery.useEffect skip fetch due to fetch policy', {
109
109
  queryState,
110
110
  expired,
111
111
  queryKey,
@@ -115,12 +115,8 @@ export const useQuery = (cache, actions, selectors, options) => {
115
115
  }
116
116
  performFetch()
117
117
  }, [cacheKey, skipFetch])
118
- logsEnabled &&
119
- log('useQuery', {
120
- cacheKey,
121
- options,
122
- queryState,
123
- })
118
+ logsEnabled && logDebug('useQuery', {cacheKey, options, queryState})
124
119
  return [queryState, performFetch]
125
120
  }
121
+
126
122
  const defaultStateComparer = createStateComparer(['result', 'loading', 'params', 'error'])
@@ -1,12 +1,23 @@
1
1
  export const PACKAGE_SHORT_NAME = 'rrc'
2
+
2
3
  export const optionalUtils = {
3
4
  deepEqual: undefined,
4
5
  }
6
+
7
+ export const logDebug = (tag, data) => {
8
+ console.debug(`@${PACKAGE_SHORT_NAME} [${tag}]`, data)
9
+ }
10
+
11
+ export const logWarn = (tag, data) => {
12
+ console.warn(`@${PACKAGE_SHORT_NAME} [${tag}]`, data)
13
+ }
14
+
5
15
  try {
6
16
  optionalUtils.deepEqual = require('fast-deep-equal/es6')
7
17
  } catch (_a) {
8
- console.debug(PACKAGE_SHORT_NAME + ': fast-deep-equal optional dependency was not installed')
18
+ logDebug('deepEqual', 'fast-deep-equal optional dependency was not installed')
9
19
  }
20
+
10
21
  export const IS_DEV = (() => {
11
22
  try {
12
23
  return __DEV__
@@ -14,9 +25,13 @@ export const IS_DEV = (() => {
14
25
  return process.env.NODE_ENV === 'development'
15
26
  }
16
27
  })()
28
+
17
29
  export const EMPTY_OBJECT = Object.freeze({})
30
+
18
31
  export const EMPTY_ARRAY = Object.freeze([])
19
- export const NOOP = () => {}
32
+
33
+ export const noop = () => {}
34
+
20
35
  export const defaultGetCacheKey = (params) => {
21
36
  switch (typeof params) {
22
37
  case 'string':
@@ -28,19 +43,11 @@ export const defaultGetCacheKey = (params) => {
28
43
  return String(params)
29
44
  }
30
45
  }
31
- export const log = (tag, data) => {
32
- console.debug(`@${PACKAGE_SHORT_NAME} [${tag}]`, data)
33
- }
34
- export const FetchPolicy = {
35
- NoCacheOrExpired: (expired, _params, state) => {
36
- return expired || state.result === undefined
37
- },
38
- Always: () => true,
39
- }
46
+
40
47
  export const applyEntityChanges = (entities, changes, options) => {
41
48
  var _a, _b, _c, _d
42
49
  if (changes.merge && changes.entities) {
43
- console.warn('react-redux-cache.applyEntityChanges: merge and entities should not be both set')
50
+ logWarn('applyEntityChanges', 'merge and entities should not be both set')
44
51
  }
45
52
  const {merge = changes.entities, replace, remove} = changes
46
53
  if (!merge && !replace && !remove) {
@@ -48,13 +55,11 @@ export const applyEntityChanges = (entities, changes, options) => {
48
55
  }
49
56
  const deepEqual = options.deepComparisonEnabled ? optionalUtils.deepEqual : undefined
50
57
  let result
51
- const typenames = new Set([
52
- ...(changes.entities ? Object.keys(changes.entities) : EMPTY_ARRAY),
53
- ...(changes.merge ? Object.keys(changes.merge) : EMPTY_ARRAY),
54
- ...(changes.remove ? Object.keys(changes.remove) : EMPTY_ARRAY),
55
- ...(changes.replace ? Object.keys(changes.replace) : EMPTY_ARRAY),
56
- ])
57
- for (const typename of typenames) {
58
+ const objectWithAllTypenames = Object.assign(
59
+ Object.assign(Object.assign({}, changes.merge), changes.remove),
60
+ changes.replace
61
+ )
62
+ for (const typename in objectWithAllTypenames) {
58
63
  const entitiesToMerge = merge === null || merge === void 0 ? void 0 : merge[typename]
59
64
  const entitiesToReplace = replace === null || replace === void 0 ? void 0 : replace[typename]
60
65
  const entitiesToRemove = remove === null || remove === void 0 ? void 0 : remove[typename]
@@ -87,10 +92,7 @@ export const applyEntityChanges = (entities, changes, options) => {
87
92
  ? _c
88
93
  : 0)
89
94
  if (totalKeysInResponse !== 0 && idsSet.size !== totalKeysInResponse) {
90
- console.warn(
91
- 'react-redux-cache.applyEntityChanges: merge, replace and remove changes have intersections for: ' +
92
- typename
93
- )
95
+ logWarn('applyEntityChanges', 'merge, replace and remove changes have intersections for: ' + typename)
94
96
  }
95
97
  }
96
98
  const oldEntities = (_d = entities[typename]) !== null && _d !== void 0 ? _d : EMPTY_OBJECT
@@ -135,7 +137,7 @@ export const applyEntityChanges = (entities, changes, options) => {
135
137
  result[typename] = newEntities
136
138
  }
137
139
  options.logsEnabled &&
138
- log('applyEntityChanges', {
140
+ logDebug('applyEntityChanges', {
139
141
  entities,
140
142
  changes,
141
143
  options,
@@ -143,12 +145,14 @@ export const applyEntityChanges = (entities, changes, options) => {
143
145
  })
144
146
  return result
145
147
  }
148
+
146
149
  export const isEmptyObject = (o) => {
147
150
  for (const _ in o) {
148
151
  return false
149
152
  }
150
153
  return true
151
154
  }
155
+
152
156
  export const createStateComparer = (fields) => {
153
157
  return (x, y) => {
154
158
  if (x === y) {
@@ -166,3 +170,10 @@ export const createStateComparer = (fields) => {
166
170
  return true
167
171
  }
168
172
  }
173
+
174
+ export const FetchPolicy = {
175
+ NoCacheOrExpired: (expired, _params, state) => {
176
+ return expired || state.result === undefined
177
+ },
178
+ Always: () => true,
179
+ }
@@ -9,6 +9,7 @@ export type Actions<
9
9
  MP = unknown,
10
10
  MR = unknown
11
11
  > = ReturnType<typeof createActions<N, T, QP, QR, MP, MR>>
12
+
12
13
  export declare const createActions: <N extends string, T extends Typenames, QP, QR, MP, MR>(
13
14
  name: N
14
15
  ) => {