create-disposabl-app 0.19.0 → 0.19.2
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/dist/disposabl.js +9 -3
- package/dist/index.js +11 -4
- package/dist/templates/default/apps/mobile/modules/storefront-observer/README.md +9 -0
- package/dist/templates/default/apps/mobile/modules/storefront-observer/expo-module.config.json +1 -0
- package/dist/templates/default/apps/mobile/modules/storefront-observer/ios/StorefrontObserver.podspec +15 -0
- package/dist/templates/default/apps/mobile/modules/storefront-observer/ios/StorefrontObserverModule.swift +59 -0
- package/dist/templates/default/apps/mobile/modules/storefront-observer/package.json +7 -0
- package/dist/templates/default/packages/cli/build.ts +2 -0
- package/dist/templates/default/packages/cli/client.ts +7 -1
- package/dist/templates/default/packages/cli/gitignore +1 -0
- package/dist/templates/default/packages/disposabl-kit/client/routes/billing-login.tsx +69 -15
- package/dist/templates/default/packages/disposabl-kit/mobile/billing/storefront-observer.ts +45 -0
- package/dist/templates/default/packages/disposabl-kit/mobile/billing/web-checkout.ts +39 -7
- package/dist/templates/default/packages/sdk/build.ts +2 -0
- package/dist/templates/default/packages/sdk/gitignore +1 -0
- package/dist/templates/default/packages/tests/components/mobile-storefront-observer.test.mjs +241 -0
- package/dist/templates/default/packages/tests/mobile-storefront-observer.test.ts +22 -0
- package/package.json +1 -1
package/dist/disposabl.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// ../cli/auth.ts
|
|
4
4
|
import { lock } from "proper-lockfile";
|
|
5
5
|
// ../cli/package.json
|
|
6
|
-
var version = "0.19.
|
|
6
|
+
var version = "0.19.2";
|
|
7
7
|
var package_default = {
|
|
8
8
|
name: "@disposabl/cli",
|
|
9
9
|
version,
|
|
@@ -514,6 +514,9 @@ function createProgram(serviceOrigin, centralAnalytics = false, dependencies = {
|
|
|
514
514
|
});
|
|
515
515
|
};
|
|
516
516
|
const program = new Command().name(Object.keys(package_default.bin)[0]).version(package_default.version);
|
|
517
|
+
program.addHelpText("after", `
|
|
518
|
+
API commands are loaded from the configured service. Use <command> --help for their options.
|
|
519
|
+
`);
|
|
517
520
|
if (centralAnalytics)
|
|
518
521
|
program.addHelpText("after", `
|
|
519
522
|
Disposabl collects limited CLI usage when configured. Set DO_NOT_TRACK=1 to opt out.
|
|
@@ -704,7 +707,7 @@ function needsContract(program, argv, origin) {
|
|
|
704
707
|
const args = argv.slice(2);
|
|
705
708
|
const { operands } = program.parseOptions(args);
|
|
706
709
|
const command = operands[0] === "help" ? operands[1] : operands[0];
|
|
707
|
-
return Boolean(origin) && !args.some((arg) => arg === "--version" || arg === "-V") && command !== "login" && command !== "logout";
|
|
710
|
+
return Boolean(origin) && command !== undefined && !args.some((arg) => arg === "--version" || arg === "-V") && command !== "login" && command !== "logout";
|
|
708
711
|
}
|
|
709
712
|
|
|
710
713
|
// ../cli/commands.ts
|
|
@@ -1012,7 +1015,10 @@ Waiting for connections. Press Ctrl+C to keep your files and finish later.`);
|
|
|
1012
1015
|
}
|
|
1013
1016
|
}
|
|
1014
1017
|
}
|
|
1015
|
-
|
|
1018
|
+
if (argv.length <= 2)
|
|
1019
|
+
program.outputHelp();
|
|
1020
|
+
else
|
|
1021
|
+
await program.parseAsync(argv);
|
|
1016
1022
|
}
|
|
1017
1023
|
const command = ["--version", "-V"].includes(argv[2] ?? "") ? "version" : ["--help", "-h"].includes(argv[2] ?? "") ? "help" : argv[2] ?? "help";
|
|
1018
1024
|
const central = serviceOrigin === SERVICE_ORIGIN;
|
package/dist/index.js
CHANGED
|
@@ -1563,7 +1563,7 @@ import { fileURLToPath as fileURLToPath2 } from "node:url";
|
|
|
1563
1563
|
import { Command, CommanderError } from "commander";
|
|
1564
1564
|
import * as p2 from "@clack/prompts";
|
|
1565
1565
|
// package.json
|
|
1566
|
-
var version = "0.19.
|
|
1566
|
+
var version = "0.19.2";
|
|
1567
1567
|
|
|
1568
1568
|
// ../cli/telemetry.ts
|
|
1569
1569
|
var import_proper_lockfile2 = __toESM(require_proper_lockfile(), 1);
|
|
@@ -9137,7 +9137,11 @@ import { existsSync } from "node:fs";
|
|
|
9137
9137
|
import { readFile as readFile3, rm as rm3, writeFile as writeFile3 } from "node:fs/promises";
|
|
9138
9138
|
import { join as join3, sep } from "node:path";
|
|
9139
9139
|
var nativeBillingPath = join3("packages", "disposabl-kit", "mobile", "billing") + sep;
|
|
9140
|
-
var
|
|
9140
|
+
var nativeBillingTests = [
|
|
9141
|
+
join3("packages", "tests", "native-billing.test.ts"),
|
|
9142
|
+
join3("packages", "tests", "mobile-storefront-observer.test.ts"),
|
|
9143
|
+
join3("packages", "tests", "components", "mobile-storefront-observer.test.mjs")
|
|
9144
|
+
];
|
|
9141
9145
|
var purchasesPackage = "react-native-purchases";
|
|
9142
9146
|
var purchasesPackages = [purchasesPackage, "react-native-purchases-ui"];
|
|
9143
9147
|
var dependencyFields = [
|
|
@@ -9192,7 +9196,8 @@ async function omitMobilePayments(root) {
|
|
|
9192
9196
|
const source = await readFile3(path, "utf8");
|
|
9193
9197
|
await writeFile3(path, source.replace(/^[ \t]*\/\/ payments:start\r?\n[\s\S]*?^[ \t]*\/\/ payments:end\r?\n/gm, "").replace(/^[ \t]*\{\/\* payments:start \*\/\}\r?\n[\s\S]*?^[ \t]*\{\/\* payments:end \*\/\}\r?\n/gm, ""));
|
|
9194
9198
|
await rm3(join3(root, nativeBillingPath), { recursive: true, force: true });
|
|
9195
|
-
|
|
9199
|
+
for (const path of nativeBillingTests)
|
|
9200
|
+
await rm3(join3(root, path), { force: true });
|
|
9196
9201
|
}
|
|
9197
9202
|
|
|
9198
9203
|
// ../disposabl-kit/android-architectures.ts
|
|
@@ -9287,6 +9292,8 @@ For Disposabl development and delivery, read [.agents/skills/disposabl-app/SKILL
|
|
|
9287
9292
|
await rm4(join4(tests, entry));
|
|
9288
9293
|
}
|
|
9289
9294
|
if (!folders.includes("apps/mobile")) {
|
|
9295
|
+
for (const path of nativeBillingTests)
|
|
9296
|
+
await rm4(join4(destination, path), { force: true });
|
|
9290
9297
|
await rm4(join4(tests, "helpers/mobile-simulator"), {
|
|
9291
9298
|
recursive: true,
|
|
9292
9299
|
force: true
|
|
@@ -9613,7 +9620,7 @@ async function reconfigureApp(template, directory, version, options) {
|
|
|
9613
9620
|
}
|
|
9614
9621
|
}
|
|
9615
9622
|
for (const path of oldFiles) {
|
|
9616
|
-
if (nextFiles.has(path) || !(path.startsWith(nativeBillingPath) || path
|
|
9623
|
+
if (nextFiles.has(path) || !(path.startsWith(nativeBillingPath) || nativeBillingTests.includes(path) || disableAuth && authStarterPaths.some((authPath) => path === authPath || path.startsWith(authPath + sep2))))
|
|
9617
9624
|
continue;
|
|
9618
9625
|
const current = await safeRead(directory, path);
|
|
9619
9626
|
if (!current)
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# Storefront observation
|
|
2
|
+
|
|
3
|
+
This local iOS Expo module invalidates cached website-checkout eligibility when StoreKit reports a storefront change. It registers a retained `SKPaymentTransactionObserver` synchronously before `start()` resolves. Its required transaction callback is intentionally empty; RevenueCat owns purchase processing.
|
|
4
|
+
|
|
5
|
+
Observation is reference-counted on the main dispatch queue. Each successful start needs a stop, and module destruction removes all remaining observation. Missing native support leaves website checkout unavailable.
|
|
6
|
+
|
|
7
|
+
StoreKit callback delivery to JavaScript is asynchronous. The billing hook also rechecks eligibility before browser handoff and cancels when an observed change invalidates that operation. These checks do not make the storefront read and browser opening atomic or monitor a checkout after the browser opens.
|
|
8
|
+
|
|
9
|
+
The queue observer API is deprecated, but provides a documented registration boundary. Do not replace it with an unawaited `Storefront.updates` task and assume observation is already active when the start call returns.
|
package/dist/templates/default/apps/mobile/modules/storefront-observer/expo-module.config.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{ "platforms": ["ios"], "ios": { "modules": ["StorefrontObserverModule"] } }
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
Pod::Spec.new do |s|
|
|
2
|
+
s.name = 'StorefrontObserver'
|
|
3
|
+
s.version = '1.0.0'
|
|
4
|
+
s.summary = 'StoreKit storefront change signal'
|
|
5
|
+
s.description = 'Observes StoreKit storefront changes.'
|
|
6
|
+
s.license = { :type => 'MIT' }
|
|
7
|
+
s.author = 'Disposabl'
|
|
8
|
+
s.homepage = 'https://expo.dev'
|
|
9
|
+
s.platform = :ios, '16.4'
|
|
10
|
+
s.swift_version = '5.9'
|
|
11
|
+
s.source = { :git => 'https://github.com/expo/expo.git' }
|
|
12
|
+
s.static_framework = true
|
|
13
|
+
s.dependency 'ExpoModulesCore'
|
|
14
|
+
s.source_files = '**/*.{h,m,swift}'
|
|
15
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import ExpoModulesCore
|
|
2
|
+
import StoreKit
|
|
3
|
+
|
|
4
|
+
// All mutable state is confined to the main dispatch queue, including teardown.
|
|
5
|
+
// Registration is synchronous. RevenueCat remains the transaction owner.
|
|
6
|
+
private final class StorefrontObservation: NSObject, SKPaymentTransactionObserver {
|
|
7
|
+
private var observers = 0
|
|
8
|
+
private var changed: (() -> Void)?
|
|
9
|
+
|
|
10
|
+
func start(changed: @escaping () -> Void) {
|
|
11
|
+
dispatchPrecondition(condition: .onQueue(.main))
|
|
12
|
+
self.changed = changed
|
|
13
|
+
observers += 1
|
|
14
|
+
if observers == 1 { SKPaymentQueue.default().add(self) }
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
func stop(all: Bool = false) {
|
|
18
|
+
dispatchPrecondition(condition: .onQueue(.main))
|
|
19
|
+
guard observers > 0 else { return }
|
|
20
|
+
observers = all ? 0 : observers - 1
|
|
21
|
+
if observers == 0 {
|
|
22
|
+
SKPaymentQueue.default().remove(self)
|
|
23
|
+
changed = nil
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
|
|
28
|
+
// Required protocol callback. Never finish, acknowledge, or modify purchases.
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
func paymentQueueDidChangeStorefront(_ queue: SKPaymentQueue) {
|
|
32
|
+
DispatchQueue.main.async { [weak self] in self?.changed?() }
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public final class StorefrontObserverModule: Module {
|
|
37
|
+
private let observation = StorefrontObservation()
|
|
38
|
+
|
|
39
|
+
public func definition() -> ModuleDefinition {
|
|
40
|
+
Name("StorefrontObserver")
|
|
41
|
+
Events("onStorefrontChange")
|
|
42
|
+
|
|
43
|
+
AsyncFunction("start") { [weak self] () -> Bool in
|
|
44
|
+
guard let self else { return false }
|
|
45
|
+
self.observation.start { [weak self] in
|
|
46
|
+
self?.sendEvent("onStorefrontChange", [:])
|
|
47
|
+
}
|
|
48
|
+
return true
|
|
49
|
+
}.runOnQueue(.main)
|
|
50
|
+
|
|
51
|
+
AsyncFunction("stop") { [observation] in
|
|
52
|
+
observation.stop()
|
|
53
|
+
}.runOnQueue(.main)
|
|
54
|
+
|
|
55
|
+
OnDestroy { [observation] in
|
|
56
|
+
DispatchQueue.main.async { observation.stop(all: true) }
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -82,6 +82,10 @@ export function createProgram(
|
|
|
82
82
|
const program = new Command()
|
|
83
83
|
.name(Object.keys(manifest.bin)[0])
|
|
84
84
|
.version(manifest.version);
|
|
85
|
+
program.addHelpText(
|
|
86
|
+
"after",
|
|
87
|
+
"\nAPI commands are loaded from the configured service. Use <command> --help for their options.\n",
|
|
88
|
+
);
|
|
85
89
|
if (centralAnalytics)
|
|
86
90
|
program.addHelpText(
|
|
87
91
|
"after",
|
|
@@ -362,7 +366,8 @@ export async function runApiCli(argv: string[], serviceOrigin: string) {
|
|
|
362
366
|
}
|
|
363
367
|
}
|
|
364
368
|
}
|
|
365
|
-
|
|
369
|
+
if (argv.length <= 2) program.outputHelp();
|
|
370
|
+
else await program.parseAsync(argv);
|
|
366
371
|
}
|
|
367
372
|
|
|
368
373
|
/** Offline auth/version help remains usable even when the API is unavailable. */
|
|
@@ -376,6 +381,7 @@ export function needsContract(
|
|
|
376
381
|
const command = operands[0] === "help" ? operands[1] : operands[0];
|
|
377
382
|
return (
|
|
378
383
|
Boolean(origin) &&
|
|
384
|
+
command !== undefined &&
|
|
379
385
|
!args.some((arg) => arg === "--version" || arg === "-V") &&
|
|
380
386
|
command !== "login" &&
|
|
381
387
|
command !== "logout"
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
LICENSE
|
|
@@ -2,7 +2,7 @@ import { createFileRoute } from "@tanstack/react-router";
|
|
|
2
2
|
import { useAuthActions } from "@convex-dev/auth/react";
|
|
3
3
|
import { makeFunctionReference } from "convex/server";
|
|
4
4
|
import { useMutation, useQuery } from "convex/react";
|
|
5
|
-
import {
|
|
5
|
+
import { useLayoutEffect, useRef, useState } from "react";
|
|
6
6
|
import { PasswordPage } from "../components/password-page";
|
|
7
7
|
import { Button } from "../components/ui/button";
|
|
8
8
|
|
|
@@ -31,7 +31,7 @@ function BillingLogin() {
|
|
|
31
31
|
const [framed] = useState(
|
|
32
32
|
() => typeof window !== "undefined" && window.top !== window.self,
|
|
33
33
|
);
|
|
34
|
-
const [code] = useState(() =>
|
|
34
|
+
const [code, setCode] = useState(() =>
|
|
35
35
|
typeof window === "undefined" ? "" : window.location.hash.slice(1),
|
|
36
36
|
);
|
|
37
37
|
const account = useQuery(inspect, code && !framed ? { code } : "skip");
|
|
@@ -39,21 +39,69 @@ function BillingLogin() {
|
|
|
39
39
|
const { setSession } = useAuthActions();
|
|
40
40
|
const [pending, setPending] = useState(false);
|
|
41
41
|
const [error, setError] = useState("");
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
42
|
+
const [adopting, setAdopting] = useState(false);
|
|
43
|
+
const generation = useRef(0);
|
|
44
|
+
const currentCode = useRef(code);
|
|
45
|
+
const consentUrl = useRef("");
|
|
46
|
+
const inFlight = useRef(false);
|
|
47
|
+
const adoption = useRef(false);
|
|
48
|
+
useLayoutEffect(() => {
|
|
49
|
+
const scrub = () => {
|
|
50
|
+
window.history.replaceState(
|
|
51
|
+
window.history.state,
|
|
52
|
+
"",
|
|
53
|
+
window.location.pathname,
|
|
54
|
+
);
|
|
55
|
+
consentUrl.current = window.location.href;
|
|
56
|
+
};
|
|
57
|
+
const changed = (event: HashChangeEvent) => {
|
|
58
|
+
const fragment = new URL(event.newURL).hash;
|
|
59
|
+
if (window.location.hash !== fragment) return;
|
|
60
|
+
generation.current++;
|
|
61
|
+
currentCode.current = fragment.slice(1);
|
|
62
|
+
inFlight.current = false;
|
|
63
|
+
setPending(false);
|
|
64
|
+
setError("");
|
|
65
|
+
setCode(fragment.slice(1));
|
|
66
|
+
scrub();
|
|
67
|
+
};
|
|
68
|
+
scrub();
|
|
69
|
+
window.addEventListener("hashchange", changed);
|
|
70
|
+
return () => {
|
|
71
|
+
generation.current++;
|
|
72
|
+
window.removeEventListener("hashchange", changed);
|
|
73
|
+
};
|
|
48
74
|
}, []);
|
|
49
75
|
async function proceed() {
|
|
50
|
-
if (
|
|
76
|
+
if (
|
|
77
|
+
!account?.name ||
|
|
78
|
+
pending ||
|
|
79
|
+
inFlight.current ||
|
|
80
|
+
adoption.current ||
|
|
81
|
+
code !== currentCode.current ||
|
|
82
|
+
window.location.href !== consentUrl.current
|
|
83
|
+
)
|
|
84
|
+
return;
|
|
85
|
+
const version = generation.current;
|
|
86
|
+
const current = () =>
|
|
87
|
+
version === generation.current &&
|
|
88
|
+
window.location.href === consentUrl.current;
|
|
89
|
+
inFlight.current = true;
|
|
51
90
|
setPending(true);
|
|
52
91
|
try {
|
|
53
92
|
const { purchase, expiresAt, returnClaim, ...tokens } = await exchange({
|
|
54
93
|
code,
|
|
55
94
|
});
|
|
56
|
-
|
|
95
|
+
if (!current()) return;
|
|
96
|
+
adoption.current = true;
|
|
97
|
+
setAdopting(true);
|
|
98
|
+
try {
|
|
99
|
+
await setSession(tokens);
|
|
100
|
+
} finally {
|
|
101
|
+
adoption.current = false;
|
|
102
|
+
setAdopting(false);
|
|
103
|
+
}
|
|
104
|
+
if (!current()) return;
|
|
57
105
|
window.sessionStorage.setItem(
|
|
58
106
|
"billing-from-ios",
|
|
59
107
|
JSON.stringify({
|
|
@@ -65,10 +113,16 @@ function BillingLogin() {
|
|
|
65
113
|
);
|
|
66
114
|
window.location.replace("/pricing?source=ios");
|
|
67
115
|
} catch {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
116
|
+
if (current()) {
|
|
117
|
+
setError(
|
|
118
|
+
"This link could not sign you in. Open billing again from the app.",
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
} finally {
|
|
122
|
+
if (current()) {
|
|
123
|
+
inFlight.current = false;
|
|
124
|
+
setPending(false);
|
|
125
|
+
}
|
|
72
126
|
}
|
|
73
127
|
}
|
|
74
128
|
return (
|
|
@@ -84,7 +138,7 @@ function BillingLogin() {
|
|
|
84
138
|
<>
|
|
85
139
|
<p className="break-words font-medium">{account.name}</p>
|
|
86
140
|
<p>This signs this browser into the account shown above.</p>
|
|
87
|
-
<Button disabled={pending} onClick={() => void proceed()}>
|
|
141
|
+
<Button disabled={pending || adopting} onClick={() => void proceed()}>
|
|
88
142
|
{pending ? "Signing in…" : `Continue as ${account.name}`}
|
|
89
143
|
</Button>
|
|
90
144
|
</>
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { requireOptionalNativeModule } from "expo";
|
|
2
|
+
|
|
3
|
+
type Observer = {
|
|
4
|
+
addListener: (event: string, callback: () => void) => { remove: () => void };
|
|
5
|
+
start: () => Promise<boolean>;
|
|
6
|
+
stop: () => Promise<void>;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
/** Subscribe before reading eligibility. Missing native support fails closed. */
|
|
10
|
+
export function observeStorefront(onChange: () => void) {
|
|
11
|
+
let removed = false;
|
|
12
|
+
let listener: { remove: () => void } | undefined;
|
|
13
|
+
let native: Observer | null = null;
|
|
14
|
+
try {
|
|
15
|
+
native = requireOptionalNativeModule<Observer>("StorefrontObserver");
|
|
16
|
+
if (native)
|
|
17
|
+
listener = native.addListener("onStorefrontChange", () => {
|
|
18
|
+
if (!removed) onChange();
|
|
19
|
+
});
|
|
20
|
+
} catch {
|
|
21
|
+
native = null;
|
|
22
|
+
}
|
|
23
|
+
const active = native;
|
|
24
|
+
const ready =
|
|
25
|
+
active && listener
|
|
26
|
+
? Promise.resolve()
|
|
27
|
+
.then(() => active.start())
|
|
28
|
+
.then(async (started) => {
|
|
29
|
+
if (started && removed) await active.stop().catch(() => undefined);
|
|
30
|
+
return started && !removed;
|
|
31
|
+
})
|
|
32
|
+
.catch(() => false)
|
|
33
|
+
: Promise.resolve(false);
|
|
34
|
+
return {
|
|
35
|
+
ready,
|
|
36
|
+
remove() {
|
|
37
|
+
if (removed) return;
|
|
38
|
+
removed = true;
|
|
39
|
+
listener?.remove();
|
|
40
|
+
void ready.then((started) => {
|
|
41
|
+
if (started) void active?.stop().catch(() => undefined);
|
|
42
|
+
});
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
@@ -4,6 +4,7 @@ import { makeFunctionReference } from "convex/server";
|
|
|
4
4
|
import { useCallback, useEffect, useRef, useState } from "react";
|
|
5
5
|
import { AppState, Linking, Platform } from "react-native";
|
|
6
6
|
import { billingSession } from "./session";
|
|
7
|
+
import { observeStorefront } from "./storefront-observer";
|
|
7
8
|
import {
|
|
8
9
|
guardedWebHandoff,
|
|
9
10
|
paymentOptions,
|
|
@@ -43,13 +44,14 @@ export function useWebCheckout(
|
|
|
43
44
|
const [topupAvailable, setTopupAvailable] = useState(false);
|
|
44
45
|
const [busy, setBusy] = useState(false);
|
|
45
46
|
const [error, setError] = useState("");
|
|
46
|
-
const lifecycle = useRef({ sequence: 0 });
|
|
47
|
+
const lifecycle = useRef({ sequence: 0, observing: false, eligible: false });
|
|
47
48
|
const refresh = useCallback(
|
|
48
49
|
async (generation: number) => {
|
|
49
50
|
const seq = ++lifecycle.current.sequence;
|
|
51
|
+
lifecycle.current.eligible = false;
|
|
50
52
|
setAvailable(false);
|
|
51
53
|
setTopupAvailable(false);
|
|
52
|
-
if (Platform.OS !== "ios" || !key) return;
|
|
54
|
+
if (Platform.OS !== "ios" || !key || !lifecycle.current.observing) return;
|
|
53
55
|
try {
|
|
54
56
|
const latest = await snapshot();
|
|
55
57
|
if (latest.customerId !== customerId) return;
|
|
@@ -96,6 +98,7 @@ export function useWebCheckout(
|
|
|
96
98
|
seq === lifecycle.current.sequence &&
|
|
97
99
|
billingSession.current(customerId, generation)
|
|
98
100
|
) {
|
|
101
|
+
lifecycle.current.eligible = true;
|
|
99
102
|
setAvailable(value.monthly && latest.hasMonthlyPlan === false);
|
|
100
103
|
setTopupAvailable(value.topup);
|
|
101
104
|
}
|
|
@@ -107,25 +110,54 @@ export function useWebCheckout(
|
|
|
107
110
|
);
|
|
108
111
|
useEffect(() => {
|
|
109
112
|
const state = lifecycle.current;
|
|
113
|
+
let active = true;
|
|
114
|
+
state.observing = false;
|
|
115
|
+
state.eligible = false;
|
|
110
116
|
const update = () => void refresh(version.current);
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
117
|
+
const observer =
|
|
118
|
+
Platform.OS === "ios"
|
|
119
|
+
? observeStorefront(() => {
|
|
120
|
+
state.sequence++;
|
|
121
|
+
state.eligible = false;
|
|
122
|
+
setAvailable(false);
|
|
123
|
+
setTopupAvailable(false);
|
|
124
|
+
if (state.observing) update();
|
|
125
|
+
})
|
|
126
|
+
: null;
|
|
127
|
+
void observer?.ready.then((ready) => {
|
|
128
|
+
if (!active || !ready) return;
|
|
129
|
+
state.observing = true;
|
|
130
|
+
update();
|
|
131
|
+
});
|
|
132
|
+
const listener = AppState.addEventListener("change", (next) => {
|
|
133
|
+
if (next === "active") update();
|
|
114
134
|
});
|
|
115
135
|
return () => {
|
|
136
|
+
active = false;
|
|
116
137
|
state.sequence++;
|
|
138
|
+
state.observing = false;
|
|
139
|
+
state.eligible = false;
|
|
140
|
+
observer?.remove();
|
|
117
141
|
listener.remove();
|
|
118
142
|
};
|
|
119
143
|
}, [refresh, version]);
|
|
120
144
|
async function open(purchase: "monthly" | "topup" = "monthly") {
|
|
121
145
|
if (
|
|
146
|
+
!lifecycle.current.observing ||
|
|
147
|
+
!lifecycle.current.eligible ||
|
|
122
148
|
!(purchase === "monthly" ? available && productId : topupAvailable) ||
|
|
123
149
|
!key ||
|
|
124
150
|
!billingSession.beginTransaction()
|
|
125
151
|
)
|
|
126
152
|
return false;
|
|
127
153
|
const generation = version.current;
|
|
128
|
-
const
|
|
154
|
+
const sequence = lifecycle.current.sequence;
|
|
155
|
+
const sameSession = () => billingSession.current(customerId, generation);
|
|
156
|
+
const current = () =>
|
|
157
|
+
sameSession() &&
|
|
158
|
+
lifecycle.current.observing &&
|
|
159
|
+
lifecycle.current.eligible &&
|
|
160
|
+
lifecycle.current.sequence === sequence;
|
|
129
161
|
setBusy(true);
|
|
130
162
|
setError("");
|
|
131
163
|
try {
|
|
@@ -193,7 +225,7 @@ export function useWebCheckout(
|
|
|
193
225
|
return false;
|
|
194
226
|
} finally {
|
|
195
227
|
billingSession.endTransaction();
|
|
196
|
-
if (
|
|
228
|
+
if (sameSession()) setBusy(false);
|
|
197
229
|
}
|
|
198
230
|
}
|
|
199
231
|
return {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
LICENSE
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { afterEach, expect, mock, test } from "bun:test";
|
|
2
|
+
import { createElement } from "react";
|
|
3
|
+
import { act, create } from "react-test-renderer";
|
|
4
|
+
globalThis.IS_REACT_ACT_ENVIRONMENT = true;
|
|
5
|
+
const listeners = new Set();
|
|
6
|
+
let starts = 0,
|
|
7
|
+
stops = 0,
|
|
8
|
+
country = "USA",
|
|
9
|
+
current = true,
|
|
10
|
+
failStart = false,
|
|
11
|
+
missing = false,
|
|
12
|
+
pending,
|
|
13
|
+
startPending,
|
|
14
|
+
onStorefrontRead;
|
|
15
|
+
const link = {
|
|
16
|
+
url: `https://fixture.invalid/billing-login#${"A".repeat(43)}`,
|
|
17
|
+
userId: "owner",
|
|
18
|
+
};
|
|
19
|
+
const handoff = mock(async () => link);
|
|
20
|
+
const openURL = mock(async () => {});
|
|
21
|
+
const observer = {
|
|
22
|
+
addListener(_event, callback) {
|
|
23
|
+
listeners.add(callback);
|
|
24
|
+
return { remove: () => listeners.delete(callback) };
|
|
25
|
+
},
|
|
26
|
+
async start() {
|
|
27
|
+
starts++;
|
|
28
|
+
if (failStart) throw Error("unavailable");
|
|
29
|
+
return startPending ? await startPending : true;
|
|
30
|
+
},
|
|
31
|
+
async stop() {
|
|
32
|
+
stops++;
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
mock.module("expo", () => ({
|
|
36
|
+
requireOptionalNativeModule: () => (missing ? null : observer),
|
|
37
|
+
}));
|
|
38
|
+
mock.module("expo-constants", () => ({ default: { expoConfig: {} } }));
|
|
39
|
+
mock.module("convex/server", () => ({
|
|
40
|
+
makeFunctionReference: () => "fixture",
|
|
41
|
+
}));
|
|
42
|
+
mock.module("convex/react", () => ({ useMutation: () => handoff }));
|
|
43
|
+
mock.module("react-native", () => ({
|
|
44
|
+
Platform: { OS: "ios" },
|
|
45
|
+
Linking: { openURL },
|
|
46
|
+
AppState: { addEventListener: () => ({ remove() {} }) },
|
|
47
|
+
}));
|
|
48
|
+
mock.module("../../../packages/disposabl-kit/mobile/billing/session", () => ({
|
|
49
|
+
billingSession: {
|
|
50
|
+
current: () => current,
|
|
51
|
+
beginTransaction: () => true,
|
|
52
|
+
endTransaction: () => {},
|
|
53
|
+
run: async (_id, _version, _key, fn) =>
|
|
54
|
+
fn({
|
|
55
|
+
canMakePayments: async () => true,
|
|
56
|
+
getStorefront: async () => {
|
|
57
|
+
const value = pending
|
|
58
|
+
? await pending
|
|
59
|
+
: country === "error"
|
|
60
|
+
? await Promise.reject(Error("unknown"))
|
|
61
|
+
: country
|
|
62
|
+
? { countryCode: country }
|
|
63
|
+
: null;
|
|
64
|
+
onStorefrontRead?.();
|
|
65
|
+
return value;
|
|
66
|
+
},
|
|
67
|
+
getCustomerInfo: async () => ({ activeSubscriptions: [] }),
|
|
68
|
+
getOfferings: async () => ({}),
|
|
69
|
+
getAppUserID: async () => "owner",
|
|
70
|
+
}),
|
|
71
|
+
},
|
|
72
|
+
}));
|
|
73
|
+
mock.module("../../../packages/disposabl-kit/mobile/billing/paywall", () => ({
|
|
74
|
+
monthlyOfferingId: "",
|
|
75
|
+
selectMonthlyOffering: () => ({}),
|
|
76
|
+
}));
|
|
77
|
+
mock.module("../../../packages/disposabl-kit/mobile/billing/topup", () => ({
|
|
78
|
+
matchingTopupRoute: () => false,
|
|
79
|
+
loadTopup: async () => null,
|
|
80
|
+
}));
|
|
81
|
+
const { useWebCheckout } =
|
|
82
|
+
await import("../../../packages/disposabl-kit/mobile/billing/web-checkout");
|
|
83
|
+
let renderer, result;
|
|
84
|
+
const version = { current: 1 };
|
|
85
|
+
const snapshot = async () => ({
|
|
86
|
+
customerId: "owner",
|
|
87
|
+
nativeSubscriptionsEnabled: true,
|
|
88
|
+
hasMonthlyPlan: false,
|
|
89
|
+
mobileWebCheckoutEnabled: true,
|
|
90
|
+
});
|
|
91
|
+
function Harness() {
|
|
92
|
+
result = useWebCheckout("owner", version, "key", "product", snapshot);
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
async function mount() {
|
|
96
|
+
await act(async () => {
|
|
97
|
+
renderer = create(createElement(Harness));
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
async function change() {
|
|
101
|
+
await act(async () => {
|
|
102
|
+
for (const listener of [...listeners]) listener();
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
afterEach(async () => {
|
|
106
|
+
await act(async () => renderer?.unmount());
|
|
107
|
+
starts = 0;
|
|
108
|
+
stops = 0;
|
|
109
|
+
country = "USA";
|
|
110
|
+
current = true;
|
|
111
|
+
failStart = false;
|
|
112
|
+
missing = false;
|
|
113
|
+
pending = undefined;
|
|
114
|
+
startPending = undefined;
|
|
115
|
+
onStorefrontRead = undefined;
|
|
116
|
+
handoff.mockReset().mockImplementation(async () => link);
|
|
117
|
+
openURL.mockClear();
|
|
118
|
+
listeners.clear();
|
|
119
|
+
});
|
|
120
|
+
test("active storefront changes and unknown/error fail closed", async () => {
|
|
121
|
+
await mount();
|
|
122
|
+
expect(result.available).toBe(true);
|
|
123
|
+
country = "CAN";
|
|
124
|
+
await change();
|
|
125
|
+
expect(result.available).toBe(false);
|
|
126
|
+
country = null;
|
|
127
|
+
await change();
|
|
128
|
+
expect(result.available).toBe(false);
|
|
129
|
+
country = "error";
|
|
130
|
+
await change();
|
|
131
|
+
expect(result.available).toBe(false);
|
|
132
|
+
country = "USA";
|
|
133
|
+
await change();
|
|
134
|
+
expect(result.available).toBe(true);
|
|
135
|
+
});
|
|
136
|
+
test("event invalidates pending eligibility and stale session results", async () => {
|
|
137
|
+
await mount();
|
|
138
|
+
let finish;
|
|
139
|
+
pending = new Promise((resolve) => {
|
|
140
|
+
finish = resolve;
|
|
141
|
+
});
|
|
142
|
+
await act(async () => {
|
|
143
|
+
void result.refresh();
|
|
144
|
+
});
|
|
145
|
+
expect(result.available).toBe(false);
|
|
146
|
+
country = "CAN";
|
|
147
|
+
pending = undefined;
|
|
148
|
+
await change();
|
|
149
|
+
expect(result.available).toBe(false);
|
|
150
|
+
await act(async () => finish({ countryCode: "USA" }));
|
|
151
|
+
expect(result.available).toBe(false);
|
|
152
|
+
current = false;
|
|
153
|
+
country = "USA";
|
|
154
|
+
await change();
|
|
155
|
+
expect(result.available).toBe(false);
|
|
156
|
+
});
|
|
157
|
+
test("observer start failure and cleanup fail closed", async () => {
|
|
158
|
+
failStart = true;
|
|
159
|
+
await mount();
|
|
160
|
+
expect(result.available).toBe(false);
|
|
161
|
+
expect(starts).toBe(1);
|
|
162
|
+
await act(async () => renderer.unmount());
|
|
163
|
+
expect(listeners.size).toBe(0);
|
|
164
|
+
expect(stops).toBe(0);
|
|
165
|
+
});
|
|
166
|
+
test("multiple mounts retain observation until each is removed", async () => {
|
|
167
|
+
await mount();
|
|
168
|
+
let second;
|
|
169
|
+
await act(async () => {
|
|
170
|
+
second = create(createElement(Harness));
|
|
171
|
+
});
|
|
172
|
+
expect(starts).toBe(2);
|
|
173
|
+
await act(async () => renderer.unmount());
|
|
174
|
+
expect(stops).toBe(1);
|
|
175
|
+
expect(listeners.size).toBe(1);
|
|
176
|
+
await act(async () => second.unmount());
|
|
177
|
+
expect(stops).toBe(2);
|
|
178
|
+
expect(listeners.size).toBe(0);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
test("missing module and unmount during native start remain closed", async () => {
|
|
182
|
+
missing = true;
|
|
183
|
+
await mount();
|
|
184
|
+
expect(result.available).toBe(false);
|
|
185
|
+
expect(listeners.size).toBe(0);
|
|
186
|
+
await act(async () => renderer.unmount());
|
|
187
|
+
missing = false;
|
|
188
|
+
let finish;
|
|
189
|
+
startPending = new Promise((resolve) => {
|
|
190
|
+
finish = resolve;
|
|
191
|
+
});
|
|
192
|
+
await mount();
|
|
193
|
+
await act(async () => renderer.unmount());
|
|
194
|
+
await act(async () => finish(true));
|
|
195
|
+
expect(stops).toBe(1);
|
|
196
|
+
expect(listeners.size).toBe(0);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
test("an unchanged US storefront opens the validated handoff", async () => {
|
|
200
|
+
await mount();
|
|
201
|
+
let opened;
|
|
202
|
+
await act(async () => {
|
|
203
|
+
opened = await result.open();
|
|
204
|
+
});
|
|
205
|
+
expect(opened).toBe(true);
|
|
206
|
+
expect(openURL).toHaveBeenCalledWith(link.url);
|
|
207
|
+
expect(result.busy).toBe(false);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
test("a change during the final storefront read cancels the handoff and clears busy", async () => {
|
|
211
|
+
await mount();
|
|
212
|
+
handoff.mockImplementationOnce(async () => {
|
|
213
|
+
onStorefrontRead = () => {
|
|
214
|
+
onStorefrontRead = undefined;
|
|
215
|
+
country = "CAN";
|
|
216
|
+
for (const listener of [...listeners]) listener();
|
|
217
|
+
};
|
|
218
|
+
return link;
|
|
219
|
+
});
|
|
220
|
+
let opened;
|
|
221
|
+
await act(async () => {
|
|
222
|
+
opened = await result.open();
|
|
223
|
+
});
|
|
224
|
+
expect(handoff).toHaveBeenCalledTimes(1);
|
|
225
|
+
expect(opened).toBe(false);
|
|
226
|
+
expect(openURL).not.toHaveBeenCalled();
|
|
227
|
+
expect(result.busy).toBe(false);
|
|
228
|
+
expect(result.available).toBe(false);
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
test("a queued callback from a removed subscription is ignored", async () => {
|
|
232
|
+
const { observeStorefront } =
|
|
233
|
+
await import("../../../packages/disposabl-kit/mobile/billing/storefront-observer");
|
|
234
|
+
const changed = mock(() => {});
|
|
235
|
+
const subscription = observeStorefront(changed);
|
|
236
|
+
await subscription.ready;
|
|
237
|
+
const queued = [...listeners][0];
|
|
238
|
+
subscription.remove();
|
|
239
|
+
queued();
|
|
240
|
+
expect(changed).not.toHaveBeenCalled();
|
|
241
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { test } from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { spawnSync } from "node:child_process";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
|
|
6
|
+
test("native storefront observation and eligibility races", () => {
|
|
7
|
+
const result = spawnSync(
|
|
8
|
+
process.execPath,
|
|
9
|
+
[
|
|
10
|
+
"test",
|
|
11
|
+
join(
|
|
12
|
+
import.meta.dirname,
|
|
13
|
+
"components/mobile-storefront-observer.test.mjs",
|
|
14
|
+
),
|
|
15
|
+
],
|
|
16
|
+
{
|
|
17
|
+
encoding: "utf8",
|
|
18
|
+
env: { ...process.env, E2E_TELEMETRY_DISABLED: "1", DO_NOT_TRACK: "1" },
|
|
19
|
+
},
|
|
20
|
+
);
|
|
21
|
+
assert.equal(result.status, 0, result.stdout + result.stderr);
|
|
22
|
+
});
|