@rocapine/react-native-onboarding 1.73.0 → 1.74.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.
Files changed (105) hide show
  1. package/README.md +164 -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 +15 -2
  15. package/dist/infra/provider/OnboardingProvider.d.ts.map +1 -1
  16. package/dist/infra/provider/OnboardingProvider.js +66 -6
  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 +65 -0
  65. package/dist/userProperties/store.d.ts.map +1 -0
  66. package/dist/userProperties/store.js +155 -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 +5 -1
  77. package/src/OnboardingStudio.ts +147 -0
  78. package/src/OnboardingStudioClient.ts +27 -11
  79. package/src/__tests__/OnboardingDataGate.test.tsx +445 -0
  80. package/src/__tests__/OnboardingStudio.test.ts +208 -0
  81. package/src/__tests__/cacheKey.test.ts +71 -0
  82. package/src/__tests__/getOnboarding.query.test.ts +47 -8
  83. package/src/index.ts +43 -0
  84. package/src/infra/provider/OnboardingProvider.tsx +98 -10
  85. package/src/infra/queries/cacheKey.ts +54 -9
  86. package/src/infra/queries/getOnboarding.query.ts +9 -1
  87. package/src/paywalls/PaywallProvider.tsx +162 -6
  88. package/src/paywalls/__tests__/catalogSettleWaiter.test.ts +134 -0
  89. package/src/paywalls/__tests__/getPaywalls.test.ts +29 -11
  90. package/src/paywalls/__tests__/register.test.ts +266 -0
  91. package/src/paywalls/catalogSettleWaiter.ts +72 -0
  92. package/src/paywalls/getPaywalls.query.ts +9 -1
  93. package/src/paywalls/register.ts +169 -0
  94. package/src/paywalls/usePaywall.ts +40 -2
  95. package/src/userProperties/__tests__/effectiveParams.test.ts +51 -0
  96. package/src/userProperties/__tests__/primitives.test.ts +96 -0
  97. package/src/userProperties/__tests__/store.test.ts +181 -0
  98. package/src/userProperties/applyPatch.ts +68 -0
  99. package/src/userProperties/effectiveParams.ts +60 -0
  100. package/src/userProperties/index.ts +11 -0
  101. package/src/userProperties/reserved.ts +44 -0
  102. package/src/userProperties/serialize.ts +61 -0
  103. package/src/userProperties/store.ts +179 -0
  104. package/src/userProperties/types.ts +22 -0
  105. package/src/userProperties/useUserProperties.ts +26 -0
