Skip to content

fix: Web compatibility #351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ checkout step for each job: &addWorkspace
default config for js: &js_defaults
<<: *defaults
docker:
- image: circleci/node:8
- image: circleci/node:10

default config for macOS: &macos_defaults
<<: *defaults
Expand Down Expand Up @@ -158,7 +158,7 @@ jobs:
- save-cache: *cache_save_yarn
- restore_cache:
key: 1-gems-{{ checksum "example/ios/Gemfile.lock" }}
- run:
- run:
name: Install CocoaPods
command: cd example/ios && bundle check || bundle install --path vendor/bundle
- save_cache:
Expand Down
2 changes: 1 addition & 1 deletion .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ munge_underscores=true
server.max_workers=1

# Support the library import in examples
module.name_mapper='^\@react-native-community/async-storage$' -> '<PROJECT_ROOT>/lib/AsyncStorage.js'
module.name_mapper='^\@react-native-community/async-storage$' -> '<PROJECT_ROOT>/src/AsyncStorage.js'

module.name_mapper='^react-native$' -> '<PROJECT_ROOT>/node_modules/react-native/Libraries/react-native/react-native-implementation'
module.name_mapper='^react-native/\(.*\)$' -> '<PROJECT_ROOT>/node_modules/react-native/\1'
Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ project.xcworkspace

# Android/IntelliJ
#
build/
.idea
.gradle
local.properties
*.iml
android/build
android/gradle/
android/gradlew
android/gradlew.bat
Expand All @@ -49,3 +49,6 @@ buck-out/
.vscode
.expo
/web-build

# builds by bob
lib/
12 changes: 0 additions & 12 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,17 @@ yarn.lock

# Project files
CONTRIBUTING.md
CODE_OF_CONDUCT.md
README.md

# Config files
.babelrc
babel.config.js
.editorconfig
.eslintrc
.flowconfig
.watchmanconfig
jsconfig.json
.npmrc
.gitattributes
.circleci
*.coverage.json
.opensource
.circleci
.eslintignore
codecov.yml

# Scripts
Expand Down Expand Up @@ -70,10 +63,8 @@ ios/build/
# Misc
.DS_Store
.DS_Store?
*.DS_Store
coverage.android.json
coverage.ios.json
coverage
npm-debug.log
.github
._*
Expand All @@ -83,8 +74,5 @@ ehthumbs.db
Thumbs.dbandroid/gradle
docs
.idea
tests/
bin/test.js
codorials
.vscode
.nyc_output
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
An asynchronous, unencrypted, persistent, key-value storage system for React Native.


### AsyncStorage v2 is out!
## Supported platforms

