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