@chipmobilesdk/rn-ads 0.4.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.
@@ -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
+ }