Skip to content

Instantly share code, notes, and snippets.

@mlynch
Forked from robwormald/cordova.ts
Created November 24, 2015 04:57
Show Gist options
  • Save mlynch/d7a8d0ee91ecbdf07e21 to your computer and use it in GitHub Desktop.
Save mlynch/d7a8d0ee91ecbdf07e21 to your computer and use it in GitHub Desktop.
const promisifyCordova = (pluginName:string, methodName:string) => {
return (...args) => {
return new Promise((resolve, reject) => {
cordova.exec(resolve, reject, pluginName, methodName, args);
})
}
}
const observablifyCordova = (pluginName:string, methodName:string, cleanUpMethodName?:string) => {
return (..args) => {
return new Observable(observer => {
cordova.exec((val) => observer.next(val), (err) => observer.error(err), pluginName, methodName, args);
return () => {
if(cleanupMethodName){
cordova.exec(noop, noop, pluginName, methodName);
}
}
})
}
}
let getPicture = promisifyCordova('camera', 'getPicture');
getPicture(options).then(...);
let GeoLocation = observablifyCordova('geolocation', 'startWatch', 'stopWatch');
let watcher = GeoLocation.subscribe(...);
//later
watcher.unsubscribe(...);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment