Skip to content

Adds interface to externally set RN Async Storage #519

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 4 commits into from
Dec 2, 2017
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
17 changes: 5 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,11 @@ branches:
- /^greenkeeper/.*$/

env:
- CXX=g++-4.8
- MONGODB_VERSION=3.2.13

services: mongodb

addons:
apt:
sources:
- mongodb-3.0-precise
- ubuntu-toolchain-r-test
packages:
- mongodb-org-server
- g++-4.8

script: npm test && ./run_integration.sh
before_script:
- npm install -g mongodb-runner
- mongodb-runner start
script: npm test -- --maxWorkers=4 && ./run_integration.sh
after_script: cat ./coverage/coverage-final.json | ./node_modules/codecov.io/bin/codecov.io.js && rm -rf ./coverage
8 changes: 8 additions & 0 deletions src/CoreManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,14 @@ module.exports = {
return config['StorageController'];
},

setAsyncStorage(storage: any) {
config['AsyncStorage'] = storage;
},

getAsyncStorage() {
return config['AsyncStorage'];
},

setUserController(controller: UserController) {
requireMethods('UserController', [
'setCurrentUser',
Expand Down
11 changes: 11 additions & 0 deletions src/Parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ var Parse = {
CoreManager.set('JAVASCRIPT_KEY', javaScriptKey);
CoreManager.set('MASTER_KEY', masterKey);
CoreManager.set('USE_MASTER_KEY', false);
},

/**
* Call this method to set your AsyncStorage engine
* Starting [email protected], the ParseSDK do not provide a React AsyncStorage as the ReactNative module
* is not provided at a stable path and changes over versions.
* @param {AsyncStorage} storage a react native async storage.
* @static
*/
setAsyncStorage(storage: any) {
CoreManager.setAsyncStorage(storage);
}
};

Expand Down
22 changes: 9 additions & 13 deletions src/StorageController.react-native.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,18 @@
*/

import ParsePromise from './ParsePromise';

// RN packager nonsense
let AsyncStorage;
try {
// for React Native 0.43+
AsyncStorage = require('react-native/Libraries/react-native/react-native-implementation').AsyncStorage;
} catch (error) {
AsyncStorage = require('react-native/Libraries/react-native/react-native.js').AsyncStorage;
}
import CoreManager from './CoreManager';

var StorageController = {
async: 1,

getAsyncStorage(): any {
return CoreManager.getAsyncStorage();
},

getItemAsync(path: string): ParsePromise {
var p = new ParsePromise();
AsyncStorage.getItem(path, function(err, value) {
this.getAsyncStorage().getItem(path, function(err, value) {
if (err) {
p.reject(err);
} else {
Expand All @@ -37,7 +33,7 @@ var StorageController = {

setItemAsync(path: string, value: string): ParsePromise {
var p = new ParsePromise();
AsyncStorage.setItem(path, value, function(err) {
this.getAsyncStorage().setItem(path, value, function(err) {
if (err) {
p.reject(err);
} else {
Expand All @@ -49,7 +45,7 @@ var StorageController = {

removeItemAsync(path: string): ParsePromise {
var p = new ParsePromise();
AsyncStorage.removeItem(path, function(err) {
this.getAsyncStorage().removeItem(path, function(err) {
if (err) {
p.reject(err);
} else {
Expand All @@ -60,7 +56,7 @@ var StorageController = {
},

clear() {
AsyncStorage.clear();
this.getAsyncStorage().clear();
}
};

Expand Down
10 changes: 1 addition & 9 deletions src/__tests__/Storage-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,6 @@ var mockRNStorageInterface = {
},
};

// for React Native 0.43+
jest.mock('react-native/Libraries/react-native/react-native-implementation', () => {
return {AsyncStorage: mockRNStorageInterface};
}, {virtual: true});

jest.mock('react-native/Libraries/react-native/react-native.js', () => {
return {AsyncStorage: mockRNStorageInterface};
}, {virtual: true});

var CoreManager = require('../CoreManager');
var ParsePromise = require('../ParsePromise').default;

Expand Down Expand Up @@ -106,6 +97,7 @@ var RNStorageController = require('../StorageController.react-native');

describe('React Native StorageController', () => {
beforeEach(() => {
CoreManager.setAsyncStorage(mockRNStorageInterface);
RNStorageController.clear();
});

Expand Down