react-native-navigation的迁移库

ScreenVisibilityListener.js 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import {
  2. NativeAppEventEmitter,
  3. DeviceEventEmitter,
  4. Platform
  5. } from 'react-native';
  6. export default class ScreenVisibilityListener {
  7. constructor(listeners) {
  8. this.emitter = Platform.OS === 'android' ? DeviceEventEmitter : NativeAppEventEmitter;
  9. this.listeners = listeners;
  10. }
  11. register() {
  12. const {willAppear, didAppear, willDisappear, didDisappear} = this.listeners;
  13. this.willAppearSubscription = willAppear && this.emitter.addListener('willAppear', willAppear);
  14. this.didAppearSubscription = didAppear && this.emitter.addListener('didAppear', didAppear);
  15. this.willDisappearSubscription = willDisappear && this.emitter.addListener('willDisappear', willDisappear);
  16. this.didDisappearSubscription = didDisappear && this.emitter.addListener('didDisappear', didDisappear);
  17. }
  18. unregister() {
  19. if (this.willAppearSubscription) {
  20. this.willAppearSubscription.remove();
  21. }
  22. if (this.didAppearSubscription) {
  23. this.didAppearSubscription.remove();
  24. }
  25. if (this.willDisappearSubscription) {
  26. this.willDisappearSubscription.remove();
  27. }
  28. if (this.didDisappearSubscription) {
  29. this.didDisappearSubscription.remove();
  30. }
  31. }
  32. }