UNPKG

react-native-navigation

Version:

React Native Navigation - truly native navigation for iOS and Android

94 lines (93 loc) 4.17 kB
"use strict"; import isString from 'lodash/isString'; import isNil from 'lodash/isNil'; import uniqueId from 'lodash/uniqueId'; import unset from 'lodash/unset'; import forEach from 'lodash/forEach'; export class ComponentEventsObserver { listeners = {}; alreadyRegistered = false; constructor(nativeEventsReceiver, store) { this.nativeEventsReceiver = nativeEventsReceiver; this.store = store; this.notifyComponentWillAppear = this.notifyComponentWillAppear.bind(this); this.notifyComponentDidAppear = this.notifyComponentDidAppear.bind(this); this.notifyComponentDidDisappear = this.notifyComponentDidDisappear.bind(this); this.notifyNavigationButtonPressed = this.notifyNavigationButtonPressed.bind(this); this.notifySearchBarUpdated = this.notifySearchBarUpdated.bind(this); this.notifySearchBarCancelPressed = this.notifySearchBarCancelPressed.bind(this); this.notifyPreviewCompleted = this.notifyPreviewCompleted.bind(this); this.notifyScreenPopped = this.notifyScreenPopped.bind(this); } registerOnceForAllComponentEvents() { if (this.alreadyRegistered) { return; } this.alreadyRegistered = true; this.nativeEventsReceiver.registerComponentWillAppearListener(this.notifyComponentWillAppear); this.nativeEventsReceiver.registerComponentDidAppearListener(this.notifyComponentDidAppear); this.nativeEventsReceiver.registerComponentDidDisappearListener(this.notifyComponentDidDisappear); this.nativeEventsReceiver.registerNavigationButtonPressedListener(this.notifyNavigationButtonPressed); this.nativeEventsReceiver.registerSearchBarUpdatedListener(this.notifySearchBarUpdated); this.nativeEventsReceiver.registerSearchBarCancelPressedListener(this.notifySearchBarCancelPressed); this.nativeEventsReceiver.registerPreviewCompletedListener(this.notifyPreviewCompleted); this.nativeEventsReceiver.registerScreenPoppedListener(this.notifyPreviewCompleted); } bindComponent(component, componentId) { const computedComponentId = componentId || component.props.componentId; if (!isString(computedComponentId)) { throw new Error(`bindComponent expects a component with a componentId in props or a componentId as the second argument`); } return this.registerComponentListener(component, computedComponentId); } registerComponentListener(listener, componentId) { if (!isString(componentId)) { throw new Error(`registerComponentListener expects a componentId as the second argument`); } if (isNil(this.listeners[componentId])) { this.listeners[componentId] = {}; } const key = uniqueId(); this.listeners[componentId][key] = listener; return { remove: () => unset(this.listeners[componentId], key) }; } unmounted(componentId) { unset(this.listeners, componentId); } notifyComponentWillAppear(event) { event.passProps = this.store.getPropsForId(event.componentId); this.triggerOnAllListenersByComponentId(event, 'componentWillAppear'); } notifyComponentDidAppear(event) { event.passProps = this.store.getPropsForId(event.componentId); this.triggerOnAllListenersByComponentId(event, 'componentDidAppear'); } notifyComponentDidDisappear(event) { this.triggerOnAllListenersByComponentId(event, 'componentDidDisappear'); } notifyNavigationButtonPressed(event) { this.triggerOnAllListenersByComponentId(event, 'navigationButtonPressed'); } notifySearchBarUpdated(event) { this.triggerOnAllListenersByComponentId(event, 'searchBarUpdated'); } notifySearchBarCancelPressed(event) { this.triggerOnAllListenersByComponentId(event, 'searchBarCancelPressed'); } notifyPreviewCompleted(event) { this.triggerOnAllListenersByComponentId(event, 'previewCompleted'); } notifyScreenPopped(event) { this.triggerOnAllListenersByComponentId(event, 'screenPopped'); } triggerOnAllListenersByComponentId(event, method) { forEach(this.listeners[event.componentId], component => { if (component && component[method]) { component[method](event); } }); } } //# sourceMappingURL=ComponentEventsObserver.js.map