@chipmobilesdk/rn-ads 0.5.0 → 0.5.1
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/CHANGELOG.md +224 -214
- package/README.md +884 -881
- package/package.json +46 -49
- package/skills/sdk-rn-ads/SKILL.md +273 -274
- package/src/config/resolveAdUnit.ts +85 -85
- package/src/config/types.ts +318 -321
- package/src/config/validate.ts +1 -4
- package/src/core/adOriginatedReturn.ts +134 -134
- package/src/core/adsInstance.ts +1929 -1933
- package/src/core/consentFlow.ts +182 -180
- package/src/core/enablement.ts +76 -76
- package/src/core/fullScreenLock.ts +54 -54
- package/src/core/gates.ts +94 -91
- package/src/core/initMachine.ts +187 -187
- package/src/core/placementMachine.ts +124 -124
- package/src/core/privacyGate.ts +93 -94
- package/src/core/retryPolicy.ts +127 -127
- package/src/core/rewardedSession.ts +84 -84
- package/src/core/timers.ts +216 -208
- package/src/diagnostics/correlation.ts +31 -31
- package/src/diagnostics/errors.ts +230 -234
- package/src/diagnostics/events.ts +238 -232
- package/src/engine/admobEngine.ts +399 -404
- package/src/engine/port.ts +144 -144
- package/src/engine/testAdUnits.ts +61 -61
- package/src/react/AdsBanner.tsx +337 -340
- package/src/react/AdsProvider.tsx +57 -57
- package/src/react/hooks.ts +75 -75
- package/src/testing/index.ts +463 -463
|
@@ -1,57 +1,57 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Instance context (US1).
|
|
3
|
-
*
|
|
4
|
-
* Supplies one `AdsInstance` to the tree so `AdsBanner` and the hooks can reach
|
|
5
|
-
* it without every screen threading it through props.
|
|
6
|
-
*
|
|
7
|
-
* The provider does not create the instance. The app does, and passes it in —
|
|
8
|
-
* because `createAds` throws on a misconfigured catalog (FR-008, FR-012), and a
|
|
9
|
-
* throw during render is a far worse place to discover that than a throw at
|
|
10
|
-
* startup where the app can decide what to do about it.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import React, { createContext, useContext, useEffect, type ReactNode } from 'react';
|
|
14
|
-
import { AppState, type AppStateStatus } from 'react-native';
|
|
15
|
-
|
|
16
|
-
import type { AdsInstance } from '../core/adsInstance';
|
|
17
|
-
|
|
18
|
-
const AdsContext = createContext<AdsInstance | null>(null);
|
|
19
|
-
|
|
20
|
-
export interface AdsProviderProps {
|
|
21
|
-
readonly instance: AdsInstance;
|
|
22
|
-
/**
|
|
23
|
-
* Whether the provider forwards foreground/background transitions to the
|
|
24
|
-
* instance. On by default: FR-034's "never show while inactive" and
|
|
25
|
-
* "re-evaluate a stale ad on return" both depend on the instance being told.
|
|
26
|
-
* Turn it off only when the app already drives `setAppActive` itself.
|
|
27
|
-
*/
|
|
28
|
-
readonly trackAppState?: boolean;
|
|
29
|
-
readonly children: ReactNode;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export function AdsProvider({ instance, trackAppState = true, children }: AdsProviderProps) {
|
|
33
|
-
useEffect(() => {
|
|
34
|
-
if (!trackAppState) {
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
const subscription = AppState.addEventListener('change', (status: AppStateStatus) => {
|
|
38
|
-
instance.setAppActive(status === 'active');
|
|
39
|
-
});
|
|
40
|
-
return () => {
|
|
41
|
-
subscription.remove();
|
|
42
|
-
};
|
|
43
|
-
}, [instance, trackAppState]);
|
|
44
|
-
|
|
45
|
-
return <AdsContext.Provider value={instance}>{children}</AdsContext.Provider>;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Returns the instance, or null outside a provider.
|
|
50
|
-
*
|
|
51
|
-
* Null rather than a throw on purpose: a screen that renders a banner should
|
|
52
|
-
* degrade to rendering nothing when ads are not wired up, not take the screen
|
|
53
|
-
* down with it (NFR-005).
|
|
54
|
-
*/
|
|
55
|
-
export function useAds(): AdsInstance | null {
|
|
56
|
-
return useContext(AdsContext);
|
|
57
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Instance context (US1).
|
|
3
|
+
*
|
|
4
|
+
* Supplies one `AdsInstance` to the tree so `AdsBanner` and the hooks can reach
|
|
5
|
+
* it without every screen threading it through props.
|
|
6
|
+
*
|
|
7
|
+
* The provider does not create the instance. The app does, and passes it in —
|
|
8
|
+
* because `createAds` throws on a misconfigured catalog (FR-008, FR-012), and a
|
|
9
|
+
* throw during render is a far worse place to discover that than a throw at
|
|
10
|
+
* startup where the app can decide what to do about it.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import React, { createContext, useContext, useEffect, type ReactNode } from 'react';
|
|
14
|
+
import { AppState, type AppStateStatus } from 'react-native';
|
|
15
|
+
|
|
16
|
+
import type { AdsInstance } from '../core/adsInstance';
|
|
17
|
+
|
|
18
|
+
const AdsContext = createContext<AdsInstance | null>(null);
|
|
19
|
+
|
|
20
|
+
export interface AdsProviderProps {
|
|
21
|
+
readonly instance: AdsInstance;
|
|
22
|
+
/**
|
|
23
|
+
* Whether the provider forwards foreground/background transitions to the
|
|
24
|
+
* instance. On by default: FR-034's "never show while inactive" and
|
|
25
|
+
* "re-evaluate a stale ad on return" both depend on the instance being told.
|
|
26
|
+
* Turn it off only when the app already drives `setAppActive` itself.
|
|
27
|
+
*/
|
|
28
|
+
readonly trackAppState?: boolean;
|
|
29
|
+
readonly children: ReactNode;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function AdsProvider({ instance, trackAppState = true, children }: AdsProviderProps) {
|
|
33
|
+
useEffect(() => {
|
|
34
|
+
if (!trackAppState) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const subscription = AppState.addEventListener('change', (status: AppStateStatus) => {
|
|
38
|
+
instance.setAppActive(status === 'active');
|
|
39
|
+
});
|
|
40
|
+
return () => {
|
|
41
|
+
subscription.remove();
|
|
42
|
+
};
|
|
43
|
+
}, [instance, trackAppState]);
|
|
44
|
+
|
|
45
|
+
return <AdsContext.Provider value={instance}>{children}</AdsContext.Provider>;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Returns the instance, or null outside a provider.
|
|
50
|
+
*
|
|
51
|
+
* Null rather than a throw on purpose: a screen that renders a banner should
|
|
52
|
+
* degrade to rendering nothing when ads are not wired up, not take the screen
|
|
53
|
+
* down with it (NFR-005).
|
|
54
|
+
*/
|
|
55
|
+
export function useAds(): AdsInstance | null {
|
|
56
|
+
return useContext(AdsContext);
|
|
57
|
+
}
|
package/src/react/hooks.ts
CHANGED
|
@@ -1,75 +1,75 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* React bindings (US1, FR-027).
|
|
3
|
-
*
|
|
4
|
-
* Each hook subscribes to exactly what it reads. `usePlacementState` subscribes
|
|
5
|
-
* to one placement rather than to the whole event stream, which is what keeps a
|
|
6
|
-
* banner from re-rendering because an unrelated interstitial was clicked —
|
|
7
|
-
* PERF-002's bounded re-render assertion depends on it.
|
|
8
|
-
*
|
|
9
|
-
* None of these hand back an ad object. A consumer queries and subscribes to
|
|
10
|
-
* state; it never holds provider-owned memory (FR-027).
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import { useEffect, useState } from 'react';
|
|
14
|
-
|
|
15
|
-
import type { InitStatus, PlacementState } from '../config/types';
|
|
16
|
-
import type { AdEventListener } from '../diagnostics/events';
|
|
17
|
-
import { useAds } from './AdsProvider';
|
|
18
|
-
|
|
19
|
-
/** Live state for one placement. `unavailable` outside a provider. */
|
|
20
|
-
export function usePlacementState(placementKey: string): PlacementState {
|
|
21
|
-
const ads = useAds();
|
|
22
|
-
const [state, setState] = useState<PlacementState>(() =>
|
|
23
|
-
ads ? ads.getPlacementState(placementKey) : 'unavailable',
|
|
24
|
-
);
|
|
25
|
-
|
|
26
|
-
useEffect(() => {
|
|
27
|
-
if (!ads) {
|
|
28
|
-
setState('unavailable');
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
// Re-read on subscribe: the state may have moved between the initial
|
|
32
|
-
// render and this effect, and dropping that transition would leave the
|
|
33
|
-
// UI showing a state the instance has already left.
|
|
34
|
-
setState(ads.getPlacementState(placementKey));
|
|
35
|
-
return ads.subscribePlacement(placementKey, setState);
|
|
36
|
-
}, [ads, placementKey]);
|
|
37
|
-
|
|
38
|
-
return state;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/** Live initialization status. `unavailable` outside a provider. */
|
|
42
|
-
export function useAdsStatus(): InitStatus {
|
|
43
|
-
const ads = useAds();
|
|
44
|
-
const [status, setStatus] = useState<InitStatus>(() => (ads ? ads.getStatus() : 'unavailable'));
|
|
45
|
-
|
|
46
|
-
useEffect(() => {
|
|
47
|
-
if (!ads) {
|
|
48
|
-
setStatus('unavailable');
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
setStatus(ads.getStatus());
|
|
52
|
-
return ads.subscribeStatus(setStatus);
|
|
53
|
-
}, [ads]);
|
|
54
|
-
|
|
55
|
-
return status;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Forwards every event to a listener without causing a render.
|
|
60
|
-
*
|
|
61
|
-
* Deliberately does not accumulate events into state: a package that kept an
|
|
62
|
-
* event log would be persisting exactly the kind of history it promises not to
|
|
63
|
-
* (FR-021), and re-rendering on every impression would defeat PERF-002. A screen
|
|
64
|
-
* that wants a visible log keeps its own.
|
|
65
|
-
*/
|
|
66
|
-
export function useAdsEvents(listener: AdEventListener): void {
|
|
67
|
-
const ads = useAds();
|
|
68
|
-
|
|
69
|
-
useEffect(() => {
|
|
70
|
-
if (!ads) {
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
return ads.subscribeEvents(listener);
|
|
74
|
-
}, [ads, listener]);
|
|
75
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* React bindings (US1, FR-027).
|
|
3
|
+
*
|
|
4
|
+
* Each hook subscribes to exactly what it reads. `usePlacementState` subscribes
|
|
5
|
+
* to one placement rather than to the whole event stream, which is what keeps a
|
|
6
|
+
* banner from re-rendering because an unrelated interstitial was clicked —
|
|
7
|
+
* PERF-002's bounded re-render assertion depends on it.
|
|
8
|
+
*
|
|
9
|
+
* None of these hand back an ad object. A consumer queries and subscribes to
|
|
10
|
+
* state; it never holds provider-owned memory (FR-027).
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { useEffect, useState } from 'react';
|
|
14
|
+
|
|
15
|
+
import type { InitStatus, PlacementState } from '../config/types';
|
|
16
|
+
import type { AdEventListener } from '../diagnostics/events';
|
|
17
|
+
import { useAds } from './AdsProvider';
|
|
18
|
+
|
|
19
|
+
/** Live state for one placement. `unavailable` outside a provider. */
|
|
20
|
+
export function usePlacementState(placementKey: string): PlacementState {
|
|
21
|
+
const ads = useAds();
|
|
22
|
+
const [state, setState] = useState<PlacementState>(() =>
|
|
23
|
+
ads ? ads.getPlacementState(placementKey) : 'unavailable',
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
if (!ads) {
|
|
28
|
+
setState('unavailable');
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
// Re-read on subscribe: the state may have moved between the initial
|
|
32
|
+
// render and this effect, and dropping that transition would leave the
|
|
33
|
+
// UI showing a state the instance has already left.
|
|
34
|
+
setState(ads.getPlacementState(placementKey));
|
|
35
|
+
return ads.subscribePlacement(placementKey, setState);
|
|
36
|
+
}, [ads, placementKey]);
|
|
37
|
+
|
|
38
|
+
return state;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Live initialization status. `unavailable` outside a provider. */
|
|
42
|
+
export function useAdsStatus(): InitStatus {
|
|
43
|
+
const ads = useAds();
|
|
44
|
+
const [status, setStatus] = useState<InitStatus>(() => (ads ? ads.getStatus() : 'unavailable'));
|
|
45
|
+
|
|
46
|
+
useEffect(() => {
|
|
47
|
+
if (!ads) {
|
|
48
|
+
setStatus('unavailable');
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
setStatus(ads.getStatus());
|
|
52
|
+
return ads.subscribeStatus(setStatus);
|
|
53
|
+
}, [ads]);
|
|
54
|
+
|
|
55
|
+
return status;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Forwards every event to a listener without causing a render.
|
|
60
|
+
*
|
|
61
|
+
* Deliberately does not accumulate events into state: a package that kept an
|
|
62
|
+
* event log would be persisting exactly the kind of history it promises not to
|
|
63
|
+
* (FR-021), and re-rendering on every impression would defeat PERF-002. A screen
|
|
64
|
+
* that wants a visible log keeps its own.
|
|
65
|
+
*/
|
|
66
|
+
export function useAdsEvents(listener: AdEventListener): void {
|
|
67
|
+
const ads = useAds();
|
|
68
|
+
|
|
69
|
+
useEffect(() => {
|
|
70
|
+
if (!ads) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
return ads.subscribeEvents(listener);
|
|
74
|
+
}, [ads, listener]);
|
|
75
|
+
}
|