@wordpress/data 10.41.0 → 10.41.1-next.v.202603161435.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.
Files changed (213) hide show
  1. package/README.md +20 -28
  2. package/build/components/async-mode-provider/context.cjs +1 -1
  3. package/build/components/async-mode-provider/context.cjs.map +3 -3
  4. package/build/components/async-mode-provider/index.cjs +1 -1
  5. package/build/components/async-mode-provider/index.cjs.map +1 -1
  6. package/build/components/async-mode-provider/use-async-mode.cjs +1 -1
  7. package/build/components/async-mode-provider/use-async-mode.cjs.map +3 -3
  8. package/build/components/registry-provider/context.cjs +1 -1
  9. package/build/components/registry-provider/context.cjs.map +1 -1
  10. package/build/components/registry-provider/index.cjs +1 -1
  11. package/build/components/registry-provider/index.cjs.map +1 -1
  12. package/build/components/registry-provider/use-registry.cjs +1 -1
  13. package/build/components/registry-provider/use-registry.cjs.map +3 -3
  14. package/build/components/use-dispatch/index.cjs +1 -1
  15. package/build/components/use-dispatch/index.cjs.map +1 -1
  16. package/build/components/use-dispatch/use-dispatch-with-map.cjs +1 -1
  17. package/build/components/use-dispatch/use-dispatch-with-map.cjs.map +3 -3
  18. package/build/components/use-dispatch/use-dispatch.cjs +1 -1
  19. package/build/components/use-dispatch/use-dispatch.cjs.map +3 -3
  20. package/build/components/use-select/index.cjs +2 -2
  21. package/build/components/use-select/index.cjs.map +3 -3
  22. package/build/components/with-dispatch/index.cjs +1 -1
  23. package/build/components/with-dispatch/index.cjs.map +3 -3
  24. package/build/components/with-registry/index.cjs +1 -1
  25. package/build/components/with-registry/index.cjs.map +3 -3
  26. package/build/components/with-select/index.cjs +1 -1
  27. package/build/components/with-select/index.cjs.map +3 -3
  28. package/build/controls.cjs +7 -6
  29. package/build/controls.cjs.map +3 -3
  30. package/build/default-registry.cjs +3 -2
  31. package/build/default-registry.cjs.map +3 -3
  32. package/build/dispatch.cjs +3 -1
  33. package/build/dispatch.cjs.map +2 -2
  34. package/build/factory.cjs.map +2 -2
  35. package/build/index.cjs +9 -8
  36. package/build/index.cjs.map +3 -3
  37. package/build/plugins/index.cjs +1 -1
  38. package/build/plugins/index.cjs.map +1 -1
  39. package/build/plugins/persistence/index.cjs +13 -8
  40. package/build/plugins/persistence/index.cjs.map +3 -3
  41. package/build/plugins/persistence/storage/default.cjs +1 -1
  42. package/build/plugins/persistence/storage/default.cjs.map +3 -3
  43. package/build/plugins/persistence/storage/object.cjs +1 -1
  44. package/build/plugins/persistence/storage/object.cjs.map +3 -3
  45. package/build/promise-middleware.cjs +2 -1
  46. package/build/promise-middleware.cjs.map +3 -3
  47. package/build/redux-store/index.cjs +32 -16
  48. package/build/redux-store/index.cjs.map +3 -3
  49. package/build/redux-store/metadata/selectors.cjs +1 -1
  50. package/build/redux-store/metadata/selectors.cjs.map +3 -3
  51. package/build/redux-store/metadata/types.cjs +19 -0
  52. package/build/redux-store/metadata/types.cjs.map +7 -0
  53. package/build/registry.cjs +11 -7
  54. package/build/registry.cjs.map +3 -3
  55. package/build/resolvers-cache-middleware.cjs +6 -3
  56. package/build/resolvers-cache-middleware.cjs.map +3 -3
  57. package/build/select.cjs +3 -1
  58. package/build/select.cjs.map +2 -2
  59. package/build/store/index.cjs +1 -1
  60. package/build/store/index.cjs.map +3 -3
  61. package/build/types.cjs.map +1 -1
  62. package/build-module/components/async-mode-provider/context.mjs +1 -1
  63. package/build-module/components/async-mode-provider/context.mjs.map +3 -3
  64. package/build-module/components/async-mode-provider/index.mjs +1 -1
  65. package/build-module/components/async-mode-provider/index.mjs.map +1 -1
  66. package/build-module/components/async-mode-provider/use-async-mode.mjs +1 -1
  67. package/build-module/components/async-mode-provider/use-async-mode.mjs.map +3 -3
  68. package/build-module/components/registry-provider/context.mjs +1 -1
  69. package/build-module/components/registry-provider/context.mjs.map +1 -1
  70. package/build-module/components/registry-provider/index.mjs +1 -1
  71. package/build-module/components/registry-provider/index.mjs.map +1 -1
  72. package/build-module/components/registry-provider/use-registry.mjs +1 -1
  73. package/build-module/components/registry-provider/use-registry.mjs.map +3 -3
  74. package/build-module/components/use-dispatch/index.mjs +1 -1
  75. package/build-module/components/use-dispatch/index.mjs.map +1 -1
  76. package/build-module/components/use-dispatch/use-dispatch-with-map.mjs +1 -1
  77. package/build-module/components/use-dispatch/use-dispatch-with-map.mjs.map +3 -3
  78. package/build-module/components/use-dispatch/use-dispatch.mjs +1 -1
  79. package/build-module/components/use-dispatch/use-dispatch.mjs.map +3 -3
  80. package/build-module/components/use-select/index.mjs +2 -2
  81. package/build-module/components/use-select/index.mjs.map +3 -3
  82. package/build-module/components/with-dispatch/index.mjs +1 -1
  83. package/build-module/components/with-dispatch/index.mjs.map +3 -3
  84. package/build-module/components/with-registry/index.mjs +1 -1
  85. package/build-module/components/with-registry/index.mjs.map +3 -3
  86. package/build-module/components/with-select/index.mjs +1 -1
  87. package/build-module/components/with-select/index.mjs.map +3 -3
  88. package/build-module/controls.mjs +7 -6
  89. package/build-module/controls.mjs.map +3 -3
  90. package/build-module/default-registry.mjs +3 -2
  91. package/build-module/default-registry.mjs.map +3 -3
  92. package/build-module/dispatch.mjs +3 -1
  93. package/build-module/dispatch.mjs.map +2 -2
  94. package/build-module/factory.mjs.map +2 -2
  95. package/build-module/index.mjs +4 -3
  96. package/build-module/index.mjs.map +2 -2
  97. package/build-module/plugins/index.mjs +1 -1
  98. package/build-module/plugins/index.mjs.map +1 -1
  99. package/build-module/plugins/persistence/index.mjs +13 -8
  100. package/build-module/plugins/persistence/index.mjs.map +3 -3
  101. package/build-module/plugins/persistence/storage/default.mjs +1 -1
  102. package/build-module/plugins/persistence/storage/default.mjs.map +3 -3
  103. package/build-module/plugins/persistence/storage/object.mjs +1 -1
  104. package/build-module/plugins/persistence/storage/object.mjs.map +3 -3
  105. package/build-module/promise-middleware.mjs +2 -1
  106. package/build-module/promise-middleware.mjs.map +3 -3
  107. package/build-module/redux-store/index.mjs +32 -16
  108. package/build-module/redux-store/index.mjs.map +3 -3
  109. package/build-module/redux-store/metadata/selectors.mjs +1 -1
  110. package/build-module/redux-store/metadata/selectors.mjs.map +3 -3
  111. package/build-module/redux-store/metadata/types.mjs +1 -0
  112. package/build-module/redux-store/metadata/types.mjs.map +7 -0
  113. package/build-module/registry.mjs +11 -7
  114. package/build-module/registry.mjs.map +3 -3
  115. package/build-module/resolvers-cache-middleware.mjs +6 -3
  116. package/build-module/resolvers-cache-middleware.mjs.map +3 -3
  117. package/build-module/select.mjs +3 -1
  118. package/build-module/select.mjs.map +2 -2
  119. package/build-module/store/index.mjs +1 -1
  120. package/build-module/store/index.mjs.map +3 -3
  121. package/build-types/components/async-mode-provider/context.d.ts +37 -3
  122. package/build-types/components/async-mode-provider/context.d.ts.map +1 -1
  123. package/build-types/components/async-mode-provider/index.d.ts +2 -2
  124. package/build-types/components/async-mode-provider/index.d.ts.map +1 -1
  125. package/build-types/components/async-mode-provider/use-async-mode.d.ts.map +1 -1
  126. package/build-types/components/registry-provider/context.d.ts +10 -3
  127. package/build-types/components/registry-provider/context.d.ts.map +1 -1
  128. package/build-types/components/registry-provider/index.d.ts +2 -2
  129. package/build-types/components/registry-provider/index.d.ts.map +1 -1
  130. package/build-types/components/registry-provider/use-registry.d.ts +3 -2
  131. package/build-types/components/registry-provider/use-registry.d.ts.map +1 -1
  132. package/build-types/components/use-dispatch/index.d.ts +2 -2
  133. package/build-types/components/use-dispatch/index.d.ts.map +1 -1
  134. package/build-types/components/use-dispatch/use-dispatch-with-map.d.ts +11 -9
  135. package/build-types/components/use-dispatch/use-dispatch-with-map.d.ts.map +1 -1
  136. package/build-types/components/use-dispatch/use-dispatch.d.ts +10 -19
  137. package/build-types/components/use-dispatch/use-dispatch.d.ts.map +1 -1
  138. package/build-types/components/use-select/index.d.ts +24 -28
  139. package/build-types/components/use-select/index.d.ts.map +1 -1
  140. package/build-types/components/with-dispatch/index.d.ts +10 -11
  141. package/build-types/components/with-dispatch/index.d.ts.map +1 -1
  142. package/build-types/components/with-registry/index.d.ts +2 -6
  143. package/build-types/components/with-registry/index.d.ts.map +1 -1
  144. package/build-types/components/with-select/index.d.ts +7 -8
  145. package/build-types/components/with-select/index.d.ts.map +1 -1
  146. package/build-types/controls.d.ts +57 -44
  147. package/build-types/controls.d.ts.map +1 -1
  148. package/build-types/default-registry.d.ts +2 -2
  149. package/build-types/default-registry.d.ts.map +1 -1
  150. package/build-types/dispatch.d.ts +4 -0
  151. package/build-types/dispatch.d.ts.map +1 -1
  152. package/build-types/factory.d.ts +2 -1
  153. package/build-types/factory.d.ts.map +1 -1
  154. package/build-types/index.d.ts.map +1 -1
  155. package/build-types/plugins/index.d.ts +1 -1
  156. package/build-types/plugins/index.d.ts.map +1 -1
  157. package/build-types/plugins/persistence/index.d.ts +38 -27
  158. package/build-types/plugins/persistence/index.d.ts.map +1 -1
  159. package/build-types/plugins/persistence/storage/default.d.ts +7 -1
  160. package/build-types/plugins/persistence/storage/default.d.ts.map +1 -1
  161. package/build-types/plugins/persistence/storage/object.d.ts +4 -5
  162. package/build-types/plugins/persistence/storage/object.d.ts.map +1 -1
  163. package/build-types/promise-middleware.d.ts +6 -4
  164. package/build-types/promise-middleware.d.ts.map +1 -1
  165. package/build-types/redux-store/index.d.ts +12 -15
  166. package/build-types/redux-store/index.d.ts.map +1 -1
  167. package/build-types/redux-store/metadata/selectors.d.ts +45 -50
  168. package/build-types/redux-store/metadata/selectors.d.ts.map +1 -1
  169. package/build-types/redux-store/metadata/types.d.ts +48 -0
  170. package/build-types/redux-store/metadata/types.d.ts.map +1 -0
  171. package/build-types/registry.d.ts +5 -49
  172. package/build-types/registry.d.ts.map +1 -1
  173. package/build-types/resolvers-cache-middleware.d.ts +13 -7
  174. package/build-types/resolvers-cache-middleware.d.ts.map +1 -1
  175. package/build-types/select.d.ts +3 -0
  176. package/build-types/select.d.ts.map +1 -1
  177. package/build-types/store/index.d.ts +5 -12
  178. package/build-types/store/index.d.ts.map +1 -1
  179. package/build-types/types.d.ts +201 -10
  180. package/build-types/types.d.ts.map +1 -1
  181. package/package.json +9 -9
  182. package/src/components/async-mode-provider/{context.js → context.ts} +0 -1
  183. package/src/components/async-mode-provider/{use-async-mode.js → use-async-mode.ts} +1 -1
  184. package/src/components/registry-provider/{use-registry.js → use-registry.ts} +3 -2
  185. package/src/components/use-dispatch/{use-dispatch-with-map.js → use-dispatch-with-map.ts} +18 -9
  186. package/src/components/use-dispatch/{use-dispatch.js → use-dispatch.ts} +25 -20
  187. package/src/components/use-select/{index.js → index.ts} +84 -70
  188. package/src/components/with-dispatch/{index.js → index.tsx} +20 -13
  189. package/src/components/with-registry/{index.js → index.tsx} +1 -5
  190. package/src/components/with-select/{index.js → index.tsx} +17 -10
  191. package/src/{controls.js → controls.ts} +53 -26
  192. package/src/{default-registry.js → default-registry.ts} +3 -1
  193. package/src/dispatch.ts +7 -1
  194. package/src/factory.ts +4 -3
  195. package/src/index.ts +4 -8
  196. package/src/plugins/persistence/{index.js → index.ts} +93 -54
  197. package/src/plugins/persistence/storage/{default.js → default.ts} +3 -2
  198. package/src/plugins/persistence/storage/object.ts +25 -0
  199. package/src/{promise-middleware.js → promise-middleware.ts} +3 -3
  200. package/src/redux-store/{index.js → index.ts} +218 -131
  201. package/src/redux-store/metadata/{selectors.js → selectors.ts} +83 -54
  202. package/src/redux-store/metadata/types.ts +100 -0
  203. package/src/{registry.js → registry.ts} +98 -88
  204. package/src/{resolvers-cache-middleware.js → resolvers-cache-middleware.ts} +22 -7
  205. package/src/select.ts +6 -1
  206. package/src/store/{index.js → index.ts} +13 -8
  207. package/src/types.ts +263 -24
  208. package/src/plugins/persistence/storage/object.js +0 -23
  209. /package/src/components/async-mode-provider/{index.js → index.ts} +0 -0
  210. /package/src/components/registry-provider/{context.js → context.ts} +0 -0
  211. /package/src/components/registry-provider/{index.js → index.ts} +0 -0
  212. /package/src/components/use-dispatch/{index.js → index.ts} +0 -0
  213. /package/src/plugins/{index.js → index.ts} +0 -0
