diff --git a/README.md b/README.md index 12358ac3..3942ea19 100644 --- a/README.md +++ b/README.md @@ -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 | ✓ | | diff --git a/packages/babel-plugin-react-native-web/src/moduleMap.js b/packages/babel-plugin-react-native-web/src/moduleMap.js index 690410fe..af1883d1 100644 --- a/packages/babel-plugin-react-native-web/src/moduleMap.js +++ b/packages/babel-plugin-react-native-web/src/moduleMap.js @@ -6,7 +6,6 @@ module.exports = { Animated: true, AppRegistry: true, AppState: true, - AsyncStorage: true, BackHandler: true, Button: true, CameraRoll: true, diff --git a/packages/react-native-web/src/exports/AsyncStorage/__tests__/__snapshots__/index-test.js.snap b/packages/react-native-web/src/exports/AsyncStorage/__tests__/__snapshots__/index-test.js.snap deleted file mode 100644 index 639f40ca..00000000 --- a/packages/react-native-web/src/exports/AsyncStorage/__tests__/__snapshots__/index-test.js.snap +++ /dev/null @@ -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, - }, -} -`; diff --git a/packages/react-native-web/src/exports/AsyncStorage/__tests__/index-test.js b/packages/react-native-web/src/exports/AsyncStorage/__tests__/index-test.js deleted file mode 100644 index 102489ea..00000000 --- a/packages/react-native-web/src/exports/AsyncStorage/__tests__/index-test.js +++ /dev/null @@ -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(); - }); - }); - }); -}); diff --git a/packages/react-native-web/src/exports/AsyncStorage/index.js b/packages/react-native-web/src/exports/AsyncStorage/index.js deleted file mode 100644 index d3f411c2..00000000 --- a/packages/react-native-web/src/exports/AsyncStorage/index.js +++ /dev/null @@ -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, 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>, 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>, 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, callback?: Function): Promise<*> { - const promises = keys.map(key => AsyncStorage.removeItem(key)); - return createPromiseAll(promises, callback); - } -} diff --git a/packages/react-native-web/src/index.js b/packages/react-native-web/src/index.js index 9ba3a951..d23b2de1 100644 --- a/packages/react-native-web/src/index.js +++ b/packages/react-native-web/src/index.js @@ -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, diff --git a/packages/website/storybook/2-apis/AsyncStorage/AsyncStorageScreen.js b/packages/website/storybook/2-apis/AsyncStorage/AsyncStorageScreen.js deleted file mode 100644 index 27d634bc..00000000 --- a/packages/website/storybook/2-apis/AsyncStorage/AsyncStorageScreen.js +++ /dev/null @@ -1,128 +0,0 @@ -/** - * @flow - */ - -import React from 'react'; -import UIExplorer, { - AppText, - Code, - Description, - DocItem, - Section, - storiesOf -} from '../../ui-explorer'; - -const AsyncStorageScreen = () => ( - - - - 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{' '} - window.localStorage to provide an asynchronous API and multi functions. Each - method returns a Promise object. - - - 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. - - - 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. - - - -
- - Erases all AsyncStorage. You probably don't want to call this - use - removeItem or multiRemove to clear only your own keys instead. - Returns a Promise object. - - } - name="static clear" - typeInfo="function" - /> - - - - - - - - - multiGet results in an array of key-value pair arrays that matches the - input format of multiSet. Returns a Promise object. - - } - example={{ - code: 'multiGet(["k1", "k2"]) -> [["k1", "val1"], ["k2", "val2"]]' - }} - name="static multiGet" - typeInfo="(keys: Array) => {}" - /> - - - multiMerge takes an array of key-value array pairs that match the output of{' '} - multiGet. It merges existing values with input values, assuming they are - stringified JSON. Returns a Promise object. - - } - name="static multiMerge" - typeInfo="(keyValuePairs: Array>) => {}" - /> - - - - - multiSet takes an array of key-value array pairs that match the output of{' '} - multiGet. Returns a Promise object. - - } - example={{ - code: 'multiSet([["k1", "val1"], ["k2", "val2"]]);' - }} - name="static multiSet" - typeInfo="(keyValuePairs: Array>) => {}" - /> - - - - -
-
-); - -storiesOf('APIs', module).add('AsyncStorage', AsyncStorageScreen);