Skip to content
This repository was archived by the owner on Nov 3, 2025. It is now read-only.

Commit f4f5595

Browse files
committed
🚧 (BackHandlerDelegate) observes back presses
1 parent a4fa345 commit f4f5595

1 file changed

Lines changed: 25 additions & 5 deletions

File tree

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
11
import { BackHandler } from 'react-native';
2+
import { fromEventPattern } from 'rxjs';
3+
import { map, elementAt } from 'rxjs/operators';
24

35
export class BackHandlerDelegate {
6+
private shouldExit: boolean = false;
47
constructor() {
5-
BackHandler.addEventListener('hardwareBackPress', this.backHandler);
8+
const addHandler = (handler: () => void) => {
9+
BackHandler.addEventListener('hardwareBackPress', () => {
10+
handler();
11+
return !this.shouldExit;
12+
});
13+
};
14+
const hardwareBacks$ = fromEventPattern<undefined>(addHandler);
15+
hardwareBacks$
16+
.pipe(map(this.resetShouldExit))
17+
.pipe(
18+
map(() => {
19+
// tslint:disable-next-line
20+
console.log("back");
21+
})
22+
)
23+
.pipe(elementAt(3))
24+
.subscribe(this.setShouldExit);
625
}
7-
8-
private backHandler = () => {
9-
// tslint:disable-next-line
10-
console.log("Back action fired !");
26+
private resetShouldExit = () => {
27+
this.shouldExit = false;
28+
};
29+
private setShouldExit = () => {
30+
this.shouldExit = true;
1131
};
1232
}

0 commit comments

Comments
 (0)