Last active
October 23, 2015 09:13
-
-
Save JeanLebrument/6dc373647b44fb4def7a to your computer and use it in GitHub Desktop.
Rx optional support for for react-native-contacts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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