Skip to content

Commit

Permalink
feat(batterystatus): complete functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ihadeed committed Mar 10, 2016
1 parent e776541 commit e7a09d9
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/plugins/batterystatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {Observable} from "rxjs/Observable";
* // watch change in battery status
* let subscription = BatteryStatus.onChange().subscribe(
* status => {
* console.log(status);
* console.log(status.level, status.isPlugged);
* }
* );
*
Expand All @@ -34,10 +34,35 @@ export class BatteryStatus {
* @returns {Observable} Returns an observable that pushes a status object
*/
static onChange () : Observable<StatusObject> {
return BatteryStatus.getObservable("batterylevel");
}

/**
* Watch when the battery level goes low
* @returns {Observable<StatusObject>} Returns an observable that pushes a status object
*/
static onLow () : Observable<StatusObject> {
return BatteryStatus.getObservable("batterylow");
}

/**
* Watch when the battery level goes to critial
* @returns {Observable<StatusObject>} Returns an observable that pushes a status object
*/
static onCritical () : Observable<StatusObject> {
return BatteryStatus.getObservable("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("batterystatus", callback, false);
return () => window.removeEventListener("batterystatus", callback, false);
window.addEventListener(event, callback, false);
return () => window.removeEventListener(event, callback, false);
});
}

Expand Down

0 comments on commit e7a09d9

Please sign in to comment.