1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- /**
- * Copyright (c) 2015-present, Parse, LLC.
- * All rights reserved.
- *
- * This source code is licensed under the BSD-style license found in the
- * LICENSE file in the root directory of this source tree. An additional grant
- * of patent rights can be found in the PATENTS file in the same directory.
- *
- * @flow
- */
- import { isLocalDatastoreKey } from './LocalDatastoreUtils';
- const LocalDatastoreController = {
- fromPinWithName(name
- /*: string*/
- )
- /*: Array<Object>*/
- {
- const values = wx.getStorageSync(name);
- if (!values) {
- return [];
- }
- return values;
- },
- pinWithName(name
- /*: string*/
- , value
- /*: any*/
- ) {
- try {
- wx.setStorageSync(name, value);
- } catch (e) {// Quota exceeded
- }
- },
- unPinWithName(name
- /*: string*/
- ) {
- wx.removeStorageSync(name);
- },
- getAllContents()
- /*: Object*/
- {
- const res = wx.getStorageInfoSync();
- const keys = res.keys;
- const LDS = {};
- for (const key of keys) {
- if (isLocalDatastoreKey(key)) {
- LDS[key] = wx.getStorageSync(key);
- }
- }
- return LDS;
- },
- getRawStorage()
- /*: Object*/
- {
- const res = wx.getStorageInfoSync();
- const keys = res.keys;
- const storage = {};
- for (const key of keys) {
- storage[key] = wx.getStorageSync(key);
- }
- return storage;
- },
- clear()
- /*: Promise*/
- {
- const res = wx.getStorageInfoSync();
- const keys = res.keys;
- const toRemove = [];
- for (const key of keys) {
- if (isLocalDatastoreKey(key)) {
- toRemove.push(key);
- }
- }
- const promises = toRemove.map(this.unPinWithName);
- return Promise.all(promises);
- }
- };
- module.exports = LocalDatastoreController;
|