@aws-amplify/core 5.8.2 → 6.0.1-console-preview.f63250a.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 (300) hide show
  1. package/README.md +3 -0
  2. package/lib/Amplify.d.ts +18 -17
  3. package/lib/Amplify.js +9 -0
  4. package/lib/AwsClients/CognitoIdentity/base.d.ts +3 -10
  5. package/lib/AwsClients/CognitoIdentity/getCredentialsForIdentity.d.ts +3 -17
  6. package/lib/AwsClients/CognitoIdentity/getCredentialsForIdentity.js +8 -8
  7. package/lib/AwsClients/CognitoIdentity/getId.d.ts +3 -17
  8. package/lib/AwsClients/Pinpoint/base.d.ts +2 -10
  9. package/lib/AwsClients/Pinpoint/getInAppMessages.d.ts +3 -17
  10. package/lib/AwsClients/Pinpoint/getInAppMessages.js +1 -1
  11. package/lib/AwsClients/Pinpoint/putEvents.d.ts +3 -17
  12. package/lib/AwsClients/Pinpoint/putEvents.js +6 -0
  13. package/lib/AwsClients/Pinpoint/updateEndpoint.d.ts +3 -17
  14. package/lib/AwsClients/Pinpoint/updateEndpoint.js +2 -2
  15. package/lib/Cache/AsyncStorageCache.d.ts +154 -0
  16. package/lib/Cache/AsyncStorageCache.js +744 -0
  17. package/lib/Cache/BrowserStorageCache.d.ts +166 -0
  18. package/lib/Cache/BrowserStorageCache.js +469 -0
  19. package/lib/Cache/InMemoryCache.d.ts +129 -0
  20. package/lib/Cache/InMemoryCache.js +349 -0
  21. package/lib/Cache/StorageCache.d.ts +39 -0
  22. package/lib/Cache/StorageCache.js +121 -0
  23. package/lib/Cache/Utils/CacheList.d.ts +89 -0
  24. package/lib/Cache/Utils/CacheList.js +196 -0
  25. package/lib/Cache/Utils/CacheUtils.d.ts +25 -0
  26. package/lib/Cache/Utils/CacheUtils.js +88 -0
  27. package/lib/Cache/Utils/index.d.ts +2 -0
  28. package/lib/Cache/Utils/index.js +16 -0
  29. package/lib/Cache/reactnative.d.ts +3 -0
  30. package/lib/Cache/reactnative.js +8 -0
  31. package/lib/Cache/types/Cache.d.ts +55 -0
  32. package/lib/Cache/types/Cache.js +4 -0
  33. package/lib/Cache/types/index.d.ts +1 -0
  34. package/lib/Cache/types/index.js +19 -0
  35. package/lib/ClientDevice/android.js +3 -2
  36. package/lib/ClientDevice/ios.d.ts +2 -2
  37. package/lib/ClientDevice/ios.js +3 -2
  38. package/lib/ClientDevice/reactnative.js +3 -2
  39. package/lib/Credentials.d.ts +1 -1
  40. package/lib/Credentials.js +2 -1
  41. package/lib/Errors.d.ts +19 -0
  42. package/lib/Errors.js +50 -1
  43. package/lib/Hub.js +1 -0
  44. package/lib/I18n/I18n.d.ts +29 -9
  45. package/lib/I18n/I18n.js +30 -5
  46. package/lib/I18n/index.d.ts +18 -5
  47. package/lib/I18n/index.js +35 -5
  48. package/lib/I18n/types.d.ts +4 -1
  49. package/lib/I18n/types.js +3 -0
  50. package/lib/JS.d.ts +6 -6
  51. package/lib/Logger/ConsoleLogger.d.ts +11 -11
  52. package/lib/OAuthHelper/GoogleOAuth.js +2 -1
  53. package/lib/Platform/detectFramework.js +2 -1
  54. package/lib/Platform/detection/Next.d.ts +1 -1
  55. package/lib/Platform/detection/React.js +1 -0
  56. package/lib/Platform/version.d.ts +1 -1
  57. package/lib/Platform/version.js +1 -1
  58. package/lib/Providers/AWSCloudWatchProvider.d.ts +2 -2
  59. package/lib/Providers/AWSCloudWatchProvider.js +35 -12
  60. package/lib/RNComponents/index.d.ts +1 -1
  61. package/lib/RNComponents/reactnative.js +3 -2
  62. package/lib/ServiceWorker/ServiceWorker.d.ts +4 -4
  63. package/lib/ServiceWorker/ServiceWorker.js +37 -11
  64. package/lib/Signer.d.ts +14 -1
  65. package/lib/StorageHelper/cookieStorage.d.ts +13 -0
  66. package/lib/StorageHelper/cookieStorage.js +167 -0
  67. package/lib/StorageHelper/inMemoryStorage.d.ts +10 -0
  68. package/lib/StorageHelper/inMemoryStorage.js +82 -0
  69. package/lib/StorageHelper/index.d.ts +7 -3
  70. package/lib/StorageHelper/index.js +9 -1
  71. package/lib/StorageHelper/localStorage.d.ts +32 -0
  72. package/lib/StorageHelper/localStorage.js +114 -0
  73. package/lib/StorageHelper/reactnative.d.ts +71 -1
  74. package/lib/StorageHelper/reactnative.js +154 -10
  75. package/lib/StorageHelper/sessionStorage.d.ts +32 -0
  76. package/lib/StorageHelper/sessionStorage.js +114 -0
  77. package/lib/UniversalStorage/index.d.ts +2 -2
  78. package/lib/Util/BackgroundProcessManager.d.ts +2 -2
  79. package/lib/Util/BackgroundProcessManager.js +1 -0
  80. package/lib/Util/DateUtils.d.ts +12 -1
  81. package/lib/Util/Reachability.js +7 -5
  82. package/lib/Util/Reachability.native.d.ts +3 -2
  83. package/lib/Util/StringUtils.js +3 -10
  84. package/lib/Util/errors/AssertError.d.ts +2 -0
  85. package/lib/Util/errors/AssertError.js +11 -0
  86. package/lib/clients/middleware/retry/defaultRetryDecider.d.ts +1 -1
  87. package/lib/clients/middleware/retry/defaultRetryDecider.js +17 -13
  88. package/lib/clients/middleware/retry/isClockSkewError.js +1 -1
  89. package/lib/clients/middleware/retry/middleware.d.ts +1 -1
  90. package/lib/clients/middleware/retry/middleware.js +13 -13
  91. package/lib/clients/middleware/signing/signer/signatureV4/utils/dataHashHelpers.js +1 -1
  92. package/lib/clients/serde/responseInfo.js +1 -1
  93. package/lib/clients/types/aws.d.ts +4 -0
  94. package/lib/clients/types/index.d.ts +1 -1
  95. package/lib/constants.d.ts +7 -0
  96. package/lib/constants.js +9 -1
  97. package/lib/index.d.ts +18 -4
  98. package/lib/index.js +32 -5
  99. package/lib/parseAWSExports.d.ts +1 -1
  100. package/lib/singleton/Auth/index.d.ts +30 -0
  101. package/lib/singleton/Auth/index.js +133 -0
  102. package/lib/singleton/Auth/types.d.ts +95 -0
  103. package/lib/singleton/Auth/types.js +6 -0
  104. package/lib/singleton/Auth/utils/index.d.ts +5 -0
  105. package/lib/singleton/Auth/utils/index.js +52 -0
  106. package/lib/singleton/Storage/types.d.ts +14 -0
  107. package/lib/singleton/Storage/types.js +4 -0
  108. package/lib/singleton/index.d.ts +46 -0
  109. package/lib/singleton/index.js +147 -0
  110. package/lib/singleton/types.d.ts +21 -0
  111. package/lib/singleton/types.js +4 -0
  112. package/lib/tsconfig.tsbuildinfo +1 -1
  113. package/lib/types/types.d.ts +31 -1
  114. package/lib/types/types.js +2 -0
  115. package/lib-esm/Amplify.d.ts +18 -17
  116. package/lib-esm/Amplify.js +9 -0
  117. package/lib-esm/AwsClients/CognitoIdentity/base.d.ts +3 -10
  118. package/lib-esm/AwsClients/CognitoIdentity/getCredentialsForIdentity.d.ts +3 -17
  119. package/lib-esm/AwsClients/CognitoIdentity/getCredentialsForIdentity.js +8 -8
  120. package/lib-esm/AwsClients/CognitoIdentity/getId.d.ts +3 -17
  121. package/lib-esm/AwsClients/Pinpoint/base.d.ts +2 -10
  122. package/lib-esm/AwsClients/Pinpoint/getInAppMessages.d.ts +3 -17
  123. package/lib-esm/AwsClients/Pinpoint/getInAppMessages.js +1 -1
  124. package/lib-esm/AwsClients/Pinpoint/putEvents.d.ts +3 -17
  125. package/lib-esm/AwsClients/Pinpoint/putEvents.js +6 -0
  126. package/lib-esm/AwsClients/Pinpoint/updateEndpoint.d.ts +3 -17
  127. package/lib-esm/AwsClients/Pinpoint/updateEndpoint.js +2 -2
  128. package/lib-esm/Cache/AsyncStorageCache.d.ts +154 -0
  129. package/lib-esm/Cache/AsyncStorageCache.js +737 -0
  130. package/lib-esm/Cache/BrowserStorageCache.d.ts +166 -0
  131. package/lib-esm/Cache/BrowserStorageCache.js +466 -0
  132. package/lib-esm/Cache/InMemoryCache.d.ts +129 -0
  133. package/lib-esm/Cache/InMemoryCache.js +346 -0
  134. package/lib-esm/Cache/StorageCache.d.ts +39 -0
  135. package/lib-esm/Cache/StorageCache.js +118 -0
  136. package/lib-esm/Cache/Utils/CacheList.d.ts +89 -0
  137. package/lib-esm/Cache/Utils/CacheList.js +194 -0
  138. package/lib-esm/Cache/Utils/CacheUtils.d.ts +25 -0
  139. package/lib-esm/Cache/Utils/CacheUtils.js +81 -0
  140. package/lib-esm/Cache/Utils/index.d.ts +2 -0
  141. package/lib-esm/Cache/Utils/index.js +4 -0
  142. package/lib-esm/Cache/reactnative.d.ts +3 -0
  143. package/lib-esm/Cache/reactnative.js +6 -0
  144. package/lib-esm/Cache/types/Cache.d.ts +55 -0
  145. package/lib-esm/Cache/types/Cache.js +3 -0
  146. package/lib-esm/Cache/types/index.d.ts +1 -0
  147. package/lib-esm/Cache/types/index.js +3 -0
  148. package/lib-esm/ClientDevice/android.js +1 -0
  149. package/lib-esm/ClientDevice/ios.d.ts +2 -2
  150. package/lib-esm/ClientDevice/ios.js +1 -0
  151. package/lib-esm/ClientDevice/reactnative.js +1 -0
  152. package/lib-esm/Credentials.d.ts +1 -1
  153. package/lib-esm/Credentials.js +2 -1
  154. package/lib-esm/Errors.d.ts +19 -0
  155. package/lib-esm/Errors.js +49 -0
  156. package/lib-esm/Hub.js +1 -0
  157. package/lib-esm/I18n/I18n.d.ts +29 -9
  158. package/lib-esm/I18n/I18n.js +30 -5
  159. package/lib-esm/I18n/index.d.ts +18 -5
  160. package/lib-esm/I18n/index.js +35 -5
  161. package/lib-esm/I18n/types.d.ts +4 -1
  162. package/lib-esm/I18n/types.js +3 -0
  163. package/lib-esm/JS.d.ts +6 -6
  164. package/lib-esm/Logger/ConsoleLogger.d.ts +11 -11
  165. package/lib-esm/OAuthHelper/GoogleOAuth.js +2 -1
  166. package/lib-esm/Platform/detectFramework.js +2 -1
  167. package/lib-esm/Platform/detection/Next.d.ts +1 -1
  168. package/lib-esm/Platform/detection/React.js +1 -0
  169. package/lib-esm/Platform/version.d.ts +1 -1
  170. package/lib-esm/Platform/version.js +1 -1
  171. package/lib-esm/Providers/AWSCloudWatchProvider.d.ts +2 -2
  172. package/lib-esm/Providers/AWSCloudWatchProvider.js +33 -10
  173. package/lib-esm/RNComponents/index.d.ts +1 -1
  174. package/lib-esm/RNComponents/reactnative.js +1 -0
  175. package/lib-esm/ServiceWorker/ServiceWorker.d.ts +4 -4
  176. package/lib-esm/ServiceWorker/ServiceWorker.js +37 -11
  177. package/lib-esm/Signer.d.ts +14 -1
  178. package/lib-esm/StorageHelper/cookieStorage.d.ts +13 -0
  179. package/lib-esm/StorageHelper/cookieStorage.js +141 -0
  180. package/lib-esm/StorageHelper/inMemoryStorage.d.ts +10 -0
  181. package/lib-esm/StorageHelper/inMemoryStorage.js +79 -0
  182. package/lib-esm/StorageHelper/index.d.ts +7 -3
  183. package/lib-esm/StorageHelper/index.js +4 -0
  184. package/lib-esm/StorageHelper/localStorage.d.ts +32 -0
  185. package/lib-esm/StorageHelper/localStorage.js +111 -0
  186. package/lib-esm/StorageHelper/reactnative.d.ts +71 -1
  187. package/lib-esm/StorageHelper/reactnative.js +153 -9
  188. package/lib-esm/StorageHelper/sessionStorage.d.ts +32 -0
  189. package/lib-esm/StorageHelper/sessionStorage.js +111 -0
  190. package/lib-esm/UniversalStorage/index.d.ts +2 -2
  191. package/lib-esm/Util/BackgroundProcessManager.d.ts +2 -2
  192. package/lib-esm/Util/BackgroundProcessManager.js +1 -0
  193. package/lib-esm/Util/DateUtils.d.ts +12 -1
  194. package/lib-esm/Util/Reachability.js +5 -3
  195. package/lib-esm/Util/Reachability.native.d.ts +3 -2
  196. package/lib-esm/Util/StringUtils.js +3 -10
  197. package/lib-esm/Util/errors/AssertError.d.ts +2 -0
  198. package/lib-esm/Util/errors/AssertError.js +7 -0
  199. package/lib-esm/clients/middleware/retry/defaultRetryDecider.d.ts +1 -1
  200. package/lib-esm/clients/middleware/retry/defaultRetryDecider.js +17 -13
  201. package/lib-esm/clients/middleware/retry/isClockSkewError.js +1 -1
  202. package/lib-esm/clients/middleware/retry/middleware.d.ts +1 -1
  203. package/lib-esm/clients/middleware/retry/middleware.js +13 -13
  204. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/dataHashHelpers.js +1 -1
  205. package/lib-esm/clients/serde/responseInfo.js +1 -1
  206. package/lib-esm/clients/types/aws.d.ts +4 -0
  207. package/lib-esm/clients/types/index.d.ts +1 -1
  208. package/lib-esm/constants.d.ts +7 -0
  209. package/lib-esm/constants.js +8 -0
  210. package/lib-esm/index.d.ts +18 -4
  211. package/lib-esm/index.js +15 -3
  212. package/lib-esm/parseAWSExports.d.ts +1 -1
  213. package/lib-esm/singleton/Auth/index.d.ts +30 -0
  214. package/lib-esm/singleton/Auth/index.js +129 -0
  215. package/lib-esm/singleton/Auth/types.d.ts +95 -0
  216. package/lib-esm/singleton/Auth/types.js +5 -0
  217. package/lib-esm/singleton/Auth/utils/index.d.ts +5 -0
  218. package/lib-esm/singleton/Auth/utils/index.js +45 -0
  219. package/lib-esm/singleton/Storage/types.d.ts +14 -0
  220. package/lib-esm/singleton/Storage/types.js +3 -0
  221. package/lib-esm/singleton/index.d.ts +46 -0
  222. package/lib-esm/singleton/index.js +143 -0
  223. package/lib-esm/singleton/types.d.ts +21 -0
  224. package/lib-esm/singleton/types.js +3 -0
  225. package/lib-esm/tsconfig.tsbuildinfo +1 -1
  226. package/lib-esm/types/types.d.ts +31 -1
  227. package/lib-esm/types/types.js +2 -0
  228. package/package.json +38 -15
  229. package/src/Amplify.ts +9 -3
  230. package/src/AwsClients/CognitoIdentity/base.ts +2 -1
  231. package/src/AwsClients/CognitoIdentity/getCredentialsForIdentity.ts +13 -6
  232. package/src/AwsClients/Pinpoint/base.ts +6 -2
  233. package/src/AwsClients/Pinpoint/getInAppMessages.ts +1 -1
  234. package/src/AwsClients/Pinpoint/putEvents.ts +6 -0
  235. package/src/AwsClients/Pinpoint/updateEndpoint.ts +2 -2
  236. package/src/Cache/AsyncStorageCache.ts +500 -0
  237. package/src/Cache/BrowserStorageCache.ts +517 -0
  238. package/src/Cache/CHANGELOG.md +1227 -0
  239. package/src/Cache/InMemoryCache.ts +354 -0
  240. package/src/Cache/StorageCache.ts +157 -0
  241. package/src/Cache/Utils/CacheList.ts +198 -0
  242. package/src/Cache/Utils/CacheUtils.ts +92 -0
  243. package/src/Cache/Utils/index.ts +11 -0
  244. package/src/Cache/reactnative.ts +9 -0
  245. package/src/Cache/types/Cache.ts +75 -0
  246. package/src/Cache/types/index.ts +4 -0
  247. package/src/ClientDevice/android.ts +1 -1
  248. package/src/ClientDevice/ios.ts +2 -2
  249. package/src/ClientDevice/reactnative.ts +1 -1
  250. package/src/Credentials.ts +19 -17
  251. package/src/Errors.ts +41 -0
  252. package/src/Hub.ts +1 -0
  253. package/src/I18n/I18n.ts +40 -12
  254. package/src/I18n/index.ts +45 -12
  255. package/src/I18n/types.ts +3 -0
  256. package/src/JS.ts +24 -17
  257. package/src/Logger/ConsoleLogger.ts +10 -10
  258. package/src/OAuthHelper/FacebookOAuth.ts +2 -2
  259. package/src/OAuthHelper/GoogleOAuth.ts +9 -6
  260. package/src/Platform/detectFramework.ts +1 -1
  261. package/src/Platform/detection/React.ts +5 -3
  262. package/src/Platform/version.ts +1 -1
  263. package/src/Providers/AWSCloudWatchProvider.ts +39 -14
  264. package/src/RNComponents/index.ts +1 -1
  265. package/src/RNComponents/reactnative.ts +1 -1
  266. package/src/ServiceWorker/ServiceWorker.ts +58 -18
  267. package/src/Signer.ts +22 -3
  268. package/src/StorageHelper/cookieStorage.ts +99 -0
  269. package/src/StorageHelper/inMemoryStorage.ts +32 -0
  270. package/src/StorageHelper/index.ts +6 -1
  271. package/src/StorageHelper/localStorage.ts +60 -0
  272. package/src/StorageHelper/reactnative.ts +94 -14
  273. package/src/StorageHelper/sessionStorage.ts +60 -0
  274. package/src/Util/BackgroundProcessManager.ts +33 -9
  275. package/src/Util/DateUtils.ts +14 -5
  276. package/src/Util/Reachability.native.ts +3 -2
  277. package/src/Util/Reachability.ts +9 -7
  278. package/src/Util/Retry.ts +2 -1
  279. package/src/Util/StringUtils.ts +3 -10
  280. package/src/Util/errors/AssertError.ts +11 -0
  281. package/src/clients/handlers/fetch.ts +2 -2
  282. package/src/clients/middleware/retry/defaultRetryDecider.ts +12 -6
  283. package/src/clients/middleware/retry/isClockSkewError.ts +1 -1
  284. package/src/clients/middleware/retry/middleware.ts +9 -9
  285. package/src/clients/middleware/signing/middleware.ts +1 -1
  286. package/src/clients/middleware/signing/signer/signatureV4/utils/dataHashHelpers.ts +1 -1
  287. package/src/clients/serde/responseInfo.ts +2 -1
  288. package/src/clients/types/aws.ts +10 -0
  289. package/src/clients/types/index.ts +1 -0
  290. package/src/constants.ts +16 -0
  291. package/src/global.d.ts +11 -0
  292. package/src/index.ts +66 -4
  293. package/src/parseAWSExports.ts +1 -1
  294. package/src/singleton/Auth/index.ts +113 -0
  295. package/src/singleton/Auth/types.ts +129 -0
  296. package/src/singleton/Auth/utils/index.ts +68 -0
  297. package/src/singleton/Storage/types.ts +21 -0
  298. package/src/singleton/index.ts +131 -0
  299. package/src/singleton/types.ts +48 -0
  300. package/src/types/types.ts +38 -1
