Nicolas Gallagher
2019-06-28 10:09:05 -07:00
parent 920211b998
commit fbdbfa5484
7 changed files with 0 additions and 637 deletions

View File

@@ -167,7 +167,6 @@ React Native v0.55
| Animated | ✓ | Missing `useNativeDriver` support. |
| AppRegistry | ✓ | Includes additional support for server rendering with `getApplication`. |
| AppState | ✓ | |
| AsyncStorage | ✓ | |
| BackHandler | (✓) | Mock. No equivalent web APIs. |
| CameraRoll | ✘ | No equivalent web APIs. |
| Clipboard | ✓ | |

View File

@@ -6,7 +6,6 @@ module.exports = {
Animated: true,
AppRegistry: true,
AppState: true,
AsyncStorage: true,
BackHandler: true,
Button: true,
CameraRoll: true,

View File

@@ -1,73 +0,0 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`apis/AsyncStorage mergeItem calls callback after setting item 1`] = `
Object {
"age": 31,
"name": "Chris",
"traits": Object {
"eyes": "blue",
"hair": "brown",
"shoe_size": 10,
},
}
`;
exports[`apis/AsyncStorage mergeItem promise after setting item 1`] = `
Object {
"age": 31,
"name": "Chris",
"traits": Object {
"eyes": "blue",
"hair": "brown",
"shoe_size": 10,
},
}
`;
exports[`apis/AsyncStorage multiMerge calls callback after setting items 1`] = `
Object {
"age": 31,
"name": "Chris",
"traits": Object {
"eyes": "blue",
"hair": "brown",
"shoe_size": 10,
},
}
`;
exports[`apis/AsyncStorage multiMerge calls callback after setting items 2`] = `
Object {
"age": 31,
"name": "Amy",
"traits": Object {
"eyes": "blue",
"hair": "black",
"shoe_size": 10,
},
}
`;
exports[`apis/AsyncStorage multiMerge promise after setting items 1`] = `
Object {
"age": 31,
"name": "Chris",
"traits": Object {
"eyes": "blue",
"hair": "brown",
"shoe_size": 10,
},
}
`;
exports[`apis/AsyncStorage multiMerge promise after setting items 2`] = `
Object {
"age": 31,
"name": "Amy",
"traits": Object {
"eyes": "blue",
"hair": "black",
"shoe_size": 10,
},
}
`;

View File

@@ -1,275 +0,0 @@
/* eslint-env jasmine, jest */
import AsyncStorage from '..';
const originalLocalStorage = window.localStorage;
let obj = {};
const mockLocalStorage = {
length: 0,
clear() {
obj = {};
mockLocalStorage.length = 0;
},
getItem(key) {
return obj[key];
},
key(index) {
return Object.keys(obj)[index];
},
removeItem(key) {
delete obj[key];
mockLocalStorage.length -= 1;
},
setItem(key, value) {
obj[key] = value;
mockLocalStorage.length += 1;
}
};
const uid123Object = {
name: 'Chris',
age: 30,
traits: { hair: 'brown', eyes: 'green' }
};
const uid123Delta = {
age: 31,
traits: { eyes: 'blue', shoe_size: 10 }
};
const uid124Object = {
name: 'Amy',
age: 28,
traits: { hair: 'black', eyes: 'brown' }
};
describe('apis/AsyncStorage', () => {
beforeEach(() => {
mockLocalStorage.setItem('UID123', JSON.stringify(uid123Object));
mockLocalStorage.setItem('UID124', JSON.stringify(uid124Object));
window.localStorage = mockLocalStorage;
});
afterEach(() => {
mockLocalStorage.clear();
window.localStorage = originalLocalStorage;
});
describe('clear', () => {
const assertResult = () => {
expect(mockLocalStorage.length).toEqual(0);
};
test('promise of erased keys', () => {
expect(mockLocalStorage.length).toEqual(2);
return AsyncStorage.clear().then(() => {
assertResult();
});
});
test('calls callback after erasing keys', done => {
expect(mockLocalStorage.length).toEqual(2);
AsyncStorage.clear(err => {
expect(err).toEqual(null);
assertResult();
done();
});
});
});
describe('getAllKeys', () => {
const assertResult = result => {
expect(result).toEqual(['UID123', 'UID124']);
};
test('promise of keys', () => {
return AsyncStorage.getAllKeys().then(result => {
assertResult(result);
});
});
test('calls callback with keys', done => {
AsyncStorage.getAllKeys((err, result) => {
expect(err).toEqual(null);
assertResult(result);
done();
});
});
});
describe('getItem', () => {
const assertResult = result => {
expect(result).toEqual(JSON.stringify(uid123Object));
};
test('promise of item', () => {
return AsyncStorage.getItem('UID123').then(result => {
assertResult(result);
});
});
test('calls callback with item', done => {
AsyncStorage.getItem('UID123', (err, result) => {
expect(err).toEqual(null);
assertResult(result);
done();
});
});
});
describe('multiGet', () => {
const assertResult = result => {
expect(result).toEqual([
['UID123', JSON.stringify(uid123Object)],
['UID124', JSON.stringify(uid124Object)]
]);
};
test('promise of items', () => {
return AsyncStorage.multiGet(['UID123', 'UID124']).then(result => {
assertResult(result);
});
});
test('calls callback with items', done => {
AsyncStorage.multiGet(['UID123', 'UID124'], (err, result) => {
expect(err).toEqual(null);
assertResult(result);
done();
});
});
});
describe('setItem', () => {
const assertResult = () => {
expect(mockLocalStorage.getItem('UID123')).toEqual(JSON.stringify(uid123Object));
};
test('promise after setting item', () => {
return AsyncStorage.setItem('UID123', JSON.stringify(uid123Object)).then(() => {
assertResult();
});
});
test('calls callback after setting item', done => {
AsyncStorage.setItem('UID123', JSON.stringify(uid123Object), (err, result) => {
expect(err).toEqual(null);
assertResult();
done();
});
});
});
describe('multiSet', () => {
const assertResult = () => {
expect(mockLocalStorage.getItem('UID123')).toEqual(JSON.stringify(uid123Object));
expect(mockLocalStorage.getItem('UID124')).toEqual(JSON.stringify(uid124Object));
};
test('promise after setting items', () => {
return AsyncStorage.multiSet([
['UID123', JSON.stringify(uid123Object)],
['UID124', JSON.stringify(uid124Object)]
]).then(() => {
assertResult();
});
});
test('calls callback after setting items', done => {
AsyncStorage.multiSet(
[['UID123', JSON.stringify(uid123Object)], ['UID124', JSON.stringify(uid124Object)]],
(err, result) => {
expect(err).toEqual(null);
assertResult();
done();
}
);
});
});
describe('mergeItem', () => {
const assertResult = () => {
expect(JSON.parse(mockLocalStorage.getItem('UID123'))).toMatchSnapshot();
};
test('promise after setting item', () => {
return AsyncStorage.mergeItem('UID123', JSON.stringify(uid123Delta)).then(() => {
assertResult();
});
});
test('calls callback after setting item', done => {
AsyncStorage.mergeItem('UID123', JSON.stringify(uid123Delta), (err, result) => {
expect(err).toEqual(null);
assertResult();
done();
});
});
});
describe('multiMerge', () => {
const assertResult = () => {
expect(JSON.parse(mockLocalStorage.getItem('UID123'))).toMatchSnapshot();
expect(JSON.parse(mockLocalStorage.getItem('UID124'))).toMatchSnapshot();
};
test('promise after setting items', () => {
return AsyncStorage.multiMerge([
['UID123', JSON.stringify(uid123Delta)],
['UID124', JSON.stringify(uid123Delta)]
]).then(() => {
assertResult();
});
});
test('calls callback after setting items', done => {
AsyncStorage.multiMerge(
[['UID123', JSON.stringify(uid123Delta)], ['UID124', JSON.stringify(uid123Delta)]],
(err, result) => {
expect(err).toEqual(null);
assertResult();
done();
}
);
});
});
describe('removeItem', () => {
const assertResult = () => {
expect(mockLocalStorage.getItem('UID123')).toBeUndefined();
};
test('promise after setting item', () => {
return AsyncStorage.removeItem('UID123').then(() => {
assertResult();
});
});
test('calls callback after setting item', done => {
AsyncStorage.removeItem('UID123', (err, result) => {
expect(err).toEqual(null);
assertResult();
done();
});
});
});
describe('multiRemove', () => {
const assertResult = () => {
expect(mockLocalStorage.getItem('UID123')).toBeUndefined();
expect(mockLocalStorage.getItem('UID124')).toBeUndefined();
};
test('promise after setting items', () => {
return AsyncStorage.multiRemove(['UID123', 'UID124']).then(() => {
assertResult();
});
});
test('calls callback after setting items', done => {
AsyncStorage.multiRemove(['UID123', 'UID124'], (err, result) => {
expect(err).toEqual(null);
assertResult();
done();
});
});
});
});