@@ -1,6 +1,7 @@
1
1
  /**
2
2
  * External dependencies
3
3
  */
4
+ // @ts-expect-error -- is-plain-object types don't resolve with package.json "exports".
4
5
  import { isPlainObject } from 'is-plain-object';
5
6
  import deepmerge from 'deepmerge';
6
7
 
@@ -9,31 +10,37 @@ import deepmerge from 'deepmerge';
9
10
  */
10
11
  import defaultStorage from './storage/default';
11
12
  import { combineReducers } from '../../';
13
+ import type {
14
+ DataRegistry,
15
+ ReduxStoreConfig,
16
+ StorageInterface,
17
+ } from '../../types';
12
18
 
13
- /** @typedef {import('../../registry').WPDataRegistry} WPDataRegistry */
14
-
15
- /** @typedef {import('../../registry').WPDataPlugin} WPDataPlugin */
19
+ interface PersistencePluginOptions {
20
+ /**
21
+ * Persistent storage implementation. This must
22
+ * at least implement `getItem` and `setItem` of
23
+ * the Web Storage API.
24
+ */
25
+ storage?: StorageInterface;
26
+ /**
27
+ * Key on which to set in persistent storage.
28
+ */
29
+ storageKey?: string;
30
+ }
16
31
 
17
- /**
18
- * @typedef {Object} WPDataPersistencePluginOptions Persistence plugin options.
19
- *
20
- * @property {Storage} storage Persistent storage implementation. This must
21
- * at least implement `getItem` and `setItem` of
22
- * the Web Storage API.
23
- * @property {string} storageKey Key on which to set in persistent storage.
24
- */
32
+ interface PersistenceInterface {
33
+ get: () => Record< string, unknown >;
34
+ set: ( key: string, value: unknown ) => void;
35
+ }
25
36
 
