1- import { IIntersectionObserver , IIntersectionObserverEntry , IScrollMonitorRegisteredAd } from '../types' ;
1+ import { IIntersectionObserver , IIntersectionObserverEntry , IIntersectionObserverRegisteredAd } from '../types' ;
22
33const MONITORING = Symbol ( 'MONITORING' ) ;
44
5- // TODO: ad.hasViewBeenScrolled = true;
6-
75class ITXObserver {
86 public static observer : IIntersectionObserver ;
9- public static registeredAds : { [ key : string ] : IScrollMonitorRegisteredAd } = { } ;
7+ public static registeredAds : { [ key : string ] : IIntersectionObserverRegisteredAd } = { } ;
108 public static adCount : number = 0 ;
9+ public static hasViewBeenScrolled : boolean = false ;
1110
1211 public static monitorViewport ( ) {
1312 if ( ITXObserver [ MONITORING ] ) {
1413 return ;
1514 }
1615
16+ window . addEventListener ( 'scroll' , ITXObserver . onFirstScroll , false ) ;
17+
1718 ITXObserver . observer = new IntersectionObserver ( ( entries : any ) => {
1819 ITXObserver . handleIntersect ( entries ) ;
1920 } , { threshold : [ 0 , 0.25 , 1 ] } ) ;
@@ -25,7 +26,7 @@ class ITXObserver {
2526 entries . forEach ( ( event : IIntersectionObserverEntry ) => {
2627 const ad = ITXObserver . registeredAds [ event . target . id ] ;
2728
28- if ( ad . enableByScroll && ! ad . hasViewBeenScrolled ) {
29+ if ( ad . enableByScroll && ! ITXObserver . hasViewBeenScrolled ) {
2930 return ;
3031 }
3132
@@ -79,13 +80,12 @@ class ITXObserver {
7980 return ;
8081 }
8182
82- const ad : IScrollMonitorRegisteredAd = {
83+ const ad : IIntersectionObserverRegisteredAd = {
8384 element,
8485 offset,
8586 inView : false ,
8687 fullyInView : false ,
8788 enableByScroll,
88- hasViewBeenScrolled : false ,
8989 onEnterViewport : onEnterViewport ? [ onEnterViewport ] : [ ] ,
9090 onFullyEnterViewport : onFullyEnterViewport ? [ onFullyEnterViewport ] : [ ] ,
9191 onExitViewport : onExitViewport ? [ onExitViewport ] : [ ] ,
@@ -116,6 +116,10 @@ class ITXObserver {
116116 return ITXObserver . registeredAds [ id ] ;
117117 }
118118
119+ private static onFirstScroll = ( ) => {
120+ ITXObserver . hasViewBeenScrolled = true ;
121+ window . removeEventListener ( 'scroll' , ITXObserver . onFirstScroll , false ) ;
122+ }
119123}
120124
121125export default ITXObserver ;
0 commit comments