@codual/react-native-ui 0.0.0-stage → 0.1.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.
- package/LICENSE +202 -0
- package/NOTICE +2 -0
- package/README.md +18 -2
- package/dist/announce.d.ts +2 -0
- package/dist/announce.js +8 -0
- package/dist/charts/data.d.ts +32 -0
- package/dist/charts/data.js +13 -0
- package/dist/charts/labels.d.ts +54 -0
- package/dist/charts/labels.js +91 -0
- package/dist/charts/machine.d.ts +62 -0
- package/dist/charts/machine.js +144 -0
- package/dist/charts/points.d.ts +23 -0
- package/dist/charts/points.js +46 -0
- package/dist/charts/summary.d.ts +29 -0
- package/dist/charts/summary.js +45 -0
- package/dist/charts/use-chart-interaction.d.ts +62 -0
- package/dist/charts/use-chart-interaction.js +79 -0
- package/dist/forms/async-checks.d.ts +34 -0
- package/dist/forms/async-checks.js +77 -0
- package/dist/forms/field-errors.d.ts +8 -0
- package/dist/forms/field-errors.js +16 -0
- package/dist/forms/form-types.d.ts +43 -0
- package/dist/forms/form-types.js +2 -0
- package/dist/forms/machine.d.ts +63 -0
- package/dist/forms/machine.js +251 -0
- package/dist/forms/numbers.d.ts +5 -0
- package/dist/forms/numbers.js +83 -0
- package/dist/forms/rules.d.ts +47 -0
- package/dist/forms/rules.js +72 -0
- package/dist/forms/server-errors.d.ts +17 -0
- package/dist/forms/server-errors.js +22 -0
- package/dist/forms/use-form.d.ts +49 -0
- package/dist/forms/use-form.js +61 -0
- package/dist/index.d.ts +50 -0
- package/dist/index.js +94 -0
- package/dist/lists/PagedList.d.ts +47 -0
- package/dist/lists/PagedList.js +81 -0
- package/dist/lists/controller.d.ts +23 -0
- package/dist/lists/controller.js +20 -0
- package/dist/lists/inverted-list.d.ts +23 -0
- package/dist/lists/inverted-list.js +60 -0
- package/dist/lists/machine.d.ts +81 -0
- package/dist/lists/machine.js +252 -0
- package/dist/lists/newest-end.d.ts +30 -0
- package/dist/lists/newest-end.js +58 -0
- package/dist/lists/paged-content.d.ts +46 -0
- package/dist/lists/paged-content.js +65 -0
- package/dist/lists/paged-list-types.d.ts +56 -0
- package/dist/lists/paged-list-types.js +2 -0
- package/dist/lists/system-footer.d.ts +28 -0
- package/dist/lists/system-footer.js +50 -0
- package/dist/lists/use-paged-list.d.ts +25 -0
- package/dist/lists/use-paged-list.js +34 -0
- package/dist/permissions/access.d.ts +30 -0
- package/dist/permissions/access.js +2 -0
- package/dist/permissions/android-permission.d.ts +52 -0
- package/dist/permissions/android-permission.js +143 -0
- package/dist/permissions/permission.d.ts +69 -0
- package/dist/permissions/permission.js +201 -0
- package/dist/permissions/setup.d.ts +25 -0
- package/dist/permissions/setup.js +41 -0
- package/dist/permissions/status.d.ts +29 -0
- package/dist/permissions/status.js +18 -0
- package/dist/permissions/use-permission.d.ts +23 -0
- package/dist/permissions/use-permission.js +29 -0
- package/dist/purchases/Paywall.d.ts +27 -0
- package/dist/purchases/Paywall.js +28 -0
- package/dist/purchases/paywall-content.d.ts +20 -0
- package/dist/purchases/paywall-content.js +34 -0
- package/dist/purchases/purchases.d.ts +92 -0
- package/dist/purchases/purchases.js +236 -0
- package/dist/purchases/status.d.ts +62 -0
- package/dist/purchases/status.js +25 -0
- package/dist/purchases/store.d.ts +71 -0
- package/dist/purchases/store.js +2 -0
- package/dist/purchases/system-views.d.ts +49 -0
- package/dist/purchases/system-views.js +91 -0
- package/dist/purchases/use-purchases.d.ts +3 -0
- package/dist/purchases/use-purchases.js +8 -0
- package/dist/react-native-iap/iap-purchases.d.ts +24 -0
- package/dist/react-native-iap/iap-purchases.js +90 -0
- package/dist/react-native-iap/iap-store.d.ts +56 -0
- package/dist/react-native-iap/iap-store.js +249 -0
- package/dist/react-native-iap/iap-types.d.ts +12 -0
- package/dist/react-native-iap/iap-types.js +11 -0
- package/dist/react-native-iap/index.d.ts +31 -0
- package/dist/react-native-iap/index.js +24 -0
- package/dist/react-native-permissions/index.d.ts +24 -0
- package/dist/react-native-permissions/index.js +88 -0
- package/dist/screen-states/ScreenStates.d.ts +38 -0
- package/dist/screen-states/ScreenStates.js +69 -0
- package/dist/screen-states/errors.d.ts +9 -0
- package/dist/screen-states/errors.js +24 -0
- package/dist/screen-states/machine.d.ts +97 -0
- package/dist/screen-states/machine.js +172 -0
- package/dist/screen-states/system-views.d.ts +40 -0
- package/dist/screen-states/system-views.js +66 -0
- package/dist/screen-states/use-screen-states.d.ts +23 -0
- package/dist/screen-states/use-screen-states.js +41 -0
- package/dist/search/SearchResults.d.ts +22 -0
- package/dist/search/SearchResults.js +25 -0
- package/dist/search/announcements.d.ts +17 -0
- package/dist/search/announcements.js +31 -0
- package/dist/search/labels.d.ts +15 -0
- package/dist/search/labels.js +14 -0
- package/dist/search/machine.d.ts +65 -0
- package/dist/search/machine.js +241 -0
- package/dist/search/recent-search-sync.d.ts +32 -0
- package/dist/search/recent-search-sync.js +87 -0
- package/dist/search/recent-searches.d.ts +22 -0
- package/dist/search/recent-searches.js +40 -0
- package/dist/search/system-views.d.ts +11 -0
- package/dist/search/system-views.js +16 -0
- package/dist/search/types.d.ts +51 -0
- package/dist/search/types.js +2 -0
- package/dist/search/use-search.d.ts +73 -0
- package/dist/search/use-search.js +123 -0
- package/dist/selection/config.d.ts +34 -0
- package/dist/selection/config.js +13 -0
- package/dist/selection/labels.d.ts +19 -0
- package/dist/selection/labels.js +23 -0
- package/dist/selection/machine.d.ts +80 -0
- package/dist/selection/machine.js +274 -0
- package/dist/selection/snapshot.d.ts +54 -0
- package/dist/selection/snapshot.js +45 -0
- package/dist/selection/state.d.ts +25 -0
- package/dist/selection/state.js +24 -0
- package/dist/selection/system-views.d.ts +11 -0
- package/dist/selection/system-views.js +22 -0
- package/dist/selection/use-selection.d.ts +73 -0
- package/dist/selection/use-selection.js +100 -0
- package/dist/sheets/machine.d.ts +101 -0
- package/dist/sheets/machine.js +213 -0
- package/dist/sheets/use-sheet.d.ts +78 -0
- package/dist/sheets/use-sheet.js +103 -0
- package/dist/victory-native/index.d.ts +37 -0
- package/dist/victory-native/index.js +41 -0
- package/package.json +94 -4
- package/react-native-iap/package.json +4 -0
- package/react-native-permissions/package.json +4 -0
- package/victory-native/package.json +4 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.usePermission = usePermission;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const permission_1 = require("./permission");
|
|
6
|
+
const status_1 = require("./status");
|
|
7
|
+
/**
|
|
8
|
+
* One permission, asked for at the right moment. It reads the status on mount and never prompts; `ask` shows the
|
|
9
|
+
* app's explanation (`phase === "explaining"`), and only `accept` shows the system prompt; once the system won't ask
|
|
10
|
+
* again, it shows the app's Settings guidance instead. It reads again each time the app becomes active.
|
|
11
|
+
*
|
|
12
|
+
* Pass an adapter to keep one for the component (the first one passed is kept), or a permission from
|
|
13
|
+
* `createPermission` to share it between components.
|
|
14
|
+
*/
|
|
15
|
+
function usePermission(source, options = {}) {
|
|
16
|
+
const [permission] = (0, react_1.useState)(() => source instanceof permission_1.Permission ? source : (0, permission_1.createPermission)(source, options));
|
|
17
|
+
const snapshot = (0, react_1.useSyncExternalStore)(permission.subscribe, permission.getSnapshot);
|
|
18
|
+
return (0, react_1.useMemo)(() => ({
|
|
19
|
+
...snapshot,
|
|
20
|
+
isUsable: (0, status_1.isUsable)(snapshot.status),
|
|
21
|
+
ask: permission.ask,
|
|
22
|
+
suggest: permission.suggest,
|
|
23
|
+
askForFullAccess: permission.askForFullAccess,
|
|
24
|
+
accept: permission.accept,
|
|
25
|
+
decline: permission.decline,
|
|
26
|
+
openSettings: permission.openSettings,
|
|
27
|
+
refresh: permission.refresh,
|
|
28
|
+
}), [snapshot, permission]);
|
|
29
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type ReactElement, type ReactNode } from "react";
|
|
2
|
+
import type { RefreshControlProps } from "react-native";
|
|
3
|
+
import { type ScreenStatesViews } from "../screen-states/ScreenStates";
|
|
4
|
+
import { type PaywallContent } from "./paywall-content";
|
|
5
|
+
import type { Purchases } from "./purchases";
|
|
6
|
+
import { type PurchaseMessage } from "./status";
|
|
7
|
+
import type { StoreProduct } from "./store";
|
|
8
|
+
import { type PaywallLabels } from "./system-views";
|
|
9
|
+
export interface PaywallProps extends Omit<ScreenStatesViews<readonly StoreProduct[]>, "content" | "refreshError"> {
|
|
10
|
+
readonly purchases: Purchases;
|
|
11
|
+
/** The products to sell, in the order to show them. */
|
|
12
|
+
readonly productIds: readonly string[];
|
|
13
|
+
/** The product selected at first, if the store returns it; otherwise the first one. */
|
|
14
|
+
readonly initialSelection?: string;
|
|
15
|
+
/** The paywall's layout, from the loaded products. Pass `refreshControl` to a scroll view to pull to refresh. */
|
|
16
|
+
readonly content: (paywall: PaywallContent, refreshControl: ReactElement<RefreshControlProps>) => ReactNode;
|
|
17
|
+
/** Pending, failed, restored, nothing to restore and restore failed. By default, a system alert. */
|
|
18
|
+
readonly message?: (message: PurchaseMessage, dismiss: () => void) => ReactNode;
|
|
19
|
+
/** The text of the system message. */
|
|
20
|
+
readonly paywallLabels?: Partial<PaywallLabels>;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* A paywall: the products load as a screen state (`empty` when the store sells none of them here), then `content`
|
|
24
|
+
* lays them out with the app's own rows and buttons, or the system ones. The purchase belongs to `purchases`, so
|
|
25
|
+
* leaving the paywall never stops it.
|
|
26
|
+
*/
|
|
27
|
+
export declare function Paywall(props: PaywallProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Paywall = Paywall;
|
|
4
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
5
|
+
const react_1 = require("react");
|
|
6
|
+
const ScreenStates_1 = require("../screen-states/ScreenStates");
|
|
7
|
+
const paywall_content_1 = require("./paywall-content");
|
|
8
|
+
const status_1 = require("./status");
|
|
9
|
+
const system_views_1 = require("./system-views");
|
|
10
|
+
const use_purchases_1 = require("./use-purchases");
|
|
11
|
+
/**
|
|
12
|
+
* A paywall: the products load as a screen state (`empty` when the store sells none of them here), then `content`
|
|
13
|
+
* lays them out with the app's own rows and buttons, or the system ones. The purchase belongs to `purchases`, so
|
|
14
|
+
* leaving the paywall never stops it.
|
|
15
|
+
*/
|
|
16
|
+
function Paywall(props) {
|
|
17
|
+
const { purchases, productIds, initialSelection, content, message, paywallLabels, ...views } = props;
|
|
18
|
+
const snapshot = (0, use_purchases_1.usePurchases)(purchases);
|
|
19
|
+
const [selection, setSelection] = (0, react_1.useState)();
|
|
20
|
+
const shown = (0, status_1.purchaseMessage)(snapshot.status);
|
|
21
|
+
const labels = { ...system_views_1.defaultPaywallLabels, ...paywallLabels };
|
|
22
|
+
const messageView = (shown) => message === undefined ? ((0, jsx_runtime_1.jsx)(system_views_1.SystemPurchaseMessageAlert, { message: shown, dismiss: purchases.dismissMessage, labels: labels })) : (message(shown, purchases.dismissMessage));
|
|
23
|
+
return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(ScreenStates_1.ScreenStates, { ...views, load: () => purchases.products(productIds), content: (products, refreshControl) => {
|
|
24
|
+
const selected = (0, paywall_content_1.selectedProduct)(products, selection ?? initialSelection);
|
|
25
|
+
const select = (product) => setSelection(product.id);
|
|
26
|
+
return content((0, paywall_content_1.paywallContent)(purchases, snapshot, products, selected, select), refreshControl);
|
|
27
|
+
} }), shown === undefined ? null : messageView(shown)] }));
|
|
28
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Purchases, PurchasesSnapshot } from "./purchases";
|
|
2
|
+
import { type BuyButtonState, type PurchaseStatus } from "./status";
|
|
3
|
+
import type { StoreProduct } from "./store";
|
|
4
|
+
/** The loaded paywall, for the app's layout: the products, which one is selected, and the actions. */
|
|
5
|
+
export interface PaywallContent {
|
|
6
|
+
readonly products: readonly StoreProduct[];
|
|
7
|
+
readonly selected: StoreProduct | undefined;
|
|
8
|
+
readonly status: PurchaseStatus;
|
|
9
|
+
readonly buyButton: BuyButtonState;
|
|
10
|
+
readonly isSelected: (product: StoreProduct) => boolean;
|
|
11
|
+
/** Whether the user owns the product, so buying it again would buy nothing. */
|
|
12
|
+
readonly isOwned: (product: StoreProduct) => boolean;
|
|
13
|
+
readonly select: (product: StoreProduct) => void;
|
|
14
|
+
/** Buys the selected product. */
|
|
15
|
+
readonly buy: () => void;
|
|
16
|
+
readonly restore: () => void;
|
|
17
|
+
}
|
|
18
|
+
/** The selected product: the one with this ID if the store returned it, otherwise the first. */
|
|
19
|
+
export declare function selectedProduct(products: readonly StoreProduct[], selection: string | undefined): StoreProduct | undefined;
|
|
20
|
+
export declare function paywallContent(purchases: Purchases, snapshot: PurchasesSnapshot, products: readonly StoreProduct[], selected: StoreProduct | undefined, select: (product: StoreProduct) => void): PaywallContent;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.selectedProduct = selectedProduct;
|
|
4
|
+
exports.paywallContent = paywallContent;
|
|
5
|
+
const status_1 = require("./status");
|
|
6
|
+
/** The selected product: the one with this ID if the store returned it, otherwise the first. */
|
|
7
|
+
function selectedProduct(products, selection) {
|
|
8
|
+
return products.find((product) => product.id === selection) ?? products[0];
|
|
9
|
+
}
|
|
10
|
+
function buyButtonState(purchases, snapshot, selected) {
|
|
11
|
+
if (selected === undefined)
|
|
12
|
+
return { status: "unavailable" };
|
|
13
|
+
if ((0, status_1.isBusy)(snapshot.status))
|
|
14
|
+
return { status: "purchasing" };
|
|
15
|
+
return purchases.isOwned(selected)
|
|
16
|
+
? { status: "owned", product: selected }
|
|
17
|
+
: { status: "ready", product: selected };
|
|
18
|
+
}
|
|
19
|
+
function paywallContent(purchases, snapshot, products, selected, select) {
|
|
20
|
+
return {
|
|
21
|
+
products,
|
|
22
|
+
selected,
|
|
23
|
+
status: snapshot.status,
|
|
24
|
+
buyButton: buyButtonState(purchases, snapshot, selected),
|
|
25
|
+
isSelected: (product) => product.id === selected?.id,
|
|
26
|
+
isOwned: purchases.isOwned,
|
|
27
|
+
select,
|
|
28
|
+
buy: () => {
|
|
29
|
+
if (selected !== undefined)
|
|
30
|
+
purchases.buy(selected);
|
|
31
|
+
},
|
|
32
|
+
restore: purchases.restore,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { type PurchaseStatus } from "./status";
|
|
2
|
+
import type { Entitlement, PurchaseStore, StoreProduct, StoreTransaction } from "./store";
|
|
3
|
+
export interface PurchasesSnapshot {
|
|
4
|
+
readonly status: PurchaseStatus;
|
|
5
|
+
/** What the user owns, or undefined until the store has answered once. */
|
|
6
|
+
readonly entitlements: readonly Entitlement[] | undefined;
|
|
7
|
+
}
|
|
8
|
+
export interface PurchasesOptions {
|
|
9
|
+
/**
|
|
10
|
+
* Called once for each new transaction, before it is finished: grant a consumable, or send `transaction.proof` to
|
|
11
|
+
* your server. Reject to leave it unfinished, so the store gives it again later. Revoked transactions skip it.
|
|
12
|
+
*/
|
|
13
|
+
readonly deliver?: (transaction: StoreTransaction) => Promise<void>;
|
|
14
|
+
/** The current time in epoch milliseconds, for subscription expiry. */
|
|
15
|
+
readonly now?: () => number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Purchases for the whole app: what the user owns, and the purchase or restore they started. Create one when the
|
|
19
|
+
* app starts, outside any component, and keep it, so transactions from outside the buy flow (renewals, Ask to Buy
|
|
20
|
+
* approvals, purchases on another device, refunds) are never missed. Read it in components with `usePurchases`.
|
|
21
|
+
*
|
|
22
|
+
* The entitlements are the store's on-device answer: an app that verifies purchases on its server sends
|
|
23
|
+
* `transaction.proof` there in `deliver` and grants access from its server's answer.
|
|
24
|
+
*/
|
|
25
|
+
export declare function createPurchases(store: PurchaseStore, options?: PurchasesOptions): Purchases;
|
|
26
|
+
export declare class Purchases {
|
|
27
|
+
private readonly store;
|
|
28
|
+
private snapshot;
|
|
29
|
+
private readonly listeners;
|
|
30
|
+
private readonly finished;
|
|
31
|
+
/** Delivered, but not yet finished: a finish that failed is tried again without delivering twice. */
|
|
32
|
+
private readonly delivered;
|
|
33
|
+
private readonly processing;
|
|
34
|
+
private readonly deliver;
|
|
35
|
+
private readonly now;
|
|
36
|
+
private readonly unsubscribe;
|
|
37
|
+
/**
|
|
38
|
+
* Products of transactions being delivered, or that failed to deliver or finish, which the user didn't own before:
|
|
39
|
+
* the store already lists them, but they unlock nothing until the transaction is delivered.
|
|
40
|
+
*/
|
|
41
|
+
private readonly withheld;
|
|
42
|
+
private entitlementsGeneration;
|
|
43
|
+
private appliedGeneration;
|
|
44
|
+
private expiryCheck;
|
|
45
|
+
constructor(store: PurchaseStore, options?: PurchasesOptions);
|
|
46
|
+
readonly subscribe: (listener: () => void) => (() => void);
|
|
47
|
+
readonly getSnapshot: () => PurchasesSnapshot;
|
|
48
|
+
/** Stops listening to the store, and checking for expiry. */
|
|
49
|
+
readonly stop: () => void;
|
|
50
|
+
/**
|
|
51
|
+
* Whether the user owns the product now. False until the store has answered; check `entitlements !== undefined`
|
|
52
|
+
* first to tell "not yet known" from "doesn't own it".
|
|
53
|
+
*/
|
|
54
|
+
readonly isEntitled: (productId: string) => boolean;
|
|
55
|
+
/** Whether buying the product again would buy nothing new. */
|
|
56
|
+
readonly isOwned: (product: StoreProduct) => boolean;
|
|
57
|
+
/** The products with these IDs, in this order; any the store doesn't sell here are left out. */
|
|
58
|
+
readonly products: (ids: readonly string[]) => Promise<readonly StoreProduct[]>;
|
|
59
|
+
/** Buys the product. Does nothing while a purchase or restore runs, or when the user already owns it. */
|
|
60
|
+
readonly buy: (product: StoreProduct) => void;
|
|
61
|
+
/** Asks the store for the user's purchases again. Does nothing while a purchase or restore runs. */
|
|
62
|
+
readonly restore: () => void;
|
|
63
|
+
/** Clears the message the paywall shows, once the user has seen it. */
|
|
64
|
+
readonly dismissMessage: () => void;
|
|
65
|
+
/**
|
|
66
|
+
* Delivers and finishes any unfinished transactions, then checks entitlements. Purchases does this when it is
|
|
67
|
+
* created and whenever the store says something may have changed; call it to check again yourself. A check that
|
|
68
|
+
* fails keeps the last entitlements.
|
|
69
|
+
*/
|
|
70
|
+
readonly refresh: () => Promise<void>;
|
|
71
|
+
private purchase;
|
|
72
|
+
private sync;
|
|
73
|
+
private handle;
|
|
74
|
+
/** A purchase that was pending, or failed (for lack of a connection, say), completed outside the flow. */
|
|
75
|
+
private showArrival;
|
|
76
|
+
/**
|
|
77
|
+
* Delivers and finishes the unfinished transactions, and returns the products they bought. A store that can't list
|
|
78
|
+
* them now lists them again on the next refresh.
|
|
79
|
+
*/
|
|
80
|
+
private deliverUnfinished;
|
|
81
|
+
/** Delivers and finishes the transaction once, however often and however concurrently the store reports it. */
|
|
82
|
+
private process;
|
|
83
|
+
private deliverAndFinish;
|
|
84
|
+
/**
|
|
85
|
+
* Replaces the entitlements with the store's, less any withheld, unless a newer check has already answered. A failed
|
|
86
|
+
* check changes nothing, so an older check that answers after it still counts.
|
|
87
|
+
*/
|
|
88
|
+
private checkEntitlements;
|
|
89
|
+
/** Checks again when the soonest subscription expires; the store decides whether it renewed or is in a grace period. */
|
|
90
|
+
private scheduleExpiryCheck;
|
|
91
|
+
private update;
|
|
92
|
+
}
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Purchases = void 0;
|
|
4
|
+
exports.createPurchases = createPurchases;
|
|
5
|
+
const status_1 = require("./status");
|
|
6
|
+
const DELIVERED = { ok: true };
|
|
7
|
+
/** The longest wait `setTimeout` takes, about 24.8 days. */
|
|
8
|
+
const MAX_TIMEOUT = 2 ** 31 - 1;
|
|
9
|
+
/**
|
|
10
|
+
* Purchases for the whole app: what the user owns, and the purchase or restore they started. Create one when the
|
|
11
|
+
* app starts, outside any component, and keep it, so transactions from outside the buy flow (renewals, Ask to Buy
|
|
12
|
+
* approvals, purchases on another device, refunds) are never missed. Read it in components with `usePurchases`.
|
|
13
|
+
*
|
|
14
|
+
* The entitlements are the store's on-device answer: an app that verifies purchases on its server sends
|
|
15
|
+
* `transaction.proof` there in `deliver` and grants access from its server's answer.
|
|
16
|
+
*/
|
|
17
|
+
function createPurchases(store, options = {}) {
|
|
18
|
+
return new Purchases(store, options);
|
|
19
|
+
}
|
|
20
|
+
class Purchases {
|
|
21
|
+
store;
|
|
22
|
+
snapshot = { status: { status: "idle" }, entitlements: undefined };
|
|
23
|
+
listeners = new Set();
|
|
24
|
+
finished = new Set();
|
|
25
|
+
/** Delivered, but not yet finished: a finish that failed is tried again without delivering twice. */
|
|
26
|
+
delivered = new Set();
|
|
27
|
+
processing = new Map();
|
|
28
|
+
deliver;
|
|
29
|
+
now;
|
|
30
|
+
unsubscribe;
|
|
31
|
+
/**
|
|
32
|
+
* Products of transactions being delivered, or that failed to deliver or finish, which the user didn't own before:
|
|
33
|
+
* the store already lists them, but they unlock nothing until the transaction is delivered.
|
|
34
|
+
*/
|
|
35
|
+
withheld = new Map();
|
|
36
|
+
entitlementsGeneration = 0;
|
|
37
|
+
appliedGeneration = 0;
|
|
38
|
+
expiryCheck;
|
|
39
|
+
constructor(store, options = {}) {
|
|
40
|
+
this.store = store;
|
|
41
|
+
this.deliver = options.deliver ?? (async () => { });
|
|
42
|
+
this.now = options.now ?? Date.now;
|
|
43
|
+
this.unsubscribe = store.subscribe((update) => void this.handle(update));
|
|
44
|
+
void this.refresh();
|
|
45
|
+
}
|
|
46
|
+
subscribe = (listener) => {
|
|
47
|
+
this.listeners.add(listener);
|
|
48
|
+
return () => this.listeners.delete(listener);
|
|
49
|
+
};
|
|
50
|
+
getSnapshot = () => this.snapshot;
|
|
51
|
+
/** Stops listening to the store, and checking for expiry. */
|
|
52
|
+
stop = () => {
|
|
53
|
+
this.unsubscribe();
|
|
54
|
+
clearTimeout(this.expiryCheck);
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Whether the user owns the product now. False until the store has answered; check `entitlements !== undefined`
|
|
58
|
+
* first to tell "not yet known" from "doesn't own it".
|
|
59
|
+
*/
|
|
60
|
+
isEntitled = (productId) => this.snapshot.entitlements?.some((entitlement) => entitlement.productId === productId) ?? false;
|
|
61
|
+
/** Whether buying the product again would buy nothing new. */
|
|
62
|
+
isOwned = (product) => product.kind !== "consumable" && this.isEntitled(product.id);
|
|
63
|
+
/** The products with these IDs, in this order; any the store doesn't sell here are left out. */
|
|
64
|
+
products = async (ids) => {
|
|
65
|
+
const sold = new Map((await this.store.products(ids)).map((product) => [product.id, product]));
|
|
66
|
+
return [...new Set(ids)].flatMap((id) => sold.get(id) ?? []);
|
|
67
|
+
};
|
|
68
|
+
/** Buys the product. Does nothing while a purchase or restore runs, or when the user already owns it. */
|
|
69
|
+
buy = (product) => {
|
|
70
|
+
if ((0, status_1.isBusy)(this.snapshot.status) || this.isOwned(product))
|
|
71
|
+
return;
|
|
72
|
+
this.update({ status: { status: "purchasing", productId: product.id } });
|
|
73
|
+
// The purchase belongs to purchases, so leaving the paywall never stops it; it never rejects.
|
|
74
|
+
void this.purchase(product);
|
|
75
|
+
};
|
|
76
|
+
/** Asks the store for the user's purchases again. Does nothing while a purchase or restore runs. */
|
|
77
|
+
restore = () => {
|
|
78
|
+
if ((0, status_1.isBusy)(this.snapshot.status))
|
|
79
|
+
return;
|
|
80
|
+
this.update({ status: { status: "restoring" } });
|
|
81
|
+
void this.sync();
|
|
82
|
+
};
|
|
83
|
+
/** Clears the message the paywall shows, once the user has seen it. */
|
|
84
|
+
dismissMessage = () => {
|
|
85
|
+
if (!(0, status_1.isBusy)(this.snapshot.status))
|
|
86
|
+
this.update({ status: { status: "idle" } });
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* Delivers and finishes any unfinished transactions, then checks entitlements. Purchases does this when it is
|
|
90
|
+
* created and whenever the store says something may have changed; call it to check again yourself. A check that
|
|
91
|
+
* fails keeps the last entitlements.
|
|
92
|
+
*/
|
|
93
|
+
refresh = async () => {
|
|
94
|
+
const arrived = await this.deliverUnfinished();
|
|
95
|
+
await this.checkEntitlements().catch(keepLastEntitlements);
|
|
96
|
+
for (const productId of arrived)
|
|
97
|
+
this.showArrival(productId);
|
|
98
|
+
};
|
|
99
|
+
async purchase(product) {
|
|
100
|
+
const productId = product.id;
|
|
101
|
+
try {
|
|
102
|
+
const result = await this.store.purchase(product);
|
|
103
|
+
if (result.status === "pending" || result.status === "cancelled") {
|
|
104
|
+
this.update({ status: { status: result.status, productId } });
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
const delivered = await this.process(result.transaction);
|
|
108
|
+
await this.checkEntitlements().catch(keepLastEntitlements);
|
|
109
|
+
this.update({
|
|
110
|
+
status: delivered.ok
|
|
111
|
+
? { status: "purchased", productId }
|
|
112
|
+
: { status: "failed", productId, error: delivered.error },
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
this.update({ status: { status: "failed", productId, error } });
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
async sync() {
|
|
120
|
+
try {
|
|
121
|
+
if ((await this.store.sync()) === "cancelled") {
|
|
122
|
+
this.update({ status: { status: "idle" } });
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
await this.deliverUnfinished();
|
|
126
|
+
await this.checkEntitlements();
|
|
127
|
+
const ownsAnything = (this.snapshot.entitlements?.length ?? 0) > 0;
|
|
128
|
+
this.update({ status: { status: ownsAnything ? "restored" : "nothing-to-restore" } });
|
|
129
|
+
}
|
|
130
|
+
catch (error) {
|
|
131
|
+
this.update({ status: { status: "restore-failed", error } });
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
async handle(update) {
|
|
135
|
+
if (update.type === "entitlements-may-have-changed")
|
|
136
|
+
return this.refresh();
|
|
137
|
+
const { transaction } = update;
|
|
138
|
+
const delivered = await this.process(transaction);
|
|
139
|
+
await this.checkEntitlements().catch(keepLastEntitlements);
|
|
140
|
+
if (delivered.ok && transaction.isRevoked !== true)
|
|
141
|
+
this.showArrival(transaction.productId);
|
|
142
|
+
}
|
|
143
|
+
/** A purchase that was pending, or failed (for lack of a connection, say), completed outside the flow. */
|
|
144
|
+
showArrival(productId) {
|
|
145
|
+
const { status } = this.snapshot;
|
|
146
|
+
const isWaiting = (status.status === "pending" || status.status === "failed") && status.productId === productId;
|
|
147
|
+
if (isWaiting)
|
|
148
|
+
this.update({ status: { status: "purchased", productId } });
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Delivers and finishes the unfinished transactions, and returns the products they bought. A store that can't list
|
|
152
|
+
* them now lists them again on the next refresh.
|
|
153
|
+
*/
|
|
154
|
+
async deliverUnfinished() {
|
|
155
|
+
const unfinished = await this.store.unfinishedTransactions().catch(() => []);
|
|
156
|
+
const arrived = [];
|
|
157
|
+
for (const transaction of unfinished) {
|
|
158
|
+
const { ok } = await this.process(transaction);
|
|
159
|
+
if (ok && transaction.isRevoked !== true)
|
|
160
|
+
arrived.push(transaction.productId);
|
|
161
|
+
}
|
|
162
|
+
return arrived;
|
|
163
|
+
}
|
|
164
|
+
/** Delivers and finishes the transaction once, however often and however concurrently the store reports it. */
|
|
165
|
+
process(transaction) {
|
|
166
|
+
if (this.finished.has(transaction.id))
|
|
167
|
+
return Promise.resolve(DELIVERED);
|
|
168
|
+
const running = this.processing.get(transaction.id);
|
|
169
|
+
if (running !== undefined)
|
|
170
|
+
return running;
|
|
171
|
+
// Withheld from the start, so a check that answers while it is delivered doesn't unlock it either.
|
|
172
|
+
if (transaction.isRevoked !== true && !this.isEntitled(transaction.productId)) {
|
|
173
|
+
this.withheld.set(transaction.id, transaction.productId);
|
|
174
|
+
}
|
|
175
|
+
const delivered = this.deliverAndFinish(transaction).then((result) => {
|
|
176
|
+
this.processing.delete(transaction.id);
|
|
177
|
+
if (result.ok) {
|
|
178
|
+
this.finished.add(transaction.id);
|
|
179
|
+
this.withheld.delete(transaction.id);
|
|
180
|
+
}
|
|
181
|
+
return result;
|
|
182
|
+
});
|
|
183
|
+
this.processing.set(transaction.id, delivered);
|
|
184
|
+
return delivered;
|
|
185
|
+
}
|
|
186
|
+
async deliverAndFinish(transaction) {
|
|
187
|
+
try {
|
|
188
|
+
if (transaction.isRevoked !== true && !this.delivered.has(transaction.id)) {
|
|
189
|
+
await this.deliver(transaction);
|
|
190
|
+
this.delivered.add(transaction.id);
|
|
191
|
+
}
|
|
192
|
+
await this.store.finish(transaction);
|
|
193
|
+
this.delivered.delete(transaction.id);
|
|
194
|
+
return DELIVERED;
|
|
195
|
+
}
|
|
196
|
+
catch (error) {
|
|
197
|
+
return { ok: false, error };
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Replaces the entitlements with the store's, less any withheld, unless a newer check has already answered. A failed
|
|
202
|
+
* check changes nothing, so an older check that answers after it still counts.
|
|
203
|
+
*/
|
|
204
|
+
async checkEntitlements() {
|
|
205
|
+
this.entitlementsGeneration += 1;
|
|
206
|
+
const id = this.entitlementsGeneration;
|
|
207
|
+
const current = await this.store.currentEntitlements();
|
|
208
|
+
if (id <= this.appliedGeneration)
|
|
209
|
+
return;
|
|
210
|
+
this.appliedGeneration = id;
|
|
211
|
+
const withheld = new Set(this.withheld.values());
|
|
212
|
+
this.update({ entitlements: current.filter(({ productId }) => !withheld.has(productId)) });
|
|
213
|
+
this.scheduleExpiryCheck(current);
|
|
214
|
+
}
|
|
215
|
+
/** Checks again when the soonest subscription expires; the store decides whether it renewed or is in a grace period. */
|
|
216
|
+
scheduleExpiryCheck(entitlements) {
|
|
217
|
+
clearTimeout(this.expiryCheck);
|
|
218
|
+
const now = this.now();
|
|
219
|
+
const upcoming = entitlements.flatMap(({ expiresAt }) => expiresAt !== undefined && expiresAt > now ? [expiresAt] : []);
|
|
220
|
+
if (upcoming.length === 0)
|
|
221
|
+
return;
|
|
222
|
+
// A longer timeout fires at once, so a far expiry wakes early, checks, and waits again.
|
|
223
|
+
const wait = Math.min(Math.min(...upcoming) - now, MAX_TIMEOUT);
|
|
224
|
+
this.expiryCheck = setTimeout(() => {
|
|
225
|
+
void this.checkEntitlements().catch(keepLastEntitlements);
|
|
226
|
+
}, wait);
|
|
227
|
+
}
|
|
228
|
+
update(change) {
|
|
229
|
+
this.snapshot = { ...this.snapshot, ...change };
|
|
230
|
+
for (const listener of this.listeners)
|
|
231
|
+
listener();
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
exports.Purchases = Purchases;
|
|
235
|
+
/** A failed check changes nothing: access is never removed for lack of a connection. */
|
|
236
|
+
function keepLastEntitlements() { }
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { StoreProduct } from "./store";
|
|
2
|
+
/** The purchase or restore the user started, and what came of it. */
|
|
3
|
+
export type PurchaseStatus = {
|
|
4
|
+
readonly status: "idle";
|
|
5
|
+
} | {
|
|
6
|
+
readonly status: "purchasing";
|
|
7
|
+
readonly productId: string;
|
|
8
|
+
}
|
|
9
|
+
/** Waiting for someone else (Ask to Buy, a deferred payment); nothing is unlocked. */
|
|
10
|
+
| {
|
|
11
|
+
readonly status: "pending";
|
|
12
|
+
readonly productId: string;
|
|
13
|
+
} | {
|
|
14
|
+
readonly status: "purchased";
|
|
15
|
+
readonly productId: string;
|
|
16
|
+
}
|
|
17
|
+
/** The user closed the store's sheet. Not an error. */
|
|
18
|
+
| {
|
|
19
|
+
readonly status: "cancelled";
|
|
20
|
+
readonly productId: string;
|
|
21
|
+
} | {
|
|
22
|
+
readonly status: "failed";
|
|
23
|
+
readonly productId: string;
|
|
24
|
+
readonly error: unknown;
|
|
25
|
+
} | {
|
|
26
|
+
readonly status: "restoring";
|
|
27
|
+
} | {
|
|
28
|
+
readonly status: "restored";
|
|
29
|
+
}
|
|
30
|
+
/** A restore found nothing the user owns. Not an error. */
|
|
31
|
+
| {
|
|
32
|
+
readonly status: "nothing-to-restore";
|
|
33
|
+
} | {
|
|
34
|
+
readonly status: "restore-failed";
|
|
35
|
+
readonly error: unknown;
|
|
36
|
+
};
|
|
37
|
+
/** A status the paywall tells the user about, until the app dismisses it. */
|
|
38
|
+
export type PurchaseMessage = Extract<PurchaseStatus, {
|
|
39
|
+
readonly status: "pending" | "failed" | "restored" | "nothing-to-restore" | "restore-failed";
|
|
40
|
+
}>;
|
|
41
|
+
/** What the buy button shows for the selected product. */
|
|
42
|
+
export type BuyButtonState =
|
|
43
|
+
/** No product is selected, or the products haven't loaded. */
|
|
44
|
+
{
|
|
45
|
+
readonly status: "unavailable";
|
|
46
|
+
} | {
|
|
47
|
+
readonly status: "ready";
|
|
48
|
+
readonly product: StoreProduct;
|
|
49
|
+
}
|
|
50
|
+
/** A purchase or restore runs. */
|
|
51
|
+
| {
|
|
52
|
+
readonly status: "purchasing";
|
|
53
|
+
}
|
|
54
|
+
/** The user owns the selected product, which isn't a consumable. */
|
|
55
|
+
| {
|
|
56
|
+
readonly status: "owned";
|
|
57
|
+
readonly product: StoreProduct;
|
|
58
|
+
};
|
|
59
|
+
/** A purchase or restore is running: buy and restore do nothing. */
|
|
60
|
+
export declare function isBusy(status: PurchaseStatus): boolean;
|
|
61
|
+
/** What to tell the user, or undefined for a status that needs no message. */
|
|
62
|
+
export declare function purchaseMessage(status: PurchaseStatus): PurchaseMessage | undefined;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isBusy = isBusy;
|
|
4
|
+
exports.purchaseMessage = purchaseMessage;
|
|
5
|
+
/** A purchase or restore is running: buy and restore do nothing. */
|
|
6
|
+
function isBusy(status) {
|
|
7
|
+
return status.status === "purchasing" || status.status === "restoring";
|
|
8
|
+
}
|
|
9
|
+
/** What to tell the user, or undefined for a status that needs no message. */
|
|
10
|
+
function purchaseMessage(status) {
|
|
11
|
+
switch (status.status) {
|
|
12
|
+
case "pending":
|
|
13
|
+
case "failed":
|
|
14
|
+
case "restored":
|
|
15
|
+
case "nothing-to-restore":
|
|
16
|
+
case "restore-failed":
|
|
17
|
+
return status;
|
|
18
|
+
case "idle":
|
|
19
|
+
case "purchasing":
|
|
20
|
+
case "purchased":
|
|
21
|
+
case "cancelled":
|
|
22
|
+
case "restoring":
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/** What a product is, which decides whether owning it can be bought again. `consumable`: a tip, coins. */
|
|
2
|
+
export type ProductKind = "consumable" | "non-consumable" | "subscription";
|
|
3
|
+
/** A product as the store sells it. `displayPrice` is the store's localized price string: show it as it is. */
|
|
4
|
+
export interface StoreProduct {
|
|
5
|
+
readonly id: string;
|
|
6
|
+
readonly displayName: string;
|
|
7
|
+
readonly description: string;
|
|
8
|
+
readonly displayPrice: string;
|
|
9
|
+
readonly kind: ProductKind;
|
|
10
|
+
}
|
|
11
|
+
/** A completed purchase, renewal or revocation the store reported, to deliver and finish once. */
|
|
12
|
+
export interface StoreTransaction {
|
|
13
|
+
/** The store's ID for it: the App Store's transaction ID, Google Play's purchase token. */
|
|
14
|
+
readonly id: string;
|
|
15
|
+
readonly productId: string;
|
|
16
|
+
readonly kind: ProductKind;
|
|
17
|
+
/** What a server verifies: the App Store's signed JWS, Google Play's purchase token. */
|
|
18
|
+
readonly proof: string;
|
|
19
|
+
/** A refund or revocation: it is finished but never delivered. */
|
|
20
|
+
readonly isRevoked?: boolean;
|
|
21
|
+
}
|
|
22
|
+
/** A product the user owns now, as the store reports it. */
|
|
23
|
+
export interface Entitlement {
|
|
24
|
+
readonly productId: string;
|
|
25
|
+
/** When a subscription runs out unless it renews, in epoch milliseconds; absent if it doesn't, or the store won't say. */
|
|
26
|
+
readonly expiresAt?: number;
|
|
27
|
+
/** When the user first bought it, in epoch milliseconds, if the store says: a member since. */
|
|
28
|
+
readonly purchasedAt?: number;
|
|
29
|
+
/** Shared by a family member through App Store Family Sharing. */
|
|
30
|
+
readonly isFamilyShared?: boolean;
|
|
31
|
+
}
|
|
32
|
+
export type StorePurchaseResult = {
|
|
33
|
+
readonly status: "purchased";
|
|
34
|
+
readonly transaction: StoreTransaction;
|
|
35
|
+
}
|
|
36
|
+
/** Waiting for someone else: Ask to Buy, a deferred payment. */
|
|
37
|
+
| {
|
|
38
|
+
readonly status: "pending";
|
|
39
|
+
} | {
|
|
40
|
+
readonly status: "cancelled";
|
|
41
|
+
};
|
|
42
|
+
/** `cancelled`: the user canceled the store's sign-in. */
|
|
43
|
+
export type StoreSyncResult = "synced" | "cancelled";
|
|
44
|
+
export type StoreUpdate =
|
|
45
|
+
/** A transaction from outside the buy flow: a renewal, an approval, a purchase on another device, a refund. */
|
|
46
|
+
{
|
|
47
|
+
readonly type: "transaction";
|
|
48
|
+
readonly transaction: StoreTransaction;
|
|
49
|
+
}
|
|
50
|
+
/** Something the store doesn't announce may have changed (a refund on Google Play): check entitlements again. */
|
|
51
|
+
| {
|
|
52
|
+
readonly type: "entitlements-may-have-changed";
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* The store behind `createPurchases`: `createReactNativeIapStore` from `@codual/react-native-ui/react-native-iap`,
|
|
56
|
+
* any other store, or a fake in tests. Failures reject; a cancellation by the user is a result, never an error.
|
|
57
|
+
*/
|
|
58
|
+
export interface PurchaseStore {
|
|
59
|
+
/** The products with these IDs that the store sells here, in any order; any it doesn't are left out. */
|
|
60
|
+
products(ids: readonly string[]): Promise<readonly StoreProduct[]>;
|
|
61
|
+
purchase(product: StoreProduct): Promise<StorePurchaseResult>;
|
|
62
|
+
/** Calls `listener` with each transaction from outside the buy flow until the returned function is called. */
|
|
63
|
+
subscribe(listener: (update: StoreUpdate) => void): () => void;
|
|
64
|
+
/** Transactions the store charged for that were never finished (the app was killed mid-purchase, say). */
|
|
65
|
+
unfinishedTransactions(): Promise<readonly StoreTransaction[]>;
|
|
66
|
+
currentEntitlements(): Promise<readonly Entitlement[]>;
|
|
67
|
+
/** Finishes, acknowledges or consumes the transaction, so the store stops giving it again. */
|
|
68
|
+
finish(transaction: StoreTransaction): Promise<void>;
|
|
69
|
+
/** Asks the store for the user's purchases again, for restore. */
|
|
70
|
+
sync(): Promise<StoreSyncResult>;
|
|
71
|
+
}
|