26
37
  /**
27
38
  * Default plugin storage.
28
- *
29
- * @type {Storage}
30
39
  */
31
40
  const DEFAULT_STORAGE = defaultStorage;
32
41
 
33
42
  /**
34
43
  * Default plugin storage key.
35
- *
36
- * @type {string}
37
44
  */
38
45
  const DEFAULT_STORAGE_KEY = 'WP_DATA';
39
46
 
@@ -42,38 +49,42 @@ const DEFAULT_STORAGE_KEY = 'WP_DATA';
42
49
  * inequal from that of the action's `nextState` property, otherwise returning
43
50
  * the original state reference.
44
51
  *
45
- * @param {Function} reducer Original reducer.
52
+ * @param reducer Original reducer.
46
53
  *
47
- * @return {Function} Enhanced reducer.
54
+ * @return Enhanced reducer.
48
55
  */
49
- export const withLazySameState = ( reducer ) => ( state, action ) => {
50
- if ( action.nextState === state ) {
51
- return state;
52
- }
56
+ export const withLazySameState =
57
+ ( reducer: ( state: any, action: any ) => any ) =>
58
+ ( state: unknown, action: { nextState: unknown } ) => {
59
+ if ( action.nextState === state ) {
60
+ return state;
61
+ }
53
62
 
54
- return reducer( state, action );
55
- };
63
+ return reducer( state, action );
64
+ };
56
65
 
