Skip to content

Commit 87f208b

Browse files
Matthew MoyaMatthew Moya
authored andcommitted
prop types and first scroll
1 parent 8932365 commit 87f208b

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

docs/error.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ Description: Parent element required for sticky plugin.
7777
Description: Ad does not have an id
7878

7979

80-
## Error 8
81-
Description: Ad Path must be defined.
82-
83-
8480
## Error 7
8581
Description: Sizes must be defined.
8682

83+
84+
## Error 8
85+
Description: Ad Path must be defined.
86+
8787

src/types.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,12 @@ export interface IAdConfiguration {
181181
targeting?: IAdTargeting;
182182
}
183183

184-
export interface IScrollMonitorRegisteredAd {
184+
export interface IIntersectionObserverRegisteredAd {
185185
element: HTMLElement;
186186
offset: number;
187187
inView: boolean;
188188
fullyInView: boolean;
189189
enableByScroll?: boolean;
190-
hasViewBeenScrolled: boolean;
191190
onEnterViewport: any[];
192191
onFullyEnterViewport: any[];
193192
onExitViewport: any[];

src/utils/intersectionObserver.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
import { IIntersectionObserver, IIntersectionObserverEntry, IScrollMonitorRegisteredAd } from '../types';
1+
import { IIntersectionObserver, IIntersectionObserverEntry, IIntersectionObserverRegisteredAd } from '../types';
22

33
const MONITORING = Symbol('MONITORING');
44

5-
// TODO: ad.hasViewBeenScrolled = true;
6-
75
class 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

121125
export default ITXObserver;

0 commit comments

Comments
 (0)