@@ -0,0 +1,737 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ var __extends = (this && this.__extends) || (function () {
4
+ var extendStatics = function (d, b) {
5
+ extendStatics = Object.setPrototypeOf ||
6
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
7
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
8
+ return extendStatics(d, b);
9
+ };
10
+ return function (d, b) {
11
+ if (typeof b !== "function" && b !== null)
12
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
13
+ extendStatics(d, b);
14
+ function __() { this.constructor = d; }
15
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
16
+ };
17
+ })();
18
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
19
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
20
+ return new (P || (P = Promise))(function (resolve, reject) {
21
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
22
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
23
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
24
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
25
+ });
26
+ };
27
+ var __generator = (this && this.__generator) || function (thisArg, body) {
28
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
29
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
30
+ function verb(n) { return function (v) { return step([n, v]); }; }
31
+ function step(op) {
32
+ if (f) throw new TypeError("Generator is already executing.");
33
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
34
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
35
+ if (y = 0, t) op = [op[0] & 2, t.value];
36
+ switch (op[0]) {
37
+ case 0: case 1: t = op; break;
38
+ case 4: _.label++; return { value: op[1], done: false };
39
+ case 5: _.label++; y = op[1]; op = [0]; continue;
40
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
41
+ default:
42
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
43
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
44
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
45
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
46
+ if (t[2]) _.ops.pop();
47
+ _.trys.pop(); continue;
48
+ }
49
+ op = body.call(thisArg, _);
50
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
51
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
52
+ }
53
+ };
54
+ import AsyncStorage from '@react-native-async-storage/async-storage';
55
+ import { ConsoleLogger as Logger } from '../Logger';
56
+ import { StorageCache } from './StorageCache';
57
+ import { defaultConfig, getCurrTime } from './Utils';
58
+ import { STORAGE_CACHE_EXCEPTION } from '../constants';
59
+ import { asserts } from '../Util/errors/AssertError';
60
+ import { getCurrSizeKey } from './Utils/CacheUtils';
61
+ var logger = new Logger('AsyncStorageCache');
62
+ /*
63
+ * Customized cache which based on the AsyncStorage with LRU implemented
64
+ */
65
+ var AsyncStorageCache = /** @class */ (function (_super) {
66
+ __extends(AsyncStorageCache, _super);
67
+ /**
68
+ * initialize the cache
69
+ *
70
+ * @param {Object} config - the configuration of the cache
71
+ */
72
+ function AsyncStorageCache(config) {
73
+ var _this = _super.call(this, config) || this;
74
+ _this.getItem = _this.getItem.bind(_this);
75
+ _this.setItem = _this.setItem.bind(_this);
76
+ _this.removeItem = _this.removeItem.bind(_this);
77
+ logger.debug('Using AsyncStorageCache');
78
+ return _this;
79
+ }
80
+ /**
81
+ * decrease current size of the cache
82
+ * @private
83
+ * @param amount - the amount of the cache size which needs to be decreased
84
+ */
85
+ AsyncStorageCache.prototype._decreaseCurSizeInBytes = function (amount) {
86
+ return __awaiter(this, void 0, void 0, function () {
87
+ var curSize;
88
+ return __generator(this, function (_a) {
89
+ switch (_a.label) {
90
+ case 0: return [4 /*yield*/, this.getCacheCurSize()];
91
+ case 1:
92
+ curSize = _a.sent();
93
+ return [4 /*yield*/, AsyncStorage.setItem(getCurrSizeKey(this.cacheConfig.keyPrefix), (curSize - amount).toString())];
94
+ case 2:
95
+ _a.sent();
96
+ return [2 /*return*/];
97
+ }
98
+ });
99
+ });
100
+ };
101
+ /**
102
+ * increase current size of the cache
103
+ * @private
104
+ * @param amount - the amount of the cache szie which need to be increased
105
+ */
106
+ AsyncStorageCache.prototype._increaseCurSizeInBytes = function (amount) {
107
+ return __awaiter(this, void 0, void 0, function () {
108
+ var curSize;
109
+ return __generator(this, function (_a) {
110
+ switch (_a.label) {
111
+ case 0: return [4 /*yield*/, this.getCacheCurSize()];
112
+ case 1:
113
+ curSize = _a.sent();
114
+ return [4 /*yield*/, AsyncStorage.setItem(getCurrSizeKey(this.cacheConfig.keyPrefix), (curSize + amount).toString())];
115
+ case 2:
116
+ _a.sent();
117
+ return [2 /*return*/];
118
+ }
119
+ });
120
+ });
121
+ };
122
+ /**
123
+ * update the visited time if item has been visited
124
+ * @private
125
+ * @param item - the item which need to be refreshed
126
+ * @param prefixedKey - the key of the item
127
+ *
128
+ * @return the refreshed item
129
+ */
130
+ AsyncStorageCache.prototype._refreshItem = function (item, prefixedKey) {
131
+ return __awaiter(this, void 0, void 0, function () {
132
+ return __generator(this, function (_a) {
133
+ switch (_a.label) {
134
+ case 0:
135
+ item.visitedTime = getCurrTime();
136
+ return [4 /*yield*/, AsyncStorage.setItem(prefixedKey, JSON.stringify(item))];
137
+ case 1:
138
+ _a.sent();
139
+ return [2 /*return*/, item];
140
+ }
141
+ });
142
+ });
143
+ };
144
+ /**
145
+ * check wether item is expired
146
+ * @private
147
+ * @param key - the key of the item
148
+ *
149
+ * @return true if the item is expired.
150
+ */
151
+ AsyncStorageCache.prototype._isExpired = function (key) {
152
+ return __awaiter(this, void 0, void 0, function () {
153
+ var text, item;
154
+ return __generator(this, function (_a) {
155
+ switch (_a.label) {
156
+ case 0: return [4 /*yield*/, AsyncStorage.getItem(key)];
157
+ case 1:
158
+ text = _a.sent();
159
+ asserts(text !== null, {
160
+ name: STORAGE_CACHE_EXCEPTION,
161
+ message: "Item not found in the storage by the key: ".concat(key, "."),
162
+ });
163
+ item = JSON.parse(text);
164
+ if (getCurrTime() >= item.expires) {
165
+ return [2 /*return*/, true];
166
+ }
167
+ return [2 /*return*/, false];
168
+ }
169
+ });
170
+ });
171
+ };
172
+ /**
173
+ * delete item from cache
174
+ * @private
175
+ * @param prefixedKey - the key of the item
176
+ * @param size - optional, the byte size of the item
177
+ */
178
+ AsyncStorageCache.prototype._removeItem = function (prefixedKey, size) {
179
+ return __awaiter(this, void 0, void 0, function () {
180
+ var config, itemSize, removeItemError_1;
181
+ return __generator(this, function (_a) {
182
+ switch (_a.label) {
183
+ case 0: return [4 /*yield*/, AsyncStorage.getItem(prefixedKey)];
184
+ case 1:
185
+ config = _a.sent();
186
+ asserts(!!config, {
187
+ name: STORAGE_CACHE_EXCEPTION,
188
+ message: "Item not found in the storage by the key: ".concat(prefixedKey, "."),
189
+ });
190
+ itemSize = size !== null && size !== void 0 ? size : JSON.parse(config).byteSize;
191
+ // first try to update the current size of the cache
192
+ return [4 /*yield*/, this._decreaseCurSizeInBytes(itemSize)];
193
+ case 2:
194
+ // first try to update the current size of the cache
195
+ _a.sent();
196
+ _a.label = 3;
197
+ case 3:
198
+ _a.trys.push([3, 5, , 7]);
199
+ return [4 /*yield*/, AsyncStorage.removeItem(prefixedKey)];
200
+ case 4:
201
+ _a.sent();
202
+ return [3 /*break*/, 7];
203
+ case 5:
204
+ removeItemError_1 = _a.sent();
205
+ // if some error happened, we need to rollback the current size
206
+ return [4 /*yield*/, this._increaseCurSizeInBytes(itemSize)];
207
+ case 6:
208
+ // if some error happened, we need to rollback the current size
209
+ _a.sent();
210
+ logger.error("Failed to remove item: ".concat(removeItemError_1));
211
+ return [3 /*break*/, 7];
212
+ case 7: return [2 /*return*/];
213
+ }
214
+ });
215
+ });
216
+ };
217
+ /**
218
+ * put item into cache
219
+ * @private
220
+ * @param prefixedKey - the key of the item
221
+ * @param itemData - the value of the item
222
+ * @param itemSizeInBytes - the byte size of the item
223
+ */
224
+ AsyncStorageCache.prototype._setItem = function (prefixedKey, item) {
225
+ return __awaiter(this, void 0, void 0, function () {
226
+ var setItemErr_1;
227
+ return __generator(this, function (_a) {
228
+ switch (_a.label) {
229
+ case 0:
230
+ // first try to update the current size of the cache.
231
+ return [4 /*yield*/, this._increaseCurSizeInBytes(item.byteSize)];
232
+ case 1:
233
+ // first try to update the current size of the cache.
234
+ _a.sent();
235
+ _a.label = 2;
236
+ case 2:
237
+ _a.trys.push([2, 4, , 6]);
238
+ return [4 /*yield*/, AsyncStorage.setItem(prefixedKey, JSON.stringify(item))];
239
+ case 3:
240
+ _a.sent();
241
+ return [3 /*break*/, 6];
242
+ case 4:
243
+ setItemErr_1 = _a.sent();
244
+ // if some error happened, we need to rollback the current size
245
+ return [4 /*yield*/, this._decreaseCurSizeInBytes(item.byteSize)];
246
+ case 5:
247
+ // if some error happened, we need to rollback the current size
248
+ _a.sent();
249
+ logger.error("Failed to set item ".concat(setItemErr_1));
250
+ return [3 /*break*/, 6];
251
+ case 6: return [2 /*return*/];
252
+ }
253
+ });
254
+ });
255
+ };
256
+ /**
257
+ * total space needed when poping out items
258
+ * @private
259
+ * @param itemSize
260
+ *
261
+ * @return total space needed
262
+ */
263
+ AsyncStorageCache.prototype._sizeToPop = function (itemSize) {
264
+ return __awaiter(this, void 0, void 0, function () {
265
+ var spaceItemNeed, cacheThresholdSpace;
266
+ return __generator(this, function (_a) {
267
+ switch (_a.label) {
268
+ case 0: return [4 /*yield*/, this.getCacheCurSize()];
269
+ case 1:
270
+ spaceItemNeed = (_a.sent()) +
271
+ itemSize -
272
+ this.cacheConfig.capacityInBytes;
273
+ cacheThresholdSpace = (1 - this.cacheConfig.warningThreshold) *
274
+ this.cacheConfig.capacityInBytes;
275
+ return [2 /*return*/, spaceItemNeed > cacheThresholdSpace
276
+ ? spaceItemNeed
277
+ : cacheThresholdSpace];
278
+ }
279
+ });
280
+ });
281
+ };
282
+ /**
283
+ * see whether cache is full
284
+ * @private
285
+ * @param itemSize
286
+ *
287
+ * @return true if cache is full
288
+ */
289
+ AsyncStorageCache.prototype._isCacheFull = function (itemSize) {
290
+ return __awaiter(this, void 0, void 0, function () {
291
+ var _a;
292
+ return __generator(this, function (_b) {
293
+ switch (_b.label) {
294
+ case 0:
295
+ _a = itemSize;
296
+ return [4 /*yield*/, this.getCacheCurSize()];
297
+ case 1: return [2 /*return*/, (_a + (_b.sent()) >
298
+ this.cacheConfig.capacityInBytes)];
299
+ }
300
+ });
301
+ });
302
+ };
303
+ /**
304
+ * scan the storage and find out all the keys owned by this cache
305
+ * also clean the expired keys while scanning
306
+ * @private
307
+ * @return array of keys
308
+ */
309
+ AsyncStorageCache.prototype._findValidKeys = function () {
310
+ return __awaiter(this, void 0, void 0, function () {
311
+ var keys, keyInCache, i, key;
312
+ return __generator(this, function (_a) {
313
+ switch (_a.label) {
314
+ case 0:
315
+ keys = [];
316
+ keyInCache = [];
317
+ return [4 /*yield*/, AsyncStorage.getAllKeys()];
318
+ case 1:
319
+ keyInCache = _a.sent();
320
+ i = 0;
321
+ _a.label = 2;
322
+ case 2:
323
+ if (!(i < keyInCache.length)) return [3 /*break*/, 7];
324
+ key = keyInCache[i];
325
+ if (!(key.indexOf(this.cacheConfig.keyPrefix) === 0 &&
326
+ key !== getCurrSizeKey(this.cacheConfig.keyPrefix))) return [3 /*break*/, 6];
327
+ return [4 /*yield*/, this._isExpired(key)];
328
+ case 3:
329
+ if (!_a.sent()) return [3 /*break*/, 5];
330
+ return [4 /*yield*/, this._removeItem(key)];
331
+ case 4:
332
+ _a.sent();
333
+ return [3 /*break*/, 6];
334
+ case 5:
335
+ keys.push(key);
336
+ _a.label = 6;
337
+ case 6:
338
+ i += 1;
339
+ return [3 /*break*/, 2];
340
+ case 7: return [2 /*return*/, keys];
341
+ }
342
+ });
343
+ });
344
+ };
345
+ /**
346
+ * get all the items we have, sort them by their priority,
347
+ * if priority is same, sort them by their last visited time
348
+ * pop out items from the low priority (5 is the lowest)
349
+ * @private
350
+ * @param keys - all the keys in this cache
351
+ * @param sizeToPop - the total size of the items which needed to be poped out
352
+ */
353
+ AsyncStorageCache.prototype._popOutItems = function (keys, sizeToPop) {
354
+ return __awaiter(this, void 0, void 0, function () {
355
+ var items, remainedSize, i, val, item, i;
356
+ return __generator(this, function (_a) {
357
+ switch (_a.label) {
358
+ case 0:
359
+ items = [];
360
+ remainedSize = sizeToPop;
361
+ i = 0;
362
+ _a.label = 1;
363
+ case 1:
364
+ if (!(i < keys.length)) return [3 /*break*/, 4];
365
+ return [4 /*yield*/, AsyncStorage.getItem(keys[i])];
366
+ case 2:
367
+ val = _a.sent();
368
+ if (val != null) {
369
+ item = JSON.parse(val);
370
+ items.push(item);
371
+ }
372
+ _a.label = 3;
373
+ case 3:
374
+ i += 1;
375
+ return [3 /*break*/, 1];
376
+ case 4:
377
+ // first compare priority
378
+ // then compare visited time
379
+ items.sort(function (a, b) {
380
+ if (a.priority > b.priority) {
381
+ return -1;
382
+ }
383
+ else if (a.priority < b.priority) {
384
+ return 1;
385
+ }
386
+ else {
387
+ if (a.visitedTime < b.visitedTime) {
388
+ return -1;
389
+ }
390
+ else
391
+ return 1;
392
+ }
393
+ });
394
+ i = 0;
395
+ _a.label = 5;
396
+ case 5:
397
+ if (!(i < items.length)) return [3 /*break*/, 8];
398
+ // pop out items until we have enough room for new item
399
+ return [4 /*yield*/, this._removeItem(items[i].key, items[i].byteSize)];
400
+ case 6:
401
+ // pop out items until we have enough room for new item
402
+ _a.sent();
403
+ remainedSize -= items[i].byteSize;
404
+ if (remainedSize <= 0) {
405
+ return [2 /*return*/];
406
+ }
407
+ _a.label = 7;
408
+ case 7:
409
+ i += 1;
410
+ return [3 /*break*/, 5];
411
+ case 8: return [2 /*return*/];
412
+ }
413
+ });
414
+ });
415
+ };
416
+ /**
417
+ * Set item into cache. You can put number, string, boolean or object.
418
+ * The cache will first check whether has the same key.
419
+ * If it has, it will delete the old item and then put the new item in
420
+ * The cache will pop out items if it is full
421
+ * You can specify the cache item options. The cache will abort and output a warning:
422
+ * If the key is invalid
423
+ * If the size of the item exceeds itemMaxSize.
424
+ * If the value is undefined
425
+ * If incorrect cache item configuration
426
+ * If error happened with browser storage
427
+ *
428
+ * @param {String} key - the key of the item
429
+ * @param {Object} value - the value of the item
430
+ * @param {Object} [options] - optional, the specified meta-data
431
+ * @return {Promise}
432
+ */
433
+ AsyncStorageCache.prototype.setItem = function (key, value, options) {
434
+ return __awaiter(this, void 0, void 0, function () {
435
+ var prefixedKey, cacheItemOptions, item, val, validKeys, sizeToPop, e_1;
436
+ return __generator(this, function (_a) {
437
+ switch (_a.label) {
438
+ case 0:
439
+ logger.debug("Set item: key is ".concat(key, ", value is ").concat(value, " with options: ").concat(options));
440
+ prefixedKey = this.cacheConfig.keyPrefix + key;
441
+ // invalid keys
442
+ if (prefixedKey === this.cacheConfig.keyPrefix ||
443
+ prefixedKey === getCurrSizeKey(this.cacheConfig.keyPrefix)) {
444
+ logger.warn("Invalid key: should not be empty or 'CurSize'");
445
+ return [2 /*return*/];
446
+ }
447
+ if (typeof value === 'undefined') {
448
+ logger.warn("The value of item should not be undefined!");
449
+ return [2 /*return*/];
450
+ }
451
+ cacheItemOptions = {
452
+ priority: options && options.priority !== undefined
453
+ ? options.priority
454
+ : this.cacheConfig.defaultPriority,
455
+ expires: options && options.expires !== undefined
456
+ ? options.expires
457
+ : this.cacheConfig.defaultTTL + getCurrTime(),
458
+ };
459
+ if (cacheItemOptions.priority < 1 || cacheItemOptions.priority > 5) {
460
+ logger.warn("Invalid parameter: priority due to out or range. It should be within 1 and 5.");
461
+ return [2 /*return*/];
462
+ }
463
+ item = this.fillCacheItem(prefixedKey, value, cacheItemOptions);
464
+ // check wether this item is too big;
465
+ if (item.byteSize > this.cacheConfig.itemMaxSize) {
466
+ logger.warn("Item with key: ".concat(key, " you are trying to put into is too big!"));
467
+ return [2 /*return*/];
468
+ }
469
+ _a.label = 1;
470
+ case 1:
471
+ _a.trys.push([1, 12, , 13]);
472
+ return [4 /*yield*/, AsyncStorage.getItem(prefixedKey)];
473
+ case 2:
474
+ val = _a.sent();
475
+ if (!val) return [3 /*break*/, 4];
476
+ return [4 /*yield*/, this._removeItem(prefixedKey, JSON.parse(val).byteSize)];
477
+ case 3:
478
+ _a.sent();
479
+ _a.label = 4;
480
+ case 4: return [4 /*yield*/, this._isCacheFull(item.byteSize)];
481
+ case 5:
482
+ if (!_a.sent()) return [3 /*break*/, 10];
483
+ return [4 /*yield*/, this._findValidKeys()];
484
+ case 6:
485
+ validKeys = _a.sent();
486
+ return [4 /*yield*/, this._isCacheFull(item.byteSize)];
487
+ case 7:
488
+ if (!_a.sent()) return [3 /*break*/, 10];
489
+ return [4 /*yield*/, this._sizeToPop(item.byteSize)];
490
+ case 8:
491
+ sizeToPop = _a.sent();
492
+ return [4 /*yield*/, this._popOutItems(validKeys, sizeToPop)];
493
+ case 9:
494
+ _a.sent();
495
+ _a.label = 10;
496
+ case 10:
497
+ // put item in the cache
498
+ return [4 /*yield*/, this._setItem(prefixedKey, item)];
499
+ case 11:
500
+ // put item in the cache
501
+ _a.sent();
502
+ return [3 /*break*/, 13];
503
+ case 12:
504
+ e_1 = _a.sent();
505
+ logger.warn("setItem failed! ".concat(e_1));
506
+ return [3 /*break*/, 13];
507
+ case 13: return [2 /*return*/];
508
+ }
509
+ });
510
+ });
511
+ };
512
+ /**
513
+ * Get item from cache. It will return null if item doesn’t exist or it has been expired.
514
+ * If you specified callback function in the options,
515
+ * then the function will be executed if no such item in the cache
516
+ * and finally put the return value into cache.
517
+ * Please make sure the callback function will return the value you want to put into the cache.
518
+ * The cache will abort output a warning:
519
+ * If the key is invalid
520
+ * If error happened with AsyncStorage
521
+ *
522
+ * @param {String} key - the key of the item
523
+ * @param {Object} [options] - the options of callback function
524
+ * @return {Promise} - return a promise resolves to be the value of the item
525
+ */
526
+ AsyncStorageCache.prototype.getItem = function (key, options) {
527
+ return __awaiter(this, void 0, void 0, function () {
528
+ var ret, prefixedKey, item, val, e_2;
529
+ return __generator(this, function (_a) {
530
+ switch (_a.label) {
531
+ case 0:
532
+ logger.debug("Get item: key is ".concat(key, " with options ").concat(options));
533
+ ret = null;
534
+ prefixedKey = this.cacheConfig.keyPrefix + key;
535
+ if (prefixedKey === this.cacheConfig.keyPrefix ||
536
+ prefixedKey === getCurrSizeKey(this.cacheConfig.keyPrefix)) {
537
+ logger.warn("Invalid key: should not be empty or 'CurSize'");
538
+ return [2 /*return*/, null];
539
+ }
540
+ _a.label = 1;
541
+ case 1:
542
+ _a.trys.push([1, 8, , 9]);
543
+ return [4 /*yield*/, AsyncStorage.getItem(prefixedKey)];
544
+ case 2:
545
+ ret = _a.sent();
546
+ if (!(ret != null)) return [3 /*break*/, 7];
547
+ return [4 /*yield*/, this._isExpired(prefixedKey)];
548
+ case 3:
549
+ if (!_a.sent()) return [3 /*break*/, 5];
550
+ // if expired, remove that item and return null
551
+ return [4 /*yield*/, this._removeItem(prefixedKey, JSON.parse(ret).byteSize)];
552
+ case 4:
553
+ // if expired, remove that item and return null
554
+ _a.sent();
555
+ return [3 /*break*/, 7];
556
+ case 5:
557
+ item = JSON.parse(ret);
558
+ return [4 /*yield*/, this._refreshItem(item, prefixedKey)];
559
+ case 6:
560
+ item = _a.sent();
561
+ return [2 /*return*/, item.data];
562
+ case 7:
563
+ if (options && options.callback !== undefined) {
564
+ val = options.callback();
565
+ if (val !== null) {
566
+ this.setItem(key, val, options);
567
+ }
568
+ return [2 /*return*/, val];
569
+ }
570
+ return [2 /*return*/, null];
571
+ case 8:
572
+ e_2 = _a.sent();
573
+ logger.warn("getItem failed! ".concat(e_2));
574
+ return [2 /*return*/, null];
575
+ case 9: return [2 /*return*/];
576
+ }
577
+ });
578
+ });
579
+ };
580
+ /**
581
+ * remove item from the cache
582
+ * The cache will abort output a warning:
583
+ * If error happened with AsyncStorage
584
+ * @param {String} key - the key of the item
585
+ * @return {Promise}
586
+ */
587
+ AsyncStorageCache.prototype.removeItem = function (key) {
588
+ return __awaiter(this, void 0, void 0, function () {
589
+ var prefixedKey, val, e_3;
590
+ return __generator(this, function (_a) {
591
+ switch (_a.label) {
592
+ case 0:
593
+ logger.debug("Remove item: key is ".concat(key));
594
+ prefixedKey = this.cacheConfig.keyPrefix + key;
595
+ if (prefixedKey === this.cacheConfig.keyPrefix ||
596
+ prefixedKey === getCurrSizeKey(this.cacheConfig.keyPrefix)) {
597
+ return [2 /*return*/];
598
+ }
599
+ _a.label = 1;
600
+ case 1:
601
+ _a.trys.push([1, 5, , 6]);
602
+ return [4 /*yield*/, AsyncStorage.getItem(prefixedKey)];
603
+ case 2:
604
+ val = _a.sent();
605
+ if (!val) return [3 /*break*/, 4];
606
+ return [4 /*yield*/, this._removeItem(prefixedKey, JSON.parse(val).byteSize)];
607
+ case 3:
608
+ _a.sent();
609
+ _a.label = 4;
610
+ case 4: return [3 /*break*/, 6];
611
+ case 5:
612
+ e_3 = _a.sent();
613
+ logger.warn("removeItem failed! ".concat(e_3));
614
+ return [3 /*break*/, 6];
615
+ case 6: return [2 /*return*/];
616
+ }
617
+ });
618
+ });
619
+ };
620
+ /**
621
+ * clear the entire cache
622
+ * The cache will abort output a warning:
623
+ * If error happened with AsyncStorage
624
+ * @return {Promise}
625
+ */
626
+ AsyncStorageCache.prototype.clear = function () {
627
+ return __awaiter(this, void 0, void 0, function () {
628
+ var keys, keysToRemove, i, i, e_4;
629
+ return __generator(this, function (_a) {
630
+ switch (_a.label) {
631
+ case 0:
632
+ logger.debug("Clear Cache");
633
+ _a.label = 1;
634
+ case 1:
635
+ _a.trys.push([1, 7, , 8]);
636
+ return [4 /*yield*/, AsyncStorage.getAllKeys()];
637
+ case 2:
638
+ keys = _a.sent();
639
+ keysToRemove = [];
640
+ for (i = 0; i < keys.length; i += 1) {
641
+ if (keys[i].indexOf(this.cacheConfig.keyPrefix) === 0) {
642
+ keysToRemove.push(keys[i]);
643
+ }
644
+ }
645
+ i = 0;
646
+ _a.label = 3;
647
+ case 3:
648
+ if (!(i < keysToRemove.length)) return [3 /*break*/, 6];
649
+ return [4 /*yield*/, AsyncStorage.removeItem(keysToRemove[i])];
650
+ case 4:
651
+ _a.sent();
652
+ _a.label = 5;
653
+ case 5:
654
+ i += 1;
655
+ return [3 /*break*/, 3];
656
+ case 6: return [3 /*break*/, 8];
657
+ case 7:
658
+ e_4 = _a.sent();
659
+ logger.warn("clear failed! ".concat(e_4));
660
+ return [3 /*break*/, 8];
661
+ case 8: return [2 /*return*/];
662
+ }
663
+ });
664
+ });
665
+ };
666
+ /**
667
+ * return the current size of the cache
668
+ * @return {Promise}
669
+ */
670
+ AsyncStorageCache.prototype.getCacheCurSize = function () {
671
+ return __awaiter(this, void 0, void 0, function () {
672
+ var ret;
673
+ return __generator(this, function (_a) {
674
+ switch (_a.label) {
675
+ case 0: return [4 /*yield*/, AsyncStorage.getItem(getCurrSizeKey(this.cacheConfig.keyPrefix))];
676
+ case 1:
677
+ ret = _a.sent();
678
+ if (!!ret) return [3 /*break*/, 3];
679
+ return [4 /*yield*/, AsyncStorage.setItem(getCurrSizeKey(this.cacheConfig.keyPrefix), '0')];
680
+ case 2:
681
+ _a.sent();
682
+ ret = '0';
683
+ _a.label = 3;
684
+ case 3: return [2 /*return*/, Number(ret)];
685
+ }
686
+ });
687
+ });
688
+ };
689
+ /**
690
+ * Return all the keys in the cache.
691
+ * Will return an empty array if error happend.
692
+ * @return {Promise}
693
+ */
694
+ AsyncStorageCache.prototype.getAllKeys = function () {
695
+ return __awaiter(this, void 0, void 0, function () {
696
+ var keys, retKeys, i, e_5;
697
+ return __generator(this, function (_a) {
698
+ switch (_a.label) {
699
+ case 0:
700
+ _a.trys.push([0, 2, , 3]);
701
+ return [4 /*yield*/, AsyncStorage.getAllKeys()];
702
+ case 1:
703
+ keys = _a.sent();
704
+ retKeys = [];
705
+ for (i = 0; i < keys.length; i += 1) {
706
+ if (keys[i].indexOf(this.cacheConfig.keyPrefix) === 0 &&
707
+ keys[i] !== getCurrSizeKey(this.cacheConfig.keyPrefix)) {
708
+ retKeys.push(keys[i].substring(this.cacheConfig.keyPrefix.length));
709
+ }
710
+ }
711
+ return [2 /*return*/, retKeys];
712
+ case 2:
713
+ e_5 = _a.sent();
714
+ logger.warn("getALlkeys failed! ".concat(e_5));
715
+ return [2 /*return*/, []];
716
+ case 3: return [2 /*return*/];
717
+ }
718
+ });
719
+ });
720
+ };
721
+ /**
722
+ * Return a new instance of cache with customized configuration.
723
+ * @param {Object} config - the customized configuration
724
+ * @return {Object} - the new instance of Cache
725
+ */
726
+ AsyncStorageCache.prototype.createInstance = function (config) {
727
+ if (config.keyPrefix === defaultConfig.keyPrefix) {
728
+ logger.error('invalid keyPrefix, setting keyPrefix with timeStamp');
729
+ config.keyPrefix = getCurrTime.toString();
730
+ }
731
+ return new AsyncStorageCache(config);
732
+ };
733
+ return AsyncStorageCache;
734
+ }(StorageCache));
735
+ export { AsyncStorageCache };
736
+ var instance = new AsyncStorageCache();
737
+ export { AsyncStorage, instance as Cache };