mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-24 10:27:10 +02:00
42 lines
937 B
JavaScript
42 lines
937 B
JavaScript
const { BackHandler } = require('react-native');
|
|
|
|
class BackButtonService {
|
|
static initialize(defaultHandler) {
|
|
this.defaultHandler_ = defaultHandler;
|
|
|
|
BackHandler.addEventListener('hardwareBackPress', async () => {
|
|
return this.back();
|
|
});
|
|
}
|
|
|
|
static async back() {
|
|
if (this.handlers_.length) {
|
|
const r = await this.handlers_[this.handlers_.length - 1]();
|
|
if (r) return r;
|
|
}
|
|
|
|
return await this.defaultHandler_();
|
|
}
|
|
|
|
static addHandler(handler) {
|
|
for (let i = this.handlers_.length - 1; i >= 0; i--) {
|
|
const h = this.handlers_[i];
|
|
if (h === handler) return;
|
|
}
|
|
|
|
return this.handlers_.push(handler);
|
|
}
|
|
|
|
static removeHandler(handler) {
|
|
for (let i = this.handlers_.length - 1; i >= 0; i--) {
|
|
const h = this.handlers_[i];
|
|
if (h === handler) this.handlers_.splice(i, 1);
|
|
}
|
|
}
|
|
}
|
|
|
|
BackButtonService.defaultHandler_ = null;
|
|
BackButtonService.handlers_ = [];
|
|
|
|
module.exports = { BackButtonService };
|