@@ -0,0 +1,155 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.userPropertyStore = exports.createUserPropertyStore = exports.USER_PROPERTIES_STORAGE_KEY = void 0;
7
+ const async_storage_1 = __importDefault(require("@react-native-async-storage/async-storage"));
8
+ const applyPatch_1 = require("./applyPatch");
9
+ /**
10
+ * ONE constant, deliberately not namespaced by `client.options.cacheKey` the way
11
+ * the payload caches are. `cacheKey` exists to pin a payload VERSION; user
12
+ * properties describe the USER, which is orthogonal. Deriving this from
13
+ * `cacheKey` would also force the store to learn about a client before it could
14
+ * hydrate, reintroducing the ordering problem hydration exists to remove.
15
+ *
16
+ * It also deliberately does NOT start with `rocapine-onboarding` or
17
+ * `rocapine-paywalls`, the two prefixes `OnboardingStudioClient.clearCache`
18
+ * scans: clearing a payload cache must not forget who the user is.
19
+ */
20
+ exports.USER_PROPERTIES_STORAGE_KEY = "rocapine-user-properties";
21
+ /**
22
+ * The mutable, persisted user-property map that feeds audience resolution.
23
+ *
24
+ * A module singleton (see `userProperties` below) rather than a provider +
25
+ * context, which is what every other piece of state in this package uses. The
26
+ * departure is deliberate and is the reason the feature exists: properties get
27
+ * set from a login handler, an analytics service, a push-token callback — code
28
+ * that is not a component and cannot call a hook. Superwall's own
29
+ * `setUserAttributes` is a singleton for the same reason.
30
+ *
31
+ * It also REMOVES a source of truth rather than adding one. With
32
+ * `customAudienceParams` passed separately to `OnboardingProvider` and
33
+ * `PaywallProvider`, an onboarding audience and a paywall audience can disagree
34
+ * about the same user. One store makes that impossible — they can now differ
35
+ * only by WHEN they read it, since each is resolved at its own serve time (see
36
+ * `useAudienceParams` in `OnboardingProvider`).
37
+ *
38
+ * Storage is injected so the whole thing is testable as a plain object.
39
+ */
40
+ const createUserPropertyStore = (storage) => {
41
+ let properties = {};
42
+ let status = "hydrating";
43
+ let snapshot = { properties, status };
44
+ const listeners = new Set();
45
+ let hydration = null;
46
+ let writeScheduled = false;
47
+ // A fresh object per change, so `useSyncExternalStore` can compare snapshots
48
+ // by identity — and the SAME object between changes, or React would re-render
49
+ // on every check.
50
+ const publish = () => {
51
+ snapshot = { properties, status };
52
+ for (const listener of listeners)
53
+ listener();
54
+ };
55
+ // Coalesced: a burst of `set` calls in one tick costs one write. Skipped
56
+ // entirely while hydrating — writing then would persist a map that has not yet
57
+ // been merged with what is already on disk, i.e. silently delete properties
58
+ // the host set on a previous launch.
59
+ const scheduleWrite = () => {
60
+ if (status !== "ready" || writeScheduled)
61
+ return;
62
+ writeScheduled = true;
63
+ setTimeout(() => {
64
+ writeScheduled = false;
65
+ void storage.setItem(exports.USER_PROPERTIES_STORAGE_KEY, JSON.stringify(properties)).catch((error) => {
66
+ // The in-memory map stays authoritative: a failed write costs
67
+ // persistence across launches, not correctness in this one.
68
+ console.warn("[user-properties] Failed to persist:", error);
69
+ });
70
+ }, 0);
71
+ };
72
+ const commit = (next, warnings) => {
73
+ for (const warning of warnings)
74
+ console.warn(warning);
75
+ if (next === properties)
76
+ return;
77
+ properties = next;
78
+ publish();
79
+ scheduleWrite();
80
+ };
81
+ return {
82
+ get: () => properties,
83
+ set: (patch) => {
84
+ const { next, warnings } = (0, applyPatch_1.applyUserPropertyPatch)(properties, patch);
85
+ commit(next, warnings);
86
+ },
87
+ remove: (key) => {
88
+ const { next, warnings } = (0, applyPatch_1.applyUserPropertyPatch)(properties, { [key]: null });
89
+ commit(next, warnings);
90
+ },
91
+ reset: () => {
92
+ if (Object.keys(properties).length > 0) {
93
+ properties = {};
94
+ publish();
95
+ }
96
+ void storage.removeItem(exports.USER_PROPERTIES_STORAGE_KEY).catch((error) => {
97
+ console.warn("[user-properties] Failed to clear persisted properties:", error);
98
+ });
99
+ },
100
+ getSnapshot: () => snapshot,
101
+ subscribe: (listener) => {
102
+ listeners.add(listener);
103
+ return () => void listeners.delete(listener);
104
+ },
105
+ // Memoized: several providers await this on mount and it must read the disk
106
+ // once. Never rejects — a store that cannot hydrate must still let the app
107
+ // fetch a catalog, so a failure degrades to "no persisted properties".
108
+ ensureHydrated: () => {
109
+ if (hydration)
110
+ return hydration;
111
+ hydration = (async () => {
112
+ let persisted = {};
113
+ try {
114
+ const raw = await storage.getItem(exports.USER_PROPERTIES_STORAGE_KEY);
115
+ if (raw) {
116
+ const parsed = JSON.parse(raw);
117
+ if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
118
+ // Through the same validation as a live `set` — defence in depth
119
+ // against a payload written by an older version or hand-edited.
120
+ // Warnings are dropped rather than logged: `set` already refused
121
+ // these values when they were first written, so anything caught
122
+ // here cannot occur in a normally-produced payload, and warning
123
+ // on every launch about it would be noise.
124
+ persisted = (0, applyPatch_1.applyUserPropertyPatch)({}, parsed).next;
125
+ }
126
+ }
127
+ }
128
+ catch (error) {
129
+ console.warn("[user-properties] Failed to hydrate, continuing empty:", error);
130
+ }
131
+ // Disk UNDER memory: anything the host set while hydration was in flight
132
+ // is newer than what was on disk and must win.
133
+ properties = Object.assign(Object.assign({}, persisted), properties);
134
+ status = "ready";
135
+ publish();
136
+ // Persist the merged result if the host wrote during hydration, which
137
+ // `scheduleWrite` refused to do while status was "hydrating".
138
+ if (Object.keys(properties).length > 0)
139
+ scheduleWrite();
140
+ })();
141
+ return hydration;
142
+ },
143
+ };
144
+ };
145
+ exports.createUserPropertyStore = createUserPropertyStore;
146
+ /**
147
+ * The process-wide store.
148
+ *
149
+ * INTERNAL: hosts reach this through `OnboardingStudio` (`setUserProperty`,
150
+ * `reset`, …) and `useUserProperties()`, never directly. Keeping one public
151
+ * front door means a host cannot half-use the facade and half-use the store, and
152
+ * leaves this module free to change shape.
153
+ */
154
+ exports.userPropertyStore = (0, exports.createUserPropertyStore)(async_storage_1.default);
155
+ //# sourceMappingURL=store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.js","sourceRoot":"","sources":["../../src/userProperties/store.ts"],"names":[],"mappings":";;;;;;AAAA,8FAAqE;AACrE,6CAAsD;AAatD;;;;;;;;;;GAUG;AACU,QAAA,2BAA2B,GA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CAAC,CAAC;AA3GW,QAAA,uBAAuB,2BA2GlC;AAEF;;;;;;;GAOG;AACU,QAAA,iBAAiB,GAAG,IAAA,+BAAuB,EAAC,uBAAY,CAAC,CAAC"}
@@ -0,0 +1,21 @@
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
+ /** The resolved property map. Always fully valid — invalid input never lands here. */
14
+ export type UserProperties = Record<string, UserPropertyValue>;
15
+ /**
16
+ * What `set` accepts. `null`/`undefined` for a key DELETES it, which is both
17
+ * how Superwall's attribute API behaves and what a caller reaching for "unset
18
+ * this" writes without thinking about it.
19
+ */
20
+ export type UserPropertyPatch = Record<string, UserPropertyValue | null | undefined>;
21
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/userProperties/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,MAAM,MAAM,iBAAiB,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC;AAE1D,sFAAsF;AACtF,MAAM,MAAM,cAAc,GAAG,MAAM,CAAC,MAAM,EAAE,iBAAiB,CAAC,CAAC;AAE/D;;;;GAIG;AACH,MAAM,MAAM,iBAAiB,GAAG,MAAM,CAAC,MAAM,EAAE,iBAAiB,GAAG,IAAI,GAAG,SAAS,CAAC,CAAC"}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/userProperties/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,20 @@
1
+ import type { UserPropertySnapshot } from "./store";
2
+ /**
3
+ * Read the user properties `OnboardingStudio` holds.
4
+ *
5
+ * The read path for React; the WRITE path is `OnboardingStudio.setUserProperty`
6
+ * / `setUserProperties`, which is callable from anywhere — a login handler, an
7
+ * analytics service — precisely because it is not a hook.
8
+ *
9
+ * `useSyncExternalStore` rather than a context: the store is process-wide so
10
+ * non-React code can write to it, and this is the supported way to read an
11
+ * external mutable source without tearing.
12
+ *
13
+ * Hydration is kicked off HERE as well as in `OnboardingStudio.init`, so a
14
+ * provider that calls this is gated correctly even if the host never called
15
+ * `init` (passing a `client` prop instead). `ensureHydrated` is memoized, so
16
+ * calling it from a render body is safe: every call after the first returns the
17
+ * same promise and touches no storage.
18
+ */
19
+ export declare const useUserProperties: () => UserPropertySnapshot;
20
+ //# sourceMappingURL=useUserProperties.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useUserProperties.d.ts","sourceRoot":"","sources":["../../src/userProperties/useUserProperties.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAC;AAEpD;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,iBAAiB,QAAO,oBAIpC,CAAC"}
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useUserProperties = void 0;
4
+ const react_1 = require("react");
5
+ const store_1 = require("./store");
6
+ /**
7
+ * Read the user properties `OnboardingStudio` holds.
8
+ *
9
+ * The read path for React; the WRITE path is `OnboardingStudio.setUserProperty`
10
+ * / `setUserProperties`, which is callable from anywhere — a login handler, an
11
+ * analytics service — precisely because it is not a hook.
12
+ *
13
+ * `useSyncExternalStore` rather than a context: the store is process-wide so
14
+ * non-React code can write to it, and this is the supported way to read an
15
+ * external mutable source without tearing.
16
+ *
17
+ * Hydration is kicked off HERE as well as in `OnboardingStudio.init`, so a
18
+ * provider that calls this is gated correctly even if the host never called
19
+ * `init` (passing a `client` prop instead). `ensureHydrated` is memoized, so
20
+ * calling it from a render body is safe: every call after the first returns the
21
+ * same promise and touches no storage.
22
+ */
23
+ const useUserProperties = () => {
24
+ const snapshot = (0, react_1.useSyncExternalStore)(store_1.userPropertyStore.subscribe, store_1.userPropertyStore.getSnapshot);
25
+ if (snapshot.status === "hydrating")
26
+ void store_1.userPropertyStore.ensureHydrated();
27
+ return snapshot;
28
+ };
29
+ exports.useUserProperties = useUserProperties;
30
+ //# sourceMappingURL=useUserProperties.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useUserProperties.js","sourceRoot":"","sources":["../../src/userProperties/useUserProperties.ts"],"names":[],"mappings":";;;AAAA,iCAA6C;AAC7C,mCAA4C;AAG5C;;;;;;;;;;;;;;;;GAgBG;AACI,MAAM,iBAAiB,GAAG,GAAyB,EAAE;IAC1D,MAAM,QAAQ,GAAG,IAAA,4BAAoB,EAAC,yBAAiB,CAAC,SAAS,EAAE,yBAAiB,CAAC,WAAW,CAAC,CAAC;IAClG,IAAI,QAAQ,CAAC,MAAM,KAAK,WAAW;QAAE,KAAK,yBAAiB,CAAC,cAAc,EAAE,CAAC;IAC7E,OAAO,QAAQ,CAAC;AAClB,CAAC,CAAC;AAJW,QAAA,iBAAiB,qBAI5B"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rocapine/react-native-onboarding",
3
- "version": "1.73.0",
3
+ "version": "1.74.1",
4
4
  "description": "Headless React Native SDK for Rocapine Onboarding Studio - Data fetching, state management, and hooks",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -8,6 +8,7 @@
