Skip to content

Commit 85c5278

Browse files
author
Krzysztof
authored
fix: Web compatibility (#351)
* build: use bob to comply with web target * docs: remove v2 mention * fix: make mock typeless, commonjs module * ci: update node image version * build: change file naming, copy flow files * docs: mention supported platforms
1 parent 724bc68 commit 85c5278

File tree

12 files changed

+1187
-88
lines changed

12 files changed

+1187
-88
lines changed

.circleci/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ checkout step for each job: &addWorkspace
1818
default config for js: &js_defaults
1919
<<: *defaults
2020
docker:
21-
- image: circleci/node:8
21+
- image: circleci/node:10
2222

2323
default config for macOS: &macos_defaults
2424
<<: *defaults
@@ -158,7 +158,7 @@ jobs:
158158
- save-cache: *cache_save_yarn
159159
- restore_cache:
160160
key: 1-gems-{{ checksum "example/ios/Gemfile.lock" }}
161-
- run:
161+
- run:
162162
name: Install CocoaPods
163163
command: cd example/ios && bundle check || bundle install --path vendor/bundle
164164
- save_cache:

.flowconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ munge_underscores=true
3939
server.max_workers=1
4040

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

4444
module.name_mapper='^react-native$' -> '<PROJECT_ROOT>/node_modules/react-native/Libraries/react-native/react-native-implementation'
4545
module.name_mapper='^react-native/\(.*\)$' -> '<PROJECT_ROOT>/node_modules/react-native/\1'

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ project.xcworkspace
2525

2626
# Android/IntelliJ
2727
#
28-
build/
2928
.idea
3029
.gradle
3130
local.properties
3231
*.iml
32+
android/build
3333
android/gradle/
3434
android/gradlew
3535
android/gradlew.bat
@@ -49,3 +49,6 @@ buck-out/
4949
.vscode
5050
.expo
5151
/web-build
52+
53+
# builds by bob
54+
lib/

.npmignore

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,17 @@ yarn.lock
44

55
# Project files
66
CONTRIBUTING.md
7-
CODE_OF_CONDUCT.md
87
README.md
98

109
# Config files
11-
.babelrc
1210
babel.config.js
13-
.editorconfig
1411
.eslintrc
1512
.flowconfig
1613
.watchmanconfig
17-
jsconfig.json
1814
.npmrc
19-
.gitattributes
2015
.circleci
2116
*.coverage.json
2217
.opensource
23-
.circleci
24-
.eslintignore
2518
codecov.yml
2619

2720
# Scripts
@@ -70,10 +63,8 @@ ios/build/
7063
# Misc
7164
.DS_Store
7265
.DS_Store?
73-
*.DS_Store
7466
coverage.android.json
7567
coverage.ios.json
76-
coverage
7768
npm-debug.log
7869
.github
7970
._*
@@ -83,8 +74,5 @@ ehthumbs.db
8374
Thumbs.dbandroid/gradle
8475
docs
8576
.idea
86-
tests/
8777
bin/test.js
8878
codorials
89-
.vscode
90-
.nyc_output

README.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33
An asynchronous, unencrypted, persistent, key-value storage system for React Native.
44

55

6-
### AsyncStorage v2 is out!
6+
## Supported platforms
7+
8+
- iOS
9+
- Android
10+
- [Web](https://github.com/react-native-community/async-storage/releases/tag/v1.9.0)
11+
- [MacOS](https://github.com/react-native-community/async-storage/releases/tag/v1.8.1)
12+
- [Windows](https://github.com/react-native-community/async-storage/releases/tag/v1.10.0)
713

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

1015
## Getting Started
1116

@@ -35,14 +40,15 @@ $ cd ios && pod install
3540
$ react-native link @react-native-community/async-storage
3641
```
3742

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

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

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

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

4753
```bash
4854
$ react-native unlink @react-native-community/async-storage

jest/async-storage-mock.js

Lines changed: 33 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,52 @@
11
/**
22
* @format
3-
* @flow
43
*/
54

6-
type KeysType = Array<string>;
7-
type KeyValueType = Array<Array<*>>;
8-
type CallbackType = ((?Error) => void) | void;
9-
type ItemGetCallbackType = ((?Error, ?string) => void) | void;
10-
type ResultCallbackType = ((?Error, ?KeyValueType) => void) | void;
11-
125
const asMock = {
136
__INTERNAL_MOCK_STORAGE__: {},
147

15-
setItem: jest.fn<[string, string, CallbackType], Promise<*>>(
16-
async (key: string, value: string, callback: CallbackType) => {
17-
const setResult = await asMock.multiSet([[key, value]], undefined);
8+
setItem: jest.fn(async (key, value, callback) => {
9+
const setResult = await asMock.multiSet([[key, value]], undefined);
1810

19-
callback && callback(setResult);
20-
return setResult;
21-
},
22-
),
23-
getItem: jest.fn<[string, ItemGetCallbackType], Promise<*>>(
24-
async (key: string, callback: ItemGetCallbackType) => {
25-
const getResult = await asMock.multiGet([key], undefined);
11+
callback && callback(setResult);
12+
return setResult;
13+
}),
2614

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

29-
callback && callback(null, result);
30-
return result;
31-
},
32-
),
33-
removeItem: jest.fn<[string, CallbackType], Promise<null>>(
34-
(key: string, callback: CallbackType) =>
35-
asMock.multiRemove([key], callback),
36-
),
37-
mergeItem: jest.fn<[string, string, CallbackType], Promise<*>>(
38-
(key: string, value: string, callback: CallbackType) =>
39-
asMock.multiMerge([[key, value]], callback),
18+
const result = getResult[0] ? getResult[0][1] : null;
19+
20+
callback && callback(null, result);
21+
return result;
22+
}),
23+
24+
removeItem: jest.fn((key, callback) => asMock.multiRemove([key], callback)),
25+
mergeItem: jest.fn((key, value, callback) =>
26+
asMock.multiMerge([[key, value]], callback),
4027
),
4128

42-
clear: jest.fn<[CallbackType], Promise<*>>(_clear),
43-
getAllKeys: jest.fn<[], Promise<string[]>>(_getAllKeys),
44-
flushGetRequests: jest.fn<[], void>(),
29+
clear: jest.fn(_clear),
30+
getAllKeys: jest.fn(_getAllKeys),
31+
flushGetRequests: jest.fn(),
4532

46-
multiGet: jest.fn<[KeysType, ResultCallbackType], Promise<*>>(_multiGet),
47-
multiSet: jest.fn<[KeyValueType, CallbackType], Promise<*>>(_multiSet),
48-
multiRemove: jest.fn<[KeysType, CallbackType], Promise<*>>(_multiRemove),
49-
multiMerge: jest.fn<[KeyValueType, CallbackType], Promise<*>>(_multiMerge),
33+
multiGet: jest.fn(_multiGet),
34+
multiSet: jest.fn(_multiSet),
35+
multiRemove: jest.fn(_multiRemove),
36+
multiMerge: jest.fn(_multiMerge),
5037
};
5138

52-
async function _multiSet(keyValuePairs: KeyValueType, callback: CallbackType) {
39+
async function _multiSet(keyValuePairs, callback) {
5340
keyValuePairs.forEach(keyValue => {
5441
const key = keyValue[0];
55-
const value = keyValue[1];
5642

57-
asMock.__INTERNAL_MOCK_STORAGE__[key] = value;
43+
asMock.__INTERNAL_MOCK_STORAGE__[key] = keyValue[1];
5844
});
5945
callback && callback(null);
6046
return null;
6147
}
6248

63-
async function _multiGet(keys: KeysType, callback: ResultCallbackType) {
49+
async function _multiGet(keys, callback) {
6450
const values = keys.map(key => [
6551
key,
6652
asMock.__INTERNAL_MOCK_STORAGE__[key] || null,
@@ -70,7 +56,7 @@ async function _multiGet(keys: KeysType, callback: ResultCallbackType) {
7056
return values;
7157
}
7258

73-
async function _multiRemove(keys: KeysType, callback: CallbackType) {
59+
async function _multiRemove(keys, callback) {
7460
keys.forEach(key => {
7561
if (asMock.__INTERNAL_MOCK_STORAGE__[key]) {
7662
delete asMock.__INTERNAL_MOCK_STORAGE__[key];
@@ -81,7 +67,7 @@ async function _multiRemove(keys: KeysType, callback: CallbackType) {
8167
return null;
8268
}
8369

84-
async function _clear(callback: CallbackType) {
70+
async function _clear(callback) {
8571
asMock.__INTERNAL_MOCK_STORAGE__ = {};
8672

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

96-
async function _multiMerge(
97-
keyValuePairs: KeyValueType,
98-
callback: CallbackType,
99-
) {
82+
async function _multiMerge(keyValuePairs, callback) {
10083
keyValuePairs.forEach(keyValue => {
10184
const key = keyValue[0];
10285
const value = JSON.parse(keyValue[1]);
10386

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

106-
const processedValue = JSON.stringify(_deepMergeInto(oldValue, value));
107-
108-
asMock.__INTERNAL_MOCK_STORAGE__[key] = processedValue;
89+
asMock.__INTERNAL_MOCK_STORAGE__[key] = JSON.stringify(
90+
_deepMergeInto(oldValue, value),
91+
);
10992
});
11093

11194
callback && callback(null);
@@ -131,4 +114,4 @@ const _deepMergeInto = (oldObject, newObject) => {
131114
return mergedObject;
132115
};
133116

134-
export default asMock;
117+
module.exports = asMock;

package.json

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,23 @@
22
"name": "@react-native-community/async-storage",
33
"version": "1.10.0",
44
"description": "Asynchronous, persistent, key-value storage system for React Native.",
5+
"main": "lib/commonjs/index.js",
6+
"module": "lib/module/index.js",
7+
"react-native": "src/index.js",
58
"types": "./types/index.d.ts",
6-
"main": "./lib/index.js",
7-
"author": "Krzysztof Borowy <krizzu.dev@gmail.com>",
9+
"files": [
10+
"lib/",
11+
"src/",
12+
"jest/",
13+
"types/",
14+
"android/src",
15+
"android/build.gradle",
16+
"ios/",
17+
"macos/",
18+
"window/",
19+
"RNCAsyncStorage.podspec"
20+
],
21+
"author": "Krzysztof Borowy <hello@krizzu.dev>",
822
"contributors": [
923
"Evan Bacon <bacon@expo.io> (https://github.com/evanbacon)"
1024
],
@@ -22,6 +36,7 @@
2236
"url": "https://github.com/react-native-community/react-native-async-storage.git"
2337
},
2438
"scripts": {
39+
"prepare": "bob build",
2540
"start": "node node_modules/react-native/local-cli/cli.js start",
2641
"start:android": "react-native run-android --root example/",
2742
"start:ios": "react-native run-ios --project-path example/ios --scheme AsyncStorageExample",
@@ -33,7 +48,7 @@
3348
"build:e2e:macos": "scripts/run_macos_e2e.sh 'build'",
3449
"test": "yarn test:lint && yarn test:flow",
3550
"test:flow": "flow check",
36-
"test:lint": "eslint 'lib/**/*.js' 'example/**/*.js'",
51+
"test:lint": "eslint 'src/**/*.js' 'example/**/*.js' 'jest/*.js'",
3752
"test:e2e:ios": "detox test -c ios",
3853
"test:e2e:android": "detox test -c android",
3954
"test:e2e:macos": "scripts/run_macos_e2e.sh 'test'"
@@ -48,6 +63,7 @@
4863
"devDependencies": {
4964
"@babel/core": "^7.6.2",
5065
"@babel/runtime": "^7.6.2",
66+
"@react-native-community/bob": "^0.14.0",
5167
"@react-native-community/cli": "^3.1.0",
5268
"@react-native-community/cli-platform-android": "^3.1.0",
5369
"@react-native-community/cli-platform-ios": "^3.1.0",
@@ -92,5 +108,13 @@
92108
"name": "Emu_E2E"
93109
}
94110
}
111+
},
112+
"@react-native-community/bob": {
113+
"source": "src",
114+
"output": "lib",
115+
"targets": [
116+
["commonjs", {"copyFlow": true}],
117+
"module"
118+
]
95119
}
96120
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)