@rocapine/react-native-onboarding 1.73.0 → 1.74.0

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.
Files changed (104) hide show
  1. package/README.md +136 -3
  2. package/dist/OnboardingStudio.d.ts +82 -0
  3. package/dist/OnboardingStudio.d.ts.map +1 -0
  4. package/dist/OnboardingStudio.js +91 -0
  5. package/dist/OnboardingStudio.js.map +1 -0
  6. package/dist/OnboardingStudioClient.d.ts +14 -5
  7. package/dist/OnboardingStudioClient.d.ts.map +1 -1
  8. package/dist/OnboardingStudioClient.js +24 -9
  9. package/dist/OnboardingStudioClient.js.map +1 -1
  10. package/dist/index.d.ts +7 -0
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +40 -4
  13. package/dist/index.js.map +1 -1
  14. package/dist/infra/provider/OnboardingProvider.d.ts +11 -2
  15. package/dist/infra/provider/OnboardingProvider.d.ts.map +1 -1
  16. package/dist/infra/provider/OnboardingProvider.js +26 -2
  17. package/dist/infra/provider/OnboardingProvider.js.map +1 -1
  18. package/dist/infra/queries/cacheKey.d.ts +41 -3
  19. package/dist/infra/queries/cacheKey.d.ts.map +1 -1
  20. package/dist/infra/queries/cacheKey.js +44 -8
  21. package/dist/infra/queries/cacheKey.js.map +1 -1
  22. package/dist/infra/queries/getOnboarding.query.d.ts.map +1 -1
  23. package/dist/infra/queries/getOnboarding.query.js +6 -1
  24. package/dist/infra/queries/getOnboarding.query.js.map +1 -1
  25. package/dist/paywalls/PaywallProvider.d.ts +52 -4
  26. package/dist/paywalls/PaywallProvider.d.ts.map +1 -1
  27. package/dist/paywalls/PaywallProvider.js +105 -7
  28. package/dist/paywalls/PaywallProvider.js.map +1 -1
  29. package/dist/paywalls/catalogSettleWaiter.d.ts +31 -0
  30. package/dist/paywalls/catalogSettleWaiter.d.ts.map +1 -0
  31. package/dist/paywalls/catalogSettleWaiter.js +58 -0
  32. package/dist/paywalls/catalogSettleWaiter.js.map +1 -0
  33. package/dist/paywalls/getPaywalls.query.d.ts.map +1 -1
  34. package/dist/paywalls/getPaywalls.query.js +6 -1
  35. package/dist/paywalls/getPaywalls.query.js.map +1 -1
  36. package/dist/paywalls/register.d.ts +105 -0
  37. package/dist/paywalls/register.d.ts.map +1 -0
  38. package/dist/paywalls/register.js +112 -0
  39. package/dist/paywalls/register.js.map +1 -0
  40. package/dist/paywalls/usePaywall.d.ts +29 -0
  41. package/dist/paywalls/usePaywall.d.ts.map +1 -1
  42. package/dist/paywalls/usePaywall.js +2 -1
  43. package/dist/paywalls/usePaywall.js.map +1 -1
  44. package/dist/userProperties/applyPatch.d.ts +18 -0
  45. package/dist/userProperties/applyPatch.d.ts.map +1 -0
  46. package/dist/userProperties/applyPatch.js +57 -0
  47. package/dist/userProperties/applyPatch.js.map +1 -0
  48. package/dist/userProperties/effectiveParams.d.ts +29 -0
  49. package/dist/userProperties/effectiveParams.d.ts.map +1 -0
  50. package/dist/userProperties/effectiveParams.js +56 -0
  51. package/dist/userProperties/effectiveParams.js.map +1 -0
  52. package/dist/userProperties/index.d.ts +9 -0
  53. package/dist/userProperties/index.d.ts.map +1 -0
  54. package/dist/userProperties/index.js +21 -0
  55. package/dist/userProperties/index.js.map +1 -0
  56. package/dist/userProperties/reserved.d.ts +33 -0
  57. package/dist/userProperties/reserved.d.ts.map +1 -0
  58. package/dist/userProperties/reserved.js +45 -0
  59. package/dist/userProperties/reserved.js.map +1 -0
  60. package/dist/userProperties/serialize.d.ts +38 -0
  61. package/dist/userProperties/serialize.d.ts.map +1 -0
  62. package/dist/userProperties/serialize.js +67 -0
  63. package/dist/userProperties/serialize.js.map +1 -0
  64. package/dist/userProperties/store.d.ts +63 -0
  65. package/dist/userProperties/store.d.ts.map +1 -0
  66. package/dist/userProperties/store.js +153 -0
  67. package/dist/userProperties/store.js.map +1 -0
  68. package/dist/userProperties/types.d.ts +21 -0
  69. package/dist/userProperties/types.d.ts.map +1 -0
  70. package/dist/userProperties/types.js +3 -0
  71. package/dist/userProperties/types.js.map +1 -0
  72. package/dist/userProperties/useUserProperties.d.ts +20 -0
  73. package/dist/userProperties/useUserProperties.d.ts.map +1 -0
  74. package/dist/userProperties/useUserProperties.js +30 -0
  75. package/dist/userProperties/useUserProperties.js.map +1 -0
  76. package/package.json +1 -1
  77. package/src/OnboardingStudio.ts +147 -0
  78. package/src/OnboardingStudioClient.ts +27 -11
  79. package/src/__tests__/OnboardingStudio.test.ts +208 -0
  80. package/src/__tests__/cacheKey.test.ts +71 -0
  81. package/src/__tests__/getOnboarding.query.test.ts +47 -8
  82. package/src/index.ts +43 -0
  83. package/src/infra/provider/OnboardingProvider.tsx +45 -4
  84. package/src/infra/queries/cacheKey.ts +54 -9
  85. package/src/infra/queries/getOnboarding.query.ts +9 -1
  86. package/src/paywalls/PaywallProvider.tsx +162 -6
  87. package/src/paywalls/__tests__/catalogSettleWaiter.test.ts +134 -0
  88. package/src/paywalls/__tests__/getPaywalls.test.ts +29 -11
  89. package/src/paywalls/__tests__/register.test.ts +266 -0
  90. package/src/paywalls/catalogSettleWaiter.ts +72 -0
  91. package/src/paywalls/getPaywalls.query.ts +9 -1
  92. package/src/paywalls/register.ts +169 -0
  93. package/src/paywalls/usePaywall.ts +40 -2
  94. package/src/userProperties/__tests__/effectiveParams.test.ts +51 -0
  95. package/src/userProperties/__tests__/primitives.test.ts +96 -0
  96. package/src/userProperties/__tests__/store.test.ts +181 -0
  97. package/src/userProperties/applyPatch.ts +68 -0
  98. package/src/userProperties/effectiveParams.ts +60 -0
  99. package/src/userProperties/index.ts +11 -0
  100. package/src/userProperties/reserved.ts +44 -0
  101. package/src/userProperties/serialize.ts +61 -0
  102. package/src/userProperties/store.ts +177 -0
  103. package/src/userProperties/types.ts +22 -0
  104. package/src/userProperties/useUserProperties.ts +26 -0