8
8
  "build": "rm -rf dist && tsc",
9
9
  "watch": "tsc --watch",
10
10
  "type:check": "tsc --noEmit",
11
+ "type:check:tests": "tsc -p tsconfig.test.json",
11
12
  "test": "vitest run",
12
13
  "test:watch": "vitest",
13
14
  "prepublishOnly": "node ../../scripts/check-versions.mjs"
@@ -29,7 +30,10 @@
29
30
  },
30
31
  "devDependencies": {
31
32
  "@types/react": "^19.0.0",
33
+ "@types/react-dom": "19.2.5",
34
+ "jsdom": "^29.1.1",
32
35
  "react": "19.2.3",
36
+ "react-dom": "19.2.3",
33
37
  "react-native": "0.85.3",
34
38
  "typescript": "^5.0.0",
35
39
  "vitest": "^4.1.5"
@@ -0,0 +1,147 @@
1
+ import { OnboardingStudioClient } from "./OnboardingStudioClient";
2
+ import type { OnboardingStudioClientOptions } from "./types";
3
+ import { userPropertyStore } from "./userProperties/store";
4
+ import type { UserPropertyStore } from "./userProperties/store";
5
+ import type {
6
+ UserProperties,
7
+ UserPropertyPatch,
8
+ UserPropertyValue,
9
+ } from "./userProperties/types";
10
+
11
+ /**
12
+ * What `OnboardingStudio.init` takes: everything `new OnboardingStudioClient()`
13
+ * takes, plus the project id it takes positionally, plus optional initial user
14
+ * properties.
15
+ *
16
+ * `userProperties` here is a genuine convenience rather than sugar. Audience
17
+ * targeting is evaluated against the property map, and persisted properties are
18
+ * only hydrated from disk — so on a first-EVER install there is nothing to
19
+ * hydrate and the first catalog fetch matches the catch-all audience. Seeding at
20
+ * `init`, which runs before any provider mounts, is the only way to be targeted
21
+ * correctly on that first launch.
22
+ */
23
+ export type OnboardingStudioConfig = { projectId: string } & OnboardingStudioClientOptions & {
24
+ userProperties?: UserPropertyPatch;
25
+ };
26
+
27
+ export type OnboardingStudioFacade = {
28
+ /**
29
+ * Configure the SDK. Returns the client it built, so a host that needs
30
+ * `clearCache()` (or wants to pass it explicitly) can keep hold of it.
31
+ *
32
+ * Idempotent for an unchanged config — Fast Refresh re-runs module scope, and
33
+ * rebuilding the client there would orphan the one the providers already hold.
34
+ * A genuinely CHANGED config replaces the client and warns, because silently
35
+ * ignoring it would be worse than either alternative.
36
+ */
37
+ init: (config: OnboardingStudioConfig) => OnboardingStudioClient;
38
+ /** The configured client, or `null` before `init`. */
39
+ getClient: () => OnboardingStudioClient | null;
40
+ isInitialized: () => boolean;
41
+ /** Set one property. `null` deletes it. */
42
+ setUserProperty: (key: string, value: UserPropertyValue | null) => void;
43
+ /** Set several at once. Merges; a `null` value deletes that key. */
44
+ setUserProperties: (patch: UserPropertyPatch) => void;
45
+ removeUserProperty: (key: string) => void;
46
+ getUserProperties: () => UserProperties;
47
+ /**
48
+ * Forget the user: clears every user property, in memory and on disk. Call it
49
+ * on logout.
50
+ *
51
+ * Deliberately does NOT clear the payload cache or the configuration. Logging
52
+ * out should forget who someone is, not force a refetch of content that has
53
+ * not changed — `getClient()?.clearCache()` is right there if both are wanted.
54
+ */
55
+ reset: () => void;
56
+ };
57
+
58
+ /** Stable signature of a config, for the idempotency check in `init`. */
59
+ const configSignature = (projectId: string, options: Record<string, unknown>): string =>
60
+ `${projectId}::${JSON.stringify(options)}`;
61
+
62
+ /**
63
+ * The SDK's front door, in the shape the SDKs it sits alongside use
64
+ * (`Superwall.configure` / `Purchases.configure` / `amplitude.init`): one
65
+ * module-level object that owns configuration and user identity.
66
+ *
67
+ * A factory with the store injected, so its tests need a fake store rather than
68
+ * a module mock — the same reason `createUserPropertyStore` is a factory.
69
+ *
70
+ * `register`/`present` deliberately do NOT live here. Presenting needs the
71
+ * mounted provider's catalog and presentation state, so they stay on
72
+ * `usePaywall()`, where that state is reachable and where a call cannot be made
73
+ * before a provider exists.
74
+ */
75
+ export const createOnboardingStudio = (store: UserPropertyStore): OnboardingStudioFacade => {
76
+ let client: OnboardingStudioClient | null = null;
77
+ let signature: string | null = null;
78
+
79
+ return {
80
+ init: (config) => {
81
+ const { projectId, userProperties: initialProperties, ...options } = config;
82
+ const next = configSignature(projectId, options);
83
+
84
+ if (client && signature === next) {
85
+ // Same config: hand back the same client. Seeding the same properties
86
+ // again would be a no-op anyway (`applyUserPropertyPatch` returns the
87
+ // same object when nothing changed), so there is nothing else to do.
88
+ return client;
89
+ }
90
+
91
+ if (client) {
92
+ console.warn(
93
+ `[onboarding-studio] init() was called again with a DIFFERENT config ` +
94
+ `(projectId "${projectId}"). The previous client has been replaced. ` +
95
+ "Providers already holding the old client keep using it until they " +
96
+ "re-render — call init() once, at module scope."
97
+ );
98
+ }
99
+
100
+ client = new OnboardingStudioClient(projectId, options);
101
+ signature = next;
102
+
103
+ // Before hydration on purpose: the store merges disk UNDER memory, so a
104
+ // property the host states here beats a stale one from a previous launch.
105
+ if (initialProperties) store.set(initialProperties);
106
+ // Start reading the persisted map now rather than at the first provider
107
+ // mount, so it is ready before anything queries.
108
+ void store.ensureHydrated();
109
+
110
+ return client;
111
+ },
112
+
113
+ getClient: () => client,
114
+ isInitialized: () => client !== null,
115
+
116
+ setUserProperty: (key, value) => store.set({ [key]: value }),
117
+ setUserProperties: (patch) => store.set(patch),
118
+ removeUserProperty: (key) => store.remove(key),
119
+ getUserProperties: () => store.get(),
120
+ reset: () => store.reset(),
121
+ };
122
+ };
123
+
124
+ /** The facade every host uses. */
125
+ export const OnboardingStudio = createOnboardingStudio(userPropertyStore);
126
+
127
+ /**
128
+ * Which client a provider should use: an explicit `client` prop wins, otherwise
129
+ * the one `OnboardingStudio.init()` built.
130
+ *
131
+ * The prop keeps winning so every host that already passes one is untouched by
132
+ * `init` existing.
133
+ */
134
+ export const resolveProviderClient = (
135
+ prop: OnboardingStudioClient | undefined,
136
+ fromStudio: OnboardingStudioClient | null
137
+ ): OnboardingStudioClient | null => prop ?? fromStudio;
138
+
139
+ /**
140
+ * Shared by both providers, because a host hitting this has exactly two fixes
141
+ * and should be told both rather than left to guess which one this provider
142
+ * wanted.
143
+ */
144
+ export const MISSING_CLIENT_MESSAGE =
145
+ "[onboarding-studio] No client. Either call OnboardingStudio.init({ projectId }) " +
146
+ "once at module scope, or pass a `client` prop built with " +
147
+ "`new OnboardingStudioClient(projectId, options)`.";
@@ -9,8 +9,8 @@ import {
9
9
  BaseStepType,
10
10
  } from "./types";
11
11
  import {
12
- getOnboardingCacheKey,
13
- getPaywallsCacheKey,
12
+ ONBOARDING_CACHE_KEY_PREFIX,
13
+ PAYWALLS_CACHE_KEY_PREFIX,
14
14
  } from "./infra/queries/cacheKey";
15
15
  import {
16
16
  PaywallCatalog,
@@ -35,18 +35,34 @@ export class OnboardingStudioClient {
35
35
  }
36
36
 
37
37
  /**
38
- * Removes this client's cached onboarding payload AND cached paywall catalog
39
- * from AsyncStorage (the keys derived from `options.cacheKey`, or the
40
- * default keys). The next query mount or app launch is then a cache miss
41
- * and refetches both. To force an in-session refetch as well, also
42
- * invalidate the React Query keys `["onboardingQuestions", ...]` and
38
+ * Removes EVERY cached onboarding payload and paywall catalog this SDK wrote,
39
+ * across all audience-param variants. The next query mount or app launch is
40
+ * then a cache miss and refetches. To force an in-session refetch as well,
41
+ * also invalidate the React Query keys `["onboardingQuestions", ...]` and
43
42
  * `["paywallCatalog", ...]`.
43
+ *
44
+ * Scans `getAllKeys()` by prefix rather than naming two keys, because the keys
45
+ * are now scoped by a hash of the resolved audience params (see
46
+ * `infra/queries/cacheKey.ts`) — so there is no longer one key per domain to
47
+ * name. This also picks up entries written before that scoping existed, which
48
+ * would otherwise be orphaned forever.
49
+ *
50
+ * It does NOT clear user properties: `USER_PROPERTIES_STORAGE_KEY` matches
51
+ * neither prefix, deliberately. Clearing a payload cache must not forget who
52
+ * the user is — that is what `OnboardingStudio.reset()` is for.
44
53
  */
45
54
  async clearCache(): Promise<void> {
46
- await Promise.all([
47
- AsyncStorage.removeItem(getOnboardingCacheKey(this.options.cacheKey)),
48
- AsyncStorage.removeItem(getPaywallsCacheKey(this.options.cacheKey)),
49
- ]);
55
+ try {
56
+ const keys = await AsyncStorage.getAllKeys();
57
+ const ours = keys.filter(
58
+ (key) =>
59
+ key.startsWith(ONBOARDING_CACHE_KEY_PREFIX) ||
60
+ key.startsWith(PAYWALLS_CACHE_KEY_PREFIX),
61
+ );
62
+ if (ours.length > 0) await AsyncStorage.multiRemove(ours);
63
+ } catch (error) {
64
+ console.warn("Failed to clear SDK caches:", error);
65
+ }
50
66
  }
51
67
 
52
68
  async getSteps<StepType extends BaseStepType = OnboardingStepType>(