@parity/product-sdk-host 0.10.3 → 0.12.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/src/theme.ts CHANGED
@@ -1,74 +1,52 @@
1
1
  // Copyright 2026 Parity Technologies (UK) Ltd.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
  /**
4
- * Higher-level wrapper for the host's theme subscription.
4
+ * Higher-level wrapper for the host's theme subscription, backed by
5
+ * `truApi.theme.subscribe`.
5
6
  *
6
- * `hostApi.themeSubscribe` is reachable via {@link getTruApi}, but consumers
7
- * have to wire the subscription envelope themselves. `getThemeProvider`
8
- * returns the `@novasamatech/host-api-wrapper` theme provider object directly,
9
- * giving callers a `subscribeTheme(cb)` method that resolves to a typed
10
- * {@link ThemeMode} — a `{ name, variant }` struct where `variant` is
11
- * `"Light" | "Dark"` — and yields a `Subscription<void>` handle.
12
- *
13
- * @remarks
14
- * As of `host-api(-wrapper)` v0.8 the theme payload is a struct, not a flat
15
- * `"light" | "dark"` string: read {@link ThemeMode.variant} for the
16
- * light/dark value (now capitalized) and {@link ThemeMode.name} for the
17
- * active theme name (`Default`, or `Custom` carrying a string id).
7
+ * `getThemeProvider` returns a handle whose `subscribeTheme(cb)` delivers a
8
+ * typed {@link ThemeMode} a `{ name, variant }` struct where `variant` is
9
+ * `"Light" | "Dark"` and `name` is `{ tag: "Default" }` or
10
+ * `{ tag: "Custom", value }` and yields a {@link HostSubscription}
11
+ * (`unsubscribe` + `onInterrupt`).
18
12
  *
19
13
  * @module
20
14
  */
21
15
 
22
- import { createLogger } from "@parity/product-sdk-logger";
23
-
24
- import type {
25
- createThemeProvider,
26
- ThemeMode as NovasamaThemeMode,
27
- } from "@novasamatech/host-api-wrapper";
28
-
29
- const log = createLogger("host:theme");
16
+ import type { HostThemeSubscribeItem, TrUApiClient } from "@parity/truapi";
30
17
 
31
- /**
32
- * Host theme provider handle. Exposes `subscribeTheme(callback)` which
33
- * receives a typed {@link ThemeMode} struct on every change and returns a
34
- * `Subscription<void>` (`unsubscribe` + `onInterrupt`).
35
- *
36
- * Type identical to `createThemeProvider()` from
37
- * `@novasamatech/host-api-wrapper`.
38
- */
39
- export type ThemeProvider = ReturnType<typeof createThemeProvider>;
18
+ import { getClient, subscribeWithInterrupt } from "./transport.js";
19
+ import type { HostSubscription } from "./types.js";
40
20
 
41
21
  /**
42
- * Host theme value. Re-exported from `@novasamatech/host-api-wrapper`.
43
- *
44
- * A `{ name, variant }` struct as of v0.8 (previously a flat
45
- * `"light" | "dark"` string).
22
+ * Host theme value. A `{ name, variant }` struct re-exported from
23
+ * `@parity/truapi`.
46
24
  */
47
- export type ThemeMode = NovasamaThemeMode;
25
+ export type ThemeMode = HostThemeSubscribeItem;
48
26
 
49
- /** Light/dark variant of the active theme: `"Light" | "Dark"`. */
50
- export type ThemeVariant = ThemeMode["variant"];
27
+ /** Light/dark variant of the active theme (`"Light" | "Dark"`) and the active theme name. Re-exported from `@parity/truapi`. */
28
+ export type { ThemeName, ThemeVariant } from "@parity/truapi";
51
29
 
52
30
  /**
53
- * Active theme name: `{ tag: "Default" }`, or `{ tag: "Custom", value }`
54
- * carrying the custom theme's string id.
31
+ * Host theme provider handle. `subscribeTheme(callback)` receives a typed
32
+ * {@link ThemeMode} on every change and returns a {@link HostSubscription}.
55
33
  */
56
- export type ThemeName = ThemeMode["name"];
34
+ export interface ThemeProvider {
35
+ subscribeTheme(callback: (theme: ThemeMode) => void): HostSubscription;
36
+ }
37
+
38
+ /** Build a {@link ThemeProvider} over a TruAPI client's `theme` domain. */
39
+ function adaptThemeProvider(client: TrUApiClient): ThemeProvider {
40
+ return {
41
+ subscribeTheme(callback) {
42
+ return subscribeWithInterrupt(client.theme.subscribe(), callback);
43
+ },
44
+ };
45
+ }
57
46
 