View File

@@ -1,157 +0,0 @@
/**
* Copyright (c) Nicolas Gallagher.
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import merge from 'deep-assign';
const mergeLocalStorageItem = (key, value) => {
const oldValue = window.localStorage.getItem(key);
const oldObject = JSON.parse(oldValue);
const newObject = JSON.parse(value);
const nextValue = JSON.stringify(merge({}, oldObject, newObject));
window.localStorage.setItem(key, nextValue);
};
const createPromise = (getValue, callback): Promise<*> => {
return new Promise((resolve, reject) => {
try {
const value = getValue();
if (callback) {
callback(null, value);
}
resolve(value);
} catch (err) {
if (callback) {
callback(err);
}
reject(err);
}
});
};
const createPromiseAll = (promises, callback, processResult): Promise<*> => {
return Promise.all(promises).then(
result => {
const value = processResult ? processResult(result) : null;
callback && callback(null, value);
return Promise.resolve(value);
},
errors => {
callback && callback(errors);
return Promise.reject(errors);
}
);
};
export default class AsyncStorage {
/**
* Erases *all* AsyncStorage for the domain.
*/
static clear(callback?: Function): Promise<*> {
return createPromise(() => {
window.localStorage.clear();
}, callback);
}
/**
* (stub) Flushes any pending requests using a single batch call to get the data.
*/
static flushGetRequests() {}
/**
* Gets *all* keys known to the app, for all callers, libraries, etc.
*/
static getAllKeys(callback?: Function): Promise<*> {
return createPromise(() => {
const numberOfKeys = window.localStorage.length;
const keys = [];
for (let i = 0; i < numberOfKeys; i += 1) {
const key = window.localStorage.key(i);
keys.push(key);
}
return keys;
}, callback);
}
/**
* Fetches `key` value.
*/
static getItem(key: string, callback?: Function): Promise<*> {
return createPromise(() => {
return window.localStorage.getItem(key);
}, callback);
}
/**
* multiGet resolves to an array of key-value pair arrays that matches the
* input format of multiSet.
*
* multiGet(['k1', 'k2']) -> [['k1', 'val1'], ['k2', 'val2']]
*/
static multiGet(keys: Array<string>, callback?: Function): Promise<*> {
const promises = keys.map(key => AsyncStorage.getItem(key));
const processResult = result => result.map((value, i) => [keys[i], value]);
return createPromiseAll(promises, callback, processResult);
}
/**
* Sets `value` for `key`.
*/
static setItem(key: string, value: string, callback?: Function): Promise<*> {
return createPromise(() => {
window.localStorage.setItem(key, value);
}, callback);
}
/**
* Takes an array of key-value array pairs.
* multiSet([['k1', 'val1'], ['k2', 'val2']])
*/
static multiSet(keyValuePairs: Array<Array<string>>, callback?: Function): Promise<*> {
const promises = keyValuePairs.map(item => AsyncStorage.setItem(item[0], item[1]));
return createPromiseAll(promises, callback);
}
/**
* Merges existing value with input value, assuming they are stringified JSON.
*/
static mergeItem(key: string, value: string, callback?: Function): Promise<*> {
return createPromise(() => {
mergeLocalStorageItem(key, value);
}, callback);
}
/**
* Takes an array of key-value array pairs and merges them with existing
* values, assuming they are stringified JSON.
*
* multiMerge([['k1', 'val1'], ['k2', 'val2']])
*/
static multiMerge(keyValuePairs: Array<Array<string>>, callback?: Function): Promise<*> {
const promises = keyValuePairs.map(item => AsyncStorage.mergeItem(item[0], item[1]));
return createPromiseAll(promises, callback);
}
/**
* Removes a `key`
*/
static removeItem(key: string, callback?: Function): Promise<*> {
return createPromise(() => {
return window.localStorage.removeItem(key);
}, callback);
}
/**
* Delete all the keys in the `keys` array.
*/
static multiRemove(keys: Array<string>, callback?: Function): Promise<*> {
const promises = keys.map(key => AsyncStorage.removeItem(key));
return createPromiseAll(promises, callback);
}
}