- iOS
- Android
- [Web](https://github.com/react-native-community/async-storage/releases/tag/v1.9.0)
- [MacOS](https://github.com/react-native-community/async-storage/releases/tag/v1.8.1)
- [Windows](https://github.com/react-native-community/async-storage/releases/tag/v1.10.0)

Release Candidate for AsyncStorage v2 is out. More info [can be found here.](https://git.io/JeSSQ).

## Getting Started

Expand Down Expand Up @@ -35,14 +40,15 @@ $ cd ios && pod install
$ react-native link @react-native-community/async-storage
```

See docs for [manual linking guide](docs/Linking.md)
*Note:* For `macOS` and `Windows` the [manual linking](docs/Linking.md) is currently the only linking option.


*Note* For `macOS` the [manual linking](docs/Linking.md) is currently the only linking option.
See docs for [manual linking guide.](docs/Linking.md)

### **Upgrading to React Native *0.60+***

New React Native comes with `autolinking` feature, which automatically links Native Modules in your project.
In order to get it to work, make sure you `unlink` `Async Storage` first:
React Native 0.60+ comes with `autolinking` feature, which automatically links Native Modules in your project.
In order to get it to work, make sure you `unlink` `Async Storage` first (if you had linked it before):

```bash
$ react-native unlink @react-native-community/async-storage
Expand Down
83 changes: 33 additions & 50 deletions jest/async-storage-mock.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,52 @@
/**
* @format
* @flow
*/

type KeysType = Array<string>;
type KeyValueType = Array<Array<*>>;
type CallbackType = ((?Error) => void) | void;
type ItemGetCallbackType = ((?Error, ?string) => void) | void;
type ResultCallbackType = ((?Error, ?KeyValueType) => void) | void;

const asMock = {
__INTERNAL_MOCK_STORAGE__: {},

setItem: jest.fn<[string, string, CallbackType], Promise<*>>(
async (key: string, value: string, callback: CallbackType) => {
const setResult = await asMock.multiSet([[key, value]], undefined);
setItem: jest.fn(async (key, value, callback) => {
const setResult = await asMock.multiSet([[key, value]], undefined);

callback && callback(setResult);
return setResult;
},
),
getItem: jest.fn<[string, ItemGetCallbackType], Promise<*>>(
async (key: string, callback: ItemGetCallbackType) => {
const getResult = await asMock.multiGet([key], undefined);
callback && callback(setResult);
return setResult;
}),

const result = getResult[0] ? getResult[0][1] : null;
getItem: jest.fn(async (key, callback) => {
const getResult = await asMock.multiGet([key], undefined);

callback && callback(null, result);
return result;
},
),
removeItem: jest.fn<[string, CallbackType], Promise<null>>(
(key: string, callback: CallbackType) =>
asMock.multiRemove([key], callback),
),
mergeItem: jest.fn<[string, string, CallbackType], Promise<*>>(
(key: string, value: string, callback: CallbackType) =>
asMock.multiMerge([[key, value]], callback),
const result = getResult[0] ? getResult[0][1] : null;

callback && callback(null, result);
return result;
}),

removeItem: jest.fn((key, callback) => asMock.multiRemove([key], callback)),
mergeItem: jest.fn((key, value, callback) =>
asMock.multiMerge([[key, value]], callback),
),

clear: jest.fn<[CallbackType], Promise<*>>(_clear),
getAllKeys: jest.fn<[], Promise<string[]>>(_getAllKeys),
flushGetRequests: jest.fn<[], void>(),
clear: jest.fn(_clear),
getAllKeys: jest.fn(_getAllKeys),
flushGetRequests: jest.fn(),

multiGet: jest.fn<[KeysType, ResultCallbackType], Promise<*>>(_multiGet),
multiSet: jest.fn<[KeyValueType, CallbackType], Promise<*>>(_multiSet),
multiRemove: jest.fn<[KeysType, CallbackType], Promise<*>>(_multiRemove),
multiMerge: jest.fn<[KeyValueType, CallbackType], Promise<*>>(_multiMerge),
multiGet: jest.fn(_multiGet),
multiSet: jest.fn(_multiSet),
multiRemove: jest.fn(_multiRemove),
multiMerge: jest.fn(_multiMerge),
};

async function _multiSet(keyValuePairs: KeyValueType, callback: CallbackType) {
async function _multiSet(keyValuePairs, callback) {
keyValuePairs.forEach(keyValue => {
const key = keyValue[0];
const value = keyValue[1];

asMock.__INTERNAL_MOCK_STORAGE__[key] = value;
asMock.__INTERNAL_MOCK_STORAGE__[key] = keyValue[1];
});
callback && callback(null);
return null;
}

async function _multiGet(keys: KeysType, callback: ResultCallbackType) {
async function _multiGet(keys, callback) {
const values = keys.map(key => [
key,
asMock.__INTERNAL_MOCK_STORAGE__[key] || null,
Expand All @@ -70,7 +56,7 @@ async function _multiGet(keys: KeysType, callback: ResultCallbackType) {
return values;
}

async function _multiRemove(keys: KeysType, callback: CallbackType) {
async function _multiRemove(keys, callback) {
keys.forEach(key => {
if (asMock.__INTERNAL_MOCK_STORAGE__[key]) {
delete asMock.__INTERNAL_MOCK_STORAGE__[key];
Expand All @@ -81,7 +67,7 @@ async function _multiRemove(keys: KeysType, callback: CallbackType) {
return null;
}

async function _clear(callback: CallbackType) {
async function _clear(callback) {
asMock.__INTERNAL_MOCK_STORAGE__ = {};

callback && callback(null);
Expand All @@ -93,19 +79,16 @@ async function _getAllKeys() {
return Object.keys(asMock.__INTERNAL_MOCK_STORAGE__);
}

async function _multiMerge(
keyValuePairs: KeyValueType,
callback: CallbackType,
) {
async function _multiMerge(keyValuePairs, callback) {
keyValuePairs.forEach(keyValue => {
const key = keyValue[0];
const value = JSON.parse(keyValue[1]);

const oldValue = JSON.parse(asMock.__INTERNAL_MOCK_STORAGE__[key]);

const processedValue = JSON.stringify(_deepMergeInto(oldValue, value));

asMock.__INTERNAL_MOCK_STORAGE__[key] = processedValue;
asMock.__INTERNAL_MOCK_STORAGE__[key] = JSON.stringify(
_deepMergeInto(oldValue, value),
);
});

callback && callback(null);
Expand All @@ -131,4 +114,4 @@ const _deepMergeInto = (oldObject, newObject) => {
return mergedObject;
};

export default asMock;
module.exports = asMock;
30 changes: 27 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,23 @@
"name": "@react-native-community/async-storage",
"version": "1.10.0",
"description": "Asynchronous, persistent, key-value storage system for React Native.",
"main": "lib/commonjs/index.js",
"module": "lib/module/index.js",
"react-native": "src/index.js",
"types": "./types/index.d.ts",
"main": "./lib/index.js",
"author": "Krzysztof Borowy <krizzu.dev@gmail.com>",
"files": [
"lib/",
"src/",
"jest/",
"types/",
"android/src",
"android/build.gradle",
"ios/",
"macos/",
"window/",
"RNCAsyncStorage.podspec"
],
"author": "Krzysztof Borowy <hello@krizzu.dev>",
"contributors": [
"Evan Bacon <bacon@expo.io> (https://github.com/evanbacon)"
],
Expand All @@ -22,6 +36,7 @@
"url": "https://github.com/react-native-community/react-native-async-storage.git"
},
"scripts": {
"prepare": "bob build",
"start": "node node_modules/react-native/local-cli/cli.js start",
"start:android": "react-native run-android --root example/",
"start:ios": "react-native run-ios --project-path example/ios --scheme AsyncStorageExample",
Expand All @@ -33,7 +48,7 @@
"build:e2e:macos": "scripts/run_macos_e2e.sh 'build'",
"test": "yarn test:lint && yarn test:flow",
"test:flow": "flow check",
"test:lint": "eslint 'lib/**/*.js' 'example/**/*.js'",
"test:lint": "eslint 'src/**/*.js' 'example/**/*.js' 'jest/*.js'",
"test:e2e:ios": "detox test -c ios",
"test:e2e:android": "detox test -c android",
"test:e2e:macos": "scripts/run_macos_e2e.sh 'test'"
Expand All @@ -48,6 +63,7 @@
"devDependencies": {
"@babel/core": "^7.6.2",
"@babel/runtime": "^7.6.2",
"@react-native-community/bob": "^0.14.0",
"@react-native-community/cli": "^3.1.0",
"@react-native-community/cli-platform-android": "^3.1.0",
"@react-native-community/cli-platform-ios": "^3.1.0",
Expand Down Expand Up @@ -92,5 +108,13 @@
"name": "Emu_E2E"
}
}
},
"@react-native-community/bob": {
"source": "src",
"output": "lib",
"targets": [
["commonjs", {"copyFlow": true}],
"module"
]
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading