@capacitor-community/admob 8.1.0 → 8.2.0-beta.pr454.sha49386d40323a
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CapacitorCommunityAdmob.podspec +1 -1
- package/Package.swift +1 -1
- package/README.md +129 -323
- package/android/build.gradle +1 -1
- package/android/src/main/java/com/getcapacitor/community/admob/AdMob.java +86 -3
- package/android/src/main/java/com/getcapacitor/community/admob/banner/BannerExecutor.java +261 -141
- package/android/src/main/java/com/getcapacitor/community/admob/helpers/FullscreenPluginCallback.kt +5 -1
- package/android/src/main/java/com/getcapacitor/community/admob/interstitial/AdInterstitialExecutor.java +27 -31
- package/android/src/main/java/com/getcapacitor/community/admob/interstitial/InterstitialAdCallbackAndListeners.kt +1 -1
- package/android/src/main/java/com/getcapacitor/community/admob/models/LoadPluginEventNames.kt +2 -1
- package/android/src/main/java/com/getcapacitor/community/admob/nativeads/NativeAdExecutor.kt +378 -0
- package/android/src/main/java/com/getcapacitor/community/admob/nativeads/NativeAdFeedSessions.kt +35 -0
- package/android/src/main/java/com/getcapacitor/community/admob/nativeads/NativeAdPlacementValue.kt +44 -0
- package/android/src/main/java/com/getcapacitor/community/admob/nativeads/NativeAdPluginEvents.kt +11 -0
- package/android/src/main/java/com/getcapacitor/community/admob/nativeads/PluginNativeAdView.kt +187 -0
- package/android/src/main/java/com/getcapacitor/community/admob/rewarded/AdRewardExecutor.java +29 -33
- package/android/src/main/java/com/getcapacitor/community/admob/rewarded/RewardAdPluginEvents.kt +2 -1
- package/android/src/main/java/com/getcapacitor/community/admob/rewarded/RewardedAdCallbackAndListeners.kt +1 -1
- package/android/src/main/java/com/getcapacitor/community/admob/rewardedinterstitial/AdRewardInterstitialExecutor.java +29 -37
- package/android/src/main/java/com/getcapacitor/community/admob/rewardedinterstitial/RewardedInterstitialAdCallbackAndListeners.kt +1 -1
- package/dist/docs.json +33 -3
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +5 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/interstitial/interstitial-definitions.interface.d.ts +3 -0
- package/dist/esm/native-ads/index.d.ts +6 -0
- package/dist/esm/native-ads/index.js +4 -0
- package/dist/esm/native-ads/index.js.map +1 -0
- package/dist/esm/native-ads/native-ad-definitions.interface.d.ts +14 -0
- package/dist/esm/native-ads/native-ad-definitions.interface.js +2 -0
- package/dist/esm/native-ads/native-ad-definitions.interface.js.map +1 -0
- package/dist/esm/native-ads/native-ad-element.d.ts +9 -0
- package/dist/esm/native-ads/native-ad-element.js +53 -0
- package/dist/esm/native-ads/native-ad-element.js.map +1 -0
- package/dist/esm/native-ads/native-ad-event.interface.d.ts +5 -0
- package/dist/esm/native-ads/native-ad-event.interface.js +2 -0
- package/dist/esm/native-ads/native-ad-event.interface.js.map +1 -0
- package/dist/esm/native-ads/native-ad-feed.d.ts +72 -0
- package/dist/esm/native-ads/native-ad-feed.js +568 -0
- package/dist/esm/native-ads/native-ad-feed.js.map +1 -0
- package/dist/esm/native-ads/native-ad-geometry.d.ts +8 -0
- package/dist/esm/native-ads/native-ad-geometry.js +91 -0
- package/dist/esm/native-ads/native-ad-geometry.js.map +1 -0
- package/dist/esm/native-ads/native-ad-options.interface.d.ts +36 -0
- package/dist/esm/native-ads/native-ad-options.interface.js +2 -0
- package/dist/esm/native-ads/native-ad-options.interface.js.map +1 -0
- package/dist/esm/native-ads/native-ad-placement.interface.d.ts +26 -0
- package/dist/esm/native-ads/native-ad-placement.interface.js +2 -0
- package/dist/esm/native-ads/native-ad-placement.interface.js.map +1 -0
- package/dist/esm/native-ads/native-ad-plugin-events.enum.d.ts +9 -0
- package/dist/esm/native-ads/native-ad-plugin-events.enum.js +11 -0
- package/dist/esm/native-ads/native-ad-plugin-events.enum.js.map +1 -0
- package/dist/esm/native-ads/native-ad-style.interface.d.ts +30 -0
- package/dist/esm/native-ads/native-ad-style.interface.js +2 -0
- package/dist/esm/native-ads/native-ad-style.interface.js.map +1 -0
- package/dist/esm/native-ads/native-ad-template.enum.d.ts +11 -0
- package/dist/esm/native-ads/native-ad-template.enum.js +13 -0
- package/dist/esm/native-ads/native-ad-template.enum.js.map +1 -0
- package/dist/esm/reward/reward-ad-plugin-events.enum.d.ts +4 -0
- package/dist/esm/reward/reward-ad-plugin-events.enum.js +4 -0
- package/dist/esm/reward/reward-ad-plugin-events.enum.js.map +1 -1
- package/dist/esm/reward/reward-definitions.interface.d.ts +8 -0
- package/dist/esm/reward-interstitial/reward-interstitial-definitions.interface.d.ts +3 -0
- package/dist/esm/web.d.ts +9 -1
- package/dist/esm/web.js +15 -0
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +755 -1
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +755 -1
- package/dist/plugin.js.map +1 -1
- package/docs/app-open.md +53 -0
- package/docs/banner.md +100 -0
- package/docs/configuration.md +30 -0
- package/docs/consent.md +80 -0
- package/docs/events.md +76 -0
- package/docs/interstitial.md +47 -0
- package/docs/migration.md +146 -0
- package/docs/native-ads.md +161 -0
- package/docs/rewarded.md +148 -0
- package/docs/testing.md +67 -0
- package/ios/Sources/AdMobPlugin/AdMobPlugin.swift +106 -41
- package/ios/Sources/AdMobPlugin/AppOpen/AppOpenAdManager.swift +1 -1
- package/ios/Sources/AdMobPlugin/Banner/BannerExecutor.swift +1 -1
- package/ios/Sources/AdMobPlugin/Interstitial/AdInterstitialExecutor.swift +6 -3
- package/ios/Sources/AdMobPlugin/NativeAds/NativeAdExecutor.swift +354 -0
- package/ios/Sources/AdMobPlugin/NativeAds/NativeAdFeedSessions.swift +34 -0
- package/ios/Sources/AdMobPlugin/NativeAds/NativeAdPlacementValue.swift +50 -0
- package/ios/Sources/AdMobPlugin/NativeAds/NativeAdPluginEvents.swift +11 -0
- package/ios/Sources/AdMobPlugin/NativeAds/NativeAdScrollTracker.swift +66 -0
- package/ios/Sources/AdMobPlugin/NativeAds/NativeAdValues.swift +27 -0
- package/ios/Sources/AdMobPlugin/NativeAds/PluginNativeAdView.swift +187 -0
- package/ios/Sources/AdMobPlugin/Rewarded/AdRewardExecutor.swift +10 -3
- package/ios/Sources/AdMobPlugin/Rewarded/RewardAdPluginEvents.swift +1 -0
- package/ios/Sources/AdMobPlugin/RewardedInterstitial/AdRewardInterstitialExecutor.swift +6 -3
- package/ios/Tests/AdMobPluginTests/AdMobPluginTests.swift +82 -10
- package/ios/Tests/AdMobPluginTests/NativeAdScrollTrackerTests.swift +47 -0
- package/package.json +4 -1
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { PluginListenerHandle } from '@capacitor/core';
|
|
2
|
+
import type { NativeAdDefinitions } from './native-ad-definitions.interface';
|
|
3
|
+
import type { NativeAdEvent, NativeAdErrorEvent, NativeAdRevenueEvent } from './native-ad-event.interface';
|
|
4
|
+
import type { NativeAdFeedOptions } from './native-ad-options.interface';
|
|
5
|
+
import type { NativeAdPluginEvents } from './native-ad-plugin-events.enum';
|
|
6
|
+
/**
|
|
7
|
+
* Experimental manager for native-ad integration research.
|
|
8
|
+
*
|
|
9
|
+
* A stable `slotKey`, rather than the element or array index, owns the native
|
|
10
|
+
* ad lifecycle. Virtual-scroller compatibility has not yet been validated.
|
|
11
|
+
*
|
|
12
|
+
* @experimental Native overlay gesture routing is still being validated on devices.
|
|
13
|
+
*/
|
|
14
|
+
export declare class NativeAdFeed {
|
|
15
|
+
private readonly options;
|
|
16
|
+
private readonly nativeScrollElement?;
|
|
17
|
+
private readonly sessionId;
|
|
18
|
+
private readonly slots;
|
|
19
|
+
private readonly elementSlots;
|
|
20
|
+
private readonly scrollRoots;
|
|
21
|
+
private readonly intersectionObserver?;
|
|
22
|
+
private readonly resizeObserver?;
|
|
23
|
+
private destroyed;
|
|
24
|
+
private nativeStarted;
|
|
25
|
+
private paused;
|
|
26
|
+
private layoutInvalidated;
|
|
27
|
+
private placementFrame?;
|
|
28
|
+
private lastPlacements?;
|
|
29
|
+
private lastPlacementWasDirect;
|
|
30
|
+
private placementAcknowledgementRequired;
|
|
31
|
+
private settleTimer?;
|
|
32
|
+
private placementUpdateTask?;
|
|
33
|
+
private operationTask;
|
|
34
|
+
private destroyTask?;
|
|
35
|
+
private updateDirty;
|
|
36
|
+
private placementSequence;
|
|
37
|
+
private readonly listenerHandles;
|
|
38
|
+
private constructor();
|
|
39
|
+
static create(options: NativeAdFeedOptions): Promise<NativeAdFeed>;
|
|
40
|
+
get feedId(): string;
|
|
41
|
+
addListener(eventName: NativeAdPluginEvents.Loaded | NativeAdPluginEvents.Clicked | NativeAdPluginEvents.AdImpression | NativeAdPluginEvents.Opened | NativeAdPluginEvents.Closed, listener: (event: NativeAdEvent) => void): Promise<PluginListenerHandle>;
|
|
42
|
+
addListener(eventName: NativeAdPluginEvents.FailedToLoad, listener: (event: NativeAdErrorEvent) => void): Promise<PluginListenerHandle>;
|
|
43
|
+
addListener(eventName: NativeAdPluginEvents.AdPaid, listener: (event: NativeAdRevenueEvent) => void): Promise<PluginListenerHandle>;
|
|
44
|
+
attach(slotKey: string, element: HTMLElement): void;
|
|
45
|
+
detach(element: HTMLElement): void;
|
|
46
|
+
reload(slotKey: string): Promise<void>;
|
|
47
|
+
/** Immediately hides this feed's overlays until {@link resume} is called. */
|
|
48
|
+
pause(): Promise<void>;
|
|
49
|
+
/** Re-evaluates slots and restores eligible overlays after the viewport settles. */
|
|
50
|
+
resume(): void;
|
|
51
|
+
/** Hides stale overlays and remeasures after an application-driven layout change. */
|
|
52
|
+
invalidateLayout(): Promise<void>;
|
|
53
|
+
destroy(): Promise<void>;
|
|
54
|
+
private readonly handleViewportMotion;
|
|
55
|
+
private schedulePlacementUpdate;
|
|
56
|
+
private updateScrollRoots;
|
|
57
|
+
private readonly handleVisibilityChange;
|
|
58
|
+
private scheduleEvaluation;
|
|
59
|
+
private evaluate;
|
|
60
|
+
private loadState;
|
|
61
|
+
private evictUnusedAds;
|
|
62
|
+
private occupiesNativeAd;
|
|
63
|
+
private getVisibleStates;
|
|
64
|
+
private isSupportedSize;
|
|
65
|
+
private removeStateAd;
|
|
66
|
+
private runOperation;
|
|
67
|
+
private destroyResources;
|
|
68
|
+
private requestPlacementUpdate;
|
|
69
|
+
private flushPlacementUpdates;
|
|
70
|
+
private assertActive;
|
|
71
|
+
}
|
|
72
|
+
export declare const configureNativeAdBridge: (adMobBridge: NativeAdDefinitions) => void;
|
|
@@ -0,0 +1,568 @@
|
|
|
1
|
+
import { Capacitor } from '@capacitor/core';
|
|
2
|
+
import { connectNativeAdElements, defineNativeAdElement } from './native-ad-element';
|
|
3
|
+
import { measureNativeAdSlot, measureNativeAdScrollContainer, nativeAdAncestors } from './native-ad-geometry';
|
|
4
|
+
import { NativeAdTemplate } from './native-ad-template.enum';
|
|
5
|
+
const SCROLL_SETTLE_MS = 120;
|
|
6
|
+
const MAX_NATIVE_ADS = 3;
|
|
7
|
+
const MAX_ACTIVE_FEEDS = 2;
|
|
8
|
+
const MIN_SMALL_SLOT_WIDTH = 120;
|
|
9
|
+
const MIN_MEDIUM_SLOT_WIDTH = 144;
|
|
10
|
+
const MIN_MEDIUM_SLOT_HEIGHT = 300;
|
|
11
|
+
const MIN_SMALL_SLOT_HEIGHT = 120;
|
|
12
|
+
const TEMPLATE_ATTRIBUTE = 'data-capacitor-admob-template';
|
|
13
|
+
const applySlotDefaults = (element, template) => {
|
|
14
|
+
const root = element.getRootNode();
|
|
15
|
+
const container = root instanceof ShadowRoot ? root : element.ownerDocument.head;
|
|
16
|
+
if (!container.querySelector('style[data-capacitor-admob-defaults]')) {
|
|
17
|
+
const style = element.ownerDocument.createElement('style');
|
|
18
|
+
style.setAttribute('data-capacitor-admob-defaults', '');
|
|
19
|
+
// Prefer the first cascade layer; older WebViews use zero-specificity rules.
|
|
20
|
+
const defaults = `
|
|
21
|
+
:where([${TEMPLATE_ATTRIBUTE}]) { display: block; height: 320px; }
|
|
22
|
+
:where([${TEMPLATE_ATTRIBUTE}="small"]) { height: 120px; }
|
|
23
|
+
`;
|
|
24
|
+
style.textContent =
|
|
25
|
+
'CSSLayerBlockRule' in globalThis ? `@layer capacitor-admob-defaults { ${defaults} }` : defaults;
|
|
26
|
+
container.prepend(style);
|
|
27
|
+
}
|
|
28
|
+
element.setAttribute(TEMPLATE_ATTRIBUTE, template);
|
|
29
|
+
};
|
|
30
|
+
let bridge;
|
|
31
|
+
let sessionSequence = 0;
|
|
32
|
+
const feeds = new Map();
|
|
33
|
+
const createSessionId = () => { var _a, _b, _c; return (_c = (_b = (_a = globalThis.crypto) === null || _a === void 0 ? void 0 : _a.randomUUID) === null || _b === void 0 ? void 0 : _b.call(_a)) !== null && _c !== void 0 ? _c : `${Date.now()}-${++sessionSequence}-${Math.random().toString(36).slice(2)}`; };
|
|
34
|
+
/**
|
|
35
|
+
* Experimental manager for native-ad integration research.
|
|
36
|
+
*
|
|
37
|
+
* A stable `slotKey`, rather than the element or array index, owns the native
|
|
38
|
+
* ad lifecycle. Virtual-scroller compatibility has not yet been validated.
|
|
39
|
+
*
|
|
40
|
+
* @experimental Native overlay gesture routing is still being validated on devices.
|
|
41
|
+
*/
|
|
42
|
+
export class NativeAdFeed {
|
|
43
|
+
constructor(options) {
|
|
44
|
+
var _a;
|
|
45
|
+
this.sessionId = createSessionId();
|
|
46
|
+
this.slots = new Map();
|
|
47
|
+
this.elementSlots = new Map();
|
|
48
|
+
this.scrollRoots = new Set();
|
|
49
|
+
this.destroyed = false;
|
|
50
|
+
this.nativeStarted = false;
|
|
51
|
+
this.paused = false;
|
|
52
|
+
this.layoutInvalidated = false;
|
|
53
|
+
this.lastPlacementWasDirect = false;
|
|
54
|
+
this.placementAcknowledgementRequired = false;
|
|
55
|
+
this.operationTask = Promise.resolve();
|
|
56
|
+
this.updateDirty = false;
|
|
57
|
+
this.placementSequence = 0;
|
|
58
|
+
this.listenerHandles = new Set();
|
|
59
|
+
this.handleViewportMotion = (event) => {
|
|
60
|
+
if (this.destroyed) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (!(this.nativeScrollElement &&
|
|
64
|
+
event instanceof Event &&
|
|
65
|
+
['scroll', 'touchmove', 'wheel'].includes(event.type))) {
|
|
66
|
+
this.schedulePlacementUpdate();
|
|
67
|
+
}
|
|
68
|
+
this.scheduleEvaluation();
|
|
69
|
+
};
|
|
70
|
+
this.handleVisibilityChange = () => {
|
|
71
|
+
if (this.destroyed) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
if (document.hidden) {
|
|
75
|
+
this.layoutInvalidated = true;
|
|
76
|
+
void this.requestPlacementUpdate();
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
this.scheduleEvaluation();
|
|
80
|
+
};
|
|
81
|
+
const id = options.feedId.trim();
|
|
82
|
+
if (!id) {
|
|
83
|
+
throw new Error('Native ad feed id must not be empty');
|
|
84
|
+
}
|
|
85
|
+
if (feeds.has(id)) {
|
|
86
|
+
throw new Error(`Native ad feed id already exists: ${id}`);
|
|
87
|
+
}
|
|
88
|
+
if (feeds.size >= MAX_ACTIVE_FEEDS) {
|
|
89
|
+
throw new Error(`At most ${MAX_ACTIVE_FEEDS} native ad feeds can be active`);
|
|
90
|
+
}
|
|
91
|
+
this.options = Object.assign(Object.assign({}, options), { feedId: id });
|
|
92
|
+
this.nativeScrollElement = Capacitor.getPlatform() === 'ios' ? options.scrollElement : undefined;
|
|
93
|
+
if (typeof IntersectionObserver !== 'undefined') {
|
|
94
|
+
this.intersectionObserver = new IntersectionObserver((entries) => {
|
|
95
|
+
for (const entry of entries) {
|
|
96
|
+
const slotKey = this.elementSlots.get(entry.target);
|
|
97
|
+
const state = slotKey ? this.slots.get(slotKey) : undefined;
|
|
98
|
+
if (state) {
|
|
99
|
+
state.intersecting = entry.isIntersecting;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
this.schedulePlacementUpdate();
|
|
103
|
+
this.scheduleEvaluation();
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
if (typeof ResizeObserver !== 'undefined') {
|
|
107
|
+
this.resizeObserver = new ResizeObserver(this.handleViewportMotion);
|
|
108
|
+
if (this.nativeScrollElement)
|
|
109
|
+
this.resizeObserver.observe(this.nativeScrollElement);
|
|
110
|
+
}
|
|
111
|
+
document.addEventListener('scroll', this.handleViewportMotion, true);
|
|
112
|
+
document.addEventListener('touchmove', this.handleViewportMotion, { capture: true, passive: true });
|
|
113
|
+
document.addEventListener('wheel', this.handleViewportMotion, { capture: true, passive: true });
|
|
114
|
+
window.addEventListener('resize', this.handleViewportMotion, { passive: true });
|
|
115
|
+
(_a = window.visualViewport) === null || _a === void 0 ? void 0 : _a.addEventListener('resize', this.handleViewportMotion, { passive: true });
|
|
116
|
+
document.addEventListener('visibilitychange', this.handleVisibilityChange);
|
|
117
|
+
document.addEventListener('load', this.handleViewportMotion, true);
|
|
118
|
+
feeds.set(id, this);
|
|
119
|
+
}
|
|
120
|
+
static async create(options) {
|
|
121
|
+
var _a;
|
|
122
|
+
if (!bridge) {
|
|
123
|
+
throw new Error('AdMob native ad bridge is not configured');
|
|
124
|
+
}
|
|
125
|
+
if (typeof document === 'undefined' || typeof window === 'undefined') {
|
|
126
|
+
throw new Error('NativeAdFeed can only be created in a browser environment');
|
|
127
|
+
}
|
|
128
|
+
if (options.isTesting !== true && !((_a = options.adId) === null || _a === void 0 ? void 0 : _a.trim())) {
|
|
129
|
+
throw new Error('adId is required unless isTesting is true');
|
|
130
|
+
}
|
|
131
|
+
const feed = new NativeAdFeed(options);
|
|
132
|
+
try {
|
|
133
|
+
await bridge.startNativeAdFeed({ feedId: feed.feedId, sessionId: feed.sessionId });
|
|
134
|
+
feed.nativeStarted = true;
|
|
135
|
+
defineNativeAdElement((feedId) => feeds.get(feedId));
|
|
136
|
+
connectNativeAdElements();
|
|
137
|
+
return feed;
|
|
138
|
+
}
|
|
139
|
+
catch (error) {
|
|
140
|
+
await feed.destroy();
|
|
141
|
+
throw error;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
get feedId() {
|
|
145
|
+
return this.options.feedId;
|
|
146
|
+
}
|
|
147
|
+
async addListener(eventName, listener) {
|
|
148
|
+
this.assertActive();
|
|
149
|
+
if (!bridge) {
|
|
150
|
+
throw new Error('AdMob native ad bridge is not configured');
|
|
151
|
+
}
|
|
152
|
+
const nativeHandle = await bridge.addListener(eventName, (event) => {
|
|
153
|
+
if (event.feedId === this.options.feedId && event.sessionId === this.sessionId) {
|
|
154
|
+
const filteredListener = listener;
|
|
155
|
+
filteredListener(event);
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
if (this.destroyed) {
|
|
159
|
+
await nativeHandle.remove();
|
|
160
|
+
this.assertActive();
|
|
161
|
+
}
|
|
162
|
+
let removed = false;
|
|
163
|
+
const handle = {
|
|
164
|
+
remove: async () => {
|
|
165
|
+
if (removed) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
removed = true;
|
|
169
|
+
this.listenerHandles.delete(handle);
|
|
170
|
+
await nativeHandle.remove();
|
|
171
|
+
},
|
|
172
|
+
};
|
|
173
|
+
this.listenerHandles.add(handle);
|
|
174
|
+
return handle;
|
|
175
|
+
}
|
|
176
|
+
attach(slotKey, element) {
|
|
177
|
+
var _a, _b, _c;
|
|
178
|
+
this.assertActive();
|
|
179
|
+
const normalizedKey = slotKey.trim();
|
|
180
|
+
if (!normalizedKey) {
|
|
181
|
+
throw new Error('Native ad slotKey must not be empty');
|
|
182
|
+
}
|
|
183
|
+
const previousKey = this.elementSlots.get(element);
|
|
184
|
+
if (previousKey === normalizedKey) {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
if (previousKey && previousKey !== normalizedKey) {
|
|
188
|
+
this.detach(element);
|
|
189
|
+
}
|
|
190
|
+
const existing = this.slots.get(normalizedKey);
|
|
191
|
+
if ((existing === null || existing === void 0 ? void 0 : existing.element) && existing.element !== element) {
|
|
192
|
+
throw new Error(`Native ad slotKey is already attached: ${normalizedKey}`);
|
|
193
|
+
}
|
|
194
|
+
const state = existing !== null && existing !== void 0 ? existing : {
|
|
195
|
+
slotKey: normalizedKey,
|
|
196
|
+
generation: 0,
|
|
197
|
+
intersecting: !this.intersectionObserver,
|
|
198
|
+
status: 'idle',
|
|
199
|
+
lastUsed: Date.now(),
|
|
200
|
+
loadVersion: 0,
|
|
201
|
+
};
|
|
202
|
+
applySlotDefaults(element, (_a = this.options.template) !== null && _a !== void 0 ? _a : NativeAdTemplate.Medium);
|
|
203
|
+
state.element = element;
|
|
204
|
+
state.generation += 1;
|
|
205
|
+
state.lastUsed = Date.now();
|
|
206
|
+
this.slots.set(normalizedKey, state);
|
|
207
|
+
this.elementSlots.set(element, normalizedKey);
|
|
208
|
+
this.updateScrollRoots();
|
|
209
|
+
(_b = this.intersectionObserver) === null || _b === void 0 ? void 0 : _b.observe(element);
|
|
210
|
+
(_c = this.resizeObserver) === null || _c === void 0 ? void 0 : _c.observe(element);
|
|
211
|
+
this.scheduleEvaluation();
|
|
212
|
+
}
|
|
213
|
+
detach(element) {
|
|
214
|
+
var _a, _b;
|
|
215
|
+
const slotKey = this.elementSlots.get(element);
|
|
216
|
+
if (!slotKey) {
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
const state = this.slots.get(slotKey);
|
|
220
|
+
if ((state === null || state === void 0 ? void 0 : state.element) === element) {
|
|
221
|
+
state.element = undefined;
|
|
222
|
+
state.intersecting = false;
|
|
223
|
+
state.generation += 1;
|
|
224
|
+
state.lastUsed = Date.now();
|
|
225
|
+
}
|
|
226
|
+
(_a = this.intersectionObserver) === null || _a === void 0 ? void 0 : _a.unobserve(element);
|
|
227
|
+
(_b = this.resizeObserver) === null || _b === void 0 ? void 0 : _b.unobserve(element);
|
|
228
|
+
this.elementSlots.delete(element);
|
|
229
|
+
this.updateScrollRoots();
|
|
230
|
+
void this.requestPlacementUpdate();
|
|
231
|
+
this.scheduleEvaluation();
|
|
232
|
+
}
|
|
233
|
+
async reload(slotKey) {
|
|
234
|
+
this.assertActive();
|
|
235
|
+
const normalizedKey = slotKey.trim();
|
|
236
|
+
const state = this.slots.get(normalizedKey);
|
|
237
|
+
if (!state) {
|
|
238
|
+
throw new Error(`Native ad slotKey is not registered: ${normalizedKey}`);
|
|
239
|
+
}
|
|
240
|
+
await this.runOperation(async () => {
|
|
241
|
+
await this.removeStateAd(state);
|
|
242
|
+
const protectedStates = new Set(this.getVisibleStates());
|
|
243
|
+
protectedStates.add(state);
|
|
244
|
+
await this.evictUnusedAds(protectedStates);
|
|
245
|
+
if ([...this.slots.values()].filter((slot) => this.occupiesNativeAd(slot)).length >= MAX_NATIVE_ADS) {
|
|
246
|
+
throw new Error('Native ad capacity is currently in use by visible slots');
|
|
247
|
+
}
|
|
248
|
+
await this.loadState(state);
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
/** Immediately hides this feed's overlays until {@link resume} is called. */
|
|
252
|
+
async pause() {
|
|
253
|
+
this.assertActive();
|
|
254
|
+
this.paused = true;
|
|
255
|
+
this.layoutInvalidated = true;
|
|
256
|
+
if (!(await this.requestPlacementUpdate())) {
|
|
257
|
+
throw new Error('Failed to hide native ad placements');
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
/** Re-evaluates slots and restores eligible overlays after the viewport settles. */
|
|
261
|
+
resume() {
|
|
262
|
+
this.assertActive();
|
|
263
|
+
this.paused = false;
|
|
264
|
+
this.scheduleEvaluation();
|
|
265
|
+
}
|
|
266
|
+
/** Hides stale overlays and remeasures after an application-driven layout change. */
|
|
267
|
+
async invalidateLayout() {
|
|
268
|
+
this.assertActive();
|
|
269
|
+
this.layoutInvalidated = true;
|
|
270
|
+
this.scheduleEvaluation();
|
|
271
|
+
if (!(await this.requestPlacementUpdate())) {
|
|
272
|
+
throw new Error('Failed to hide native ad placements');
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
async destroy() {
|
|
276
|
+
var _a, _b, _c;
|
|
277
|
+
if (!this.destroyed) {
|
|
278
|
+
this.destroyed = true;
|
|
279
|
+
feeds.delete(this.options.feedId);
|
|
280
|
+
document.removeEventListener('scroll', this.handleViewportMotion, true);
|
|
281
|
+
for (const root of this.scrollRoots) {
|
|
282
|
+
root.removeEventListener('scroll', this.handleViewportMotion, true);
|
|
283
|
+
}
|
|
284
|
+
this.scrollRoots.clear();
|
|
285
|
+
document.removeEventListener('touchmove', this.handleViewportMotion, true);
|
|
286
|
+
document.removeEventListener('wheel', this.handleViewportMotion, true);
|
|
287
|
+
window.removeEventListener('resize', this.handleViewportMotion);
|
|
288
|
+
(_a = window.visualViewport) === null || _a === void 0 ? void 0 : _a.removeEventListener('resize', this.handleViewportMotion);
|
|
289
|
+
document.removeEventListener('visibilitychange', this.handleVisibilityChange);
|
|
290
|
+
document.removeEventListener('load', this.handleViewportMotion, true);
|
|
291
|
+
if (this.settleTimer) {
|
|
292
|
+
clearTimeout(this.settleTimer);
|
|
293
|
+
}
|
|
294
|
+
if (this.placementFrame !== undefined) {
|
|
295
|
+
cancelAnimationFrame(this.placementFrame);
|
|
296
|
+
this.placementFrame = undefined;
|
|
297
|
+
}
|
|
298
|
+
(_b = this.intersectionObserver) === null || _b === void 0 ? void 0 : _b.disconnect();
|
|
299
|
+
(_c = this.resizeObserver) === null || _c === void 0 ? void 0 : _c.disconnect();
|
|
300
|
+
}
|
|
301
|
+
if (this.destroyTask) {
|
|
302
|
+
return this.destroyTask;
|
|
303
|
+
}
|
|
304
|
+
this.destroyTask = this.destroyResources();
|
|
305
|
+
try {
|
|
306
|
+
await this.destroyTask;
|
|
307
|
+
}
|
|
308
|
+
finally {
|
|
309
|
+
this.destroyTask = undefined;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
schedulePlacementUpdate() {
|
|
313
|
+
if (this.destroyed || this.placementFrame !== undefined) {
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
this.placementFrame = requestAnimationFrame(() => {
|
|
317
|
+
this.placementFrame = undefined;
|
|
318
|
+
void this.requestPlacementUpdate(true);
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
updateScrollRoots() {
|
|
322
|
+
const roots = new Set();
|
|
323
|
+
for (const element of this.elementSlots.keys()) {
|
|
324
|
+
for (const ancestor of [element, ...nativeAdAncestors(element)]) {
|
|
325
|
+
const root = ancestor.getRootNode();
|
|
326
|
+
if (root instanceof ShadowRoot)
|
|
327
|
+
roots.add(root);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
for (const root of this.scrollRoots) {
|
|
331
|
+
if (!roots.has(root)) {
|
|
332
|
+
root.removeEventListener('scroll', this.handleViewportMotion, true);
|
|
333
|
+
this.scrollRoots.delete(root);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
for (const root of roots) {
|
|
337
|
+
if (!this.scrollRoots.has(root)) {
|
|
338
|
+
// Element scroll events do not cross a shadow boundary.
|
|
339
|
+
root.addEventListener('scroll', this.handleViewportMotion, true);
|
|
340
|
+
this.scrollRoots.add(root);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
scheduleEvaluation() {
|
|
345
|
+
if (this.destroyed) {
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
if (this.settleTimer) {
|
|
349
|
+
clearTimeout(this.settleTimer);
|
|
350
|
+
}
|
|
351
|
+
this.settleTimer = setTimeout(() => {
|
|
352
|
+
this.layoutInvalidated = false;
|
|
353
|
+
void this.runOperation(() => this.evaluate());
|
|
354
|
+
}, SCROLL_SETTLE_MS);
|
|
355
|
+
}
|
|
356
|
+
async evaluate() {
|
|
357
|
+
if (this.destroyed || this.paused || document.hidden) {
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
const visibleStates = this.getVisibleStates();
|
|
361
|
+
for (const state of visibleStates) {
|
|
362
|
+
state.lastUsed = Date.now();
|
|
363
|
+
}
|
|
364
|
+
await this.evictUnusedAds(new Set(visibleStates));
|
|
365
|
+
const occupied = [...this.slots.values()].filter((state) => this.occupiesNativeAd(state)).length;
|
|
366
|
+
const loadable = visibleStates
|
|
367
|
+
.filter((state) => state.status === 'idle')
|
|
368
|
+
.slice(0, Math.max(0, MAX_NATIVE_ADS - occupied));
|
|
369
|
+
await Promise.allSettled(loadable.map((state) => this.loadState(state)));
|
|
370
|
+
void this.requestPlacementUpdate();
|
|
371
|
+
}
|
|
372
|
+
async loadState(state) {
|
|
373
|
+
var _a;
|
|
374
|
+
if (state.status !== 'idle' || !bridge) {
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
state.status = 'loading';
|
|
378
|
+
const loadVersion = ++state.loadVersion;
|
|
379
|
+
try {
|
|
380
|
+
await bridge.loadNativeAd({
|
|
381
|
+
feedId: this.options.feedId,
|
|
382
|
+
sessionId: this.sessionId,
|
|
383
|
+
slotKey: state.slotKey,
|
|
384
|
+
template: (_a = this.options.template) !== null && _a !== void 0 ? _a : NativeAdTemplate.Medium,
|
|
385
|
+
style: this.options.style,
|
|
386
|
+
adId: this.options.adId,
|
|
387
|
+
isTesting: this.options.isTesting,
|
|
388
|
+
npa: this.options.npa,
|
|
389
|
+
});
|
|
390
|
+
if (state.loadVersion === loadVersion) {
|
|
391
|
+
state.status = 'loaded';
|
|
392
|
+
state.lastUsed = Date.now();
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
catch (error) {
|
|
396
|
+
if (state.loadVersion === loadVersion) {
|
|
397
|
+
state.status = 'failed';
|
|
398
|
+
}
|
|
399
|
+
throw error;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
async evictUnusedAds(visibleStates) {
|
|
403
|
+
const occupied = [...this.slots.values()].filter((state) => this.occupiesNativeAd(state));
|
|
404
|
+
const requestedLoads = [...visibleStates].filter((state) => state.status === 'idle').length;
|
|
405
|
+
const evictionCount = Math.max(0, occupied.length + Math.min(requestedLoads, MAX_NATIVE_ADS) - MAX_NATIVE_ADS);
|
|
406
|
+
const evicted = occupied
|
|
407
|
+
.filter((state) => !visibleStates.has(state))
|
|
408
|
+
.sort((left, right) => left.lastUsed - right.lastUsed)
|
|
409
|
+
.slice(0, evictionCount);
|
|
410
|
+
await Promise.allSettled(evicted.map((state) => this.removeStateAd(state)));
|
|
411
|
+
const detached = [...this.slots.values()]
|
|
412
|
+
.filter((state) => !state.element && !this.occupiesNativeAd(state))
|
|
413
|
+
.sort((left, right) => right.lastUsed - left.lastUsed);
|
|
414
|
+
for (const state of detached.slice(MAX_NATIVE_ADS)) {
|
|
415
|
+
this.slots.delete(state.slotKey);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
occupiesNativeAd(state) {
|
|
419
|
+
return ['loading', 'loaded', 'removing', 'removeFailed'].includes(state.status);
|
|
420
|
+
}
|
|
421
|
+
getVisibleStates() {
|
|
422
|
+
return [...this.slots.values()].filter((state) => {
|
|
423
|
+
const measured = state.element && state.intersecting ? measureNativeAdSlot(state.element) : undefined;
|
|
424
|
+
return Boolean(measured && this.isSupportedSize(measured));
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
isSupportedSize(measured) {
|
|
428
|
+
var _a, _b;
|
|
429
|
+
const minimumHeight = ((_a = this.options.template) !== null && _a !== void 0 ? _a : NativeAdTemplate.Medium) === NativeAdTemplate.Small
|
|
430
|
+
? MIN_SMALL_SLOT_HEIGHT
|
|
431
|
+
: MIN_MEDIUM_SLOT_HEIGHT;
|
|
432
|
+
const minimumWidth = ((_b = this.options.template) !== null && _b !== void 0 ? _b : NativeAdTemplate.Medium) === NativeAdTemplate.Small
|
|
433
|
+
? MIN_SMALL_SLOT_WIDTH
|
|
434
|
+
: MIN_MEDIUM_SLOT_WIDTH;
|
|
435
|
+
return measured.rect.width >= minimumWidth && measured.rect.height >= minimumHeight;
|
|
436
|
+
}
|
|
437
|
+
async removeStateAd(state) {
|
|
438
|
+
if (state.status === 'removing') {
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
if (!bridge || state.status === 'idle' || state.status === 'failed') {
|
|
442
|
+
state.status = 'idle';
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
state.status = 'removing';
|
|
446
|
+
const removeVersion = ++state.loadVersion;
|
|
447
|
+
await this.requestPlacementUpdate();
|
|
448
|
+
try {
|
|
449
|
+
await bridge.removeNativeAd({
|
|
450
|
+
feedId: this.options.feedId,
|
|
451
|
+
sessionId: this.sessionId,
|
|
452
|
+
slotKey: state.slotKey,
|
|
453
|
+
});
|
|
454
|
+
if (state.loadVersion === removeVersion) {
|
|
455
|
+
state.status = 'idle';
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
catch (error) {
|
|
459
|
+
if (state.loadVersion === removeVersion) {
|
|
460
|
+
state.status = 'removeFailed';
|
|
461
|
+
}
|
|
462
|
+
throw error;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
runOperation(operation) {
|
|
466
|
+
const result = this.operationTask.then(operation);
|
|
467
|
+
this.operationTask = result.catch(() => undefined);
|
|
468
|
+
return result;
|
|
469
|
+
}
|
|
470
|
+
async destroyResources() {
|
|
471
|
+
await Promise.all([
|
|
472
|
+
...[...this.listenerHandles].map((handle) => handle.remove()),
|
|
473
|
+
...(this.nativeStarted && bridge
|
|
474
|
+
? [bridge.destroyNativeAdFeed({ feedId: this.options.feedId, sessionId: this.sessionId })]
|
|
475
|
+
: []),
|
|
476
|
+
]);
|
|
477
|
+
this.nativeStarted = false;
|
|
478
|
+
this.listenerHandles.clear();
|
|
479
|
+
this.slots.clear();
|
|
480
|
+
this.elementSlots.clear();
|
|
481
|
+
}
|
|
482
|
+
requestPlacementUpdate(allowDirect = false) {
|
|
483
|
+
this.updateDirty = true;
|
|
484
|
+
this.placementAcknowledgementRequired || (this.placementAcknowledgementRequired = !allowDirect);
|
|
485
|
+
if (!this.placementUpdateTask) {
|
|
486
|
+
this.placementUpdateTask = Promise.resolve().then(() => this.flushPlacementUpdates());
|
|
487
|
+
}
|
|
488
|
+
return this.placementUpdateTask;
|
|
489
|
+
}
|
|
490
|
+
async flushPlacementUpdates() {
|
|
491
|
+
if (!bridge || this.destroyed) {
|
|
492
|
+
return true;
|
|
493
|
+
}
|
|
494
|
+
try {
|
|
495
|
+
while (this.updateDirty && !this.destroyed) {
|
|
496
|
+
this.updateDirty = false;
|
|
497
|
+
const allowDirect = !this.placementAcknowledgementRequired;
|
|
498
|
+
this.placementAcknowledgementRequired = false;
|
|
499
|
+
const placements = [...this.slots.values()]
|
|
500
|
+
.filter((state) => state.status === 'loaded')
|
|
501
|
+
.map((state) => {
|
|
502
|
+
const measured = state.element ? measureNativeAdSlot(state.element, this.nativeScrollElement) : undefined;
|
|
503
|
+
const visible = Boolean(!this.paused && !this.layoutInvalidated && measured && this.isSupportedSize(measured) && !document.hidden);
|
|
504
|
+
return {
|
|
505
|
+
feedId: this.options.feedId,
|
|
506
|
+
slotKey: state.slotKey,
|
|
507
|
+
generation: state.generation,
|
|
508
|
+
visible,
|
|
509
|
+
rect: measured === null || measured === void 0 ? void 0 : measured.rect,
|
|
510
|
+
clipRect: measured === null || measured === void 0 ? void 0 : measured.clipRect,
|
|
511
|
+
};
|
|
512
|
+
});
|
|
513
|
+
const scrollContainer = this.nativeScrollElement
|
|
514
|
+
? measureNativeAdScrollContainer(this.nativeScrollElement)
|
|
515
|
+
: undefined;
|
|
516
|
+
const serializedPlacements = JSON.stringify({ placements, scrollContainer });
|
|
517
|
+
if (serializedPlacements === this.lastPlacements && (allowDirect || !this.lastPlacementWasDirect)) {
|
|
518
|
+
continue;
|
|
519
|
+
}
|
|
520
|
+
try {
|
|
521
|
+
const batch = Object.assign({ feedId: this.options.feedId, sessionId: this.sessionId, sequence: ++this.placementSequence, placements }, (scrollContainer ? { scrollContainer } : {}));
|
|
522
|
+
const channel = Capacitor.getPlatform() === 'android'
|
|
523
|
+
? window
|
|
524
|
+
.capacitorAdMobPlacements
|
|
525
|
+
: undefined;
|
|
526
|
+
let sentDirectly = false;
|
|
527
|
+
if (allowDirect && channel) {
|
|
528
|
+
try {
|
|
529
|
+
channel.postMessage(JSON.stringify(batch));
|
|
530
|
+
sentDirectly = true;
|
|
531
|
+
}
|
|
532
|
+
catch (_a) {
|
|
533
|
+
// Keep the acknowledged bridge available if the direct channel fails.
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
if (!sentDirectly)
|
|
537
|
+
await bridge.updateNativeAdPlacements(batch);
|
|
538
|
+
this.lastPlacementWasDirect = sentDirectly;
|
|
539
|
+
this.lastPlacements = serializedPlacements;
|
|
540
|
+
}
|
|
541
|
+
catch (_b) {
|
|
542
|
+
this.lastPlacements = undefined;
|
|
543
|
+
// Retry on the next scroll, resize, or lifecycle update without
|
|
544
|
+
// creating an unhandled rejection from this fire-and-forget task.
|
|
545
|
+
this.updateDirty = true;
|
|
546
|
+
return false;
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
return true;
|
|
550
|
+
}
|
|
551
|
+
catch (_c) {
|
|
552
|
+
this.updateDirty = true;
|
|
553
|
+
return false;
|
|
554
|
+
}
|
|
555
|
+
finally {
|
|
556
|
+
this.placementUpdateTask = undefined;
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
assertActive() {
|
|
560
|
+
if (this.destroyed) {
|
|
561
|
+
throw new Error(`Native ad feed has been destroyed: ${this.options.feedId}`);
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
export const configureNativeAdBridge = (adMobBridge) => {
|
|
566
|
+
bridge = adMobBridge;
|
|
567
|
+
};
|
|
568
|
+
//# sourceMappingURL=native-ad-feed.js.map
|