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