57
66
  /**
58
67
  * Creates a persistence interface, exposing getter and setter methods (`get`
59
68
  * and `set` respectively).
60
69
  *
61
- * @param {WPDataPersistencePluginOptions} options Plugin options.
70
+ * @param options Plugin options.
62
71
  *
63
- * @return {Object} Persistence interface.
72
+ * @return Persistence interface.
64
73
  */
65
- export function createPersistenceInterface( options ) {
74
+ export function createPersistenceInterface(
75
+ options: PersistencePluginOptions
76
+ ): PersistenceInterface {
66
77
  const { storage = DEFAULT_STORAGE, storageKey = DEFAULT_STORAGE_KEY } =
67
78
  options;
68
79
 
69
- let data;
80
+ let data: Record< string, unknown > | undefined;
70
81
 
71
82
  /**
72
83
  * Returns the persisted data as an object, defaulting to an empty object.
73
84
  *
74
- * @return {Object} Persisted data.
85
+ * @return Persisted data.
75
86
  */
76
- function getData() {
87
+ function getData(): Record< string, unknown > {
77
88
  if ( data === undefined ) {
78
89
  // If unset, getItem is expected to return null. Fall back to
79
90
  // empty object.
@@ -91,16 +102,16 @@ export function createPersistenceInterface( options ) {
91
102
  }
92
103
  }
93
104
 
94
- return data;
105
+ return data!;
95
106
  }
96
107
 
97
108
  /**
98
109
  * Merges an updated reducer state into the persisted data.
99
110
  *
100
- * @param {string} key Key to update.
101
- * @param {*} value Updated value.
111
+ * @param key Key to update.
112
+ * @param value Updated value.
102
113
  */
103
- function setData( key, value ) {
114
+ function setData( key: string, value: unknown ): void {
104
115
  data = { ...data, [ key ]: value };
105
116
  storage.setItem( storageKey, JSON.stringify( data ) );
106
117
  }
@@ -114,26 +125,33 @@ export function createPersistenceInterface( options ) {
114
125
  /**
115
126
  * Data plugin to persist store state into a single storage key.
116
127
  *
117
- * @param {WPDataRegistry} registry Data registry.
118
- * @param {?WPDataPersistencePluginOptions} pluginOptions Plugin options.
128
+ * @param registry Data registry.
129
+ * @param pluginOptions Plugin options.
119
130
  *
120
- * @return {WPDataPlugin} Data plugin.
131
+ * @return Data plugin.
121
132
  */
122
- function persistencePlugin( registry, pluginOptions ) {
133
+ function persistencePlugin(
134
+ registry: DataRegistry,
135
+ pluginOptions: PersistencePluginOptions
136
+ ): Partial< DataRegistry > {
123
137
  const persistence = createPersistenceInterface( pluginOptions );
124
138
 
125
139
  /**
126
140
  * Creates an enhanced store dispatch function, triggering the state of the
127
141
  * given store name to be persisted when changed.
128
142
  *
129
- * @param {Function} getState Function which returns current state.
130
- * @param {string} storeName Store name.
131
- * @param {?Array<string>} keys Optional subset of keys to save.
143
+ * @param getState Function which returns current state.
144
+ * @param storeName Store name.
145
+ * @param keys Optional subset of keys to save.
132
146
  *
133
- * @return {Function} Enhanced dispatch function.
147
+ * @return Enhanced dispatch function.
134
148
  */
135
- function createPersistOnChange( getState, storeName, keys ) {
136
- let getPersistedState;
149
+ function createPersistOnChange(
150
+ getState: () => unknown,
151
+ storeName: string,
152
+ keys: boolean | string[]
153
+ ): () => void {
154
+ let getPersistedState: ( state: any, action: any ) => any;
137
155
  if ( Array.isArray( keys ) ) {
138
156
  // Given keys, the persisted state should by produced as an object
139
157
  // of the subset of keys. This implementation uses combineReducers
@@ -141,9 +159,18 @@ function persistencePlugin( registry, pluginOptions ) {
141
159
  // of the property values changes. This allows a strict reference
142
160
  // equality to bypass a persistence set on an unchanging state.
143
161
  const reducers = keys.reduce(
144
- ( accumulator, key ) =>
162
+ (
163
+ accumulator: Record<
164
+ string,
165
+ ( state: any, action: any ) => any
166
+ >,
167
+ key: string
168
+ ) =>
145
169
  Object.assign( accumulator, {
146
- [ key ]: ( state, action ) => action.nextState[ key ],
170
+ [ key ]: (
171
+ state: unknown,
172
+ action: { nextState: Record< string, unknown > }
173
+ ) => action.nextState[ key ],
147
174
  } ),
148
175
  {}
149
176
  );
@@ -152,7 +179,10 @@ function persistencePlugin( registry, pluginOptions ) {
152
179
  combineReducers( reducers )
153
180
  );
154
181
  } else {
155
- getPersistedState = ( state, action ) => action.nextState;
182
+ getPersistedState = (
183
+ _state: unknown,
184
+ action: { nextState: unknown }
185
+ ) => action.nextState;
156
186
  }
157
187
 
158
188
  let lastState = getPersistedState( undefined, {
@@ -171,7 +201,12 @@ function persistencePlugin( registry, pluginOptions ) {
171
201
  }
172
202
 
173
203
  return {
174
- registerStore( storeName, options ) {
204
+ registerStore(
205
+ storeName: string,
206
+ options: ReduxStoreConfig< any, any, any > & {
207
+ persist?: boolean | string[];
208
+ }
209
+ ) {
175
210
  if ( ! options.persist ) {
176
211
  return registry.registerStore( storeName, options );
177
212
  }
@@ -192,9 +227,13 @@ function persistencePlugin( registry, pluginOptions ) {
192
227
  // subset of keys.
193
228
  // - New keys in what would otherwise be used as initial
194
229
  // state are deeply merged as base for persisted value.
195
- initialState = deepmerge( initialState, persistedState, {
196
- isMergeableObject: isPlainObject,
197
- } );
230
+ initialState = deepmerge(
231
+ initialState as Record< string, unknown >,
232
+ persistedState as Record< string, unknown >,
233
+ {
234
+ isMergeableObject: isPlainObject,
235
+ }
236
+ );
198
237
  } else {
199
238
  // If there is a mismatch in object-likeness of default
200
239
  // initial or persisted state, defer to persisted value.
@@ -213,7 +252,7 @@ function persistencePlugin( registry, pluginOptions ) {
213
252
  createPersistOnChange(
214
253
  store.getState,
215
254
  storeName,
216
- options.persist
255
+ options.persist!
217
256
  )
218
257
  );
219
258
 
@@ -222,6 +261,6 @@ function persistencePlugin( registry, pluginOptions ) {
222
261
  };
223
262
  }
224
263
 
225
- persistencePlugin.__unstableMigrate = () => {};
226
-
227
- export default persistencePlugin;
264
+ export default Object.assign( persistencePlugin, {
265
+ __unstableMigrate: () => {},
266
+ } );
@@ -1,9 +1,10 @@
1
1
  /**
2
2
  * Internal dependencies
3
3
  */
4
+ import type { StorageInterface } from '../../../types';
4
5
  import objectStorage from './object';
5
6
 
6
- let storage;
7
+ let storage: StorageInterface & { removeItem?: ( key: string ) => void };
7
8
 
8
9
  try {
9
10
  // Private Browsing in Safari 10 and earlier will throw an error when
@@ -11,7 +12,7 @@ try {
11
12
  // causing a thrown error as condition for using fallback object storage.
12
13
  storage = window.localStorage;
13
14
  storage.setItem( '__wpDataTestLocalStorage', '' );
14
- storage.removeItem( '__wpDataTestLocalStorage' );
15
+ storage.removeItem!( '__wpDataTestLocalStorage' );
15
16
  } catch ( error ) {
16
17
  storage = objectStorage;
17
18
  }
@@ -0,0 +1,25 @@
1
+ import type { StorageInterface } from '../../../types';
2
+
3
+ let objectStorage: Record< string, string > | undefined;
4
+
5
+ const storage: StorageInterface & { clear: VoidFunction } = {
6
+ getItem( key: string ): string | null {
7
+ if ( ! objectStorage || ! objectStorage[ key ] ) {
8
+ return null;
9
+ }
10
+
11
+ return objectStorage[ key ];
12
+ },
13
+ setItem( key: string, value: string ) {
14
+ if ( ! objectStorage ) {
15
+ storage.clear();
16
+ }
17
+
18
+ objectStorage![ key ] = String( value );
19
+ },
20
+ clear() {
21
+ objectStorage = Object.create( null );
22
+ },
23
+ };
24
+
25
+ export default storage;
@@ -1,19 +1,19 @@
1
1
  /**
2
2
  * External dependencies
3
3
  */
4
+ import type { Middleware } from 'redux';
4
5
  import isPromise from 'is-promise';
5
6
 
6
7
  /**
7
8
  * Simplest possible promise redux middleware.
8
- *
9
- * @type {import('redux').Middleware}
10
9
  */
11
- const promiseMiddleware = () => ( next ) => ( action ) => {
10
+ const promiseMiddleware: Middleware = () => ( next ) => ( action ) => {
12
11
  if ( isPromise( action ) ) {
13
12
  return action.then( ( resolvedAction ) => {
14
13
  if ( resolvedAction ) {
15
14
  return next( resolvedAction );
16
15
  }
16
+ return undefined;
17
17
  } );
18
18
  }
19
19