@wix/himalaya-cli 0.1852.0 → 0.1854.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/dist/cli.mjs +1104 -711
- package/guides/himi-json-output.md +8 -2
- package/guides/iap-products.md +4 -0
- package/guides/one-app-section-mappings.md +2 -2
- package/guides/one-app-to-himi-app.md +28 -12
- package/guides/push-cli-onboarding.md +20 -0
- package/package.json +2 -2
- package/template-icons/bare-fashion.png +0 -0
- package/templates/bare-fashion/README.md +52 -0
- package/templates/bare-fashion/config.ts +474 -0
- package/templates/bare-fashion/dev/index.ts +16526 -0
- package/templates/bare-fashion/himalaya.content.json +70 -0
- package/templates/bare-fashion/himalaya.template.json +18 -0
- package/templates/bare-fashion/icon.png +0 -0
- package/templates/bare-fashion/platforms.json +3 -0
- package/templates/bare-fashion/tier5-src/_shared/app-locale.ts +70 -0
- package/templates/bare-fashion/tier5-src/_shared/bare-fashion-address-book.ts +116 -0
- package/templates/bare-fashion/tier5-src/_shared/bare-fashion-address-countries.ts +261 -0
- package/templates/bare-fashion/tier5-src/_shared/bare-fashion-bag-count.ts +24 -0
- package/templates/bare-fashion/tier5-src/_shared/bare-fashion-categories.ts +48 -0
- package/templates/bare-fashion/tier5-src/_shared/bare-fashion-facets.ts +265 -0
- package/templates/bare-fashion/tier5-src/_shared/bare-fashion-load-error.ts +38 -0
- package/templates/bare-fashion/tier5-src/_shared/bare-fashion-product-card.ts +136 -0
- package/templates/bare-fashion/tier5-src/_shared/bare-fashion-store-options.ts +129 -0
- package/templates/bare-fashion/tier5-src/_shared/branded-lite-hosted.ts +282 -0
- package/templates/bare-fashion/tier5-src/_shared/collection-contract.ts +228 -0
- package/templates/bare-fashion/tier5-src/_shared/collection-query.ts +201 -0
- package/templates/bare-fashion/tier5-src/_shared/collection-service.ts +429 -0
- package/templates/bare-fashion/tier5-src/_shared/engine-intl.ts +529 -0
- package/templates/bare-fashion/tier5-src/_shared/himi-account.ts +209 -0
- package/templates/bare-fashion/tier5-src/_shared/himi-copy.ts +123 -0
- package/templates/bare-fashion/tier5-src/_shared/himi-layouts.ts +142 -0
- package/templates/bare-fashion/tier5-src/_shared/himi-store-categories.ts +108 -0
- package/templates/bare-fashion/tier5-src/_shared/himi-store-saved.ts +57 -0
- package/templates/bare-fashion/tier5-src/_shared/himi-store.ts +7 -0
- package/templates/bare-fashion/tier5-src/_shared/himi-text.ts +24 -0
- package/templates/bare-fashion/tier5-src/_shared/hosted-checkout.ts +70 -0
- package/templates/bare-fashion/tier5-src/_shared/live-session.ts +172 -0
- package/templates/bare-fashion/tier5-src/_shared/local-preferences.ts +38 -0
- package/templates/bare-fashion/tier5-src/_shared/member-access.ts +63 -0
- package/templates/bare-fashion/tier5-src/_shared/member-avatar.ts +76 -0
- package/templates/bare-fashion/tier5-src/_shared/member-orders.ts +8 -0
- package/templates/bare-fashion/tier5-src/_shared/online-actions.ts +76 -0
- package/templates/bare-fashion/tier5-src/_shared/owner-load-error.ts +348 -0
- package/templates/bare-fashion/tier5-src/himi-bare-fashion-account/index.ts +127 -0
- package/templates/bare-fashion/tier5-src/himi-bare-fashion-address-form/index.ts +367 -0
- package/templates/bare-fashion/tier5-src/himi-bare-fashion-addresses/index.ts +202 -0
- package/templates/bare-fashion/tier5-src/himi-bare-fashion-appearance/index.ts +73 -0
- package/templates/bare-fashion/tier5-src/himi-bare-fashion-cart/index.ts +540 -0
- package/templates/bare-fashion/tier5-src/himi-bare-fashion-collection/index.ts +111 -0
- package/templates/bare-fashion/tier5-src/himi-bare-fashion-home/index.ts +241 -0
- package/templates/bare-fashion/tier5-src/himi-bare-fashion-nav/index.ts +56 -0
- package/templates/bare-fashion/tier5-src/himi-bare-fashion-order/index.ts +437 -0
- package/templates/bare-fashion/tier5-src/himi-bare-fashion-product/index.ts +650 -0
- package/templates/bare-fashion/tier5-src/himi-bare-fashion-products/index.ts +142 -0
- package/templates/bare-fashion/tier5-src/himi-bare-fashion-saved/index.ts +45 -0
- package/templates/bare-fashion/tier5-src/himi-bare-fashion-search/index.ts +199 -0
- package/templates/bare-fashion/tier5-src/himi-bare-fashion-shop/index.ts +79 -0
- package/templates/bare-fashion/tier5-src/himi-bare-fashion-sign-in/index.ts +269 -0
- package/templates/bare-fashion/tier5-src/himi-bare-fashion-wallet/index.ts +224 -0
- package/templates/bare-fashion/tier5-src/himi-branded-lite-info/index.ts +209 -0
- package/templates/bare-fashion/tier5-src/himi-branded-lite-orders/index.ts +327 -0
- package/templates/bare-fashion/tier5-src/wix-spaces-collections/index.ts +48 -0
- package/templates/bare-fashion/tier5-src/wix-spaces-delete-account/index.ts +59 -0
- package/templates/bare-fashion/tier5-src/wix-spaces-view-profile/index.ts +119 -0
- package/templates/bare-fashion/tokens.json +348 -0
- package/templates/beauty-salon/tier5-src/_shared/collection-query.ts +8 -0
- package/templates/beauty-salon/tier5-src/_shared/collection-service.ts +7 -1
- package/templates/beauty-salon-owner/tier5-src/_shared/collection-query.ts +8 -0
- package/templates/beauty-salon-owner/tier5-src/_shared/collection-service.ts +7 -1
- package/templates/branded-lite/recipes/upcoming-bookings.json +118 -6
- package/templates/branded-lite/tier5-src/_shared/announcement-section.ts +6 -14
- package/templates/branded-lite/tier5-src/_shared/collection-query-legacy.ts +7 -0
- package/templates/branded-lite/tier5-src/_shared/collection-query.ts +7 -0
- package/templates/branded-lite/tier5-src/_shared/collection-service.ts +7 -1
- package/templates/branded-lite/tier5-src/_shared/draft-content.ts +66 -0
- package/templates/branded-lite/tier5-src/_shared/events-inline-options.ts +55 -3
- package/templates/branded-lite/tier5-src/_shared/events-inline.ts +5 -3
- package/templates/branded-lite/tier5-src/_shared/upcoming-bookings-section.ts +15 -4
- package/templates/branded-lite/tier5-src/himi-events-list/index.ts +17 -0
- package/templates/brandy/tier5-src/_shared/collection-query-legacy.ts +7 -0
- package/templates/brandy/tier5-src/_shared/collection-query.ts +7 -0
- package/templates/brandy/tier5-src/_shared/collection-service.ts +7 -1
- package/templates/community-club/tier5-src/_shared/collection-query.ts +8 -0
- package/templates/community-club/tier5-src/_shared/collection-service.ts +7 -1
- package/templates/community-club/tier5-src/_shared/events-inline-options.ts +77 -0
- package/templates/community-club/tier5-src/himi-events-list/index.ts +17 -0
- package/templates/community-club-owner/tier5-src/_shared/collection-query.ts +8 -0
- package/templates/community-club-owner/tier5-src/_shared/collection-service.ts +7 -1
- package/templates/conference/tier5-src/_shared/collection-query.ts +8 -0
- package/templates/conference/tier5-src/_shared/collection-service.ts +7 -1
- package/templates/conference/tier5-src/_shared/events-inline-options.ts +77 -0
- package/templates/conference/tier5-src/himi-events-list/index.ts +17 -0
- package/templates/events-meetups/tier5-src/_shared/collection-query.ts +8 -0
- package/templates/events-meetups/tier5-src/_shared/collection-service.ts +7 -1
- package/templates/events-meetups/tier5-src/_shared/events-inline-options.ts +77 -0
- package/templates/events-meetups/tier5-src/himi-events-list/index.ts +17 -0
- package/templates/events-meetups-owner/tier5-src/_shared/collection-query.ts +8 -0
- package/templates/events-meetups-owner/tier5-src/_shared/collection-service.ts +7 -1
- package/templates/fitness-gym/tier5-src/_shared/collection-query.ts +8 -0
- package/templates/fitness-gym/tier5-src/_shared/collection-service.ts +7 -1
- package/templates/gym-fitness/tier5-src/_shared/collection-query.ts +8 -0
- package/templates/gym-fitness/tier5-src/_shared/collection-service.ts +7 -1
- package/templates/gym-fitness/tier5-src/_shared/events-inline-options.ts +77 -0
- package/templates/gym-fitness/tier5-src/himi-events-list/index.ts +17 -0
- package/templates/gym-fitness-owner/tier5-src/_shared/collection-query.ts +8 -0
- package/templates/gym-fitness-owner/tier5-src/_shared/collection-service.ts +7 -1
- package/templates/medical-services/tier5-src/_shared/collection-query.ts +8 -0
- package/templates/medical-services/tier5-src/_shared/collection-service.ts +7 -1
- package/templates/nonprofit-cause/tier5-src/_shared/collection-query.ts +8 -0
- package/templates/nonprofit-cause/tier5-src/_shared/collection-service.ts +7 -1
- package/templates/nonprofit-cause/tier5-src/_shared/events-inline-options.ts +77 -0
- package/templates/nonprofit-cause/tier5-src/himi-events-list/index.ts +17 -0
- package/templates/nonprofit-cause-owner/tier5-src/_shared/collection-query.ts +8 -0
- package/templates/nonprofit-cause-owner/tier5-src/_shared/collection-service.ts +7 -1
- package/templates/owner-lite/tier5-src/_shared/collection-query.ts +8 -0
- package/templates/owner-lite/tier5-src/_shared/collection-service.ts +7 -1
- package/templates/personal-fitness-trainer/tier5-src/_shared/collection-query.ts +8 -0
- package/templates/personal-fitness-trainer/tier5-src/_shared/collection-service.ts +7 -1
- package/templates/podcast-creator/tier5-src/_shared/collection-query.ts +8 -0
- package/templates/podcast-creator/tier5-src/_shared/collection-service.ts +7 -1
- package/templates/podcast-creator/tier5-src/_shared/events-inline-options.ts +77 -0
- package/templates/podcast-creator/tier5-src/himi-events-list/index.ts +17 -0
- package/templates/podcast-creator-owner/tier5-src/_shared/collection-query.ts +8 -0
- package/templates/podcast-creator-owner/tier5-src/_shared/collection-service.ts +7 -1
- package/templates/portfolio-creative/tier5-src/_shared/collection-query.ts +8 -0
- package/templates/portfolio-creative/tier5-src/_shared/collection-service.ts +7 -1
- package/templates/portfolio-creative/tier5-src/_shared/events-inline-options.ts +77 -0
- package/templates/portfolio-creative/tier5-src/himi-events-list/index.ts +17 -0
- package/templates/portfolio-creative-owner/tier5-src/_shared/collection-query.ts +8 -0
- package/templates/portfolio-creative-owner/tier5-src/_shared/collection-service.ts +7 -1
- package/templates/restaurant/tier5-src/_shared/collection-query.ts +8 -0
- package/templates/restaurant/tier5-src/_shared/collection-service.ts +7 -1
- package/templates/restaurant/tier5-src/_shared/events-inline-options.ts +77 -0
- package/templates/restaurant/tier5-src/himi-events-list/index.ts +17 -0
- package/templates/restaurant-owner/tier5-src/_shared/collection-query.ts +8 -0
- package/templates/restaurant-owner/tier5-src/_shared/collection-service.ts +7 -1
- package/templates/retail-store/tier5-src/_shared/collection-query-legacy.ts +7 -0
- package/templates/retail-store/tier5-src/_shared/collection-query.ts +7 -0
- package/templates/retail-store/tier5-src/_shared/collection-service.ts +7 -1
- package/templates/retail-store-owner/tier5-src/_shared/collection-query.ts +8 -0
- package/templates/retail-store-owner/tier5-src/_shared/collection-service.ts +7 -1
- package/templates/salon-spa/tier5-src/_shared/collection-query.ts +8 -0
- package/templates/salon-spa/tier5-src/_shared/collection-service.ts +7 -1
- package/templates/wholesale-store/tier5-src/_shared/collection-query-legacy.ts +7 -0
- package/templates/wholesale-store/tier5-src/_shared/collection-query.ts +7 -0
- package/templates/wholesale-store/tier5-src/_shared/collection-service.ts +7 -1
- package/templates/yoga-studio/tier5-src/_shared/collection-query.ts +8 -0
- package/templates/yoga-studio/tier5-src/_shared/collection-service.ts +7 -1
- package/templates/yoga-studio/tier5-src/_shared/events-inline-options.ts +77 -0
- package/templates/yoga-studio/tier5-src/himi-events-list/index.ts +17 -0
- package/templates/yoga-studio-owner/tier5-src/_shared/collection-query.ts +8 -0
- package/templates/yoga-studio-owner/tier5-src/_shared/collection-service.ts +7 -1
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Shared display-text helpers for himi cards: trim catalog artifacts from titles and
|
|
2
|
+
// derive up-to-two-letter placeholder initials so same-first-letter lists still vary.
|
|
3
|
+
|
|
4
|
+
/** Trim trailing artifact markers (stray asterisks / footnote daggers) and whitespace
|
|
5
|
+
* that leak from test/seed catalogs, without touching interior punctuation. Falls back
|
|
6
|
+
* to the original when trimming would empty the string. */
|
|
7
|
+
export function cleanTitle(value?: string): string {
|
|
8
|
+
const raw = (value ?? "").trim();
|
|
9
|
+
const cleaned = raw.replace(/[\s*†‡]+$/u, "").trim();
|
|
10
|
+
return cleaned || raw;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Up to two significant initials for a placeholder avatar. Two-plus words → first
|
|
14
|
+
* letter of the first two; a single word → its first two alphanumerics. Keeps a list
|
|
15
|
+
* of similar names (e.g. many "Booking…") from collapsing to one identical glyph. */
|
|
16
|
+
export function initials(value?: string): string {
|
|
17
|
+
const words = (value ?? "").trim().split(/\s+/).filter(Boolean);
|
|
18
|
+
if (words.length === 0) return "#";
|
|
19
|
+
if (words.length === 1) {
|
|
20
|
+
const alnum = words[0].replace(/[^\p{L}\p{N}]/gu, "");
|
|
21
|
+
return (alnum.slice(0, 2) || "#").toUpperCase();
|
|
22
|
+
}
|
|
23
|
+
return ((words[0][0] ?? "") + (words[1][0] ?? "")).toUpperCase();
|
|
24
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// Hosted checkout. A Branded Lite hosted in a host shell (Spaces) pays as the member the host
|
|
2
|
+
// lent it: Headless `createRedirectSession` does not take a Member instance, so the HOST opens the site's
|
|
3
|
+
// checkout signed in as that member. The worker names only its ecom checkout; the host reads Wix's
|
|
4
|
+
// checkout URL, mints a one-time member session and opens the site's `createSessionCookie` → checkout.
|
|
5
|
+
// The session token never reaches this worker.
|
|
6
|
+
//
|
|
7
|
+
// Classic Wix Events tickets and classic Pricing Plans have no ecom checkout: for those the
|
|
8
|
+
// worker names the page by id (`himi.host.memberSitePage`) and the host opens the site's own event or
|
|
9
|
+
// plans page the same way, signed in — where the member picks and pays on the site. A member's orders
|
|
10
|
+
// are the same (Wix lists them to no member identity), so My orders opens the site's own page too.
|
|
11
|
+
|
|
12
|
+
import { dispatch } from "@himalaya/dispatch";
|
|
13
|
+
import { hostSiteFromInput } from "./branded-lite-hosted.js";
|
|
14
|
+
|
|
15
|
+
/** The host verbs (registered by the host shell, never by the app itself). The first prepares the
|
|
16
|
+
* checkout and answers a one-time ticket; the second opens it. Two steps so the caller's charge guard
|
|
17
|
+
* is re-read after the host's network round trips and right before the checkout opens — the prepared
|
|
18
|
+
* URL (which carries the member's session token) stays in the host; only the ticket crosses here. */
|
|
19
|
+
export const HOSTED_CHECKOUT_ACTION = "himi.host.memberCheckout";
|
|
20
|
+
export const HOSTED_CHECKOUT_OPEN_ACTION = "himi.host.memberCheckout.open";
|
|
21
|
+
/** Prepares a site PAGE (not an ecom checkout) the same way; opened with [HOSTED_CHECKOUT_OPEN_ACTION]. */
|
|
22
|
+
export const HOSTED_SITE_PAGE_ACTION = "himi.host.memberSitePage";
|
|
23
|
+
|
|
24
|
+
/** A site page a hosted worker may ask its host for — ids only, never a URL: the host reads the URL
|
|
25
|
+
* from Wix itself and decides whether the member may be signed in there. */
|
|
26
|
+
export type HostedSitePage = { page: "event"; eventId: string } | { page: "plans"; planId?: string } | { page: "orders" };
|
|
27
|
+
|
|
28
|
+
/** Whether this worker runs inside a host that opened a site for it. */
|
|
29
|
+
export function isHostedWorker(): boolean {
|
|
30
|
+
return hostSiteFromInput((globalThis as { himalaya?: { input?: unknown } }).himalaya?.input) !== null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
type HostAnswer = { opened?: unknown; ticket?: unknown; payload?: { opened?: unknown; ticket?: unknown } } | null;
|
|
34
|
+
type HostOpener = (stillMayCharge: () => Promise<boolean>) => Promise<boolean | "withdrawn">;
|
|
35
|
+
|
|
36
|
+
/** Prepare with [action] [params] → re-read the guard → open by ticket. A host without the verbs, or one
|
|
37
|
+
* that refused (Wix named no page, an unsafe URL, no account session), → false, never a throw. */
|
|
38
|
+
function hostOpener(action: string, params: Record<string, string>): HostOpener {
|
|
39
|
+
return async (stillMayCharge) => {
|
|
40
|
+
try {
|
|
41
|
+
const prepared = await dispatch(action, params) as HostAnswer;
|
|
42
|
+
const ticket = prepared?.ticket ?? prepared?.payload?.ticket;
|
|
43
|
+
if (typeof ticket !== "string" || !ticket) return false;
|
|
44
|
+
// An unopened ticket simply expires in the host: nothing to cancel.
|
|
45
|
+
if (!(await stillMayCharge())) return "withdrawn";
|
|
46
|
+
const result = await dispatch(HOSTED_CHECKOUT_OPEN_ACTION, { ticket }) as HostAnswer;
|
|
47
|
+
const opened = result?.opened ?? result?.payload?.opened;
|
|
48
|
+
return opened === true || opened === "true";
|
|
49
|
+
} catch {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** `payCashierOrder`'s `openCashier` for a hosted app: the host prepares [checkoutId]'s checkout as the
|
|
56
|
+
* lent member, the caller's guard is re-read, and only then does the host open it. Resolves whether it
|
|
57
|
+
* opened ("withdrawn" when the guard said no); a host without the verbs (or that refused) → false. */
|
|
58
|
+
export function hostedCheckoutOpener(checkoutId: string): HostOpener {
|
|
59
|
+
return hostOpener(HOSTED_CHECKOUT_ACTION, { checkoutId });
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The site's own [page] opened by the host, signed in as the lent member where the host deems it safe
|
|
63
|
+
* (a visitor otherwise) — the same two steps and answers as [hostedCheckoutOpener]. */
|
|
64
|
+
export function hostedSitePageOpener(page: HostedSitePage): HostOpener {
|
|
65
|
+
const params: Record<string, string> = page.page === "event"
|
|
66
|
+
? { page: "event", eventId: page.eventId }
|
|
67
|
+
: page.page === "orders" ? { page: "orders" }
|
|
68
|
+
: { page: "plans", ...(page.planId ? { planId: page.planId } : {}) };
|
|
69
|
+
return hostOpener(HOSTED_SITE_PAGE_ACTION, params);
|
|
70
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
// #2050 — the worker side of a followed live session, shared by the Owner and the
|
|
2
|
+
// Branded order flows.
|
|
3
|
+
//
|
|
4
|
+
// Shared deliberately rather than written twice. The two flows differ only in which
|
|
5
|
+
// record they follow and what they call it; everything that decides whether a
|
|
6
|
+
// background frame can ever reach the device — that a session ref is present, that
|
|
7
|
+
// it carries an expiry, that `pushUpdates` is on so iOS has a token to register — is
|
|
8
|
+
// identical, and two copies of it would drift into one flow silently losing
|
|
9
|
+
// background delivery while the other kept it.
|
|
10
|
+
//
|
|
11
|
+
// What is NOT here, on purpose: the account, the business, and the push token. The
|
|
12
|
+
// worker does not know them and must not choose them. The host reads its own signed-in
|
|
13
|
+
// scope at start and freezes it into the mapping, so a worker cannot scope a
|
|
14
|
+
// subscription to another tenant even by mistake.
|
|
15
|
+
|
|
16
|
+
import { start, end, type LiveActivityComponent } from "@himalaya/live-activity";
|
|
17
|
+
|
|
18
|
+
/** The fields a following worker adds to its own state. Flags are pre-negated
|
|
19
|
+
* because descriptors bind booleans through `hiddenWhen`, which takes one path. */
|
|
20
|
+
export interface LiveSessionFields {
|
|
21
|
+
/** Token for `end`. Empty when nothing is running. */
|
|
22
|
+
liveActivityId: string;
|
|
23
|
+
liveTracking: boolean;
|
|
24
|
+
notLiveTracking: boolean;
|
|
25
|
+
/** One honest line about the live surface — including "this device has none". */
|
|
26
|
+
liveNote: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const IDLE_LIVE_SESSION: LiveSessionFields = {
|
|
30
|
+
liveActivityId: "",
|
|
31
|
+
liveTracking: false,
|
|
32
|
+
notLiveTracking: true,
|
|
33
|
+
liveNote: "",
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
function node(id: string, type: string, props: Record<string, unknown>): LiveActivityComponent {
|
|
37
|
+
return { id, body: { package: "himalaya", type, props } };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The frozen widget-safe frame the live surface renders (family
|
|
42
|
+
* `ios_live_activity`): headline + one detail line. Only widget-safe components —
|
|
43
|
+
* the activity process has no worker, so a Card, Button or list would be dropped by
|
|
44
|
+
* the native renderer rather than rendered.
|
|
45
|
+
*
|
|
46
|
+
* This is the FIRST frame only. Every later frame arrives from the serve plane with
|
|
47
|
+
* its own content, which is the entire point of the subscription: the worker is gone
|
|
48
|
+
* by then.
|
|
49
|
+
*/
|
|
50
|
+
export function liveSessionFrame(headline: string, detail: string): LiveActivityComponent {
|
|
51
|
+
return node("live_root", "VStack", {
|
|
52
|
+
spacing: 6,
|
|
53
|
+
children: [
|
|
54
|
+
node("live_headline", "Text", { text: headline, style: "bodyEmphasis" }),
|
|
55
|
+
node("live_detail", "Text", { text: detail, style: "caption" }),
|
|
56
|
+
],
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface BeginLiveSessionOptions {
|
|
61
|
+
/** Groups related activities (iOS attributes type / Android channel tag). */
|
|
62
|
+
activityType: string;
|
|
63
|
+
entityType: "order" | "booking" | "session";
|
|
64
|
+
/** The exact record id. Scoped by the host's account/business, never global. */
|
|
65
|
+
entityId: string;
|
|
66
|
+
headline: string;
|
|
67
|
+
detail: string;
|
|
68
|
+
/** Exact record route, so the surface opens the record it is about. Required by
|
|
69
|
+
* the subscription registry — a subscription that cannot name its record cannot
|
|
70
|
+
* satisfy "deep-link into the exact record after process death". */
|
|
71
|
+
deepLink: string;
|
|
72
|
+
/** How long the session may live, in seconds from now. */
|
|
73
|
+
ttlSeconds: number;
|
|
74
|
+
/** Re-entry with the activity token (or "" when the device has no live surface). */
|
|
75
|
+
onStarted: (activityId: string) => void;
|
|
76
|
+
/** Copy for the two terminal notes, so each flow speaks its own language. */
|
|
77
|
+
startingNote: string;
|
|
78
|
+
unavailableNote: string;
|
|
79
|
+
now?: () => number;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Which start attempt is still wanted.
|
|
84
|
+
*
|
|
85
|
+
* `beginLiveSession` returns `liveTracking: true` with an EMPTY `liveActivityId`, so
|
|
86
|
+
* the row offers "Stop following" while `start` is still in flight. Stopping in that
|
|
87
|
+
* window used to be silently undone: `stopLiveSession` had no id to end, and the
|
|
88
|
+
* pending `.then` then called `onStarted` and flipped the row back to following —
|
|
89
|
+
* over a surface the OS had just created and the revoked subscription would never
|
|
90
|
+
* address again. A tap the user made is not a tap the app may discard, so the stop
|
|
91
|
+
* invalidates the start instead. (#2237 review)
|
|
92
|
+
*/
|
|
93
|
+
let startGeneration = 0;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Begin following a record. Returns the "starting" fields immediately and re-enters
|
|
97
|
+
* through `onStarted` — `start` is not awaited so the button flips at once, the same
|
|
98
|
+
* pattern the demo live-activity worker uses and for the same reason: T5 is
|
|
99
|
+
* request/response, so an awaited network round trip would freeze the row.
|
|
100
|
+
*/
|
|
101
|
+
export function beginLiveSession(o: BeginLiveSessionOptions): LiveSessionFields {
|
|
102
|
+
const nowSeconds = Math.floor((o.now?.() ?? Date.now()) / 1000);
|
|
103
|
+
const generation = ++startGeneration;
|
|
104
|
+
start({
|
|
105
|
+
activityType: o.activityType,
|
|
106
|
+
content: liveSessionFrame(o.headline, o.detail),
|
|
107
|
+
title: o.headline,
|
|
108
|
+
deepLink: o.deepLink,
|
|
109
|
+
// Both are load-bearing on iOS: `session` records the mapping, `pushUpdates`
|
|
110
|
+
// asks ActivityKit for the token the host registers. With only the first, the
|
|
111
|
+
// device would hold a mapping nothing can ever address.
|
|
112
|
+
pushUpdates: true,
|
|
113
|
+
// Android promotion: a user-followed, bounded, time-sensitive activity is
|
|
114
|
+
// exactly what it is for. The system may still keep it an ordinary notification.
|
|
115
|
+
promoted: true,
|
|
116
|
+
// The SAME TTL the registry session gets, handed to the NATIVE surface too.
|
|
117
|
+
// Omitting it did not mean "use a sensible default": the SDK sends 0,
|
|
118
|
+
// `LiveUpdatePolicy.DEFAULT_TTL_MS` is 0, `duration(0)` returns 0, and the
|
|
119
|
+
// capability then stores `Long.MAX_VALUE` and calls `setTimeoutAfter(0)` — so
|
|
120
|
+
// an Android Live Update with no terminal push and no revocation stayed on
|
|
121
|
+
// the shade forever, while the declared follow TTL said 12 or 24 hours.
|
|
122
|
+
// Android clamps to its own MAX_TTL_MS (8h), which is its bound to enforce.
|
|
123
|
+
// (#2237 review)
|
|
124
|
+
staleAfterMs: o.ttlSeconds * 1000,
|
|
125
|
+
session: {
|
|
126
|
+
entityType: o.entityType,
|
|
127
|
+
entityId: o.entityId,
|
|
128
|
+
expiresAt: nowSeconds + o.ttlSeconds,
|
|
129
|
+
},
|
|
130
|
+
})
|
|
131
|
+
.then((r) => {
|
|
132
|
+
if (generation !== startGeneration) {
|
|
133
|
+
// Unfollowed while this was starting. The surface exists NOW and the
|
|
134
|
+
// unfollow could not end it — there was no id yet — so end it here.
|
|
135
|
+
// Skipping this is what leaves an unaddressable live surface on the
|
|
136
|
+
// device after the user has already stopped.
|
|
137
|
+
if (r.activityId) end({ activityId: r.activityId, dismissAfterMs: 0 });
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
o.onStarted(r.activityId);
|
|
141
|
+
})
|
|
142
|
+
.catch(() => {
|
|
143
|
+
// A failed start has nothing to clean up; it must still not report itself
|
|
144
|
+
// onto a row the user has moved on from.
|
|
145
|
+
if (generation === startGeneration) o.onStarted("");
|
|
146
|
+
});
|
|
147
|
+
return { liveActivityId: "", liveTracking: true, notLiveTracking: false, liveNote: o.startingNote };
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Resolve the deferred start. An empty id means the device has no live surface
|
|
151
|
+
* (web, iOS < 16.2, activities or notifications disabled) — reported, not hidden. */
|
|
152
|
+
export function liveSessionStarted(
|
|
153
|
+
activityId: string,
|
|
154
|
+
followingNote: string,
|
|
155
|
+
unavailableNote: string,
|
|
156
|
+
): LiveSessionFields {
|
|
157
|
+
const id = (activityId ?? "").trim();
|
|
158
|
+
if (!id) return { ...IDLE_LIVE_SESSION, liveNote: unavailableNote };
|
|
159
|
+
return { liveActivityId: id, liveTracking: true, notLiveTracking: false, liveNote: followingNote };
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Stop following. Ends the OS surface; the host's own cleanup drops the server
|
|
164
|
+
* subscription, so the server cannot keep pushing to a surface the user dismissed.
|
|
165
|
+
*/
|
|
166
|
+
export function stopLiveSession(activityId: string, stoppedNote: string): LiveSessionFields {
|
|
167
|
+
// Cancels a start still in flight. Unconditional, because the case this exists
|
|
168
|
+
// for is exactly the one where `activityId` is still empty.
|
|
169
|
+
startGeneration++;
|
|
170
|
+
if (activityId) end({ activityId, dismissAfterMs: 0 });
|
|
171
|
+
return { ...IDLE_LIVE_SESSION, liveNote: stoppedNote };
|
|
172
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/** Small local preferences only; never entity records or credentials. The host
|
|
2
|
+
* overwrites these reserved input fields from its authenticated account/site.
|
|
3
|
+
* Older hosts and visitors deliberately stay session-only. */
|
|
4
|
+
export interface PreferenceStore {
|
|
5
|
+
getItem(key: string): string | null;
|
|
6
|
+
setItem(key: string, value: string): void;
|
|
7
|
+
}
|
|
8
|
+
export interface PreferenceRuntime { input?: Record<string, unknown>; storage?: PreferenceStore; }
|
|
9
|
+
export function localPreferences<T>(name: string, normalize: (value: unknown) => T, runtime?: PreferenceRuntime) {
|
|
10
|
+
const host = runtime ?? (globalThis as { himalaya?: PreferenceRuntime }).himalaya;
|
|
11
|
+
const account = host?.input?.__himiPreferenceAccount;
|
|
12
|
+
const business = host?.input?.__himiPreferenceBusiness;
|
|
13
|
+
const valid = (value: unknown): value is string => typeof value === 'string' && /^[A-Za-z0-9_.:-]{1,128}$/.test(value) && value !== 'default';
|
|
14
|
+
const key = valid(account) && valid(business) && host?.storage
|
|
15
|
+
? `himi.preferences.v1.${account}.${encodeURIComponent(business)}.${name}` : '';
|
|
16
|
+
let available = !!key;
|
|
17
|
+
return {
|
|
18
|
+
get available() { return available; },
|
|
19
|
+
read(): T {
|
|
20
|
+
if (!key) return normalize(undefined);
|
|
21
|
+
try {
|
|
22
|
+
const raw = host!.storage!.getItem(key);
|
|
23
|
+
if (!raw || raw.length > 32768) return normalize(undefined);
|
|
24
|
+
const data = JSON.parse(raw);
|
|
25
|
+
return normalize(data?.version === 1 ? data.value : undefined);
|
|
26
|
+
} catch { available = false; return normalize(undefined); }
|
|
27
|
+
},
|
|
28
|
+
write(value: T): void {
|
|
29
|
+
if (!key) return;
|
|
30
|
+
try {
|
|
31
|
+
const raw = JSON.stringify({ version: 1, value: normalize(value) });
|
|
32
|
+
if (raw.length > 32768) { available = false; return; }
|
|
33
|
+
host!.storage!.setItem(key, raw);
|
|
34
|
+
available = host!.storage!.getItem(key) === raw;
|
|
35
|
+
} catch { available = false; }
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { dispatch } from "@himalaya/dispatch";
|
|
2
|
+
import { getMyData } from "@himalaya/wix-users";
|
|
3
|
+
|
|
4
|
+
export interface MemberAccess { email: string; scope: string; signedIn?: boolean; }
|
|
5
|
+
export function memberAccessChanged(): Error {
|
|
6
|
+
const error = new Error("Member access changed"); error.name = "MemberAccessChanged"; return error;
|
|
7
|
+
}
|
|
8
|
+
export async function memberAccess(options: { throwOnUnavailable?: boolean } = {}): Promise<MemberAccess> {
|
|
9
|
+
let email = "";
|
|
10
|
+
let signedIn = false;
|
|
11
|
+
let identityUnavailable = false;
|
|
12
|
+
try {
|
|
13
|
+
const value = await dispatch("himi.wix.memberIdentity") as { status?: unknown; ok?: unknown; email?: unknown; signedIn?: unknown; payload?: { email?: unknown; signedIn?: unknown } } | null;
|
|
14
|
+
if (value?.status === "error" || value?.ok === false) throw value;
|
|
15
|
+
const raw = value?.email ?? value?.payload?.email;
|
|
16
|
+
email = typeof raw === "string" ? raw.trim().toLowerCase() : "";
|
|
17
|
+
signedIn = (value?.signedIn ?? value?.payload?.signedIn) === true;
|
|
18
|
+
} catch { identityUnavailable = true; }
|
|
19
|
+
// Older native hosts predate the memberIdentity action but can still expose
|
|
20
|
+
// the authenticated profile through wix-users. Only a missing action uses
|
|
21
|
+
// that rung. A successful host response is authoritative, including a signed-in
|
|
22
|
+
// session whose email has not arrived yet; it must not consult owner records.
|
|
23
|
+
if (!email && identityUnavailable) {
|
|
24
|
+
try {
|
|
25
|
+
const profile = await getMyData();
|
|
26
|
+
const raw = profile.email ?? profile.contactEmail;
|
|
27
|
+
email = typeof raw === "string" ? raw.trim().toLowerCase() : "";
|
|
28
|
+
} catch (error) {
|
|
29
|
+
const failure = error as { status?: unknown; statusCode?: unknown; code?: unknown } | null;
|
|
30
|
+
const anonymous = failure?.status === 401 || failure?.statusCode === 401 || failure?.code === "UNAUTHENTICATED";
|
|
31
|
+
// Legacy bridges can lose the structured status. An unknown failure is
|
|
32
|
+
// unavailable in strict consumers, never evidence of a signed-out session.
|
|
33
|
+
if (options.throwOnUnavailable && !anonymous) throw error;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
const input = (globalThis as { himalaya?: { input?: Record<string, unknown> } }).himalaya?.input;
|
|
37
|
+
const account = input?.__himiPreferenceAccount, business = input?.__himiPreferenceBusiness;
|
|
38
|
+
const valid = (v: unknown): v is string => typeof v === "string" && /^[A-Za-z0-9_.:-]{1,128}$/.test(v) && v !== "default";
|
|
39
|
+
// These fields are overwritten by authenticated hosts. Older binaries stay online-only.
|
|
40
|
+
// A session signal does not grant an email-scoped cache or private query.
|
|
41
|
+
return { email, signedIn: signedIn || Boolean(email), scope: email && valid(account) && valid(business) ? JSON.stringify([account, business, email]) : "" };
|
|
42
|
+
}
|
|
43
|
+
export async function assertMemberAccess(member: MemberAccess): Promise<void> {
|
|
44
|
+
const current = await memberAccess();
|
|
45
|
+
if (!member.email || current.email !== member.email || current.scope !== member.scope) throw memberAccessChanged();
|
|
46
|
+
}
|
|
47
|
+
export function memberAccessDenied(error: unknown): boolean {
|
|
48
|
+
return (error as { name?: string } | null)?.name === "MemberAccessChanged" || /(?:HTTP\s*(?:401|403)|READ_ORDER_FORBIDDEN|permission denied|forbidden)/i.test(String(error));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Did this write fail because the member changed (signed out, or another account)? Then
|
|
52
|
+
* the screen reloads for whoever is signed in now. A 401/403 for the SAME member is only a
|
|
53
|
+
* failed write: the caller keeps what the member typed and says so. */
|
|
54
|
+
export async function memberChanged(member: MemberAccess | undefined, error: unknown): Promise<boolean> {
|
|
55
|
+
if ((error as { name?: string } | null)?.name === "MemberAccessChanged") return true;
|
|
56
|
+
if (!member || !memberAccessDenied(error)) return false;
|
|
57
|
+
try {
|
|
58
|
+
await assertMemberAccess(member);
|
|
59
|
+
return false;
|
|
60
|
+
} catch {
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// The signed-in member's photo and initials for a top-bar avatar on a SHARED list screen
|
|
2
|
+
// (`defineBackendCollection({ memberAvatar })`). A screen opts in with the onAppear param
|
|
3
|
+
// `memberAvatar: "1"`; a screen that does not ask pays nothing. A visitor costs one local
|
|
4
|
+
// identity read per load. A member's profile is read again on each visit, while the last photo
|
|
5
|
+
// stays on screen, so a photo or name saved in Personal info reaches this bar on the next visit.
|
|
6
|
+
import { loadMemberProfile, memberIdentity } from "./himi-account.js";
|
|
7
|
+
|
|
8
|
+
/** The state keys the avatar item binds (`$service:<name>.memberImage` / `.memberInitials`). */
|
|
9
|
+
export const MEMBER_AVATAR_KEYS = ["memberImage", "memberInitials"] as const;
|
|
10
|
+
export interface MemberAvatarFields { memberImage: string; memberInitials: string }
|
|
11
|
+
const NONE: MemberAvatarFields = { memberImage: "", memberInitials: "" };
|
|
12
|
+
|
|
13
|
+
export interface MemberAvatarSource {
|
|
14
|
+
/** The state keys this source writes: kept out of a persisted or restored snapshot. */
|
|
15
|
+
readonly keys: readonly string[];
|
|
16
|
+
/** The load action's argument: starts a read when the screen asked for the avatar. */
|
|
17
|
+
onLoad(arg: unknown): void;
|
|
18
|
+
/** The state with the avatar fields, once the screen asked for them. */
|
|
19
|
+
merge<T extends object>(state: T): T;
|
|
20
|
+
/** Call once after registration. A runtime that ignores `notHydrated` restored the last run's
|
|
21
|
+
* photo on a cold boot (maybe another member's): clear it until this run reads its own. */
|
|
22
|
+
resetRestored(svc: { getState(): unknown }): void;
|
|
23
|
+
}
|
|
24
|
+
export type MemberAvatarFactory = (changed: () => void) => MemberAvatarSource;
|
|
25
|
+
|
|
26
|
+
export interface MemberAvatarReads {
|
|
27
|
+
identity(): Promise<{ email: string; signedIn: boolean }>;
|
|
28
|
+
profile(): Promise<{ isMember: boolean; memberImage: string; memberInitials: string } | undefined>;
|
|
29
|
+
}
|
|
30
|
+
const LIVE: MemberAvatarReads = { identity: memberIdentity, profile: () => loadMemberProfile("").catch(() => undefined) };
|
|
31
|
+
|
|
32
|
+
const asked = (arg: unknown): boolean => {
|
|
33
|
+
const value = arg && typeof arg === "object" ? (arg as { memberAvatar?: unknown }).memberAvatar : undefined;
|
|
34
|
+
return value === "1" || value === true;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/** `changed` republishes the list's state (the collection's `collectionDataChanged`). */
|
|
38
|
+
export function memberAvatarSource(changed: () => void, reads: MemberAvatarReads = LIVE): MemberAvatarSource {
|
|
39
|
+
let wanted = false;
|
|
40
|
+
let fields = NONE;
|
|
41
|
+
let seq = 0;
|
|
42
|
+
async function read(): Promise<MemberAvatarFields> {
|
|
43
|
+
const who = await reads.identity();
|
|
44
|
+
if (!who.signedIn) return NONE;
|
|
45
|
+
// No per-account cache: no worker is told when Personal info saves a new photo or name, so
|
|
46
|
+
// only a fresh read sees it. The last fields stay on screen until this read answers.
|
|
47
|
+
const profile = await reads.profile();
|
|
48
|
+
if (!profile?.isMember) return NONE;
|
|
49
|
+
return { memberImage: profile.memberImage, memberInitials: profile.memberInitials };
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
keys: MEMBER_AVATAR_KEYS,
|
|
53
|
+
onLoad(arg) {
|
|
54
|
+
if (!asked(arg)) return;
|
|
55
|
+
wanted = true;
|
|
56
|
+
const mine = ++seq;
|
|
57
|
+
void read().then((next) => {
|
|
58
|
+
// A later load (a sign-out, another account) owns the result.
|
|
59
|
+
if (mine !== seq) return;
|
|
60
|
+
if (next.memberImage === fields.memberImage && next.memberInitials === fields.memberInitials) return;
|
|
61
|
+
fields = next;
|
|
62
|
+
changed();
|
|
63
|
+
}, () => undefined);
|
|
64
|
+
},
|
|
65
|
+
merge(state) {
|
|
66
|
+
return wanted ? { ...state, ...fields } : state;
|
|
67
|
+
},
|
|
68
|
+
resetRestored(svc) {
|
|
69
|
+
// The same signal `resetUnhydratedKeysOnOlderRuntime` (iap-worker.ts) reads.
|
|
70
|
+
if ((globalThis as { himalaya?: { hydrationOptOut?: unknown } }).himalaya?.hydrationOptOut === true) return;
|
|
71
|
+
const booted = svc.getState();
|
|
72
|
+
if (!booted || typeof booted !== "object") return;
|
|
73
|
+
for (const key of MEMBER_AVATAR_KEYS) (booted as Record<string, unknown>)[key] = "";
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { MemberAccess as OrderMember } from "./member-access.js";
|
|
2
|
+
export { memberAccess as orderMember, assertMemberAccess as assertOrderMember, memberAccessChanged as orderAccessChanged,
|
|
3
|
+
memberAccessDenied as orderAccessDenied, type MemberAccess as OrderMember } from "./member-access.js";
|
|
4
|
+
export function ownedOrderCache(row: unknown, member: OrderMember): row is Record<string, unknown> & { id: string } {
|
|
5
|
+
if (!member.email || !member.scope || !row || typeof row !== "object") return false;
|
|
6
|
+
const value = row as Record<string, unknown>;
|
|
7
|
+
return typeof value.id === "string" && !!value.id && value.memberOrderScope === member.scope;
|
|
8
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { connectivity, defineService as register, type ServiceDefinition } from '@himalaya/state';
|
|
2
|
+
|
|
3
|
+
// Template generation enables this behavior only in the selected two starter families.
|
|
4
|
+
export const offlineWritesEnabled = false;
|
|
5
|
+
|
|
6
|
+
export const offlineWriteMessage = 'You’re offline. Connect to save this change, then submit it again.';
|
|
7
|
+
|
|
8
|
+
/** The guard runs before the reducer: entered values survive and no optimistic
|
|
9
|
+
* changes, success state, or queued writes can occur. It never submits on reconnect. */
|
|
10
|
+
export function offlineWriteState<S extends object>(state: S): S {
|
|
11
|
+
const next: Record<string, unknown> = { ...state, error: offlineWriteMessage, message: offlineWriteMessage };
|
|
12
|
+
for (const key of ['saving', 'isSaving', 'submitting', 'isSubmitting', 'adding', 'added', 'bookingInFlight']) {
|
|
13
|
+
if (key in next) next[key] = false;
|
|
14
|
+
}
|
|
15
|
+
for (const key of ['savedMessage', 'successMessage']) {
|
|
16
|
+
if (key in next) next[key] = '';
|
|
17
|
+
}
|
|
18
|
+
// A saved/favourited item is business state, not a submission-success flag.
|
|
19
|
+
return next as S;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface OfflineWriteOptions<S> {
|
|
23
|
+
derive?: (state: S) => S;
|
|
24
|
+
/** Exempt only non-mutating branches of a registered action. */
|
|
25
|
+
requiresConnectivity?: (action: string, state: S, argument?: unknown) => boolean;
|
|
26
|
+
messageFields?: readonly string[] | ((action: string) => readonly string[]);
|
|
27
|
+
draftFields?: readonly string[] | Readonly<Record<string, string>>;
|
|
28
|
+
secretFields?: readonly string[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function withOnlineWrites<S extends object>(registerService: typeof register, names: readonly string[], options: OfflineWriteOptions<S> = {}): typeof register {
|
|
32
|
+
if (!offlineWritesEnabled) return registerService;
|
|
33
|
+
return ((name: string, definition: ServiceDefinition<Record<string, unknown>>) => {
|
|
34
|
+
let currentState: (() => Record<string, unknown>) | undefined;
|
|
35
|
+
const handle = registerService(name, {
|
|
36
|
+
...definition,
|
|
37
|
+
notHydrated: [...new Set([...(definition.notHydrated ?? []), ...(options.secretFields ?? [])])],
|
|
38
|
+
actions: Object.fromEntries(Object.entries(definition.actions ?? {}).map(([action, reduce]) => [action,
|
|
39
|
+
(state: Record<string, unknown>, ...args: never[]) => {
|
|
40
|
+
if (!names.includes(action)) return reduce(state, ...args);
|
|
41
|
+
if (options.requiresConnectivity?.(action, state as S, args[0]) === false) return reduce(state, ...args);
|
|
42
|
+
const fields = typeof options.messageFields === 'function' ? options.messageFields(action) : options.messageFields ?? ['message'];
|
|
43
|
+
if (connectivity().online) {
|
|
44
|
+
let cleared = false;
|
|
45
|
+
for (const field of ['error', 'message', ...fields]) {
|
|
46
|
+
if (state[field] === offlineWriteMessage) { state[field] = ''; cleared = true; }
|
|
47
|
+
}
|
|
48
|
+
// Keep the runtime's state object: reducers may mutate it and return undefined.
|
|
49
|
+
const result = reduce(state, ...args);
|
|
50
|
+
if (!cleared) return result;
|
|
51
|
+
// Publish cleared feedback even for a void reducer. An async completion must
|
|
52
|
+
// keep any newer state published by another action while it was awaiting.
|
|
53
|
+
const finish = (next: Record<string, unknown> | undefined) => next === undefined ? currentState?.() ?? state : next;
|
|
54
|
+
return result && typeof (result as Promise<unknown>).then === 'function'
|
|
55
|
+
? Promise.resolve(result).then(finish)
|
|
56
|
+
: finish(result as Record<string, unknown> | undefined);
|
|
57
|
+
}
|
|
58
|
+
const draft = { ...state };
|
|
59
|
+
const argument = args[0] as Record<string, unknown> | undefined;
|
|
60
|
+
const draftFields = options.draftFields ?? [];
|
|
61
|
+
const entries = Array.isArray(draftFields) ? draftFields.map(key => [key, key]) : Object.entries(draftFields);
|
|
62
|
+
for (const [key, source] of entries) {
|
|
63
|
+
if (argument && key in state && typeof argument[source] === typeof state[key]) draft[key] = argument[source];
|
|
64
|
+
}
|
|
65
|
+
for (const key of options.secretFields ?? []) draft[key] = '';
|
|
66
|
+
const feedback = Object.fromEntries(fields.map(key => [key, offlineWriteMessage]));
|
|
67
|
+
let next = { ...offlineWriteState(draft), ...feedback };
|
|
68
|
+
if (options.derive) next = options.derive(next as S) as Record<string, unknown>;
|
|
69
|
+
return { ...next, ...feedback };
|
|
70
|
+
},
|
|
71
|
+
])),
|
|
72
|
+
});
|
|
73
|
+
if (typeof handle.getState === 'function') currentState = () => handle.getState();
|
|
74
|
+
return handle;
|
|
75
|
+
}) as typeof register;
|
|
76
|
+
}
|