You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BackButtonRegistry.js 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // @flow
  2. /**
  3. * An registry that dispatches hardware back button events for subscribers with a custom logic.
  4. */
  5. class BackButtonRegistry {
  6. _listeners: Array<Function>;
  7. /**
  8. * Instantiates a new instance of the registry.
  9. */
  10. constructor() {
  11. this._listeners = [];
  12. }
  13. /**
  14. * Adds a listener to the registry.
  15. *
  16. * NOTE: Due to the different order of component mounts, we allow a component to register
  17. * its listener to the top of the list, so then that will be invoked before other, 'non-top'
  18. * listeners. For example a 'non-top' listener can be the one that puts the app into PiP mode,
  19. * while a 'top' listener is the one that closes a modal in a conference.
  20. *
  21. * @param {Function} listener - The listener function.
  22. * @param {boolean?} top - If true, the listener will be put on the top (eg for modal-like components).
  23. * @returns {void}
  24. */
  25. addListener(listener: Function, top: boolean = false) {
  26. if (top) {
  27. this._listeners.splice(0, 0, listener);
  28. } else {
  29. this._listeners.push(listener);
  30. }
  31. }
  32. /**
  33. * Removes a listener from the registry.
  34. *
  35. * @param {Function} listener - The listener to remove.
  36. * @returns {void}
  37. */
  38. removeListener(listener: Function) {
  39. this._listeners = this._listeners.filter(f => f !== listener);
  40. }
  41. onHardwareBackPress: () => boolean
  42. /**
  43. * Callback for the back button press event.
  44. *
  45. * @returns {boolean}
  46. */
  47. onHardwareBackPress() {
  48. for (const listener of this._listeners) {
  49. const result = listener();
  50. if (result === true) {
  51. return true;
  52. }
  53. }
  54. return false;
  55. }
  56. }
  57. export default new BackButtonRegistry();