@aws-amplify/core 6.0.1-api-v6-models.8035dfc.0 → 6.0.1-api-v6-models.3eb7748.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/lib/Cache/StorageCache.d.ts +14 -31
- package/lib/Cache/StorageCache.js +97 -102
- package/lib/Cache/StorageCache.native.d.ts +19 -0
- package/lib/Cache/StorageCache.native.js +164 -0
- package/{lib-esm/Cache/AsyncStorageCache.d.ts → lib/Cache/StorageCacheCommon.d.ts} +124 -99
- package/lib/Cache/{AsyncStorageCache.js → StorageCacheCommon.js} +494 -432
- package/lib/Cache/constants.d.ts +6 -0
- package/lib/Cache/constants.js +17 -0
- package/lib/Cache/index.d.ts +2 -0
- package/lib/Cache/index.js +7 -0
- package/lib/Cache/types/cache.d.ts +38 -0
- package/lib/Cache/types/index.d.ts +1 -1
- package/lib/Cache/types/index.js +1 -1
- package/lib/Cache/{Utils → utils}/CacheList.d.ts +1 -1
- package/lib/Cache/{Utils → utils}/CacheList.js +2 -1
- package/lib/Cache/utils/cacheHelpers.d.ts +14 -0
- package/lib/Cache/utils/cacheHelpers.js +51 -0
- package/lib/Cache/{Utils → utils}/errorHelpers.d.ts +0 -1
- package/lib/Cache/{Utils → utils}/errorHelpers.js +0 -4
- package/lib/Cache/utils/index.d.ts +2 -0
- package/lib/Cache/utils/index.js +12 -0
- package/lib/I18n/I18n.js +1 -6
- package/lib/Platform/types.d.ts +18 -8
- package/lib/Platform/types.js +10 -2
- package/lib/Platform/version.d.ts +1 -1
- package/lib/Platform/version.js +1 -1
- package/lib/Reachability/Reachability.d.ts +1 -0
- package/lib/Reachability/Reachability.js +42 -1
- package/lib/ServiceWorker/ServiceWorker.js +69 -17
- package/lib/ServiceWorker/index.d.ts +0 -12
- package/lib/ServiceWorker/index.js +0 -12
- package/lib/Signer/Signer.js +3 -2
- package/lib/awsClients/cognitoIdentity/base.js +2 -1
- package/lib/awsClients/pinpoint/base.js +2 -1
- package/lib/awsClients/pinpoint/getInAppMessages.js +2 -1
- package/lib/awsClients/pinpoint/putEvents.js +2 -1
- package/lib/awsClients/pinpoint/updateEndpoint.js +2 -1
- package/lib/clients/middleware/signing/signer/signatureV4/index.d.ts +0 -1
- package/lib/clients/middleware/signing/signer/signatureV4/index.js +0 -1
- package/lib/clients/middleware/signing/signer/signatureV4/presignUrl.js +2 -1
- package/lib/errors/errorHelpers.d.ts +2 -0
- package/lib/errors/errorHelpers.js +20 -0
- package/lib/errors/index.d.ts +1 -0
- package/lib/errors/index.js +3 -1
- package/lib/index.d.ts +4 -4
- package/lib/index.js +9 -6
- package/lib/libraryUtils.d.ts +8 -7
- package/lib/libraryUtils.js +17 -17
- package/lib/parseAWSExports.js +34 -19
- package/lib/providers/pinpoint/apis/flushEvents.d.ts +2 -2
- package/lib/providers/pinpoint/apis/record.d.ts +1 -1
- package/lib/providers/pinpoint/apis/record.js +8 -24
- package/lib/providers/pinpoint/apis/updateEndpoint.js +6 -6
- package/lib/providers/pinpoint/index.d.ts +2 -2
- package/lib/providers/pinpoint/index.js +4 -2
- package/lib/providers/pinpoint/types/errors.d.ts +9 -0
- package/lib/providers/pinpoint/types/errors.js +15 -0
- package/lib/providers/pinpoint/types/index.d.ts +1 -0
- package/lib/providers/pinpoint/types/index.js +3 -0
- package/lib/providers/pinpoint/types/pinpoint.d.ts +3 -3
- package/lib/providers/pinpoint/utils/index.d.ts +1 -0
- package/lib/providers/pinpoint/utils/index.js +3 -1
- package/lib/providers/pinpoint/utils/resolveEndpointId.d.ts +8 -0
- package/lib/providers/pinpoint/utils/resolveEndpointId.js +85 -0
- package/lib/singleton/Amplify.d.ts +2 -2
- package/lib/singleton/Amplify.js +6 -5
- package/lib/singleton/Auth/types.d.ts +3 -3
- package/lib/singleton/Cache/types.d.ts +18 -0
- package/lib/singleton/Cache/types.js +4 -0
- package/lib/singleton/Interactions/types.d.ts +21 -0
- package/lib/singleton/Interactions/types.js +4 -0
- package/lib/singleton/Notifications/PushNotification/types.d.ts +2 -0
- package/lib/singleton/Notifications/PushNotification/types.js +4 -0
- package/lib/singleton/Notifications/types.d.ts +3 -1
- package/lib/singleton/types.d.ts +3 -3
- package/lib/storage/CookieStorage.js +8 -5
- package/lib/storage/DefaultStorage.js +1 -1
- package/lib/storage/utils.d.ts +1 -1
- package/lib/storage/utils.js +3 -3
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/types/errors.d.ts +1 -0
- package/lib/types/errors.js +1 -0
- package/lib/utils/amplifyUrl/index.d.ts +13 -0
- package/lib/utils/amplifyUrl/index.js +10 -0
- package/lib/utils/amplifyUrl/polyfill.d.ts +0 -0
- package/lib/utils/amplifyUrl/polyfill.js +4 -0
- package/lib/utils/amplifyUrl/polyfill.native.d.ts +1 -0
- package/lib/utils/amplifyUuid/index.d.ts +3 -0
- package/lib/utils/amplifyUuid/index.js +9 -0
- package/lib/utils/amplifyUuid/polyfill.d.ts +0 -0
- package/lib/utils/amplifyUuid/polyfill.js +4 -0
- package/lib/utils/amplifyUuid/polyfill.native.d.ts +1 -0
- package/lib/utils/amplifyUuid/polyfill.native.js +6 -0
- package/lib/utils/deepFreeze.d.ts +1 -0
- package/lib/utils/deepFreeze.js +38 -0
- package/lib/{ClientDevice/android.d.ts → utils/getClientInfo/getClientInfo.android.d.ts} +1 -1
- package/lib/{ClientDevice/android.js → utils/getClientInfo/getClientInfo.android.js} +4 -4
- package/lib/{ClientDevice/browser.d.ts → utils/getClientInfo/getClientInfo.d.ts} +1 -9
- package/lib/{ClientDevice/ios.d.ts → utils/getClientInfo/getClientInfo.ios.d.ts} +1 -1
- package/lib/{ClientDevice/ios.js → utils/getClientInfo/getClientInfo.ios.js} +4 -4
- package/lib/{ClientDevice/browser.js → utils/getClientInfo/getClientInfo.js} +6 -18
- package/lib/utils/getClientInfo/index.d.ts +1 -0
- package/lib/utils/getClientInfo/index.js +7 -0
- package/lib/utils/index.d.ts +2 -0
- package/lib/utils/index.js +5 -1
- package/lib/utils/sessionListener/SessionListener.d.ts +10 -0
- package/lib/utils/sessionListener/SessionListener.js +55 -0
- package/lib/utils/sessionListener/SessionListener.native.d.ts +10 -0
- package/lib/utils/sessionListener/SessionListener.native.js +54 -0
- package/lib/utils/sessionListener/index.d.ts +2 -0
- package/lib/utils/sessionListener/index.js +7 -0
- package/lib/utils/sessionListener/types.d.ts +6 -0
- package/lib/utils/sessionListener/types.js +4 -0
- package/lib-esm/Cache/StorageCache.d.ts +14 -31
- package/lib-esm/Cache/StorageCache.js +97 -102
- package/lib-esm/Cache/StorageCache.native.d.ts +19 -0
- package/lib-esm/Cache/StorageCache.native.js +161 -0
- package/{lib/Cache/AsyncStorageCache.d.ts → lib-esm/Cache/StorageCacheCommon.d.ts} +124 -99
- package/lib-esm/Cache/{AsyncStorageCache.js → StorageCacheCommon.js} +493 -430
- package/lib-esm/Cache/constants.d.ts +6 -0
- package/lib-esm/Cache/constants.js +14 -0
- package/lib-esm/Cache/index.d.ts +2 -0
- package/lib-esm/Cache/index.js +4 -0
- package/lib-esm/Cache/types/cache.d.ts +38 -0
- package/lib-esm/Cache/types/index.d.ts +1 -1
- package/lib-esm/Cache/types/index.js +1 -1
- package/lib-esm/Cache/{Utils → utils}/CacheList.d.ts +1 -1
- package/lib-esm/Cache/{Utils → utils}/CacheList.js +1 -1
- package/lib-esm/Cache/utils/cacheHelpers.d.ts +14 -0
- package/lib-esm/Cache/utils/cacheHelpers.js +44 -0
- package/lib-esm/Cache/{Utils → utils}/errorHelpers.d.ts +0 -1
- package/lib-esm/Cache/{Utils → utils}/errorHelpers.js +0 -4
- package/lib-esm/Cache/utils/index.d.ts +2 -0
- package/lib-esm/Cache/utils/index.js +4 -0
- package/lib-esm/I18n/I18n.js +1 -6
- package/lib-esm/Platform/types.d.ts +18 -8
- package/lib-esm/Platform/types.js +10 -2
- package/lib-esm/Platform/version.d.ts +1 -1
- package/lib-esm/Platform/version.js +1 -1
- package/lib-esm/Reachability/Reachability.d.ts +1 -0
- package/lib-esm/Reachability/Reachability.js +43 -2
- package/lib-esm/ServiceWorker/ServiceWorker.js +69 -17
- package/lib-esm/ServiceWorker/index.d.ts +0 -12
- package/lib-esm/ServiceWorker/index.js +0 -12
- package/lib-esm/Signer/Signer.js +3 -2
- package/lib-esm/awsClients/cognitoIdentity/base.js +2 -1
- package/lib-esm/awsClients/pinpoint/base.js +2 -1
- package/lib-esm/awsClients/pinpoint/getInAppMessages.js +2 -1
- package/lib-esm/awsClients/pinpoint/putEvents.js +2 -1
- package/lib-esm/awsClients/pinpoint/updateEndpoint.js +2 -1
- package/lib-esm/clients/middleware/signing/signer/signatureV4/index.d.ts +0 -1
- package/lib-esm/clients/middleware/signing/signer/signatureV4/index.js +0 -1
- package/lib-esm/clients/middleware/signing/signer/signatureV4/presignUrl.js +2 -1
- package/lib-esm/errors/errorHelpers.d.ts +2 -0
- package/lib-esm/errors/errorHelpers.js +17 -0
- package/lib-esm/errors/index.d.ts +1 -0
- package/lib-esm/errors/index.js +1 -0
- package/lib-esm/index.d.ts +4 -4
- package/lib-esm/index.js +5 -4
- package/lib-esm/libraryUtils.d.ts +8 -7
- package/lib-esm/libraryUtils.js +7 -7
- package/lib-esm/parseAWSExports.js +34 -19
- package/lib-esm/providers/pinpoint/apis/flushEvents.d.ts +2 -2
- package/lib-esm/providers/pinpoint/apis/record.d.ts +1 -1
- package/lib-esm/providers/pinpoint/apis/record.js +9 -25
- package/lib-esm/providers/pinpoint/apis/updateEndpoint.js +6 -6
- package/lib-esm/providers/pinpoint/index.d.ts +2 -2
- package/lib-esm/providers/pinpoint/index.js +2 -1
- package/lib-esm/providers/pinpoint/types/errors.d.ts +9 -0
- package/lib-esm/providers/pinpoint/types/errors.js +12 -0
- package/lib-esm/providers/pinpoint/types/index.d.ts +1 -0
- package/lib-esm/providers/pinpoint/types/index.js +1 -0
- package/lib-esm/providers/pinpoint/types/pinpoint.d.ts +3 -3
- package/lib-esm/providers/pinpoint/utils/index.d.ts +1 -0
- package/lib-esm/providers/pinpoint/utils/index.js +1 -0
- package/lib-esm/providers/pinpoint/utils/resolveEndpointId.d.ts +8 -0
- package/lib-esm/providers/pinpoint/utils/resolveEndpointId.js +81 -0
- package/lib-esm/singleton/Amplify.d.ts +2 -2
- package/lib-esm/singleton/Amplify.js +6 -5
- package/lib-esm/singleton/Auth/types.d.ts +3 -3
- package/lib-esm/singleton/Cache/types.d.ts +18 -0
- package/lib-esm/singleton/Cache/types.js +3 -0
- package/lib-esm/singleton/Interactions/types.d.ts +21 -0
- package/lib-esm/singleton/Interactions/types.js +3 -0
- package/lib-esm/singleton/Notifications/PushNotification/types.d.ts +2 -0
- package/lib-esm/singleton/Notifications/PushNotification/types.js +3 -0
- package/lib-esm/singleton/Notifications/types.d.ts +3 -1
- package/lib-esm/singleton/types.d.ts +3 -3
- package/lib-esm/storage/CookieStorage.js +5 -5
- package/lib-esm/storage/DefaultStorage.js +2 -2
- package/lib-esm/storage/utils.d.ts +1 -1
- package/lib-esm/storage/utils.js +1 -1
- package/lib-esm/tsconfig.tsbuildinfo +1 -1
- package/lib-esm/types/errors.d.ts +1 -0
- package/lib-esm/types/errors.js +1 -0
- package/lib-esm/utils/amplifyUrl/index.d.ts +13 -0
- package/lib-esm/utils/amplifyUrl/index.js +6 -0
- package/lib-esm/utils/amplifyUuid/index.d.ts +3 -0
- package/lib-esm/utils/amplifyUuid/index.js +6 -0
- package/lib-esm/utils/amplifyUuid/polyfill.native.js +4 -0
- package/lib-esm/utils/deepFreeze.d.ts +1 -0
- package/lib-esm/utils/deepFreeze.js +34 -0
- package/lib-esm/{ClientDevice/android.d.ts → utils/getClientInfo/getClientInfo.android.d.ts} +1 -1
- package/lib-esm/{ClientDevice/android.js → utils/getClientInfo/getClientInfo.android.js} +2 -2
- package/lib-esm/{ClientDevice/browser.d.ts → utils/getClientInfo/getClientInfo.d.ts} +1 -9
- package/lib-esm/{ClientDevice/ios.d.ts → utils/getClientInfo/getClientInfo.ios.d.ts} +1 -1
- package/lib-esm/{ClientDevice/ios.js → utils/getClientInfo/getClientInfo.ios.js} +2 -2
- package/lib-esm/{ClientDevice/browser.js → utils/getClientInfo/getClientInfo.js} +4 -14
- package/lib-esm/utils/getClientInfo/index.d.ts +1 -0
- package/lib-esm/utils/getClientInfo/index.js +3 -0
- package/lib-esm/utils/index.d.ts +2 -0
- package/lib-esm/utils/index.js +2 -0
- package/lib-esm/utils/sessionListener/SessionListener.d.ts +10 -0
- package/lib-esm/utils/sessionListener/SessionListener.js +52 -0
- package/lib-esm/utils/sessionListener/SessionListener.native.d.ts +10 -0
- package/lib-esm/utils/sessionListener/SessionListener.native.js +51 -0
- package/lib-esm/utils/sessionListener/index.d.ts +2 -0
- package/lib-esm/utils/sessionListener/index.js +4 -0
- package/lib-esm/utils/sessionListener/types.d.ts +6 -0
- package/lib-esm/utils/sessionListener/types.js +3 -0
- package/package.json +9 -19
- package/src/Cache/StorageCache.native.ts +65 -0
- package/src/Cache/StorageCache.ts +61 -158
- package/src/Cache/StorageCacheCommon.ts +577 -0
- package/src/Cache/constants.ts +18 -0
- package/src/Cache/index.ts +6 -0
- package/src/Cache/types/cache.ts +51 -0
- package/src/Cache/types/index.ts +4 -4
- package/src/Cache/{Utils → utils}/CacheList.ts +185 -185
- package/src/Cache/utils/cacheHelpers.ts +52 -0
- package/src/Cache/{Utils → utils}/errorHelpers.ts +0 -4
- package/src/Cache/{Utils → utils}/index.ts +4 -5
- package/src/I18n/I18n.ts +1 -6
- package/src/Platform/types.ts +20 -11
- package/src/Platform/version.ts +1 -1
- package/src/Reachability/Reachability.ts +22 -2
- package/src/ServiceWorker/ServiceWorker.ts +20 -17
- package/src/ServiceWorker/index.ts +0 -12
- package/src/Signer/Signer.ts +3 -2
- package/src/awsClients/cognitoIdentity/base.ts +4 -1
- package/src/awsClients/pinpoint/base.ts +2 -1
- package/src/awsClients/pinpoint/getInAppMessages.ts +2 -1
- package/src/awsClients/pinpoint/putEvents.ts +2 -1
- package/src/awsClients/pinpoint/updateEndpoint.ts +2 -1
- package/src/clients/middleware/signing/signer/signatureV4/index.ts +0 -2
- package/src/clients/middleware/signing/signer/signatureV4/presignUrl.ts +2 -1
- package/src/errors/errorHelpers.ts +20 -0
- package/src/errors/index.ts +1 -0
- package/src/index.ts +8 -4
- package/src/libraryUtils.ts +17 -11
- package/src/parseAWSExports.ts +28 -9
- package/src/providers/pinpoint/apis/flushEvents.ts +2 -2
- package/src/providers/pinpoint/apis/record.ts +14 -28
- package/src/providers/pinpoint/apis/updateEndpoint.ts +6 -6
- package/src/providers/pinpoint/index.ts +6 -2
- package/src/providers/pinpoint/types/errors.ts +12 -0
- package/src/providers/pinpoint/types/index.ts +1 -0
- package/src/providers/pinpoint/types/pinpoint.ts +3 -2
- package/src/providers/pinpoint/utils/index.ts +1 -0
- package/src/providers/pinpoint/utils/resolveEndpointId.ts +46 -0
- package/src/singleton/Amplify.ts +10 -8
- package/src/singleton/Auth/types.ts +3 -3
- package/src/singleton/Cache/types.ts +27 -0
- package/src/singleton/Interactions/types.ts +29 -0
- package/src/singleton/Notifications/PushNotification/types.ts +6 -0
- package/src/singleton/Notifications/types.ts +3 -1
- package/src/singleton/types.ts +3 -5
- package/src/storage/CookieStorage.ts +5 -9
- package/src/storage/DefaultStorage.ts +2 -2
- package/src/storage/utils.ts +1 -1
- package/src/types/errors.ts +1 -0
- package/src/utils/amplifyUrl/index.ts +9 -0
- package/src/{clients/middleware/signing/signer/signatureV4/polyfills/index.native.ts → utils/amplifyUrl/polyfill.native.ts} +1 -0
- package/src/utils/amplifyUuid/index.ts +9 -0
- package/src/utils/amplifyUuid/polyfill.native.ts +6 -0
- package/src/utils/amplifyUuid/polyfill.ts +4 -0
- package/src/utils/deepFreeze.ts +16 -0
- package/src/{ClientDevice/android.ts → utils/getClientInfo/getClientInfo.android.ts} +2 -2
- package/src/{ClientDevice/ios.ts → utils/getClientInfo/getClientInfo.ios.ts} +3 -3
- package/src/{ClientDevice/browser.ts → utils/getClientInfo/getClientInfo.ts} +4 -16
- package/src/utils/getClientInfo/index.ts +4 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/sessionListener/SessionListener.native.ts +70 -0
- package/src/utils/sessionListener/SessionListener.ts +74 -0
- package/src/utils/sessionListener/index.ts +6 -0
- package/src/utils/sessionListener/types.ts +11 -0
- package/lib/Amplify.d.ts +0 -29
- package/lib/Amplify.js +0 -118
- package/lib/Cache/BrowserStorageCache.d.ts +0 -166
- package/lib/Cache/BrowserStorageCache.js +0 -456
- package/lib/Cache/InMemoryCache.d.ts +0 -129
- package/lib/Cache/InMemoryCache.js +0 -342
- package/lib/Cache/Utils/CacheUtils.d.ts +0 -25
- package/lib/Cache/Utils/CacheUtils.js +0 -86
- package/lib/Cache/Utils/index.d.ts +0 -2
- package/lib/Cache/Utils/index.js +0 -16
- package/lib/Cache/reactnative.d.ts +0 -3
- package/lib/Cache/reactnative.js +0 -8
- package/lib/Cache/types/Cache.d.ts +0 -55
- package/lib/ClientDevice/index.d.ts +0 -23
- package/lib/ClientDevice/index.js +0 -18
- package/lib/ClientDevice/reactnative.d.ts +0 -7
- package/lib/ClientDevice/reactnative.js +0 -24
- package/lib-esm/Amplify.d.ts +0 -29
- package/lib-esm/Amplify.js +0 -115
- package/lib-esm/Cache/BrowserStorageCache.d.ts +0 -166
- package/lib-esm/Cache/BrowserStorageCache.js +0 -453
- package/lib-esm/Cache/InMemoryCache.d.ts +0 -129
- package/lib-esm/Cache/InMemoryCache.js +0 -339
- package/lib-esm/Cache/Utils/CacheUtils.d.ts +0 -25
- package/lib-esm/Cache/Utils/CacheUtils.js +0 -79
- package/lib-esm/Cache/Utils/index.d.ts +0 -2
- package/lib-esm/Cache/Utils/index.js +0 -4
- package/lib-esm/Cache/reactnative.d.ts +0 -3
- package/lib-esm/Cache/reactnative.js +0 -6
- package/lib-esm/Cache/types/Cache.d.ts +0 -55
- package/lib-esm/ClientDevice/index.d.ts +0 -23
- package/lib-esm/ClientDevice/index.js +0 -15
- package/lib-esm/ClientDevice/reactnative.d.ts +0 -7
- package/lib-esm/ClientDevice/reactnative.js +0 -21
- package/src/Amplify.ts +0 -106
- package/src/Cache/AsyncStorageCache.ts +0 -495
- package/src/Cache/BrowserStorageCache.ts +0 -504
- package/src/Cache/CHANGELOG.md +0 -1239
- package/src/Cache/InMemoryCache.ts +0 -347
- package/src/Cache/Utils/CacheUtils.ts +0 -90
- package/src/Cache/reactnative.ts +0 -9
- package/src/Cache/types/Cache.ts +0 -75
- package/src/ClientDevice/index.ts +0 -14
- package/src/ClientDevice/reactnative.ts +0 -18
- package/lib/Cache/types/{Cache.js → cache.js} +0 -0
- package/lib/{clients/middleware/signing/signer/signatureV4/polyfills/index.native.js → utils/amplifyUrl/polyfill.native.js} +1 -1
- /package/lib-esm/Cache/types/{Cache.js → cache.js} +0 -0
- /package/lib-esm/{clients/middleware/signing/signer/signatureV4/polyfills/index.d.ts → utils/amplifyUrl/polyfill.d.ts} +0 -0
- /package/lib-esm/{clients/middleware/signing/signer/signatureV4/polyfills/index.js → utils/amplifyUrl/polyfill.js} +0 -0
- /package/lib-esm/{clients/middleware/signing/signer/signatureV4/polyfills/index.native.d.ts → utils/amplifyUrl/polyfill.native.d.ts} +0 -0
- /package/lib-esm/{clients/middleware/signing/signer/signatureV4/polyfills/index.native.js → utils/amplifyUrl/polyfill.native.js} +0 -0
- /package/{lib/clients/middleware/signing/signer/signatureV4/polyfills/index.d.ts → lib-esm/utils/amplifyUuid/polyfill.d.ts} +0 -0
- /package/{lib/clients/middleware/signing/signer/signatureV4/polyfills/index.js → lib-esm/utils/amplifyUuid/polyfill.js} +0 -0
- /package/{lib/clients/middleware/signing/signer/signatureV4/polyfills/index.native.d.ts → lib-esm/utils/amplifyUuid/polyfill.native.d.ts} +0 -0
- /package/src/{clients/middleware/signing/signer/signatureV4/polyfills/index.ts → utils/amplifyUrl/polyfill.ts} +0 -0
|
@@ -1,20 +1,16 @@
|
|
|
1
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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 __());
|
|
3
|
+
var __assign = (this && this.__assign) || function () {
|
|
4
|
+
__assign = Object.assign || function(t) {
|
|
5
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
6
|
+
s = arguments[i];
|
|
7
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
8
|
+
t[p] = s[p];
|
|
9
|
+
}
|
|
10
|
+
return t;
|
|
16
11
|
};
|
|
17
|
-
|
|
12
|
+
return __assign.apply(this, arguments);
|
|
13
|
+
};
|
|
18
14
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
19
15
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
20
16
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -51,114 +47,323 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
51
47
|
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
52
48
|
}
|
|
53
49
|
};
|
|
54
|
-
|
|
50
|
+
var __values = (this && this.__values) || function(o) {
|
|
51
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
52
|
+
if (m) return m.call(o);
|
|
53
|
+
if (o && typeof o.length === "number") return {
|
|
54
|
+
next: function () {
|
|
55
|
+
if (o && i >= o.length) o = void 0;
|
|
56
|
+
return { value: o && o[i++], done: !o };
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
60
|
+
};
|
|
55
61
|
import { ConsoleLogger as Logger } from '../Logger';
|
|
56
|
-
import {
|
|
57
|
-
import {
|
|
58
|
-
import {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
* Customized cache which based on the AsyncStorage with LRU implemented
|
|
62
|
+
import { currentSizeKey, defaultConfig } from './constants';
|
|
63
|
+
import { getCurrentSizeKey, getCurrentTime, getByteLength } from './utils';
|
|
64
|
+
import { assert, CacheErrorCode } from './utils/errorHelpers';
|
|
65
|
+
var logger = new Logger('StorageCache');
|
|
66
|
+
/**
|
|
67
|
+
* Initialization of the cache
|
|
68
|
+
*
|
|
64
69
|
*/
|
|
65
|
-
var
|
|
66
|
-
__extends(AsyncStorageCache, _super);
|
|
70
|
+
var StorageCacheCommon = /** @class */ (function () {
|
|
67
71
|
/**
|
|
68
|
-
*
|
|
72
|
+
* Initialize the cache
|
|
69
73
|
*
|
|
70
|
-
* @param
|
|
74
|
+
* @param config - Custom configuration for this instance.
|
|
71
75
|
*/
|
|
72
|
-
function
|
|
73
|
-
var
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
logger.debug('Using AsyncStorageCache');
|
|
78
|
-
return _this;
|
|
76
|
+
function StorageCacheCommon(_a) {
|
|
77
|
+
var config = _a.config, keyValueStorage = _a.keyValueStorage;
|
|
78
|
+
this.config = __assign(__assign({}, defaultConfig), config);
|
|
79
|
+
this.keyValueStorage = keyValueStorage;
|
|
80
|
+
this.sanitizeConfig();
|
|
79
81
|
}
|
|
82
|
+
StorageCacheCommon.prototype.getModuleName = function () {
|
|
83
|
+
return 'Cache';
|
|
84
|
+
};
|
|
80
85
|
/**
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
* @param
|
|
86
|
+
* Set custom configuration for the cache instance.
|
|
87
|
+
*
|
|
88
|
+
* @param config - customized configuration (without keyPrefix, which can't be changed)
|
|
89
|
+
*
|
|
90
|
+
* @return - the current configuration
|
|
84
91
|
*/
|
|
85
|
-
|
|
92
|
+
StorageCacheCommon.prototype.configure = function (config) {
|
|
93
|
+
if (config) {
|
|
94
|
+
if (config.keyPrefix) {
|
|
95
|
+
logger.warn('keyPrefix can not be re-configured on an existing Cache instance.');
|
|
96
|
+
}
|
|
97
|
+
this.config = __assign(__assign({}, this.config), config);
|
|
98
|
+
}
|
|
99
|
+
this.sanitizeConfig();
|
|
100
|
+
return this.config;
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* return the current size of the cache
|
|
104
|
+
* @return {Promise}
|
|
105
|
+
*/
|
|
106
|
+
StorageCacheCommon.prototype.getCurrentCacheSize = function () {
|
|
86
107
|
return __awaiter(this, void 0, void 0, function () {
|
|
87
|
-
var
|
|
108
|
+
var size;
|
|
88
109
|
return __generator(this, function (_a) {
|
|
89
110
|
switch (_a.label) {
|
|
90
|
-
case 0: return [4 /*yield*/, this.
|
|
111
|
+
case 0: return [4 /*yield*/, this.getStorage().getItem(getCurrentSizeKey(this.config.keyPrefix))];
|
|
91
112
|
case 1:
|
|
92
|
-
|
|
93
|
-
return [
|
|
113
|
+
size = _a.sent();
|
|
114
|
+
if (!!size) return [3 /*break*/, 3];
|
|
115
|
+
return [4 /*yield*/, this.getStorage().setItem(getCurrentSizeKey(this.config.keyPrefix), '0')];
|
|
94
116
|
case 2:
|
|
95
117
|
_a.sent();
|
|
96
|
-
|
|
118
|
+
size = '0';
|
|
119
|
+
_a.label = 3;
|
|
120
|
+
case 3: return [2 /*return*/, Number(size)];
|
|
97
121
|
}
|
|
98
122
|
});
|
|
99
123
|
});
|
|
100
124
|
};
|
|
101
125
|
/**
|
|
102
|
-
*
|
|
103
|
-
*
|
|
104
|
-
*
|
|
126
|
+
* Set item into cache. You can put number, string, boolean or object.
|
|
127
|
+
* The cache will first check whether has the same key.
|
|
128
|
+
* If it has, it will delete the old item and then put the new item in
|
|
129
|
+
* The cache will pop out items if it is full
|
|
130
|
+
* You can specify the cache item options. The cache will abort and output a warning:
|
|
131
|
+
* If the key is invalid
|
|
132
|
+
* If the size of the item exceeds itemMaxSize.
|
|
133
|
+
* If the value is undefined
|
|
134
|
+
* If incorrect cache item configuration
|
|
135
|
+
* If error happened with browser storage
|
|
136
|
+
*
|
|
137
|
+
* @param {String} key - the key of the item
|
|
138
|
+
* @param {Object} value - the value of the item
|
|
139
|
+
* @param {Object} [options] - optional, the specified meta-data
|
|
140
|
+
*
|
|
141
|
+
* @return {Promise}
|
|
105
142
|
*/
|
|
106
|
-
|
|
143
|
+
StorageCacheCommon.prototype.setItem = function (key, value, options) {
|
|
107
144
|
return __awaiter(this, void 0, void 0, function () {
|
|
108
|
-
var
|
|
145
|
+
var cacheItemOptions, prefixedKey, item, val, validKeys, sizeToPop, e_1;
|
|
109
146
|
return __generator(this, function (_a) {
|
|
110
147
|
switch (_a.label) {
|
|
111
|
-
case 0:
|
|
148
|
+
case 0:
|
|
149
|
+
logger.debug("Set item: key is ".concat(key, ", value is ").concat(value, " with options: ").concat(options));
|
|
150
|
+
if (!key || key === currentSizeKey) {
|
|
151
|
+
logger.warn("Invalid key: should not be empty or reserved key: '".concat(currentSizeKey, "'"));
|
|
152
|
+
return [2 /*return*/];
|
|
153
|
+
}
|
|
154
|
+
if (typeof value === 'undefined') {
|
|
155
|
+
logger.warn("The value of item should not be undefined!");
|
|
156
|
+
return [2 /*return*/];
|
|
157
|
+
}
|
|
158
|
+
cacheItemOptions = {
|
|
159
|
+
priority: (options === null || options === void 0 ? void 0 : options.priority) !== undefined
|
|
160
|
+
? options.priority
|
|
161
|
+
: this.config.defaultPriority,
|
|
162
|
+
expires: (options === null || options === void 0 ? void 0 : options.expires) !== undefined
|
|
163
|
+
? options.expires
|
|
164
|
+
: this.config.defaultTTL + getCurrentTime(),
|
|
165
|
+
};
|
|
166
|
+
if (cacheItemOptions.priority < 1 || cacheItemOptions.priority > 5) {
|
|
167
|
+
logger.warn("Invalid parameter: priority due to out or range. It should be within 1 and 5.");
|
|
168
|
+
return [2 /*return*/];
|
|
169
|
+
}
|
|
170
|
+
prefixedKey = "".concat(this.config.keyPrefix).concat(key);
|
|
171
|
+
item = this.fillCacheItem(prefixedKey, value, cacheItemOptions);
|
|
172
|
+
// check whether this item is too big;
|
|
173
|
+
if (item.byteSize > this.config.itemMaxSize) {
|
|
174
|
+
logger.warn("Item with key: ".concat(key, " you are trying to put into is too big!"));
|
|
175
|
+
return [2 /*return*/];
|
|
176
|
+
}
|
|
177
|
+
_a.label = 1;
|
|
112
178
|
case 1:
|
|
113
|
-
|
|
114
|
-
return [4 /*yield*/,
|
|
179
|
+
_a.trys.push([1, 11, , 12]);
|
|
180
|
+
return [4 /*yield*/, this.getStorage().getItem(prefixedKey)];
|
|
115
181
|
case 2:
|
|
182
|
+
val = _a.sent();
|
|
183
|
+
if (!val) return [3 /*break*/, 4];
|
|
184
|
+
return [4 /*yield*/, this.removeCacheItem(prefixedKey, JSON.parse(val).byteSize)];
|
|
185
|
+
case 3:
|
|
116
186
|
_a.sent();
|
|
117
|
-
|
|
187
|
+
_a.label = 4;
|
|
188
|
+
case 4: return [4 /*yield*/, this.isCacheFull(item.byteSize)];
|
|
189
|
+
case 5:
|
|
190
|
+
if (!_a.sent()) return [3 /*break*/, 10];
|
|
191
|
+
return [4 /*yield*/, this.clearInvalidAndGetRemainingKeys()];
|
|
192
|
+
case 6:
|
|
193
|
+
validKeys = _a.sent();
|
|
194
|
+
return [4 /*yield*/, this.isCacheFull(item.byteSize)];
|
|
195
|
+
case 7:
|
|
196
|
+
if (!_a.sent()) return [3 /*break*/, 10];
|
|
197
|
+
return [4 /*yield*/, this.sizeToPop(item.byteSize)];
|
|
198
|
+
case 8:
|
|
199
|
+
sizeToPop = _a.sent();
|
|
200
|
+
return [4 /*yield*/, this.popOutItems(validKeys, sizeToPop)];
|
|
201
|
+
case 9:
|
|
202
|
+
_a.sent();
|
|
203
|
+
_a.label = 10;
|
|
204
|
+
case 10:
|
|
205
|
+
// put item in the cache
|
|
206
|
+
return [2 /*return*/, this.setCacheItem(prefixedKey, item)];
|
|
207
|
+
case 11:
|
|
208
|
+
e_1 = _a.sent();
|
|
209
|
+
logger.warn("setItem failed! ".concat(e_1));
|
|
210
|
+
return [3 /*break*/, 12];
|
|
211
|
+
case 12: return [2 /*return*/];
|
|
118
212
|
}
|
|
119
213
|
});
|
|
120
214
|
});
|
|
121
215
|
};
|
|
122
216
|
/**
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
*
|
|
217
|
+
* Get item from cache. It will return null if item doesn’t exist or it has been expired.
|
|
218
|
+
* If you specified callback function in the options,
|
|
219
|
+
* then the function will be executed if no such item in the cache
|
|
220
|
+
* and finally put the return value into cache.
|
|
221
|
+
* Please make sure the callback function will return the value you want to put into the cache.
|
|
222
|
+
* The cache will abort output a warning:
|
|
223
|
+
* If the key is invalid
|
|
224
|
+
* If error happened with AsyncStorage
|
|
225
|
+
*
|
|
226
|
+
* @param {String} key - the key of the item
|
|
227
|
+
* @param {Object} [options] - the options of callback function
|
|
127
228
|
*
|
|
128
|
-
* @return the
|
|
229
|
+
* @return {Promise} - return a promise resolves to be the value of the item
|
|
129
230
|
*/
|
|
130
|
-
|
|
231
|
+
StorageCacheCommon.prototype.getItem = function (key, options) {
|
|
131
232
|
return __awaiter(this, void 0, void 0, function () {
|
|
233
|
+
var cached, prefixedKey, item, val, e_2;
|
|
132
234
|
return __generator(this, function (_a) {
|
|
133
235
|
switch (_a.label) {
|
|
134
236
|
case 0:
|
|
135
|
-
item.
|
|
136
|
-
|
|
237
|
+
logger.debug("Get item: key is ".concat(key, " with options ").concat(options));
|
|
238
|
+
if (!key || key === currentSizeKey) {
|
|
239
|
+
logger.warn("Invalid key: should not be empty or reserved key: '".concat(currentSizeKey, "'"));
|
|
240
|
+
return [2 /*return*/, null];
|
|
241
|
+
}
|
|
242
|
+
prefixedKey = "".concat(this.config.keyPrefix).concat(key);
|
|
243
|
+
_a.label = 1;
|
|
137
244
|
case 1:
|
|
245
|
+
_a.trys.push([1, 11, , 12]);
|
|
246
|
+
return [4 /*yield*/, this.getStorage().getItem(prefixedKey)];
|
|
247
|
+
case 2:
|
|
248
|
+
cached = _a.sent();
|
|
249
|
+
if (!(cached != null)) return [3 /*break*/, 7];
|
|
250
|
+
return [4 /*yield*/, this.isExpired(prefixedKey)];
|
|
251
|
+
case 3:
|
|
252
|
+
if (!_a.sent()) return [3 /*break*/, 5];
|
|
253
|
+
// if expired, remove that item and return null
|
|
254
|
+
return [4 /*yield*/, this.removeCacheItem(prefixedKey, JSON.parse(cached).byteSize)];
|
|
255
|
+
case 4:
|
|
256
|
+
// if expired, remove that item and return null
|
|
138
257
|
_a.sent();
|
|
139
|
-
return [
|
|
258
|
+
return [3 /*break*/, 7];
|
|
259
|
+
case 5: return [4 /*yield*/, this.updateVisitedTime(JSON.parse(cached), prefixedKey)];
|
|
260
|
+
case 6:
|
|
261
|
+
item = _a.sent();
|
|
262
|
+
return [2 /*return*/, item.data];
|
|
263
|
+
case 7:
|
|
264
|
+
if (!(options === null || options === void 0 ? void 0 : options.callback)) return [3 /*break*/, 10];
|
|
265
|
+
val = options.callback();
|
|
266
|
+
if (!(val !== null)) return [3 /*break*/, 9];
|
|
267
|
+
return [4 /*yield*/, this.setItem(key, val, options)];
|
|
268
|
+
case 8:
|
|
269
|
+
_a.sent();
|
|
270
|
+
_a.label = 9;
|
|
271
|
+
case 9: return [2 /*return*/, val];
|
|
272
|
+
case 10: return [2 /*return*/, null];
|
|
273
|
+
case 11:
|
|
274
|
+
e_2 = _a.sent();
|
|
275
|
+
logger.warn("getItem failed! ".concat(e_2));
|
|
276
|
+
return [2 /*return*/, null];
|
|
277
|
+
case 12: return [2 /*return*/];
|
|
140
278
|
}
|
|
141
279
|
});
|
|
142
280
|
});
|
|
143
281
|
};
|
|
144
282
|
/**
|
|
145
|
-
*
|
|
146
|
-
*
|
|
283
|
+
* remove item from the cache
|
|
284
|
+
* The cache will abort output a warning:
|
|
285
|
+
* If error happened with AsyncStorage
|
|
286
|
+
* @param {String} key - the key of the item
|
|
287
|
+
* @return {Promise}
|
|
288
|
+
*/
|
|
289
|
+
StorageCacheCommon.prototype.removeItem = function (key) {
|
|
290
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
291
|
+
var prefixedKey, val, e_3;
|
|
292
|
+
return __generator(this, function (_a) {
|
|
293
|
+
switch (_a.label) {
|
|
294
|
+
case 0:
|
|
295
|
+
logger.debug("Remove item: key is ".concat(key));
|
|
296
|
+
if (!key || key === currentSizeKey) {
|
|
297
|
+
logger.warn("Invalid key: should not be empty or reserved key: '".concat(currentSizeKey, "'"));
|
|
298
|
+
return [2 /*return*/];
|
|
299
|
+
}
|
|
300
|
+
prefixedKey = "".concat(this.config.keyPrefix).concat(key);
|
|
301
|
+
_a.label = 1;
|
|
302
|
+
case 1:
|
|
303
|
+
_a.trys.push([1, 5, , 6]);
|
|
304
|
+
return [4 /*yield*/, this.getStorage().getItem(prefixedKey)];
|
|
305
|
+
case 2:
|
|
306
|
+
val = _a.sent();
|
|
307
|
+
if (!val) return [3 /*break*/, 4];
|
|
308
|
+
return [4 /*yield*/, this.removeCacheItem(prefixedKey, JSON.parse(val).byteSize)];
|
|
309
|
+
case 3:
|
|
310
|
+
_a.sent();
|
|
311
|
+
_a.label = 4;
|
|
312
|
+
case 4: return [3 /*break*/, 6];
|
|
313
|
+
case 5:
|
|
314
|
+
e_3 = _a.sent();
|
|
315
|
+
logger.warn("removeItem failed! ".concat(e_3));
|
|
316
|
+
return [3 /*break*/, 6];
|
|
317
|
+
case 6: return [2 /*return*/];
|
|
318
|
+
}
|
|
319
|
+
});
|
|
320
|
+
});
|
|
321
|
+
};
|
|
322
|
+
/**
|
|
323
|
+
* Return all the keys owned by this cache.
|
|
324
|
+
* Will return an empty array if error occurred.
|
|
325
|
+
*
|
|
326
|
+
* @return {Promise}
|
|
327
|
+
*/
|
|
328
|
+
StorageCacheCommon.prototype.getAllKeys = function () {
|
|
329
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
330
|
+
var e_4;
|
|
331
|
+
return __generator(this, function (_a) {
|
|
332
|
+
switch (_a.label) {
|
|
333
|
+
case 0:
|
|
334
|
+
_a.trys.push([0, 2, , 3]);
|
|
335
|
+
return [4 /*yield*/, this.getAllCacheKeys()];
|
|
336
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
337
|
+
case 2:
|
|
338
|
+
e_4 = _a.sent();
|
|
339
|
+
logger.warn("getAllkeys failed! ".concat(e_4));
|
|
340
|
+
return [2 /*return*/, []];
|
|
341
|
+
case 3: return [2 /*return*/];
|
|
342
|
+
}
|
|
343
|
+
});
|
|
344
|
+
});
|
|
345
|
+
};
|
|
346
|
+
StorageCacheCommon.prototype.getStorage = function () {
|
|
347
|
+
return this.keyValueStorage;
|
|
348
|
+
};
|
|
349
|
+
/**
|
|
350
|
+
* check whether item is expired
|
|
351
|
+
*
|
|
147
352
|
* @param key - the key of the item
|
|
148
353
|
*
|
|
149
354
|
* @return true if the item is expired.
|
|
150
355
|
*/
|
|
151
|
-
|
|
356
|
+
StorageCacheCommon.prototype.isExpired = function (key) {
|
|
152
357
|
return __awaiter(this, void 0, void 0, function () {
|
|
153
358
|
var text, item;
|
|
154
359
|
return __generator(this, function (_a) {
|
|
155
360
|
switch (_a.label) {
|
|
156
|
-
case 0: return [4 /*yield*/,
|
|
361
|
+
case 0: return [4 /*yield*/, this.getStorage().getItem(key)];
|
|
157
362
|
case 1:
|
|
158
363
|
text = _a.sent();
|
|
159
364
|
assert(text !== null, CacheErrorCode.NoCacheItem, "Key: ".concat(key));
|
|
160
365
|
item = JSON.parse(text);
|
|
161
|
-
if (
|
|
366
|
+
if (getCurrentTime() >= item.expires) {
|
|
162
367
|
return [2 /*return*/, true];
|
|
163
368
|
}
|
|
164
369
|
return [2 /*return*/, false];
|
|
@@ -168,36 +373,36 @@ var AsyncStorageCache = /** @class */ (function (_super) {
|
|
|
168
373
|
};
|
|
169
374
|
/**
|
|
170
375
|
* delete item from cache
|
|
171
|
-
*
|
|
376
|
+
*
|
|
172
377
|
* @param prefixedKey - the key of the item
|
|
173
378
|
* @param size - optional, the byte size of the item
|
|
174
379
|
*/
|
|
175
|
-
|
|
380
|
+
StorageCacheCommon.prototype.removeCacheItem = function (prefixedKey, size) {
|
|
176
381
|
return __awaiter(this, void 0, void 0, function () {
|
|
177
|
-
var
|
|
382
|
+
var item, itemSize, removeItemError_1;
|
|
178
383
|
return __generator(this, function (_a) {
|
|
179
384
|
switch (_a.label) {
|
|
180
|
-
case 0: return [4 /*yield*/,
|
|
385
|
+
case 0: return [4 /*yield*/, this.getStorage().getItem(prefixedKey)];
|
|
181
386
|
case 1:
|
|
182
|
-
|
|
183
|
-
assert(
|
|
184
|
-
itemSize = size !== null && size !== void 0 ? size : JSON.parse(
|
|
387
|
+
item = _a.sent();
|
|
388
|
+
assert(item !== null, CacheErrorCode.NoCacheItem, "Key: ".concat(prefixedKey));
|
|
389
|
+
itemSize = size !== null && size !== void 0 ? size : JSON.parse(item).byteSize;
|
|
185
390
|
// first try to update the current size of the cache
|
|
186
|
-
return [4 /*yield*/, this.
|
|
391
|
+
return [4 /*yield*/, this.decreaseCurrentSizeInBytes(itemSize)];
|
|
187
392
|
case 2:
|
|
188
393
|
// first try to update the current size of the cache
|
|
189
394
|
_a.sent();
|
|
190
395
|
_a.label = 3;
|
|
191
396
|
case 3:
|
|
192
397
|
_a.trys.push([3, 5, , 7]);
|
|
193
|
-
return [4 /*yield*/,
|
|
398
|
+
return [4 /*yield*/, this.getStorage().removeItem(prefixedKey)];
|
|
194
399
|
case 4:
|
|
195
400
|
_a.sent();
|
|
196
401
|
return [3 /*break*/, 7];
|
|
197
402
|
case 5:
|
|
198
403
|
removeItemError_1 = _a.sent();
|
|
199
404
|
// if some error happened, we need to rollback the current size
|
|
200
|
-
return [4 /*yield*/, this.
|
|
405
|
+
return [4 /*yield*/, this.increaseCurrentSizeInBytes(itemSize)];
|
|
201
406
|
case 6:
|
|
202
407
|
// if some error happened, we need to rollback the current size
|
|
203
408
|
_a.sent();
|
|
@@ -208,35 +413,145 @@ var AsyncStorageCache = /** @class */ (function (_super) {
|
|
|
208
413
|
});
|
|
209
414
|
});
|
|
210
415
|
};
|
|
416
|
+
/**
|
|
417
|
+
* produce a JSON object with meta-data and data value
|
|
418
|
+
* @param value - the value of the item
|
|
419
|
+
* @param options - optional, the specified meta-data
|
|
420
|
+
*
|
|
421
|
+
* @return - the item which has the meta-data and the value
|
|
422
|
+
*/
|
|
423
|
+
StorageCacheCommon.prototype.fillCacheItem = function (key, value, options) {
|
|
424
|
+
var _a, _b;
|
|
425
|
+
var item = {
|
|
426
|
+
key: key,
|
|
427
|
+
data: value,
|
|
428
|
+
timestamp: getCurrentTime(),
|
|
429
|
+
visitedTime: getCurrentTime(),
|
|
430
|
+
priority: (_a = options.priority) !== null && _a !== void 0 ? _a : 0,
|
|
431
|
+
expires: (_b = options.expires) !== null && _b !== void 0 ? _b : 0,
|
|
432
|
+
type: typeof value,
|
|
433
|
+
byteSize: 0,
|
|
434
|
+
};
|
|
435
|
+
// calculate byte size
|
|
436
|
+
item.byteSize = getByteLength(JSON.stringify(item));
|
|
437
|
+
// re-calculate using cache item with updated byteSize property
|
|
438
|
+
item.byteSize = getByteLength(JSON.stringify(item));
|
|
439
|
+
return item;
|
|
440
|
+
};
|
|
441
|
+
StorageCacheCommon.prototype.sanitizeConfig = function () {
|
|
442
|
+
if (this.config.itemMaxSize > this.config.capacityInBytes) {
|
|
443
|
+
logger.error('Invalid parameter: itemMaxSize. It should be smaller than capacityInBytes. Setting back to default.');
|
|
444
|
+
this.config.itemMaxSize = defaultConfig.itemMaxSize;
|
|
445
|
+
}
|
|
446
|
+
if (this.config.defaultPriority > 5 || this.config.defaultPriority < 1) {
|
|
447
|
+
logger.error('Invalid parameter: defaultPriority. It should be between 1 and 5. Setting back to default.');
|
|
448
|
+
this.config.defaultPriority = defaultConfig.defaultPriority;
|
|
449
|
+
}
|
|
450
|
+
if (Number(this.config.warningThreshold) > 1 ||
|
|
451
|
+
Number(this.config.warningThreshold) < 0) {
|
|
452
|
+
logger.error('Invalid parameter: warningThreshold. It should be between 0 and 1. Setting back to default.');
|
|
453
|
+
this.config.warningThreshold = defaultConfig.warningThreshold;
|
|
454
|
+
}
|
|
455
|
+
// Set 5MB limit
|
|
456
|
+
var cacheLimit = 5 * 1024 * 1024;
|
|
457
|
+
if (this.config.capacityInBytes > cacheLimit) {
|
|
458
|
+
logger.error('Cache Capacity should be less than 5MB. Setting back to default. Setting back to default.');
|
|
459
|
+
this.config.capacityInBytes = defaultConfig.capacityInBytes;
|
|
460
|
+
}
|
|
461
|
+
};
|
|
462
|
+
/**
|
|
463
|
+
* increase current size of the cache
|
|
464
|
+
*
|
|
465
|
+
* @param amount - the amount of the cache szie which need to be increased
|
|
466
|
+
*/
|
|
467
|
+
StorageCacheCommon.prototype.increaseCurrentSizeInBytes = function (amount) {
|
|
468
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
469
|
+
var size;
|
|
470
|
+
return __generator(this, function (_a) {
|
|
471
|
+
switch (_a.label) {
|
|
472
|
+
case 0: return [4 /*yield*/, this.getCurrentCacheSize()];
|
|
473
|
+
case 1:
|
|
474
|
+
size = _a.sent();
|
|
475
|
+
return [4 /*yield*/, this.getStorage().setItem(getCurrentSizeKey(this.config.keyPrefix), (size + amount).toString())];
|
|
476
|
+
case 2:
|
|
477
|
+
_a.sent();
|
|
478
|
+
return [2 /*return*/];
|
|
479
|
+
}
|
|
480
|
+
});
|
|
481
|
+
});
|
|
482
|
+
};
|
|
483
|
+
/**
|
|
484
|
+
* decrease current size of the cache
|
|
485
|
+
*
|
|
486
|
+
* @param amount - the amount of the cache size which needs to be decreased
|
|
487
|
+
*/
|
|
488
|
+
StorageCacheCommon.prototype.decreaseCurrentSizeInBytes = function (amount) {
|
|
489
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
490
|
+
var size;
|
|
491
|
+
return __generator(this, function (_a) {
|
|
492
|
+
switch (_a.label) {
|
|
493
|
+
case 0: return [4 /*yield*/, this.getCurrentCacheSize()];
|
|
494
|
+
case 1:
|
|
495
|
+
size = _a.sent();
|
|
496
|
+
return [4 /*yield*/, this.getStorage().setItem(getCurrentSizeKey(this.config.keyPrefix), (size - amount).toString())];
|
|
497
|
+
case 2:
|
|
498
|
+
_a.sent();
|
|
499
|
+
return [2 /*return*/];
|
|
500
|
+
}
|
|
501
|
+
});
|
|
502
|
+
});
|
|
503
|
+
};
|
|
504
|
+
/**
|
|
505
|
+
* update the visited time if item has been visited
|
|
506
|
+
*
|
|
507
|
+
* @param item - the item which need to be updated
|
|
508
|
+
* @param prefixedKey - the key of the item
|
|
509
|
+
*
|
|
510
|
+
* @return the updated item
|
|
511
|
+
*/
|
|
512
|
+
StorageCacheCommon.prototype.updateVisitedTime = function (item, prefixedKey) {
|
|
513
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
514
|
+
return __generator(this, function (_a) {
|
|
515
|
+
switch (_a.label) {
|
|
516
|
+
case 0:
|
|
517
|
+
item.visitedTime = getCurrentTime();
|
|
518
|
+
return [4 /*yield*/, this.getStorage().setItem(prefixedKey, JSON.stringify(item))];
|
|
519
|
+
case 1:
|
|
520
|
+
_a.sent();
|
|
521
|
+
return [2 /*return*/, item];
|
|
522
|
+
}
|
|
523
|
+
});
|
|
524
|
+
});
|
|
525
|
+
};
|
|
211
526
|
/**
|
|
212
527
|
* put item into cache
|
|
213
|
-
*
|
|
528
|
+
*
|
|
214
529
|
* @param prefixedKey - the key of the item
|
|
215
530
|
* @param itemData - the value of the item
|
|
216
531
|
* @param itemSizeInBytes - the byte size of the item
|
|
217
532
|
*/
|
|
218
|
-
|
|
533
|
+
StorageCacheCommon.prototype.setCacheItem = function (prefixedKey, item) {
|
|
219
534
|
return __awaiter(this, void 0, void 0, function () {
|
|
220
535
|
var setItemErr_1;
|
|
221
536
|
return __generator(this, function (_a) {
|
|
222
537
|
switch (_a.label) {
|
|
223
538
|
case 0:
|
|
224
539
|
// first try to update the current size of the cache.
|
|
225
|
-
return [4 /*yield*/, this.
|
|
540
|
+
return [4 /*yield*/, this.increaseCurrentSizeInBytes(item.byteSize)];
|
|
226
541
|
case 1:
|
|
227
542
|
// first try to update the current size of the cache.
|
|
228
543
|
_a.sent();
|
|
229
544
|
_a.label = 2;
|
|
230
545
|
case 2:
|
|
231
546
|
_a.trys.push([2, 4, , 6]);
|
|
232
|
-
return [4 /*yield*/,
|
|
547
|
+
return [4 /*yield*/, this.getStorage().setItem(prefixedKey, JSON.stringify(item))];
|
|
233
548
|
case 3:
|
|
234
549
|
_a.sent();
|
|
235
550
|
return [3 /*break*/, 6];
|
|
236
551
|
case 4:
|
|
237
552
|
setItemErr_1 = _a.sent();
|
|
238
553
|
// if some error happened, we need to rollback the current size
|
|
239
|
-
return [4 /*yield*/, this.
|
|
554
|
+
return [4 /*yield*/, this.decreaseCurrentSizeInBytes(item.byteSize)];
|
|
240
555
|
case 5:
|
|
241
556
|
// if some error happened, we need to rollback the current size
|
|
242
557
|
_a.sent();
|
|
@@ -249,23 +564,21 @@ var AsyncStorageCache = /** @class */ (function (_super) {
|
|
|
249
564
|
};
|
|
250
565
|
/**
|
|
251
566
|
* total space needed when poping out items
|
|
252
|
-
*
|
|
567
|
+
*
|
|
253
568
|
* @param itemSize
|
|
254
569
|
*
|
|
255
570
|
* @return total space needed
|
|
256
571
|
*/
|
|
257
|
-
|
|
572
|
+
StorageCacheCommon.prototype.sizeToPop = function (itemSize) {
|
|
258
573
|
return __awaiter(this, void 0, void 0, function () {
|
|
259
|
-
var spaceItemNeed, cacheThresholdSpace;
|
|
574
|
+
var cur, spaceItemNeed, cacheThresholdSpace;
|
|
260
575
|
return __generator(this, function (_a) {
|
|
261
576
|
switch (_a.label) {
|
|
262
|
-
case 0: return [4 /*yield*/, this.
|
|
577
|
+
case 0: return [4 /*yield*/, this.getCurrentCacheSize()];
|
|
263
578
|
case 1:
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
cacheThresholdSpace = (1 - this.cacheConfig.warningThreshold) *
|
|
268
|
-
this.cacheConfig.capacityInBytes;
|
|
579
|
+
cur = _a.sent();
|
|
580
|
+
spaceItemNeed = cur + itemSize - this.config.capacityInBytes;
|
|
581
|
+
cacheThresholdSpace = (1 - this.config.warningThreshold) * this.config.capacityInBytes;
|
|
269
582
|
return [2 /*return*/, spaceItemNeed > cacheThresholdSpace
|
|
270
583
|
? spaceItemNeed
|
|
271
584
|
: cacheThresholdSpace];
|
|
@@ -275,63 +588,20 @@ var AsyncStorageCache = /** @class */ (function (_super) {
|
|
|
275
588
|
};
|
|
276
589
|
/**
|
|
277
590
|
* see whether cache is full
|
|
278
|
-
*
|
|
591
|
+
*
|
|
279
592
|
* @param itemSize
|
|
280
593
|
*
|
|
281
594
|
* @return true if cache is full
|
|
282
595
|
*/
|
|
283
|
-
|
|
596
|
+
StorageCacheCommon.prototype.isCacheFull = function (itemSize) {
|
|
284
597
|
return __awaiter(this, void 0, void 0, function () {
|
|
285
|
-
var
|
|
286
|
-
return __generator(this, function (_b) {
|
|
287
|
-
switch (_b.label) {
|
|
288
|
-
case 0:
|
|
289
|
-
_a = itemSize;
|
|
290
|
-
return [4 /*yield*/, this.getCacheCurSize()];
|
|
291
|
-
case 1: return [2 /*return*/, (_a + (_b.sent()) >
|
|
292
|
-
this.cacheConfig.capacityInBytes)];
|
|
293
|
-
}
|
|
294
|
-
});
|
|
295
|
-
});
|
|
296
|
-
};
|
|
297
|
-
/**
|
|
298
|
-
* scan the storage and find out all the keys owned by this cache
|
|
299
|
-
* also clean the expired keys while scanning
|
|
300
|
-
* @private
|
|
301
|
-
* @return array of keys
|
|
302
|
-
*/
|
|
303
|
-
AsyncStorageCache.prototype._findValidKeys = function () {
|
|
304
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
305
|
-
var keys, keyInCache, i, key;
|
|
598
|
+
var cur;
|
|
306
599
|
return __generator(this, function (_a) {
|
|
307
600
|
switch (_a.label) {
|
|
308
|
-
case 0:
|
|
309
|
-
keys = [];
|
|
310
|
-
keyInCache = [];
|
|
311
|
-
return [4 /*yield*/, AsyncStorage.getAllKeys()];
|
|
601
|
+
case 0: return [4 /*yield*/, this.getCurrentCacheSize()];
|
|
312
602
|
case 1:
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
_a.label = 2;
|
|
316
|
-
case 2:
|
|
317
|
-
if (!(i < keyInCache.length)) return [3 /*break*/, 7];
|
|
318
|
-
key = keyInCache[i];
|
|
319
|
-
if (!(key.indexOf(this.cacheConfig.keyPrefix) === 0 &&
|
|
320
|
-
key !== getCurrSizeKey(this.cacheConfig.keyPrefix))) return [3 /*break*/, 6];
|
|
321
|
-
return [4 /*yield*/, this._isExpired(key)];
|
|
322
|
-
case 3:
|
|
323
|
-
if (!_a.sent()) return [3 /*break*/, 5];
|
|
324
|
-
return [4 /*yield*/, this._removeItem(key)];
|
|
325
|
-
case 4:
|
|
326
|
-
_a.sent();
|
|
327
|
-
return [3 /*break*/, 6];
|
|
328
|
-
case 5:
|
|
329
|
-
keys.push(key);
|
|
330
|
-
_a.label = 6;
|
|
331
|
-
case 6:
|
|
332
|
-
i += 1;
|
|
333
|
-
return [3 /*break*/, 2];
|
|
334
|
-
case 7: return [2 /*return*/, keys];
|
|
603
|
+
cur = _a.sent();
|
|
604
|
+
return [2 /*return*/, itemSize + cur > this.config.capacityInBytes];
|
|
335
605
|
}
|
|
336
606
|
});
|
|
337
607
|
});
|
|
@@ -344,7 +614,7 @@ var AsyncStorageCache = /** @class */ (function (_super) {
|
|
|
344
614
|
* @param keys - all the keys in this cache
|
|
345
615
|
* @param sizeToPop - the total size of the items which needed to be poped out
|
|
346
616
|
*/
|
|
347
|
-
|
|
617
|
+
StorageCacheCommon.prototype.popOutItems = function (keys, sizeToPop) {
|
|
348
618
|
return __awaiter(this, void 0, void 0, function () {
|
|
349
619
|
var items, remainedSize, i, val, item, i;
|
|
350
620
|
return __generator(this, function (_a) {
|
|
@@ -356,7 +626,7 @@ var AsyncStorageCache = /** @class */ (function (_super) {
|
|
|
356
626
|
_a.label = 1;
|
|
357
627
|
case 1:
|
|
358
628
|
if (!(i < keys.length)) return [3 /*break*/, 4];
|
|
359
|
-
return [4 /*yield*/,
|
|
629
|
+
return [4 /*yield*/, this.getStorage().getItem(keys[i])];
|
|
360
630
|
case 2:
|
|
361
631
|
val = _a.sent();
|
|
362
632
|
if (val != null) {
|
|
@@ -390,7 +660,7 @@ var AsyncStorageCache = /** @class */ (function (_super) {
|
|
|
390
660
|
case 5:
|
|
391
661
|
if (!(i < items.length)) return [3 /*break*/, 8];
|
|
392
662
|
// pop out items until we have enough room for new item
|
|
393
|
-
return [4 /*yield*/, this.
|
|
663
|
+
return [4 /*yield*/, this.removeCacheItem(items[i].key, items[i].byteSize)];
|
|
394
664
|
case 6:
|
|
395
665
|
// pop out items until we have enough room for new item
|
|
396
666
|
_a.sent();
|
|
@@ -408,324 +678,117 @@ var AsyncStorageCache = /** @class */ (function (_super) {
|
|
|
408
678
|
});
|
|
409
679
|
};
|
|
410
680
|
/**
|
|
411
|
-
*
|
|
412
|
-
*
|
|
413
|
-
*
|
|
414
|
-
* The cache will pop out items if it is full
|
|
415
|
-
* You can specify the cache item options. The cache will abort and output a warning:
|
|
416
|
-
* If the key is invalid
|
|
417
|
-
* If the size of the item exceeds itemMaxSize.
|
|
418
|
-
* If the value is undefined
|
|
419
|
-
* If incorrect cache item configuration
|
|
420
|
-
* If error happened with browser storage
|
|
681
|
+
* Scan the storage and combine the following operations for efficiency
|
|
682
|
+
* 1. Clear out all expired keys owned by this cache, not including the size key.
|
|
683
|
+
* 2. Return the remaining keys.
|
|
421
684
|
*
|
|
422
|
-
* @
|
|
423
|
-
* @param {Object} value - the value of the item
|
|
424
|
-
* @param {Object} [options] - optional, the specified meta-data
|
|
425
|
-
* @return {Promise}
|
|
685
|
+
* @return The remaining valid keys
|
|
426
686
|
*/
|
|
427
|
-
|
|
687
|
+
StorageCacheCommon.prototype.clearInvalidAndGetRemainingKeys = function () {
|
|
428
688
|
return __awaiter(this, void 0, void 0, function () {
|
|
429
|
-
var
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
logger.debug("Set item: key is ".concat(key, ", value is ").concat(value, " with options: ").concat(options));
|
|
434
|
-
prefixedKey = this.cacheConfig.keyPrefix + key;
|
|
435
|
-
// invalid keys
|
|
436
|
-
if (prefixedKey === this.cacheConfig.keyPrefix ||
|
|
437
|
-
prefixedKey === getCurrSizeKey(this.cacheConfig.keyPrefix)) {
|
|
438
|
-
logger.warn("Invalid key: should not be empty or 'CurSize'");
|
|
439
|
-
return [2 /*return*/];
|
|
440
|
-
}
|
|
441
|
-
if (typeof value === 'undefined') {
|
|
442
|
-
logger.warn("The value of item should not be undefined!");
|
|
443
|
-
return [2 /*return*/];
|
|
444
|
-
}
|
|
445
|
-
cacheItemOptions = {
|
|
446
|
-
priority: options && options.priority !== undefined
|
|
447
|
-
? options.priority
|
|
448
|
-
: this.cacheConfig.defaultPriority,
|
|
449
|
-
expires: options && options.expires !== undefined
|
|
450
|
-
? options.expires
|
|
451
|
-
: this.cacheConfig.defaultTTL + getCurrTime(),
|
|
452
|
-
};
|
|
453
|
-
if (cacheItemOptions.priority < 1 || cacheItemOptions.priority > 5) {
|
|
454
|
-
logger.warn("Invalid parameter: priority due to out or range. It should be within 1 and 5.");
|
|
455
|
-
return [2 /*return*/];
|
|
456
|
-
}
|
|
457
|
-
item = this.fillCacheItem(prefixedKey, value, cacheItemOptions);
|
|
458
|
-
// check wether this item is too big;
|
|
459
|
-
if (item.byteSize > this.cacheConfig.itemMaxSize) {
|
|
460
|
-
logger.warn("Item with key: ".concat(key, " you are trying to put into is too big!"));
|
|
461
|
-
return [2 /*return*/];
|
|
462
|
-
}
|
|
463
|
-
_a.label = 1;
|
|
464
|
-
case 1:
|
|
465
|
-
_a.trys.push([1, 12, , 13]);
|
|
466
|
-
return [4 /*yield*/, AsyncStorage.getItem(prefixedKey)];
|
|
467
|
-
case 2:
|
|
468
|
-
val = _a.sent();
|
|
469
|
-
if (!val) return [3 /*break*/, 4];
|
|
470
|
-
return [4 /*yield*/, this._removeItem(prefixedKey, JSON.parse(val).byteSize)];
|
|
471
|
-
case 3:
|
|
472
|
-
_a.sent();
|
|
473
|
-
_a.label = 4;
|
|
474
|
-
case 4: return [4 /*yield*/, this._isCacheFull(item.byteSize)];
|
|
475
|
-
case 5:
|
|
476
|
-
if (!_a.sent()) return [3 /*break*/, 10];
|
|
477
|
-
return [4 /*yield*/, this._findValidKeys()];
|
|
478
|
-
case 6:
|
|
479
|
-
validKeys = _a.sent();
|
|
480
|
-
return [4 /*yield*/, this._isCacheFull(item.byteSize)];
|
|
481
|
-
case 7:
|
|
482
|
-
if (!_a.sent()) return [3 /*break*/, 10];
|
|
483
|
-
return [4 /*yield*/, this._sizeToPop(item.byteSize)];
|
|
484
|
-
case 8:
|
|
485
|
-
sizeToPop = _a.sent();
|
|
486
|
-
return [4 /*yield*/, this._popOutItems(validKeys, sizeToPop)];
|
|
487
|
-
case 9:
|
|
488
|
-
_a.sent();
|
|
489
|
-
_a.label = 10;
|
|
490
|
-
case 10:
|
|
491
|
-
// put item in the cache
|
|
492
|
-
return [4 /*yield*/, this._setItem(prefixedKey, item)];
|
|
493
|
-
case 11:
|
|
494
|
-
// put item in the cache
|
|
495
|
-
_a.sent();
|
|
496
|
-
return [3 /*break*/, 13];
|
|
497
|
-
case 12:
|
|
498
|
-
e_1 = _a.sent();
|
|
499
|
-
logger.warn("setItem failed! ".concat(e_1));
|
|
500
|
-
return [3 /*break*/, 13];
|
|
501
|
-
case 13: return [2 /*return*/];
|
|
502
|
-
}
|
|
503
|
-
});
|
|
504
|
-
});
|
|
505
|
-
};
|
|
506
|
-
/**
|
|
507
|
-
* Get item from cache. It will return null if item doesn’t exist or it has been expired.
|
|
508
|
-
* If you specified callback function in the options,
|
|
509
|
-
* then the function will be executed if no such item in the cache
|
|
510
|
-
* and finally put the return value into cache.
|
|
511
|
-
* Please make sure the callback function will return the value you want to put into the cache.
|
|
512
|
-
* The cache will abort output a warning:
|
|
513
|
-
* If the key is invalid
|
|
514
|
-
* If error happened with AsyncStorage
|
|
515
|
-
*
|
|
516
|
-
* @param {String} key - the key of the item
|
|
517
|
-
* @param {Object} [options] - the options of callback function
|
|
518
|
-
* @return {Promise} - return a promise resolves to be the value of the item
|
|
519
|
-
*/
|
|
520
|
-
AsyncStorageCache.prototype.getItem = function (key, options) {
|
|
521
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
522
|
-
var ret, prefixedKey, item, val, e_2;
|
|
523
|
-
return __generator(this, function (_a) {
|
|
524
|
-
switch (_a.label) {
|
|
689
|
+
var remainingKeys, keys, keys_1, keys_1_1, key, e_5_1;
|
|
690
|
+
var e_5, _a;
|
|
691
|
+
return __generator(this, function (_b) {
|
|
692
|
+
switch (_b.label) {
|
|
525
693
|
case 0:
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
prefixedKey === getCurrSizeKey(this.cacheConfig.keyPrefix)) {
|
|
531
|
-
logger.warn("Invalid key: should not be empty or 'CurSize'");
|
|
532
|
-
return [2 /*return*/, null];
|
|
533
|
-
}
|
|
534
|
-
_a.label = 1;
|
|
694
|
+
remainingKeys = [];
|
|
695
|
+
return [4 /*yield*/, this.getAllCacheKeys({
|
|
696
|
+
omitSizeKey: true,
|
|
697
|
+
})];
|
|
535
698
|
case 1:
|
|
536
|
-
|
|
537
|
-
|
|
699
|
+
keys = _b.sent();
|
|
700
|
+
_b.label = 2;
|
|
538
701
|
case 2:
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
702
|
+
_b.trys.push([2, 9, 10, 11]);
|
|
703
|
+
keys_1 = __values(keys), keys_1_1 = keys_1.next();
|
|
704
|
+
_b.label = 3;
|
|
542
705
|
case 3:
|
|
543
|
-
if (
|
|
544
|
-
|
|
545
|
-
return [4 /*yield*/, this.
|
|
706
|
+
if (!!keys_1_1.done) return [3 /*break*/, 8];
|
|
707
|
+
key = keys_1_1.value;
|
|
708
|
+
return [4 /*yield*/, this.isExpired(key)];
|
|
546
709
|
case 4:
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
return [3 /*break*/, 7];
|
|
710
|
+
if (!_b.sent()) return [3 /*break*/, 6];
|
|
711
|
+
return [4 /*yield*/, this.removeCacheItem(key)];
|
|
550
712
|
case 5:
|
|
551
|
-
|
|
552
|
-
return [
|
|
713
|
+
_b.sent();
|
|
714
|
+
return [3 /*break*/, 7];
|
|
553
715
|
case 6:
|
|
554
|
-
|
|
555
|
-
|
|
716
|
+
remainingKeys.push(key);
|
|
717
|
+
_b.label = 7;
|
|
556
718
|
case 7:
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
logger.warn("getItem failed! ".concat(e_2));
|
|
568
|
-
return [2 /*return*/, null];
|
|
569
|
-
case 9: return [2 /*return*/];
|
|
570
|
-
}
|
|
571
|
-
});
|
|
572
|
-
});
|
|
573
|
-
};
|
|
574
|
-
/**
|
|
575
|
-
* remove item from the cache
|
|
576
|
-
* The cache will abort output a warning:
|
|
577
|
-
* If error happened with AsyncStorage
|
|
578
|
-
* @param {String} key - the key of the item
|
|
579
|
-
* @return {Promise}
|
|
580
|
-
*/
|
|
581
|
-
AsyncStorageCache.prototype.removeItem = function (key) {
|
|
582
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
583
|
-
var prefixedKey, val, e_3;
|
|
584
|
-
return __generator(this, function (_a) {
|
|
585
|
-
switch (_a.label) {
|
|
586
|
-
case 0:
|
|
587
|
-
logger.debug("Remove item: key is ".concat(key));
|
|
588
|
-
prefixedKey = this.cacheConfig.keyPrefix + key;
|
|
589
|
-
if (prefixedKey === this.cacheConfig.keyPrefix ||
|
|
590
|
-
prefixedKey === getCurrSizeKey(this.cacheConfig.keyPrefix)) {
|
|
591
|
-
return [2 /*return*/];
|
|
719
|
+
keys_1_1 = keys_1.next();
|
|
720
|
+
return [3 /*break*/, 3];
|
|
721
|
+
case 8: return [3 /*break*/, 11];
|
|
722
|
+
case 9:
|
|
723
|
+
e_5_1 = _b.sent();
|
|
724
|
+
e_5 = { error: e_5_1 };
|
|
725
|
+
return [3 /*break*/, 11];
|
|
726
|
+
case 10:
|
|
727
|
+
try {
|
|
728
|
+
if (keys_1_1 && !keys_1_1.done && (_a = keys_1.return)) _a.call(keys_1);
|
|
592
729
|
}
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
return [4 /*yield*/, AsyncStorage.getItem(prefixedKey)];
|
|
597
|
-
case 2:
|
|
598
|
-
val = _a.sent();
|
|
599
|
-
if (!val) return [3 /*break*/, 4];
|
|
600
|
-
return [4 /*yield*/, this._removeItem(prefixedKey, JSON.parse(val).byteSize)];
|
|
601
|
-
case 3:
|
|
602
|
-
_a.sent();
|
|
603
|
-
_a.label = 4;
|
|
604
|
-
case 4: return [3 /*break*/, 6];
|
|
605
|
-
case 5:
|
|
606
|
-
e_3 = _a.sent();
|
|
607
|
-
logger.warn("removeItem failed! ".concat(e_3));
|
|
608
|
-
return [3 /*break*/, 6];
|
|
609
|
-
case 6: return [2 /*return*/];
|
|
730
|
+
finally { if (e_5) throw e_5.error; }
|
|
731
|
+
return [7 /*endfinally*/];
|
|
732
|
+
case 11: return [2 /*return*/, remainingKeys];
|
|
610
733
|
}
|
|
611
734
|
});
|
|
612
735
|
});
|
|
613
736
|
};
|
|
614
737
|
/**
|
|
615
738
|
* clear the entire cache
|
|
616
|
-
* The cache will abort output a warning
|
|
617
|
-
* If error happened with AsyncStorage
|
|
739
|
+
* The cache will abort and output a warning if error occurs
|
|
618
740
|
* @return {Promise}
|
|
619
741
|
*/
|
|
620
|
-
|
|
742
|
+
StorageCacheCommon.prototype.clear = function () {
|
|
621
743
|
return __awaiter(this, void 0, void 0, function () {
|
|
622
|
-
var keys,
|
|
623
|
-
|
|
624
|
-
|
|
744
|
+
var keys, keys_2, keys_2_1, key, e_6_1, e_7;
|
|
745
|
+
var e_6, _a;
|
|
746
|
+
return __generator(this, function (_b) {
|
|
747
|
+
switch (_b.label) {
|
|
625
748
|
case 0:
|
|
626
749
|
logger.debug("Clear Cache");
|
|
627
|
-
|
|
750
|
+
_b.label = 1;
|
|
628
751
|
case 1:
|
|
629
|
-
|
|
630
|
-
return [4 /*yield*/,
|
|
752
|
+
_b.trys.push([1, 11, , 12]);
|
|
753
|
+
return [4 /*yield*/, this.getAllKeys()];
|
|
631
754
|
case 2:
|
|
632
|
-
keys =
|
|
633
|
-
|
|
634
|
-
for (i = 0; i < keys.length; i += 1) {
|
|
635
|
-
if (keys[i].indexOf(this.cacheConfig.keyPrefix) === 0) {
|
|
636
|
-
keysToRemove.push(keys[i]);
|
|
637
|
-
}
|
|
638
|
-
}
|
|
639
|
-
i = 0;
|
|
640
|
-
_a.label = 3;
|
|
755
|
+
keys = _b.sent();
|
|
756
|
+
_b.label = 3;
|
|
641
757
|
case 3:
|
|
642
|
-
|
|
643
|
-
|
|
758
|
+
_b.trys.push([3, 8, 9, 10]);
|
|
759
|
+
keys_2 = __values(keys), keys_2_1 = keys_2.next();
|
|
760
|
+
_b.label = 4;
|
|
644
761
|
case 4:
|
|
645
|
-
|
|
646
|
-
|
|
762
|
+
if (!!keys_2_1.done) return [3 /*break*/, 7];
|
|
763
|
+
key = keys_2_1.value;
|
|
764
|
+
return [4 /*yield*/, this.getStorage().removeItem(key)];
|
|
647
765
|
case 5:
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
case 6:
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
* return the current size of the cache
|
|
662
|
-
* @return {Promise}
|
|
663
|
-
*/
|
|
664
|
-
AsyncStorageCache.prototype.getCacheCurSize = function () {
|
|
665
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
666
|
-
var ret;
|
|
667
|
-
return __generator(this, function (_a) {
|
|
668
|
-
switch (_a.label) {
|
|
669
|
-
case 0: return [4 /*yield*/, AsyncStorage.getItem(getCurrSizeKey(this.cacheConfig.keyPrefix))];
|
|
670
|
-
case 1:
|
|
671
|
-
ret = _a.sent();
|
|
672
|
-
if (!!ret) return [3 /*break*/, 3];
|
|
673
|
-
return [4 /*yield*/, AsyncStorage.setItem(getCurrSizeKey(this.cacheConfig.keyPrefix), '0')];
|
|
674
|
-
case 2:
|
|
675
|
-
_a.sent();
|
|
676
|
-
ret = '0';
|
|
677
|
-
_a.label = 3;
|
|
678
|
-
case 3: return [2 /*return*/, Number(ret)];
|
|
679
|
-
}
|
|
680
|
-
});
|
|
681
|
-
});
|
|
682
|
-
};
|
|
683
|
-
/**
|
|
684
|
-
* Return all the keys in the cache.
|
|
685
|
-
* Will return an empty array if error happend.
|
|
686
|
-
* @return {Promise}
|
|
687
|
-
*/
|
|
688
|
-
AsyncStorageCache.prototype.getAllKeys = function () {
|
|
689
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
690
|
-
var keys, retKeys, i, e_5;
|
|
691
|
-
return __generator(this, function (_a) {
|
|
692
|
-
switch (_a.label) {
|
|
693
|
-
case 0:
|
|
694
|
-
_a.trys.push([0, 2, , 3]);
|
|
695
|
-
return [4 /*yield*/, AsyncStorage.getAllKeys()];
|
|
696
|
-
case 1:
|
|
697
|
-
keys = _a.sent();
|
|
698
|
-
retKeys = [];
|
|
699
|
-
for (i = 0; i < keys.length; i += 1) {
|
|
700
|
-
if (keys[i].indexOf(this.cacheConfig.keyPrefix) === 0 &&
|
|
701
|
-
keys[i] !== getCurrSizeKey(this.cacheConfig.keyPrefix)) {
|
|
702
|
-
retKeys.push(keys[i].substring(this.cacheConfig.keyPrefix.length));
|
|
703
|
-
}
|
|
766
|
+
_b.sent();
|
|
767
|
+
_b.label = 6;
|
|
768
|
+
case 6:
|
|
769
|
+
keys_2_1 = keys_2.next();
|
|
770
|
+
return [3 /*break*/, 4];
|
|
771
|
+
case 7: return [3 /*break*/, 10];
|
|
772
|
+
case 8:
|
|
773
|
+
e_6_1 = _b.sent();
|
|
774
|
+
e_6 = { error: e_6_1 };
|
|
775
|
+
return [3 /*break*/, 10];
|
|
776
|
+
case 9:
|
|
777
|
+
try {
|
|
778
|
+
if (keys_2_1 && !keys_2_1.done && (_a = keys_2.return)) _a.call(keys_2);
|
|
704
779
|
}
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
780
|
+
finally { if (e_6) throw e_6.error; }
|
|
781
|
+
return [7 /*endfinally*/];
|
|
782
|
+
case 10: return [3 /*break*/, 12];
|
|
783
|
+
case 11:
|
|
784
|
+
e_7 = _b.sent();
|
|
785
|
+
logger.warn("clear failed! ".concat(e_7));
|
|
786
|
+
return [3 /*break*/, 12];
|
|
787
|
+
case 12: return [2 /*return*/];
|
|
711
788
|
}
|
|
712
789
|
});
|
|
713
790
|
});
|
|
714
791
|
};
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
* @return {Object} - the new instance of Cache
|
|
719
|
-
*/
|
|
720
|
-
AsyncStorageCache.prototype.createInstance = function (config) {
|
|
721
|
-
if (config.keyPrefix === defaultConfig.keyPrefix) {
|
|
722
|
-
logger.error('invalid keyPrefix, setting keyPrefix with timeStamp');
|
|
723
|
-
config.keyPrefix = getCurrTime.toString();
|
|
724
|
-
}
|
|
725
|
-
return new AsyncStorageCache(config);
|
|
726
|
-
};
|
|
727
|
-
return AsyncStorageCache;
|
|
728
|
-
}(StorageCache));
|
|
729
|
-
export { AsyncStorageCache };
|
|
730
|
-
var instance = new AsyncStorageCache();
|
|
731
|
-
export { AsyncStorage, instance as Cache };
|
|
792
|
+
return StorageCacheCommon;
|
|
793
|
+
}());
|
|
794
|
+
export { StorageCacheCommon };
|