View File

@@ -13,7 +13,6 @@ import Alert from './exports/Alert';
import Animated from './exports/Animated';
import AppRegistry from './exports/AppRegistry';
import AppState from './exports/AppState';
import AsyncStorage from './exports/AsyncStorage';
import BackHandler from './exports/BackHandler';
import Clipboard from './exports/Clipboard';
import DeviceInfo from './exports/DeviceInfo';
@@ -122,7 +121,6 @@ export {
Animated,
AppRegistry,
AppState,
AsyncStorage,
BackHandler,
Clipboard,
DeviceInfo,

View File

@@ -1,128 +0,0 @@
/**
* @flow
*/
import React from 'react';
import UIExplorer, {
AppText,
Code,
Description,
DocItem,
Section,
storiesOf
} from '../../ui-explorer';
const AsyncStorageScreen = () => (
<UIExplorer title="AsyncStorage" url="2-apis/AsyncStorage">
<Description>
<AppText>
AsyncStorage is a simple, unencrypted, asynchronous, persistent, key-value storage system
that is global to the domain. It's a facade over, and should be used instead of{' '}
<Code>window.localStorage</Code> to provide an asynchronous API and multi functions. Each
method returns a <Code>Promise</Code> object.
</AppText>
<AppText>
It is recommended that you use an abstraction on top of <Code>AsyncStorage</Code> instead of{' '}
<Code>AsyncStorage</Code> directly for anything more than light usage since it operates
globally.
</AppText>
<AppText>
The batched functions are useful for executing a lot of operations at once, allowing for
optimizations to provide the convenience of a single promise after all operations are
complete.
</AppText>
</Description>
<Section title="Methods">
<DocItem
description={
<AppText>
Erases all AsyncStorage. You probably don't want to call this - use
<Code>removeItem</Code> or <Code>multiRemove</Code> to clear only your own keys instead.
Returns a Promise object.
</AppText>
}
name="static clear"
typeInfo="function"
/>
<DocItem
description="Gets all known keys. Returns a Promise object."
name="static getAllKeys"
typeInfo=""
/>
<DocItem
description="Fetches the value of the given key. Returns a Promise object.."
name="static getItem"
typeInfo="(key: string) => {}"
/>
<DocItem
description="Merges existing value with input value, assuming they are stringified JSON. Returns a Promise object."
name="static mergeItem"
typeInfo="(key: string, value: string) => {}"
/>
<DocItem
description={
<AppText>
<Code>multiGet</Code> results in an array of key-value pair arrays that matches the
input format of <Code>multiSet</Code>. Returns a Promise object.
</AppText>
}
example={{
code: 'multiGet(["k1", "k2"]) -> [["k1", "val1"], ["k2", "val2"]]'
}}
name="static multiGet"
typeInfo="(keys: Array<string>) => {}"
/>
<DocItem
description={
<AppText>
multiMerge takes an array of key-value array pairs that match the output of{' '}
<Code>multiGet</Code>. It merges existing values with input values, assuming they are
stringified JSON. Returns a Promise object.
</AppText>
}
name="static multiMerge"
typeInfo="(keyValuePairs: Array<Array<string>>) => {}"
/>
<DocItem
description="Delete all the keys in the keys array. Returns a Promise object."
name="static multiRemove"
typeInfo="(keys: Array<string>) => {}"
/>
<DocItem
description={
<AppText>
<Code>multiSet</Code> takes an array of key-value array pairs that match the output of{' '}
<Code>multiGet</Code>. Returns a Promise object.
</AppText>
}
example={{
code: 'multiSet([["k1", "val1"], ["k2", "val2"]]);'
}}
name="static multiSet"
typeInfo="(keyValuePairs: Array<Array<string>>) => {}"
/>
<DocItem
description="Removes the value of the given key. Returns a Promise object."
name="static removeItem"
typeInfo="(key: string) => {}"
/>
<DocItem
description="Sets the value of the given key. Returns a Promise object."
name="static setItem"
typeInfo="(key: string, value: string) => {}"
/>
</Section>
</UIExplorer>
);
storiesOf('APIs', module).add('AsyncStorage', AsyncStorageScreen);