58
47
  /**
59
- * Get the host theme provider.
60
- *
61
- * Returns the theme-subscription handle exported by
62
- * `@novasamatech/host-api-wrapper`, or `null` if the package is unavailable
63
- * (running outside a host container or the optional peer dep isn't
64
- * installed).
65
- *
66
- * Implementation note: upstream `@novasamatech/host-api-wrapper` exports only
67
- * the `createThemeProvider` factory and no `themeProvider` singleton, so
68
- * this getter constructs a fresh instance on each call (unlike
69
- * {@link getPreimageManager} or {@link getHostLocalStorage}, which return
70
- * upstream singletons). The constructed provider is cheap to allocate; it
71
- * only opens a subscription when `subscribeTheme` is called.
48
+ * Get the host theme provider, backed by `truApi.theme.*`. Returns `null` when
49
+ * running outside a host container.
72
50
  *
73
51
  * @returns The theme provider, or `null` if unavailable.
74
52
  *
@@ -87,20 +65,14 @@ export type ThemeName = ThemeMode["name"];
87
65
  * ```
88
66
  */
89
67
  export async function getThemeProvider(): Promise<ThemeProvider | null> {
90
- try {
91
- const sdk = await import("@novasamatech/host-api-wrapper");
92
- return sdk.createThemeProvider();
93
- } catch (err) {
94
- log.debug("getThemeProvider unavailable", err);
95
- return null;
96
- }
68
+ const client = await getClient();
69
+ return client ? adaptThemeProvider(client) : null;
97
70
  }
98
71
 
99
72
  if (import.meta.vitest) {
100
73
  const { test, expect } = import.meta.vitest;
101
74
 
102
- test("getThemeProvider returns provider when SDK is available", async () => {
103
- const provider = await getThemeProvider();
104
- expect(provider === null || typeof provider === "object").toBe(true);
75
+ test("getThemeProvider returns null outside a container", async () => {
76
+ expect(await getThemeProvider()).toBeNull();
105
77
  });
106
78
  }
@@ -0,0 +1,71 @@
1
+ // Copyright 2026 Parity Technologies (UK) Ltd.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ /**
4
+ * Access to the in-house TruAPI client (`@parity/truapi`) for the host package.
5
+ *
6
+ * Environment detection and the lazily-built, cached client come from
7
+ * `@parity/truapi/sandbox`; this module layers the product-sdk-specific glue on
8
+ * top — an async {@link getClient} accessor and {@link subscribeWithInterrupt},
9
+ * which adapts a truapi stream into the host's {@link HostSubscription} shape.
10
+ *
11
+ * @module
12
+ */
13
+
14
+ import type { ObservableLike, TrUApiClient } from "@parity/truapi";
15
+ import { getClientSync, isCorrectEnvironment } from "@parity/truapi/sandbox";
16
+
17
+ import type { HostSubscription } from "./types.js";
18
+
19
+ export { getClientSync, isCorrectEnvironment };
20
+
21
+ /**
22
+ * Get the TruAPI client. Returns `null` outside a host container. Async wrapper
23
+ * over {@link getClientSync} for the host wrappers that already `await` it.
24
+ */
25
+ export async function getClient(): Promise<TrUApiClient | null> {
26
+ return getClientSync();
27
+ }
28
+
29
+ /**
30
+ * Adapt a truapi `ObservableLike` stream into the host's callback-style
31
+ * {@link HostSubscription} (`unsubscribe` + `onInterrupt`). `onNext` fires for
32
+ * each item; the registered `onInterrupt` callback fires when the host ends the
33
+ * subscription server-side — which the generated client surfaces as either
34
+ * `complete` (a host interrupt frame) or `error` (transport close). Shared by
35
+ * the statement-store and preimage adapters, which both expose this shape.
36
+ */
37
+ export function subscribeWithInterrupt<Item, Reason = never>(
38
+ observable: ObservableLike<Item, Reason>,
39
+ onNext: (item: Item) => void,
40
+ ): HostSubscription {
41
+ let interruptCallback: ((reason?: unknown) => void) | undefined;
42
+ const sub = observable.subscribe({
43
+ next: onNext,
44
+ error: (reason) => interruptCallback?.(reason),
45
+ complete: () => interruptCallback?.(),
46
+ });
47
+ return {
48
+ unsubscribe: () => sub.unsubscribe(),
49
+ onInterrupt: (callback) => {
50
+ interruptCallback = callback;
51
+ return () => {
52
+ if (interruptCallback === callback) interruptCallback = undefined;
53
+ };
54
+ },
55
+ };
56
+ }
57
+
58
+ if (import.meta.vitest) {
59
+ const { test, expect } = import.meta.vitest;
60
+
61
+ // Environment detection and client building are covered by `@parity/truapi`'s
62
+ // own sandbox tests; here we only assert the local glue degrades outside a
63
+ // host container.
64
+ test("getClientSync returns null outside a container", () => {
65
+ expect(getClientSync()).toBeNull();
66
+ });
67
+
68
+ test("getClient resolves null outside a container", async () => {
69
+ expect(await getClient()).toBeNull();
70
+ });
71
+ }