Skip to content

Instantly share code, notes, and snippets.

@JeanLebrument
Last active October 23, 2015 09:13
Show Gist options
  • Save JeanLebrument/6dc373647b44fb4def7a to your computer and use it in GitHub Desktop.
Save JeanLebrument/6dc373647b44fb4def7a to your computer and use it in GitHub Desktop.
Rx optional support for for react-native-contacts
var ReactNative = require('react-native')
var Contacts = ReactNative.NativeModules.Contacts;
try {
var Rx = require('rx');
if (Rx !== 'undefined') {
function _execAddressBookMethodWithParamsErrorData(func) {
return Rx.Observable.create(function(observer) {
func(function(error, data) {
if (error) {
observer.onError(error);
} else {
observer.onNext(data);
observer.onCompleted();
}
});
return null;
});
}
function _execAddressBookMethodWithParamError(contact, func) {
return Rx.Observable.create(function(observer) {
func(contact, function(error) {
if (error) {
observer.onError(error);
} else {
observer.onCompleted();
}
});
return null;
});
}
Contacts.rx_checkPermission = function() {
return _execAddressBookMethodWithParamsErrorData(Contacts.checkPermission);
};
Contacts.rx_requestPermission = function() {
return _execAddressBookMethodWithParamsErrorData(Contacts.requestPermission);
};
Contacts.rx_getAll = function() {
return _execAddressBookMethodWithParamsErrorData(Contacts.getAll);
};
Contacts.rx_addContact = function(contact) {
return _execAddressBookMethodWithParamError(contact, Contacts.addContact);
}
Contacts.rx_updateContact = function(contact) {
return _execAddressBookMethodWithParamError(contact, Contacts.updateContact);
}
Contacts.rx_deleteContact = function(contact) {
return _execAddressBookMethodWithParamError(contact, Contacts.deleteContact);
}
}
} catch (ex) {
}
module.exports = Contacts;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment