@getuserfeedback/protocol 0.6.1 → 0.8.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/app-event.d.ts +779 -10
- package/dist/app-event.d.ts.map +1 -1
- package/dist/app-event.js +81 -8
- package/dist/flow-assignments.d.ts +7 -4
- package/dist/flow-assignments.d.ts.map +1 -1
- package/dist/flow-assignments.js +15 -5
- package/dist/host/constants.d.ts +2 -2
- package/dist/host/constants.d.ts.map +1 -1
- package/dist/host/constants.js +3 -1
- package/dist/host/external-id-argument-guards.d.ts +10 -0
- package/dist/host/external-id-argument-guards.d.ts.map +1 -0
- package/dist/host/external-id-argument-guards.js +27 -0
- package/dist/host/index.d.ts +1 -0
- package/dist/host/index.d.ts.map +1 -1
- package/dist/host/index.js +1 -0
- package/dist/host/sdk-types.d.ts +92 -27
- package/dist/host/sdk-types.d.ts.map +1 -1
- package/dist/host/sdk.d.ts.map +1 -1
- package/dist/identity-type.d.ts +2 -1
- package/dist/identity-type.d.ts.map +1 -1
- package/dist/identity-type.js +1 -1
- package/dist/index.d.ts +3 -87
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -6
- package/dist/widget-commands.d.ts +124 -3
- package/dist/widget-commands.d.ts.map +1 -1
- package/dist/widget-commands.js +10 -0
- package/dist/widget-config.d.ts +23 -5
- package/dist/widget-config.d.ts.map +1 -1
- package/dist/widget-config.js +2 -1
- package/package.json +22 -2
- package/src/app-event.ts +235 -0
- package/src/client-meta.ts +25 -0
- package/src/defaults.ts +4 -0
- package/src/errors.ts +58 -0
- package/src/flow-assignments.test.ts +82 -0
- package/src/flow-assignments.ts +151 -0
- package/src/host/command-dispatch.ts +59 -0
- package/src/host/command-envelope.ts +41 -0
- package/src/host/command-settlement.ts +121 -0
- package/src/host/constants.ts +103 -0
- package/src/host/external-id-argument-guards.ts +57 -0
- package/src/host/host-event-contract.ts +277 -0
- package/src/host/index.ts +14 -0
- package/src/host/lazy-handle-client.ts +207 -0
- package/src/host/request-id.ts +3 -0
- package/src/host/sdk-types.ts +528 -0
- package/src/host/sdk.ts +43 -0
- package/src/host/unique-id.ts +17 -0
- package/src/identity-type.ts +96 -0
- package/src/index.ts +139 -0
- package/src/protocol-root.test.ts +582 -0
- package/src/public-grant-scope.ts +25 -0
- package/src/runtime-endpoints.ts +69 -0
- package/src/scopes.ts +79 -0
- package/src/trpc-envelope.ts +9 -0
- package/src/version-resolution.ts +18 -0
- package/src/widget-commands.ts +162 -0
- package/src/widget-config.ts +164 -0
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
type Subscriber<THandleId extends string> = (handleId: THandleId) => void;
|
|
2
|
+
type StageSubscriber<TStage extends string> = (stage: TStage) => void;
|
|
3
|
+
|
|
4
|
+
type LifecycleStage = "uninitialized" | "ready" | "invalidated";
|
|
5
|
+
type StageMap<TStage extends string> = {
|
|
6
|
+
uninitialized: TStage;
|
|
7
|
+
ready: TStage;
|
|
8
|
+
invalidated: TStage;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const DEFAULT_LIFECYCLE_STAGES: StageMap<LifecycleStage> = {
|
|
12
|
+
uninitialized: "uninitialized",
|
|
13
|
+
ready: "ready",
|
|
14
|
+
invalidated: "invalidated",
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export type LazyHandleClient<
|
|
18
|
+
THandleId extends string,
|
|
19
|
+
TSettlement,
|
|
20
|
+
TStage extends string = LifecycleStage,
|
|
21
|
+
> = {
|
|
22
|
+
runCommand<TPayload>(
|
|
23
|
+
buildPayload: (handleId: THandleId | undefined) => TPayload,
|
|
24
|
+
dispatchAndWait: (payload: TPayload) => Promise<TSettlement>,
|
|
25
|
+
): Promise<TSettlement>;
|
|
26
|
+
getHandleId(): THandleId | undefined;
|
|
27
|
+
subscribeHandle(subscriber: Subscriber<THandleId>): () => void;
|
|
28
|
+
getStage(): TStage;
|
|
29
|
+
setStage(stage: TStage): void;
|
|
30
|
+
subscribeStage(subscriber: StageSubscriber<TStage>): () => void;
|
|
31
|
+
reset(): void;
|
|
32
|
+
invalidate(): void;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export type CreateLazyHandleClientOptions<
|
|
36
|
+
THandleId extends string,
|
|
37
|
+
TSettlement,
|
|
38
|
+
TStage extends string = LifecycleStage,
|
|
39
|
+
> = {
|
|
40
|
+
getHandleIdFromSettlement: (settlement: TSettlement) => THandleId | undefined;
|
|
41
|
+
stages?: StageMap<TStage>;
|
|
42
|
+
initialStage?: TStage;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
type CreateLazyHandleClientDefaultOptions<
|
|
46
|
+
THandleId extends string,
|
|
47
|
+
TSettlement,
|
|
48
|
+
> = {
|
|
49
|
+
getHandleIdFromSettlement: (settlement: TSettlement) => THandleId | undefined;
|
|
50
|
+
initialStage?: LifecycleStage;
|
|
51
|
+
stages?: undefined;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
type CreateLazyHandleClientCustomOptions<
|
|
55
|
+
THandleId extends string,
|
|
56
|
+
TSettlement,
|
|
57
|
+
TStage extends string,
|
|
58
|
+
> = {
|
|
59
|
+
getHandleIdFromSettlement: (settlement: TSettlement) => THandleId | undefined;
|
|
60
|
+
stages: StageMap<TStage>;
|
|
61
|
+
initialStage?: TStage;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Serializes commands for a lazily initialized handle.
|
|
66
|
+
* Each command payload is built only when its turn runs, so it always sees
|
|
67
|
+
* the latest settled handle id from prior commands.
|
|
68
|
+
*/
|
|
69
|
+
const createLazyHandleClientWithStages = <
|
|
70
|
+
THandleId extends string,
|
|
71
|
+
TSettlement,
|
|
72
|
+
TStage extends string,
|
|
73
|
+
>(input: {
|
|
74
|
+
getHandleIdFromSettlement: (settlement: TSettlement) => THandleId | undefined;
|
|
75
|
+
stages: StageMap<TStage>;
|
|
76
|
+
initialStage?: TStage;
|
|
77
|
+
}): LazyHandleClient<THandleId, TSettlement, TStage> => {
|
|
78
|
+
let handleId: THandleId | undefined;
|
|
79
|
+
let tail = Promise.resolve();
|
|
80
|
+
let isIdle = true;
|
|
81
|
+
let epoch = 0;
|
|
82
|
+
const { stages } = input;
|
|
83
|
+
let stage = input.initialStage ?? stages.uninitialized;
|
|
84
|
+
const subscribers = new Set<Subscriber<THandleId>>();
|
|
85
|
+
const stageSubscribers = new Set<StageSubscriber<TStage>>();
|
|
86
|
+
|
|
87
|
+
const setStage = (nextStage: TStage): void => {
|
|
88
|
+
if (stage === nextStage) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
if (stage === stages.invalidated && nextStage !== stages.invalidated) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
stage = nextStage;
|
|
95
|
+
stageSubscribers.forEach((subscriber) => {
|
|
96
|
+
subscriber(nextStage);
|
|
97
|
+
});
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const publishHandle = (nextHandleId: THandleId): void => {
|
|
101
|
+
if (stage === stages.invalidated) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
handleId = nextHandleId;
|
|
105
|
+
setStage(stages.ready);
|
|
106
|
+
subscribers.forEach((subscriber) => {
|
|
107
|
+
subscriber(nextHandleId);
|
|
108
|
+
});
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
const runCommand: LazyHandleClient<
|
|
112
|
+
THandleId,
|
|
113
|
+
TSettlement,
|
|
114
|
+
TStage
|
|
115
|
+
>["runCommand"] = (buildPayload, dispatchAndWait) => {
|
|
116
|
+
const execute = async (): Promise<TSettlement> => {
|
|
117
|
+
if (stage === stages.invalidated) {
|
|
118
|
+
throw new Error("Handle client is invalidated");
|
|
119
|
+
}
|
|
120
|
+
const commandEpoch = epoch;
|
|
121
|
+
const payload = buildPayload(handleId);
|
|
122
|
+
const settlement = await dispatchAndWait(payload);
|
|
123
|
+
const settledHandleId = input.getHandleIdFromSettlement(settlement);
|
|
124
|
+
if (settledHandleId && commandEpoch === epoch) {
|
|
125
|
+
publishHandle(settledHandleId);
|
|
126
|
+
}
|
|
127
|
+
return settlement;
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
const run = isIdle ? execute() : tail.then(execute, execute);
|
|
131
|
+
isIdle = false;
|
|
132
|
+
tail = run.then(
|
|
133
|
+
() => {
|
|
134
|
+
isIdle = true;
|
|
135
|
+
},
|
|
136
|
+
() => {
|
|
137
|
+
isIdle = true;
|
|
138
|
+
},
|
|
139
|
+
);
|
|
140
|
+
return run;
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
return {
|
|
144
|
+
runCommand,
|
|
145
|
+
getHandleId: () => handleId,
|
|
146
|
+
subscribeHandle: (subscriber) => {
|
|
147
|
+
subscribers.add(subscriber);
|
|
148
|
+
if (handleId) {
|
|
149
|
+
subscriber(handleId);
|
|
150
|
+
}
|
|
151
|
+
return () => {
|
|
152
|
+
subscribers.delete(subscriber);
|
|
153
|
+
};
|
|
154
|
+
},
|
|
155
|
+
getStage: () => stage,
|
|
156
|
+
setStage,
|
|
157
|
+
subscribeStage: (subscriber) => {
|
|
158
|
+
stageSubscribers.add(subscriber);
|
|
159
|
+
subscriber(stage);
|
|
160
|
+
return () => {
|
|
161
|
+
stageSubscribers.delete(subscriber);
|
|
162
|
+
};
|
|
163
|
+
},
|
|
164
|
+
reset: () => {
|
|
165
|
+
epoch += 1;
|
|
166
|
+
handleId = undefined;
|
|
167
|
+
stage = stages.uninitialized;
|
|
168
|
+
stageSubscribers.forEach((subscriber) => {
|
|
169
|
+
subscriber(stage);
|
|
170
|
+
});
|
|
171
|
+
},
|
|
172
|
+
invalidate: () => {
|
|
173
|
+
epoch += 1;
|
|
174
|
+
handleId = undefined;
|
|
175
|
+
setStage(stages.invalidated);
|
|
176
|
+
},
|
|
177
|
+
};
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
export function createLazyHandleClient<THandleId extends string, TSettlement>(
|
|
181
|
+
options: CreateLazyHandleClientDefaultOptions<THandleId, TSettlement>,
|
|
182
|
+
): LazyHandleClient<THandleId, TSettlement, LifecycleStage>;
|
|
183
|
+
export function createLazyHandleClient<
|
|
184
|
+
THandleId extends string,
|
|
185
|
+
TSettlement,
|
|
186
|
+
TStage extends string,
|
|
187
|
+
>(
|
|
188
|
+
options: CreateLazyHandleClientCustomOptions<THandleId, TSettlement, TStage>,
|
|
189
|
+
): LazyHandleClient<THandleId, TSettlement, TStage>;
|
|
190
|
+
export function createLazyHandleClient<THandleId extends string, TSettlement>(
|
|
191
|
+
options:
|
|
192
|
+
| CreateLazyHandleClientDefaultOptions<THandleId, TSettlement>
|
|
193
|
+
| CreateLazyHandleClientCustomOptions<THandleId, TSettlement, string>,
|
|
194
|
+
): LazyHandleClient<THandleId, TSettlement, string> {
|
|
195
|
+
if ("stages" in options && options.stages) {
|
|
196
|
+
return createLazyHandleClientWithStages({
|
|
197
|
+
getHandleIdFromSettlement: options.getHandleIdFromSettlement,
|
|
198
|
+
stages: options.stages,
|
|
199
|
+
initialStage: options.initialStage,
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
return createLazyHandleClientWithStages({
|
|
203
|
+
getHandleIdFromSettlement: options.getHandleIdFromSettlement,
|
|
204
|
+
stages: DEFAULT_LIFECYCLE_STAGES,
|
|
205
|
+
initialStage: options.initialStage,
|
|
206
|
+
});
|
|
207
|
+
}
|