2015-03-11 19:06:03 +00:00
|
|
|
/**
|
2015-03-23 20:35:08 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* 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.
|
2015-03-11 19:06:03 +00:00
|
|
|
*
|
|
|
|
* @providesModule AsyncStorage
|
|
|
|
* @flow-weak
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2015-03-17 10:08:49 +00:00
|
|
|
var NativeModules = require('NativeModules');
|
2015-03-18 05:22:03 +00:00
|
|
|
var RCTAsyncLocalStorage = NativeModules.AsyncLocalStorage;
|
|
|
|
var RCTAsyncRocksDBStorage = NativeModules.AsyncRocksDBStorage;
|
2015-03-11 19:06:03 +00:00
|
|
|
|
|
|
|
// We use RocksDB if available.
|
2015-03-17 10:08:46 +00:00
|
|
|
var RCTAsyncStorage = RCTAsyncRocksDBStorage || RCTAsyncLocalStorage;
|
2015-03-11 19:06:03 +00:00
|
|
|
|
|
|
|
/**
|
2015-06-15 21:17:27 +00:00
|
|
|
* AsyncStorage is a simple, asynchronous, persistent, key-value storage
|
|
|
|
* system that is global to the app. It should be used instead of LocalStorage.
|
2015-03-11 19:06:03 +00:00
|
|
|
*
|
|
|
|
* It is recommended that you use an abstraction on top of AsyncStorage instead
|
|
|
|
* of AsyncStorage directly for anything more than light usage since it
|
|
|
|
* operates globally.
|
|
|
|
*
|
2015-06-15 21:17:27 +00:00
|
|
|
* This JS code is a simple facade over the native iOS implementation to provide
|
2015-04-07 09:02:05 +00:00
|
|
|
* a clear JS API, real Error objects, and simple non-multi functions. Each
|
|
|
|
* method returns a `Promise` object.
|
2015-03-11 19:06:03 +00:00
|
|
|
*/
|
|
|
|
var AsyncStorage = {
|
|
|
|
/**
|
|
|
|
* Fetches `key` and passes the result to `callback`, along with an `Error` if
|
2015-04-07 09:02:05 +00:00
|
|
|
* there is any. Returns a `Promise` object.
|
2015-03-11 19:06:03 +00:00
|
|
|
*/
|
|
|
|
getItem: function(
|
|
|
|
key: string,
|
2015-05-02 01:29:43 +00:00
|
|
|
callback?: ?(error: ?Error, result: ?string) => void
|
2015-04-07 09:02:05 +00:00
|
|
|
): Promise {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
RCTAsyncStorage.multiGet([key], function(errors, result) {
|
|
|
|
// Unpack result to get value from [[key,value]]
|
|
|
|
var value = (result && result[0] && result[0][1]) ? result[0][1] : null;
|
|
|
|
callback && callback((errors && convertError(errors[0])) || null, value);
|
|
|
|
if (errors) {
|
|
|
|
reject(convertError(errors[0]));
|
|
|
|
} else {
|
|
|
|
resolve(value);
|
|
|
|
}
|
|
|
|
});
|
2015-03-11 19:06:03 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets `value` for `key` and calls `callback` on completion, along with an
|
2015-04-07 09:02:05 +00:00
|
|
|
* `Error` if there is any. Returns a `Promise` object.
|
2015-03-11 19:06:03 +00:00
|
|
|
*/
|
|
|
|
setItem: function(
|
|
|
|
key: string,
|
|
|
|
value: string,
|
2015-05-02 01:29:43 +00:00
|
|
|
callback?: ?(error: ?Error) => void
|
2015-04-07 09:02:05 +00:00
|
|
|
): Promise {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
RCTAsyncStorage.multiSet([[key,value]], function(errors) {
|
|
|
|
callback && callback((errors && convertError(errors[0])) || null);
|
|
|
|
if (errors) {
|
|
|
|
reject(convertError(errors[0]));
|
|
|
|
} else {
|
|
|
|
resolve(null);
|
|
|
|
}
|
|
|
|
});
|
2015-03-11 19:06:03 +00:00
|
|
|
});
|
|
|
|
},
|
2015-04-07 09:02:05 +00:00
|
|
|
/**
|
|
|
|
* Returns a `Promise` object.
|
|
|
|
*/
|
2015-03-11 19:06:03 +00:00
|
|
|
removeItem: function(
|
|
|
|
key: string,
|
2015-05-02 01:29:43 +00:00
|
|
|
callback?: ?(error: ?Error) => void
|
2015-04-07 09:02:05 +00:00
|
|
|
): Promise {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
RCTAsyncStorage.multiRemove([key], function(errors) {
|
|
|
|
callback && callback((errors && convertError(errors[0])) || null);
|
|
|
|
if (errors) {
|
|
|
|
reject(convertError(errors[0]));
|
|
|
|
} else {
|
|
|
|
resolve(null);
|
|
|
|
}
|
|
|
|
});
|
2015-03-11 19:06:03 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2015-04-07 09:02:05 +00:00
|
|
|
* Merges existing value with input value, assuming they are stringified json. Returns a `Promise` object.
|
2015-03-11 19:06:03 +00:00
|
|
|
*
|
|
|
|
* Not supported by all native implementations.
|
|
|
|
*/
|
|
|
|
mergeItem: function(
|
|
|
|
key: string,
|
|
|
|
value: string,
|
2015-05-02 01:29:43 +00:00
|
|
|
callback?: ?(error: ?Error) => void
|
2015-04-07 09:02:05 +00:00
|
|
|
): Promise {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
RCTAsyncStorage.multiMerge([[key,value]], function(errors) {
|
|
|
|
callback && callback((errors && convertError(errors[0])) || null);
|
|
|
|
if (errors) {
|
|
|
|
reject(convertError(errors[0]));
|
|
|
|
} else {
|
|
|
|
resolve(null);
|
|
|
|
}
|
|
|
|
});
|
2015-03-11 19:06:03 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Erases *all* AsyncStorage for all clients, libraries, etc. You probably
|
|
|
|
* don't want to call this - use removeItem or multiRemove to clear only your
|
2015-04-07 09:02:05 +00:00
|
|
|
* own keys instead. Returns a `Promise` object.
|
2015-03-11 19:06:03 +00:00
|
|
|
*/
|
2015-05-02 01:29:43 +00:00
|
|
|
clear: function(callback?: ?(error: ?Error) => void): Promise {
|
2015-04-07 09:02:05 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
RCTAsyncStorage.clear(function(error) {
|
|
|
|
callback && callback(convertError(error));
|
|
|
|
if (error && convertError(error)){
|
|
|
|
reject(convertError(error));
|
|
|
|
} else {
|
|
|
|
resolve(null);
|
|
|
|
}
|
|
|
|
});
|
2015-03-11 19:06:03 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2015-06-15 21:17:27 +00:00
|
|
|
* Gets *all* keys known to the app, for all callers, libraries, etc. Returns a `Promise` object.
|
2015-03-11 19:06:03 +00:00
|
|
|
*/
|
2015-05-02 01:29:43 +00:00
|
|
|
getAllKeys: function(callback?: ?(error: ?Error, keys: ?Array<string>) => void): Promise {
|
2015-04-07 09:02:05 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
RCTAsyncStorage.getAllKeys(function(error, keys) {
|
|
|
|
callback && callback(convertError(error), keys);
|
|
|
|
if (error) {
|
|
|
|
reject(convertError(error));
|
|
|
|
} else {
|
|
|
|
resolve(keys);
|
|
|
|
}
|
|
|
|
});
|
2015-03-11 19:06:03 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The following batched functions are useful for executing a lot of
|
|
|
|
* operations at once, allowing for native optimizations and provide the
|
|
|
|
* convenience of a single callback after all operations are complete.
|
|
|
|
*
|
|
|
|
* These functions return arrays of errors, potentially one for every key.
|
|
|
|
* For key-specific errors, the Error object will have a key property to
|
|
|
|
* indicate which key caused the error.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* multiGet invokes callback with an array of key-value pair arrays that
|
2015-04-07 09:02:05 +00:00
|
|
|
* matches the input format of multiSet. Returns a `Promise` object.
|
2015-03-11 19:06:03 +00:00
|
|
|
*
|
|
|
|
* multiGet(['k1', 'k2'], cb) -> cb([['k1', 'val1'], ['k2', 'val2']])
|
|
|
|
*/
|
|
|
|
multiGet: function(
|
|
|
|
keys: Array<string>,
|
2015-05-02 01:29:43 +00:00
|
|
|
callback?: ?(errors: ?Array<Error>, result: ?Array<Array<string>>) => void
|
2015-04-07 09:02:05 +00:00
|
|
|
): Promise {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
RCTAsyncStorage.multiGet(keys, function(errors, result) {
|
|
|
|
var error = (errors && errors.map((error) => convertError(error))) || null;
|
|
|
|
callback && callback(error, result);
|
|
|
|
if (errors) {
|
|
|
|
reject(error);
|
|
|
|
} else {
|
|
|
|
resolve(result);
|
|
|
|
}
|
|
|
|
});
|
2015-03-11 19:06:03 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* multiSet and multiMerge take arrays of key-value array pairs that match
|
2015-04-07 09:02:05 +00:00
|
|
|
* the output of multiGet, e.g. Returns a `Promise` object.
|
2015-03-11 19:06:03 +00:00
|
|
|
*
|
|
|
|
* multiSet([['k1', 'val1'], ['k2', 'val2']], cb);
|
|
|
|
*/
|
|
|
|
multiSet: function(
|
|
|
|
keyValuePairs: Array<Array<string>>,
|
2015-05-02 01:29:43 +00:00
|
|
|
callback?: ?(errors: ?Array<Error>) => void
|
2015-04-07 09:02:05 +00:00
|
|
|
): Promise {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
RCTAsyncStorage.multiSet(keyValuePairs, function(errors) {
|
|
|
|
var error = (errors && errors.map((error) => convertError(error))) || null;
|
|
|
|
callback && callback(error);
|
|
|
|
if (errors) {
|
|
|
|
reject(error);
|
|
|
|
} else {
|
|
|
|
resolve(null);
|
|
|
|
}
|
|
|
|
});
|
2015-03-11 19:06:03 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2015-04-07 09:02:05 +00:00
|
|
|
* Delete all the keys in the `keys` array. Returns a `Promise` object.
|
2015-03-11 19:06:03 +00:00
|
|
|
*/
|
|
|
|
multiRemove: function(
|
|
|
|
keys: Array<string>,
|
2015-05-02 01:29:43 +00:00
|
|
|
callback?: ?(errors: ?Array<Error>) => void
|
2015-04-07 09:02:05 +00:00
|
|
|
): Promise {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
RCTAsyncStorage.multiRemove(keys, function(errors) {
|
|
|
|
var error = (errors && errors.map((error) => convertError(error))) || null;
|
|
|
|
callback && callback(error);
|
|
|
|
if (errors) {
|
|
|
|
reject(error);
|
|
|
|
} else {
|
|
|
|
resolve(null);
|
|
|
|
}
|
|
|
|
});
|
2015-03-11 19:06:03 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Merges existing values with input values, assuming they are stringified
|
2015-04-07 09:02:05 +00:00
|
|
|
* json. Returns a `Promise` object.
|
2015-03-11 19:06:03 +00:00
|
|
|
*
|
|
|
|
* Not supported by all native implementations.
|
|
|
|
*/
|
|
|
|
multiMerge: function(
|
|
|
|
keyValuePairs: Array<Array<string>>,
|
2015-05-02 01:29:43 +00:00
|
|
|
callback?: ?(errors: ?Array<Error>) => void
|
2015-04-07 09:02:05 +00:00
|
|
|
): Promise {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
RCTAsyncStorage.multiMerge(keyValuePairs, function(errors) {
|
|
|
|
var error = (errors && errors.map((error) => convertError(error))) || null;
|
|
|
|
callback && callback(error);
|
|
|
|
if (errors) {
|
|
|
|
reject(error);
|
|
|
|
} else {
|
|
|
|
resolve(null);
|
|
|
|
}
|
|
|
|
});
|
2015-03-11 19:06:03 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
// Not all native implementations support merge.
|
2015-03-17 10:08:46 +00:00
|
|
|
if (!RCTAsyncStorage.multiMerge) {
|
2015-03-11 19:06:03 +00:00
|
|
|
delete AsyncStorage.mergeItem;
|
|
|
|
delete AsyncStorage.multiMerge;
|
|
|
|
}
|
|
|
|
|
|
|
|
function convertError(error) {
|
|
|
|
if (!error) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
var out = new Error(error.message);
|
|
|
|
out.key = error.key; // flow doesn't like this :(
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = AsyncStorage;
|