mirror of
https://github.com/danielsogl/awesome-cordova-plugins.git
synced 2025-02-22 01:19:36 +08:00
refactor(batterystatus): extract getObservable() from class
extracted the getObservable() function from class so it doesn't show as a method (in docs and auto-complete)
This commit is contained in:
parent
e7a09d9018
commit
35bfe40d9e
@ -34,7 +34,7 @@ export class BatteryStatus {
|
|||||||
* @returns {Observable} Returns an observable that pushes a status object
|
* @returns {Observable} Returns an observable that pushes a status object
|
||||||
*/
|
*/
|
||||||
static onChange () : Observable<StatusObject> {
|
static onChange () : Observable<StatusObject> {
|
||||||
return BatteryStatus.getObservable("batterylevel");
|
return getEventObservable("batterylevel");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -42,7 +42,7 @@ export class BatteryStatus {
|
|||||||
* @returns {Observable<StatusObject>} Returns an observable that pushes a status object
|
* @returns {Observable<StatusObject>} Returns an observable that pushes a status object
|
||||||
*/
|
*/
|
||||||
static onLow () : Observable<StatusObject> {
|
static onLow () : Observable<StatusObject> {
|
||||||
return BatteryStatus.getObservable("batterylow");
|
return getEventObservable("batterylow");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -50,20 +50,7 @@ export class BatteryStatus {
|
|||||||
* @returns {Observable<StatusObject>} Returns an observable that pushes a status object
|
* @returns {Observable<StatusObject>} Returns an observable that pushes a status object
|
||||||
*/
|
*/
|
||||||
static onCritical () : Observable<StatusObject> {
|
static onCritical () : Observable<StatusObject> {
|
||||||
return BatteryStatus.getObservable("batterycritical");
|
return getEventObservable("batterycritical");
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Wrap the event with an observable
|
|
||||||
* @param event
|
|
||||||
* @returns {Observable}
|
|
||||||
*/
|
|
||||||
static getObservable (event : string) : Observable<StatusObject> {
|
|
||||||
return new Observable(observer => {
|
|
||||||
let callback = (status : any) => observer.next(status);
|
|
||||||
window.addEventListener(event, callback, false);
|
|
||||||
return () => window.removeEventListener(event, callback, false);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -78,4 +65,17 @@ interface StatusObject {
|
|||||||
* A boolean that indicates whether the device is plugged in
|
* A boolean that indicates whether the device is plugged in
|
||||||
*/
|
*/
|
||||||
isPlugged : boolean
|
isPlugged : boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrap the event with an observable
|
||||||
|
* @param event
|
||||||
|
* @returns {Observable}
|
||||||
|
*/
|
||||||
|
function getEventObservable (event : string) : Observable<StatusObject> {
|
||||||
|
return new Observable(observer => {
|
||||||
|
let callback = (status : any) => observer.next(status);
|
||||||
|
window.addEventListener(event, callback, false);
|
||||||
|
return () => window.removeEventListener(event, callback, false);
|
||||||
|
});
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user