@oatscenter/trellis-svelte 0.100.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -0
- package/dist/authorization_context.d.ts +18 -0
- package/dist/authorization_context.js +150 -0
- package/dist/components/TrellisContextProvider.svelte +24 -0
- package/dist/components/TrellisContextProvider.svelte.d.ts +11 -0
- package/dist/components/TrellisProvider.svelte +192 -0
- package/dist/components/TrellisProvider.svelte.d.ts +6 -0
- package/dist/components/TrellisProvider.types.d.ts +15 -0
- package/dist/components/TrellisProvider.types.js +1 -0
- package/dist/context.d.ts +72 -0
- package/dist/context.js +116 -0
- package/dist/device_activation.d.ts +6 -0
- package/dist/device_activation.js +19 -0
- package/dist/device_activation_controller.d.ts +91 -0
- package/dist/device_activation_controller.js +318 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +5 -0
- package/dist/internal/activation_view.d.ts +44 -0
- package/dist/internal/activation_view.js +95 -0
- package/dist/internal/callback_state.d.ts +9 -0
- package/dist/internal/callback_state.js +24 -0
- package/dist/internal/portal_url.d.ts +13 -0
- package/dist/internal/portal_url.js +57 -0
- package/dist/portal_flow.d.ts +24 -0
- package/dist/portal_flow.js +173 -0
- package/package.json +42 -0
- package/src/authorization_context.svelte.ts +153 -0
- package/src/components/TrellisContextProvider.svelte +24 -0
- package/src/components/TrellisProvider.svelte +192 -0
- package/src/components/TrellisProvider.types.ts +28 -0
- package/src/context.svelte.ts +205 -0
- package/src/device_activation.svelte.ts +28 -0
- package/src/device_activation_controller.ts +528 -0
- package/src/index.ts +31 -0
- package/src/internal/activation_view.ts +179 -0
- package/src/internal/callback_state.ts +48 -0
- package/src/internal/portal_url.ts +94 -0
- package/src/portal_flow.svelte.ts +143 -0
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CallerParticipant,
|
|
3
|
+
ConnectedTrellisClient,
|
|
4
|
+
TrellisConnection,
|
|
5
|
+
TrellisConnectionStatus,
|
|
6
|
+
} from "@oatscenter/trellis";
|
|
7
|
+
import { createContext } from "svelte";
|
|
8
|
+
import { createSubscriber } from "svelte/reactivity";
|
|
9
|
+
|
|
10
|
+
/** Minimal contract shape required to create a typed Trellis Svelte app context. */
|
|
11
|
+
export type TrellisParticipantLike = CallerParticipant;
|
|
12
|
+
|
|
13
|
+
/** Real connected Trellis client type exposed by a Svelte app context. */
|
|
14
|
+
export type TrellisClientFor<TContract extends TrellisParticipantLike> =
|
|
15
|
+
ConnectedTrellisClient<TContract>;
|
|
16
|
+
|
|
17
|
+
/** Minimal client surface required for Trellis Svelte context clients. */
|
|
18
|
+
export type TrellisContextClient = {
|
|
19
|
+
readonly connection: TrellisConnection;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/** URL value accepted by a Trellis Svelte app owner. */
|
|
23
|
+
export type TrellisAppUrl = string | URL;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Trellis URL configuration for a Svelte app owner.
|
|
27
|
+
*
|
|
28
|
+
* A function resolver supports apps whose Trellis instance is selected at
|
|
29
|
+
* runtime. Returning `undefined` means no instance is currently available.
|
|
30
|
+
*/
|
|
31
|
+
export type TrellisAppUrlResolver =
|
|
32
|
+
| TrellisAppUrl
|
|
33
|
+
| (() => TrellisAppUrl | undefined);
|
|
34
|
+
|
|
35
|
+
/** Options used to create a Trellis Svelte app owner. */
|
|
36
|
+
export type CreateTrellisAppOptions<
|
|
37
|
+
TContract extends TrellisParticipantLike = TrellisParticipantLike,
|
|
38
|
+
> = {
|
|
39
|
+
/** Generated participant used by this app context and its connections. */
|
|
40
|
+
participant: TContract;
|
|
41
|
+
|
|
42
|
+
/** Trellis URL, or a resolver for runtime-selected Trellis URLs. */
|
|
43
|
+
trellisUrl: TrellisAppUrlResolver;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/** Resolves a Trellis app URL config to the string shape required by the client. */
|
|
47
|
+
export function resolveTrellisAppUrl(
|
|
48
|
+
trellisUrl: TrellisAppUrlResolver,
|
|
49
|
+
): string | undefined {
|
|
50
|
+
const resolved = typeof trellisUrl === "function" ? trellisUrl() : trellisUrl;
|
|
51
|
+
return resolved?.toString();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Svelte-reactive adapter around a framework-neutral Trellis connection. */
|
|
55
|
+
export class SvelteTrellisConnection {
|
|
56
|
+
#connection: TrellisConnection;
|
|
57
|
+
#subscribe: () => void;
|
|
58
|
+
|
|
59
|
+
/** Creates a reactive connection adapter for a connected Trellis runtime. */
|
|
60
|
+
constructor(connection: TrellisConnection) {
|
|
61
|
+
this.#connection = connection;
|
|
62
|
+
this.#subscribe = createSubscriber((update) => {
|
|
63
|
+
return this.#connection.subscribe(() => update());
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Latest connection status, reactive when read by Svelte effects or markup. */
|
|
68
|
+
get status(): TrellisConnectionStatus {
|
|
69
|
+
this.#subscribe();
|
|
70
|
+
return this.#connection.status;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Closes the underlying Trellis runtime connection. */
|
|
74
|
+
close(): Promise<void> {
|
|
75
|
+
return this.#connection.close();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
type TrellisAppContext<TClient extends TrellisContextClient> = {
|
|
80
|
+
trellis: TClient;
|
|
81
|
+
connection: SvelteTrellisConnection;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const provideTrellisContext = Symbol("provideTrellisContext");
|
|
85
|
+
const trellisAppOwnerBrand: unique symbol = Symbol("trellisAppOwner");
|
|
86
|
+
|
|
87
|
+
/** Minimal branded app owner surface accepted by the Trellis Svelte provider. */
|
|
88
|
+
export type TrellisAppOwner<
|
|
89
|
+
TContract extends TrellisParticipantLike = TrellisParticipantLike,
|
|
90
|
+
> = {
|
|
91
|
+
readonly participant: TContract;
|
|
92
|
+
readonly trellisUrl: TrellisAppUrlResolver;
|
|
93
|
+
readonly [trellisAppOwnerBrand]: true;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Public app-scoped typed Svelte context owner for a Trellis browser app.
|
|
98
|
+
*
|
|
99
|
+
* `TClient` is a type-only refinement over the descriptor-inferred caller runtime.
|
|
100
|
+
*/
|
|
101
|
+
export interface TrellisApp<
|
|
102
|
+
TContract extends TrellisParticipantLike = TrellisParticipantLike,
|
|
103
|
+
TClient extends TrellisContextClient = TrellisClientFor<TContract>,
|
|
104
|
+
> extends TrellisAppOwner<TContract> {
|
|
105
|
+
/** Generated participant used by this app context and its connections. */
|
|
106
|
+
readonly participant: TContract;
|
|
107
|
+
|
|
108
|
+
/** Trellis URL configuration used by `TrellisProvider` connections. */
|
|
109
|
+
readonly trellisUrl: TrellisAppUrlResolver;
|
|
110
|
+
|
|
111
|
+
/** Returns the contract-typed connected Trellis client from Svelte context synchronously. */
|
|
112
|
+
getTrellis(): TClient;
|
|
113
|
+
|
|
114
|
+
/** Returns a Svelte-reactive adapter for the real Trellis connection. */
|
|
115
|
+
getConnection(): SvelteTrellisConnection;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Internal app-scoped typed Svelte context implementation. */
|
|
119
|
+
class TrellisAppImpl<
|
|
120
|
+
TContract extends TrellisParticipantLike = TrellisParticipantLike,
|
|
121
|
+
TClient extends TrellisContextClient = TrellisClientFor<TContract>,
|
|
122
|
+
> {
|
|
123
|
+
readonly [trellisAppOwnerBrand] = true as const;
|
|
124
|
+
readonly #participant: TContract;
|
|
125
|
+
readonly #trellisUrl: TrellisAppUrlResolver;
|
|
126
|
+
readonly #getContext: () => TrellisAppContext<TClient>;
|
|
127
|
+
readonly #setContext: (
|
|
128
|
+
context: TrellisAppContext<TClient>,
|
|
129
|
+
) => TrellisAppContext<TClient>;
|
|
130
|
+
|
|
131
|
+
/** Creates an app-scoped context owner for a specific Trellis contract. */
|
|
132
|
+
constructor(options: CreateTrellisAppOptions<TContract>) {
|
|
133
|
+
const { participant, trellisUrl } = options;
|
|
134
|
+
this.#participant = participant;
|
|
135
|
+
this.#trellisUrl = trellisUrl;
|
|
136
|
+
const [getContext, setContext] = createContext<
|
|
137
|
+
TrellisAppContext<TClient>
|
|
138
|
+
>();
|
|
139
|
+
this.#getContext = getContext;
|
|
140
|
+
this.#setContext = setContext;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Generated participant used by `TrellisProvider` connections. */
|
|
144
|
+
get participant(): TContract {
|
|
145
|
+
return this.#participant;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Trellis URL configuration used by `TrellisProvider` connections. */
|
|
149
|
+
get trellisUrl(): TrellisAppUrlResolver {
|
|
150
|
+
return this.#trellisUrl;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Returns the contract-typed connected Trellis client from Svelte context synchronously. */
|
|
154
|
+
getTrellis(): TClient {
|
|
155
|
+
return this.#getContext().trellis;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** Returns a Svelte-reactive adapter for the real Trellis connection. */
|
|
159
|
+
getConnection(): SvelteTrellisConnection {
|
|
160
|
+
return this.#getContext().connection;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Installs the connected Trellis runtime into Svelte context synchronously. */
|
|
164
|
+
[provideTrellisContext](trellis: TrellisContextClient): void {
|
|
165
|
+
this.#setContext({
|
|
166
|
+
trellis: trellis as TClient,
|
|
167
|
+
connection: new SvelteTrellisConnection(trellis.connection),
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Creates an app-scoped typed Svelte context owner for a Trellis contract and URL.
|
|
174
|
+
*
|
|
175
|
+
* The optional `TClient` type parameter refines the connected caller runtime.
|
|
176
|
+
*/
|
|
177
|
+
export function createTrellisApp<
|
|
178
|
+
TContract extends TrellisParticipantLike,
|
|
179
|
+
TClient extends TrellisContextClient = TrellisClientFor<TContract>,
|
|
180
|
+
>(
|
|
181
|
+
options: CreateTrellisAppOptions<TContract>,
|
|
182
|
+
): TrellisApp<TContract, TClient> {
|
|
183
|
+
return new TrellisAppImpl<TContract, TClient>(options);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function isTrellisAppImpl(
|
|
187
|
+
app: TrellisAppOwner,
|
|
188
|
+
): app is TrellisAppImpl<TrellisParticipantLike> {
|
|
189
|
+
return app instanceof TrellisAppImpl;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Internal provider helper that synchronously installs connected Trellis context.
|
|
194
|
+
*
|
|
195
|
+
* This is intentionally not exported from `src/index.ts`.
|
|
196
|
+
*/
|
|
197
|
+
export function provideConnectedTrellisContext(
|
|
198
|
+
app: TrellisAppOwner,
|
|
199
|
+
trellis: TrellisContextClient,
|
|
200
|
+
): void {
|
|
201
|
+
if (!isTrellisAppImpl(app)) {
|
|
202
|
+
throw new TypeError("Expected an app created by createTrellisApp");
|
|
203
|
+
}
|
|
204
|
+
app[provideTrellisContext](trellis);
|
|
205
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createInitialDeviceActivationState,
|
|
3
|
+
type DeviceActivationAuth,
|
|
4
|
+
type DeviceActivationClient,
|
|
5
|
+
type DeviceActivationControllerConfig,
|
|
6
|
+
DeviceActivationControllerCore,
|
|
7
|
+
type DeviceActivationOperationRef,
|
|
8
|
+
} from "./device_activation_controller.ts";
|
|
9
|
+
|
|
10
|
+
export type {
|
|
11
|
+
DeviceActivationAuth,
|
|
12
|
+
DeviceActivationClient,
|
|
13
|
+
DeviceActivationControllerConfig,
|
|
14
|
+
DeviceActivationOperationRef,
|
|
15
|
+
} from "./device_activation_controller.ts";
|
|
16
|
+
|
|
17
|
+
export class DeviceActivationController extends DeviceActivationControllerCore {
|
|
18
|
+
constructor(config: DeviceActivationControllerConfig) {
|
|
19
|
+
const state = $state(createInitialDeviceActivationState());
|
|
20
|
+
super(config, state);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function createDeviceActivationController(
|
|
25
|
+
config: DeviceActivationControllerConfig,
|
|
26
|
+
): DeviceActivationController {
|
|
27
|
+
return new DeviceActivationController(config);
|
|
28
|
+
}
|