@aws-amplify/core
Version:
Core category of aws-amplify
106 lines (104 loc) • 3.85 kB
JavaScript
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
Object.defineProperty(exports, "__esModule", { value: true });
exports.createQueuedStorage = exports.keyPrefix = void 0;
const react_native_1 = require("@aws-amplify/react-native");
const constants_1 = require("./constants");
const getAddItemBytesSize_1 = require("./getAddItemBytesSize");
exports.keyPrefix = `@${constants_1.DATABASE_NAME}`;
const createQueuedStorage = () => {
let currentBytesSize = 0;
let error;
const openDBPromise = new Promise((resolve, _reject) => {
try {
const asyncStorage = (0, react_native_1.loadAsyncStorage)();
getQueuedItemKeys(asyncStorage)
.then(keys => getQueuedItems(asyncStorage, keys))
.then(items => {
for (const item of items) {
currentBytesSize += item.bytesSize;
}
return undefined;
})
.then(__ => {
resolve(asyncStorage);
});
}
catch (err) {
error = err;
resolve(undefined);
}
});
const getAsyncStorage = async () => {
const as = await openDBPromise;
if (!as) {
throw error;
}
return as;
};
const _peek = async (n) => {
const as = await getAsyncStorage();
const queuedItemKeys = await getQueuedItemKeys(as, true);
const keysToGetValues = queuedItemKeys.slice(0, n);
return getQueuedItems(as, keysToGetValues);
};
return {
async add(item, { dequeueBeforeEnqueue } = { dequeueBeforeEnqueue: false }) {
if (dequeueBeforeEnqueue) {
const itemsToDelete = await this.peek(1);
await this.delete(itemsToDelete);
}
const as = await getAsyncStorage();
const itemBytesSize = (0, getAddItemBytesSize_1.getAddItemBytesSize)(item);
const key = `${exports.keyPrefix}_${Date.now()}`;
const queuedItem = {
...item,
bytesSize: itemBytesSize,
key,
};
await as.setItem(key, JSON.stringify(queuedItem));
currentBytesSize += itemBytesSize;
},
async peek(n) {
return _peek(n);
},
async peekAll() {
return _peek();
},
async delete(items) {
const as = await getAsyncStorage();
const keysToDelete = items
.map(item => item.key)
.filter((id) => id !== undefined);
await as.multiRemove(keysToDelete);
for (const item of items) {
currentBytesSize -= item.bytesSize;
}
},
async clear() {
const as = await getAsyncStorage();
const keysToDelete = await getQueuedItemKeys(as);
await as.multiRemove(keysToDelete);
currentBytesSize = 0;
},
isFull(maxBytesSizeInMiB) {
return currentBytesSize >= maxBytesSizeInMiB * 1024 * 1024;
},
};
};
exports.createQueuedStorage = createQueuedStorage;
const getQueuedItemKeys = async (as, sortKeys = false) => {
const keys = (await as.getAllKeys()).filter(key => key.startsWith(exports.keyPrefix));
return sortKeys
? keys.sort((a, b) => {
const timestampA = a.split('_').pop();
const timestampB = b.split('_').pop();
return parseInt(timestampA) - parseInt(timestampB);
})
: keys;
};
const getQueuedItems = async (as, keys) => (await as.multiGet(keys))
.filter((item) => item[1] !== null)
.map(([_, value]) => JSON.parse(value));
//# sourceMappingURL=createQueuedStorage.native.js.map
;