自定义 cordova pushNotification 包装器返回 observable
custom cordova pushNotification wrapper returning observable
cordova 的 phonegap-plugin-push 有几个函数,语法如下
function name(successCallback(),errorCallback(),options)
我想编写一个包装器函数和 return 一个 Observable,但我对如何使用 angular2 的打字稿创建它有点困惑。
所以
name.subscribe((data)=>{...},(err)=>{})
到目前为止,我大多只订阅了现有的可观察对象,例如 http。
所以它应该看起来像
public unregister(options):Observable<any>{
if(window.PushNotification){
window.PushNotification.ungregister(...)
}
}
提前感谢您的帮助!!
您可以使用 create
或直接使用构造函数创建新的 Observable
。
//Higher order function for binding a cordova style method to a method
//that returns Observables
function bindCordovaCallback(fn) {
//Returns a new function
return (...opts) =>
Rx.Observable.create(observer => {
// This assumes a single call to success will be made
const nextCallback = (v) => { observer.next(v); observer.complete(); }
const errCallback = (e) => observer.error(e);
//Invoke the passed in method.
fn(nextCallback, errCallback, ...opts);
//Return empty unless you have some cancellation logic
return Subscription.EMPTY;
});
}
const nameObservable = bindCordovaCallback(name)(opts);
//Each subscribe will reinvoke the underlying method just like $http in angular2
nameObservable.subscribe(data => {...}, err => {...}
cordova 的 phonegap-plugin-push 有几个函数,语法如下
function name(successCallback(),errorCallback(),options)
我想编写一个包装器函数和 return 一个 Observable,但我对如何使用 angular2 的打字稿创建它有点困惑。
所以
name.subscribe((data)=>{...},(err)=>{})
到目前为止,我大多只订阅了现有的可观察对象,例如 http。
所以它应该看起来像
public unregister(options):Observable<any>{
if(window.PushNotification){
window.PushNotification.ungregister(...)
}
}
提前感谢您的帮助!!
您可以使用 create
或直接使用构造函数创建新的 Observable
。
//Higher order function for binding a cordova style method to a method
//that returns Observables
function bindCordovaCallback(fn) {
//Returns a new function
return (...opts) =>
Rx.Observable.create(observer => {
// This assumes a single call to success will be made
const nextCallback = (v) => { observer.next(v); observer.complete(); }
const errCallback = (e) => observer.error(e);
//Invoke the passed in method.
fn(nextCallback, errCallback, ...opts);
//Return empty unless you have some cancellation logic
return Subscription.EMPTY;
});
}
const nameObservable = bindCordovaCallback(name)(opts);
//Each subscribe will reinvoke the underlying method just like $http in angular2
nameObservable.subscribe(data => {...}, err => {...}