import { TableTopNotificationState } from './TableTopNotificationState'; import { action, makeObservable } from 'mobx'; import { EventEmitter } from 'events'; import { TypedEmitter } from '@wix/bex-core/events'; export class ScrollAwayScheduleStrategy { state; events = new EventEmitter() as TypedEmitter<{ enter: () => unknown; }>; constructor(state: TableTopNotificationState) { this.state = state; makeObservable(this, { preventEnter: action.bound, preventExit: action.bound, onIntersectionRatioChanged: action.bound, }); } _scheduleEnter() { const { topNotification } = this.state; const { nextData } = topNotification; this.events.removeAllListeners('enter'); this.events.once( 'enter', action(() => { if (nextData === topNotification.nextData) { topNotification.set(nextData); } }), ); } preventEnter() { const { topNotification, intersection, consideredVisibleRatio } = this.state; if (intersection.intersectionRatio < consideredVisibleRatio) { topNotification.show = false; // console.log('enter prevented'); this._scheduleEnter(); } } preventExit() { const { topNotification, intersection, consideredVisibleRatio } = this.state; if (intersection.intersectionRatio < consideredVisibleRatio) { topNotification.show = true; // console.log('exit prevented'); this._scheduleEnter(); } } onIntersectionRatioChanged(ratio: number) { const { consideredVisibleRatio, topNotification } = this.state; // when scrolling in trigger enter if (ratio >= consideredVisibleRatio) { this.events.emit('enter'); topNotification.events.off('beforeExit', this.preventExit); topNotification.events.off('enter', this.preventEnter); } // when scrolling out schedule prevention else { topNotification.events.off('beforeExit', this.preventExit); topNotification.events.off('enter', this.preventEnter); if (topNotification.show) { topNotification.events.on('beforeExit', this.preventExit); } else { topNotification.events.on('enter', this.preventEnter); } } } init() {} }