@@ -0,0 +1,177 @@
1
+ import AsyncStorage from "@react-native-async-storage/async-storage";
2
+ import { applyUserPropertyPatch } from "./applyPatch";
3
+ import type { UserProperties, UserPropertyPatch } from "./types";
4
+
5
+ /**
6
+ * The slice of AsyncStorage this needs, injected so the store is testable
7
+ * without mocking a module.
8
+ */
9
+ export type UserPropertyStorage = {
10
+ getItem: (key: string) => Promise<string | null>;
11
+ setItem: (key: string, value: string) => Promise<void>;
12
+ removeItem: (key: string) => Promise<void>;
13
+ };
14
+
15
+ /**
16
+ * ONE constant, deliberately not namespaced by `client.options.cacheKey` the way
17
+ * the payload caches are. `cacheKey` exists to pin a payload VERSION; user
18
+ * properties describe the USER, which is orthogonal. Deriving this from
19
+ * `cacheKey` would also force the store to learn about a client before it could
20
+ * hydrate, reintroducing the ordering problem hydration exists to remove.
21
+ *
22
+ * It also deliberately does NOT start with `rocapine-onboarding` or
23
+ * `rocapine-paywalls`, the two prefixes `OnboardingStudioClient.clearCache`
24
+ * scans: clearing a payload cache must not forget who the user is.
25
+ */
26
+ export const USER_PROPERTIES_STORAGE_KEY = "rocapine-user-properties";
27
+
28
+ export type UserPropertySnapshot = {
29
+ properties: UserProperties;
30
+ status: "hydrating" | "ready";
31
+ };
32
+
33
+ export type UserPropertyStore = {
34
+ get: () => UserProperties;
35
+ set: (patch: UserPropertyPatch) => void;
36
+ remove: (key: string) => void;
37
+ reset: () => void;
38
+ getSnapshot: () => UserPropertySnapshot;
39
+ subscribe: (listener: () => void) => () => void;
40
+ ensureHydrated: () => Promise<void>;
41
+ };
42
+
43
+ /**
44
+ * The mutable, persisted user-property map that feeds audience resolution.
45
+ *
46
+ * A module singleton (see `userProperties` below) rather than a provider +
47
+ * context, which is what every other piece of state in this package uses. The
48
+ * departure is deliberate and is the reason the feature exists: properties get
49
+ * set from a login handler, an analytics service, a push-token callback — code
50
+ * that is not a component and cannot call a hook. Superwall's own
51
+ * `setUserAttributes` is a singleton for the same reason.
52
+ *
53
+ * It also REMOVES a source of truth rather than adding one. With
54
+ * `customAudienceParams` passed separately to `OnboardingProvider` and
55
+ * `PaywallProvider`, an onboarding audience and a paywall audience can disagree
56
+ * about the same user. One store makes that impossible.
57
+ *
58
+ * Storage is injected so the whole thing is testable as a plain object.
59
+ */
60
+ export const createUserPropertyStore = (storage: UserPropertyStorage): UserPropertyStore => {
61
+ let properties: UserProperties = {};
62
+ let status: UserPropertySnapshot["status"] = "hydrating";
63
+ let snapshot: UserPropertySnapshot = { properties, status };
64
+ const listeners = new Set<() => void>();
65
+ let hydration: Promise<void> | null = null;
66
+ let writeScheduled = false;
67
+
68
+ // A fresh object per change, so `useSyncExternalStore` can compare snapshots
69
+ // by identity — and the SAME object between changes, or React would re-render
70
+ // on every check.
71
+ const publish = () => {
72
+ snapshot = { properties, status };
73
+ for (const listener of listeners) listener();
74
+ };
75
+
76
+ // Coalesced: a burst of `set` calls in one tick costs one write. Skipped
77
+ // entirely while hydrating — writing then would persist a map that has not yet
78
+ // been merged with what is already on disk, i.e. silently delete properties
79
+ // the host set on a previous launch.
80
+ const scheduleWrite = () => {
81
+ if (status !== "ready" || writeScheduled) return;
82
+ writeScheduled = true;
83
+ setTimeout(() => {
84
+ writeScheduled = false;
85
+ void storage.setItem(USER_PROPERTIES_STORAGE_KEY, JSON.stringify(properties)).catch((error) => {
86
+ // The in-memory map stays authoritative: a failed write costs
87
+ // persistence across launches, not correctness in this one.
88
+ console.warn("[user-properties] Failed to persist:", error);
89
+ });
90
+ }, 0);
91
+ };
92
+
93
+ const commit = (next: UserProperties, warnings: string[]) => {
94
+ for (const warning of warnings) console.warn(warning);
95
+ if (next === properties) return;
96
+ properties = next;
97
+ publish();
98
+ scheduleWrite();
99
+ };
100
+
101
+ return {
102
+ get: () => properties,
103
+
104
+ set: (patch) => {
105
+ const { next, warnings } = applyUserPropertyPatch(properties, patch);
106
+ commit(next, warnings);
107
+ },
108
+
109
+ remove: (key) => {
110
+ const { next, warnings } = applyUserPropertyPatch(properties, { [key]: null });
111
+ commit(next, warnings);
112
+ },
113
+
114
+ reset: () => {
115
+ if (Object.keys(properties).length > 0) {
116
+ properties = {};
117
+ publish();
118
+ }
119
+ void storage.removeItem(USER_PROPERTIES_STORAGE_KEY).catch((error) => {
120
+ console.warn("[user-properties] Failed to clear persisted properties:", error);
121
+ });
122
+ },
123
+
124
+ getSnapshot: () => snapshot,
125
+
126
+ subscribe: (listener) => {
127
+ listeners.add(listener);
128
+ return () => void listeners.delete(listener);
129
+ },
130
+
131
+ // Memoized: several providers await this on mount and it must read the disk
132
+ // once. Never rejects — a store that cannot hydrate must still let the app
133
+ // fetch a catalog, so a failure degrades to "no persisted properties".
134
+ ensureHydrated: () => {
135
+ if (hydration) return hydration;
136
+ hydration = (async () => {
137
+ let persisted: UserProperties = {};
138
+ try {
139
+ const raw = await storage.getItem(USER_PROPERTIES_STORAGE_KEY);
140
+ if (raw) {
141
+ const parsed = JSON.parse(raw);
142
+ if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
143
+ // Through the same validation as a live `set` — defence in depth
144
+ // against a payload written by an older version or hand-edited.
145
+ // Warnings are dropped rather than logged: `set` already refused
146
+ // these values when they were first written, so anything caught
147
+ // here cannot occur in a normally-produced payload, and warning
148
+ // on every launch about it would be noise.
149
+ persisted = applyUserPropertyPatch({}, parsed as UserPropertyPatch).next;
150
+ }
151
+ }
152
+ } catch (error) {
153
+ console.warn("[user-properties] Failed to hydrate, continuing empty:", error);
154
+ }
155
+ // Disk UNDER memory: anything the host set while hydration was in flight
156
+ // is newer than what was on disk and must win.
157
+ properties = { ...persisted, ...properties };
158
+ status = "ready";
159
+ publish();
160
+ // Persist the merged result if the host wrote during hydration, which
161
+ // `scheduleWrite` refused to do while status was "hydrating".
162
+ if (Object.keys(properties).length > 0) scheduleWrite();
163
+ })();
164
+ return hydration;
165
+ },
166
+ };
167
+ };
168
+
169
+ /**
170
+ * The process-wide store.
171
+ *
172
+ * INTERNAL: hosts reach this through `OnboardingStudio` (`setUserProperty`,
173
+ * `reset`, …) and `useUserProperties()`, never directly. Keeping one public
174
+ * front door means a host cannot half-use the facade and half-use the store, and
175
+ * leaves this module free to change shape.
176
+ */
177
+ export const userPropertyStore = createUserPropertyStore(AsyncStorage);
@@ -0,0 +1,22 @@
1
+ /**
2
+ * The value types a user property may hold.
3
+ *
4
+ * Deliberately narrower than `customAudienceParams`' `Record<string, any>`,
5
+ * which this store supersedes as the place runtime targeting values live.
6
+ * Everything reaches the server as a STRING (the client builds a
7
+ * `URLSearchParams`; the edge function reads
8
+ * `Object.fromEntries(url.searchParams.entries())`), so a value type that
9
+ * cannot be stringified unambiguously has no meaning on the wire — see
10
+ * `serialize.ts`.
11
+ */
12
+ export type UserPropertyValue = string | number | boolean;
13
+
14
+ /** The resolved property map. Always fully valid — invalid input never lands here. */
15
+ export type UserProperties = Record<string, UserPropertyValue>;
16
+
17
+ /**
18
+ * What `set` accepts. `null`/`undefined` for a key DELETES it, which is both
19
+ * how Superwall's attribute API behaves and what a caller reaching for "unset
20
+ * this" writes without thinking about it.
21
+ */
22
+ export type UserPropertyPatch = Record<string, UserPropertyValue | null | undefined>;
@@ -0,0 +1,26 @@
1
+ import { useSyncExternalStore } from "react";
2
+ import { userPropertyStore } from "./store";
3
+ import type { UserPropertySnapshot } from "./store";
4
+
5
+ /**
6
+ * Read the user properties `OnboardingStudio` holds.
7
+ *
8
+ * The read path for React; the WRITE path is `OnboardingStudio.setUserProperty`
9
+ * / `setUserProperties`, which is callable from anywhere — a login handler, an
10
+ * analytics service — precisely because it is not a hook.
11
+ *
12
+ * `useSyncExternalStore` rather than a context: the store is process-wide so
13
+ * non-React code can write to it, and this is the supported way to read an
14
+ * external mutable source without tearing.
15
+ *
16
+ * Hydration is kicked off HERE as well as in `OnboardingStudio.init`, so a
17
+ * provider that calls this is gated correctly even if the host never called
18
+ * `init` (passing a `client` prop instead). `ensureHydrated` is memoized, so
19
+ * calling it from a render body is safe: every call after the first returns the
20
+ * same promise and touches no storage.
21
+ */
22
+ export const useUserProperties = (): UserPropertySnapshot => {
23
+ const snapshot = useSyncExternalStore(userPropertyStore.subscribe, userPropertyStore.getSnapshot);
24
+ if (snapshot.status === "hydrating") void userPropertyStore.ensureHydrated();
25
+ return snapshot;
26
+ };