-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9bea485
commit 4eb0b94
Showing
4 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
(function (Scratch) { | ||
'use strict'; | ||
|
||
/** @type {Promise<BatteryManager>|null} */ | ||
let getBatteryPromise = null; | ||
/** @type {BatteryManager|null} */ | ||
let cachedBattery = null; | ||
/** @type {boolean} */ | ||
let batteryError = false; | ||
const withBattery = (callback) => { | ||
// Getting the BatteryManager is async the first time. Usually it's very fast, but we shouldn't assume that it is. | ||
// All the logic here lets us return values immediately when we have already got the battery instead of forcing | ||
// a delay by returning a promise. | ||
if (!navigator.getBattery || batteryError) { | ||
return callback(null); | ||
} | ||
if (cachedBattery) { | ||
return callback(cachedBattery); | ||
} | ||
if (!getBatteryPromise) { | ||
getBatteryPromise = navigator.getBattery() | ||
.then(battery => { | ||
getBatteryPromise = null; | ||
cachedBattery = battery; | ||
|
||
cachedBattery.addEventListener('chargingchange', () => { | ||
Scratch.vm.runtime.startHats('battery_chargingChanged'); | ||
}); | ||
cachedBattery.addEventListener('levelchange', () => { | ||
Scratch.vm.runtime.startHats('battery_levelChanged'); | ||
}); | ||
cachedBattery.addEventListener('chargingtimechange', () => { | ||
Scratch.vm.runtime.startHats('battery_chargeTimeChanged'); | ||
}); | ||
cachedBattery.addEventListener('dischargingtimechange', () => { | ||
Scratch.vm.runtime.startHats('battery_dischargeTimeChanged'); | ||
}); | ||
|
||
return cachedBattery; | ||
}) | ||
.catch(error => { | ||
getBatteryPromise = null; | ||
console.error('Could not get battery', error); | ||
batteryError = true; | ||
return null; | ||
}); | ||
} | ||
return getBatteryPromise.then(battery => { | ||
return callback(battery); | ||
}); | ||
}; | ||
|
||
// Try to get the battery immediately so that event blocks work. | ||
withBattery(() => {}); | ||
|
||
class BatteryExtension { | ||
getInfo () { | ||
return { | ||
name: 'Battery', | ||
id: 'battery', | ||
blocks: [ | ||
{ | ||
opcode: 'charging', | ||
blockType: Scratch.BlockType.BOOLEAN, | ||
text: 'charging?' | ||
}, | ||
{ | ||
opcode: 'level', | ||
blockType: Scratch.BlockType.REPORTER, | ||
text: 'battery level' | ||
}, | ||
{ | ||
opcode: 'chargeTime', | ||
blockType: Scratch.BlockType.REPORTER, | ||
text: 'seconds until charged' | ||
}, | ||
{ | ||
opcode: 'dischargeTime', | ||
blockType: Scratch.BlockType.REPORTER, | ||
text: 'seconds until empty' | ||
}, | ||
{ | ||
opcode: 'chargingChanged', | ||
blockType: Scratch.BlockType.HAT, | ||
text: 'when charging changed', | ||
isEdgeActivated: false | ||
}, | ||
{ | ||
opcode: 'levelChanged', | ||
blockType: Scratch.BlockType.HAT, | ||
text: 'when battery level changed', | ||
isEdgeActivated: false | ||
}, | ||
{ | ||
opcode: 'chargeTimeChanged', | ||
blockType: Scratch.BlockType.HAT, | ||
text: 'when time until charged changed', | ||
isEdgeActivated: false | ||
}, | ||
{ | ||
opcode: 'dischargeTimeChanged', | ||
blockType: Scratch.BlockType.HAT, | ||
text: 'when time until empty changed', | ||
isEdgeActivated: false | ||
}, | ||
] | ||
}; | ||
} | ||
charging () { | ||
return withBattery(battery => { | ||
if (!battery) return true; | ||
return battery.charging; | ||
}); | ||
} | ||
level () { | ||
return withBattery(battery => { | ||
if (!battery) return 100; | ||
return battery.level * 100; | ||
}); | ||
} | ||
chargeTime () { | ||
return withBattery(battery => { | ||
if (!battery) return 0; | ||
return battery.chargingTime; | ||
}); | ||
} | ||
dischargeTime () { | ||
return withBattery(battery => { | ||
if (!battery) return Infinity; | ||
return battery.dischargingTime; | ||
}); | ||
} | ||
} | ||
|
||
Scratch.extensions.register(new BatteryExtension()); | ||
})(Scratch); |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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