@notionhq/custom-blocks-host 0.1.30 → 0.2.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/README.md +39 -19
- package/dist/createCustomBlockHost.d.ts +8 -12
- package/dist/createCustomBlockHost.d.ts.map +1 -1
- package/dist/createCustomBlockHost.js +138 -162
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/lifecycle/initErrors.d.ts.map +1 -1
- package/dist/lifecycle/initErrors.js +6 -0
- package/dist/lifecycle/initializationController.d.ts +5 -30
- package/dist/lifecycle/initializationController.d.ts.map +1 -1
- package/dist/lifecycle/initializationController.js +90 -42
- package/dist/lifecycle/types.d.ts +14 -8
- package/dist/lifecycle/types.d.ts.map +1 -1
- package/dist/messages/sandboxMessageRouter.d.ts +2 -2
- package/dist/messages/sandboxMessageRouter.d.ts.map +1 -1
- package/dist/messages/sandboxMessageRouter.js +5 -5
- package/dist/protocol/messages/init.d.ts +1 -1
- package/package.json +1 -1
- package/src/createCustomBlockHost.ts +176 -187
- package/src/index.ts +2 -1
- package/src/lifecycle/initErrors.ts +6 -0
- package/src/lifecycle/initializationController.ts +100 -77
- package/src/lifecycle/types.ts +20 -11
- package/src/messages/sandboxMessageRouter.ts +7 -7
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"initializationController.d.ts","sourceRoot":"","sources":["../../src/lifecycle/initializationController.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"initializationController.d.ts","sourceRoot":"","sources":["../../src/lifecycle/initializationController.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sDAAsD,CAAA;AAC1F,OAAO,KAAK,EACX,wBAAwB,EACxB,WAAW,EACX,MAAM,mDAAmD,CAAA;AAC1D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,yDAAyD,CAAA;AAChG,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,sB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gBAAgB,EAAE,MAAM,GAAG,SAAS,CAAA;QACpC,KAAK,EAAE,wBAAwB,CAAA;KAC/B,KAAK,IAAI,CAAA;IACV,YAAY,EAAE,MAAM,IAAI,CAAA;IACxB,IAAI,EAAE,MAAM,IAAI,CAAA;CAChB,CAAA;AAED,wBAAgB,8BAA8B,CAC7C,OAAO,EAAE,+BAA+B,GACtC,wBAAwB,CAiU1B"}
|
|
@@ -1,11 +1,15 @@
|
|
|
1
|
+
import { DEFAULT_CONTRAST_MODE } from "../protocol/contrast.js";
|
|
2
|
+
import { unreachable } from "../utils.js";
|
|
1
3
|
import { initErrorForFailureReason } from "./initErrors.js";
|
|
2
4
|
import { isValidBridgeProtocolVersion } from "./protocolVersion.js";
|
|
3
5
|
const DEFAULT_NO_CONNECT_TIMEOUT_MS = 5_000;
|
|
6
|
+
const DEFAULT_INITIAL_STATE_TIMEOUT_MS = 5_000;
|
|
4
7
|
const DEFAULT_NO_INIT_RESULT_TIMEOUT_MS = 5_000;
|
|
5
8
|
export function createInitializationController(options) {
|
|
6
|
-
const {
|
|
9
|
+
const { prepareInit, sendInitMessage, warn, flushPendingHostStateChanges, minBridgeProtocolVersion, noConnectTimeoutMs = DEFAULT_NO_CONNECT_TIMEOUT_MS, initialStateTimeoutMs = DEFAULT_INITIAL_STATE_TIMEOUT_MS, noInitResultTimeoutMs = DEFAULT_NO_INIT_RESULT_TIMEOUT_MS, stopHostWithFailure, onInitResult, onFailure, } = options;
|
|
7
10
|
let initStatus = "waitingForConnect";
|
|
8
11
|
let noConnectTimeoutId;
|
|
12
|
+
let initialStateTimeoutId;
|
|
9
13
|
let noInitResultTimeoutId;
|
|
10
14
|
let activeInitializationId;
|
|
11
15
|
let hasObservedIframeLoad = false;
|
|
@@ -16,6 +20,12 @@ export function createInitializationController(options) {
|
|
|
16
20
|
noConnectTimeoutId = undefined;
|
|
17
21
|
}
|
|
18
22
|
}
|
|
23
|
+
function clearInitialStateTimeout() {
|
|
24
|
+
if (initialStateTimeoutId !== undefined) {
|
|
25
|
+
window.clearTimeout(initialStateTimeoutId);
|
|
26
|
+
initialStateTimeoutId = undefined;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
19
29
|
function clearNoInitResultTimeout() {
|
|
20
30
|
if (noInitResultTimeoutId !== undefined) {
|
|
21
31
|
window.clearTimeout(noInitResultTimeoutId);
|
|
@@ -24,13 +34,23 @@ export function createInitializationController(options) {
|
|
|
24
34
|
}
|
|
25
35
|
function sendInitError(args) {
|
|
26
36
|
const { initializationId, error } = args;
|
|
27
|
-
|
|
28
|
-
|
|
37
|
+
switch (initStatus) {
|
|
38
|
+
case "waitingForConnect":
|
|
39
|
+
case "preparingInit":
|
|
40
|
+
break;
|
|
41
|
+
case "waitingForInitResult":
|
|
42
|
+
case "initialized":
|
|
43
|
+
case "error":
|
|
44
|
+
case "stopped":
|
|
45
|
+
return;
|
|
46
|
+
default:
|
|
47
|
+
return unreachable(initStatus);
|
|
29
48
|
}
|
|
30
49
|
initStatus = "error";
|
|
31
50
|
activeInitializationId = initializationId;
|
|
32
51
|
clearNoConnectTimeout();
|
|
33
|
-
|
|
52
|
+
clearInitialStateTimeout();
|
|
53
|
+
sendInitMessage({
|
|
34
54
|
type: "init",
|
|
35
55
|
initializationId,
|
|
36
56
|
status: "error",
|
|
@@ -38,49 +58,78 @@ export function createInitializationController(options) {
|
|
|
38
58
|
});
|
|
39
59
|
onFailure?.(error);
|
|
40
60
|
}
|
|
41
|
-
function
|
|
61
|
+
async function prepareAndSendInit(connect) {
|
|
42
62
|
if (initStatus !== "waitingForConnect") {
|
|
43
|
-
return
|
|
63
|
+
return;
|
|
44
64
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
65
|
+
initStatus = "preparingInit";
|
|
66
|
+
activeInitializationId = connect.initializationId;
|
|
67
|
+
clearNoConnectTimeout();
|
|
68
|
+
initialStateTimeoutId = window.setTimeout(() => {
|
|
69
|
+
if (initStatus !== "preparingInit") {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const error = initErrorForFailureReason("initial_state_timeout");
|
|
73
|
+
warn({
|
|
74
|
+
...error,
|
|
48
75
|
initializationId: connect.initializationId,
|
|
49
|
-
error: preparation.error,
|
|
50
76
|
});
|
|
51
|
-
|
|
77
|
+
sendInitError({ initializationId: connect.initializationId, error });
|
|
78
|
+
}, initialStateTimeoutMs);
|
|
79
|
+
try {
|
|
80
|
+
const preparation = await prepareInit(connect);
|
|
81
|
+
if (initStatus !== "preparingInit") {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
clearInitialStateTimeout();
|
|
85
|
+
if (preparation.status === "error") {
|
|
86
|
+
sendInitError({
|
|
87
|
+
initializationId: connect.initializationId,
|
|
88
|
+
error: preparation.error,
|
|
89
|
+
});
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
initStatus = "waitingForInitResult";
|
|
93
|
+
const message = {
|
|
94
|
+
type: "init",
|
|
95
|
+
initializationId: connect.initializationId,
|
|
96
|
+
status: "success",
|
|
97
|
+
theme: preparation.initialState.theme,
|
|
98
|
+
contrastMode: preparation.initialState.contrastMode ?? DEFAULT_CONTRAST_MODE,
|
|
99
|
+
blockId: preparation.initialState.blockId,
|
|
100
|
+
parent: preparation.initialState.parent,
|
|
101
|
+
page: preparation.initialState.page,
|
|
102
|
+
currentUser: preparation.initialState.currentUser,
|
|
103
|
+
manifest: preparation.initialState.manifest,
|
|
104
|
+
dataSources: preparation.initialState.dataSources,
|
|
105
|
+
};
|
|
106
|
+
sendInitMessage(message);
|
|
107
|
+
noInitResultTimeoutId = window.setTimeout(() => {
|
|
108
|
+
if (initStatus !== "waitingForInitResult") {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
initStatus = "error";
|
|
112
|
+
const error = initErrorForFailureReason("no_init_result");
|
|
113
|
+
onFailure?.(error);
|
|
114
|
+
warn({
|
|
115
|
+
...error,
|
|
116
|
+
initializationId: connect.initializationId,
|
|
117
|
+
});
|
|
118
|
+
}, noInitResultTimeoutMs);
|
|
52
119
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
clearNoConnectTimeout();
|
|
56
|
-
const state = getState();
|
|
57
|
-
const message = {
|
|
58
|
-
type: "init",
|
|
59
|
-
initializationId: connect.initializationId,
|
|
60
|
-
status: "success",
|
|
61
|
-
theme: state.theme,
|
|
62
|
-
contrastMode: state.contrastMode,
|
|
63
|
-
blockId,
|
|
64
|
-
parent: state.parent,
|
|
65
|
-
page: state.page,
|
|
66
|
-
manifest: preparation.manifest,
|
|
67
|
-
dataSources: preparation.dataSources,
|
|
68
|
-
currentUser: state.currentUser,
|
|
69
|
-
};
|
|
70
|
-
post(message);
|
|
71
|
-
noInitResultTimeoutId = window.setTimeout(() => {
|
|
72
|
-
if (initStatus !== "waitingForInitResult") {
|
|
120
|
+
catch (error) {
|
|
121
|
+
if (initStatus !== "preparingInit") {
|
|
73
122
|
return;
|
|
74
123
|
}
|
|
75
|
-
initStatus = "error";
|
|
76
|
-
const error = initErrorForFailureReason("no_init_result");
|
|
77
124
|
warn({
|
|
78
|
-
message: "
|
|
125
|
+
message: "Failed to prepare custom block initialization",
|
|
126
|
+
error,
|
|
127
|
+
});
|
|
128
|
+
sendInitError({
|
|
79
129
|
initializationId: connect.initializationId,
|
|
130
|
+
error: initErrorForFailureReason("unknown_error"),
|
|
80
131
|
});
|
|
81
|
-
|
|
82
|
-
}, noInitResultTimeoutMs);
|
|
83
|
-
return true;
|
|
132
|
+
}
|
|
84
133
|
}
|
|
85
134
|
function handleConnect(message) {
|
|
86
135
|
if (initStatus === "stopped") {
|
|
@@ -119,9 +168,8 @@ export function createInitializationController(options) {
|
|
|
119
168
|
});
|
|
120
169
|
return;
|
|
121
170
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
}
|
|
171
|
+
hasConnectedSandbox = true;
|
|
172
|
+
void prepareAndSendInit(message);
|
|
125
173
|
}
|
|
126
174
|
function handleInvalidConnect(args) {
|
|
127
175
|
if (initStatus === "stopped") {
|
|
@@ -203,12 +251,11 @@ export function createInitializationController(options) {
|
|
|
203
251
|
return;
|
|
204
252
|
}
|
|
205
253
|
clearNoInitResultTimeout();
|
|
254
|
+
initStatus = message.status === "success" ? "initialized" : "error";
|
|
206
255
|
if (message.status === "success") {
|
|
207
|
-
initStatus = "success";
|
|
208
256
|
flushPendingHostStateChanges();
|
|
209
257
|
}
|
|
210
258
|
else {
|
|
211
|
-
initStatus = "error";
|
|
212
259
|
onFailure?.(message.error);
|
|
213
260
|
}
|
|
214
261
|
onInitResult?.(message);
|
|
@@ -216,6 +263,7 @@ export function createInitializationController(options) {
|
|
|
216
263
|
function stop() {
|
|
217
264
|
initStatus = "stopped";
|
|
218
265
|
clearNoConnectTimeout();
|
|
266
|
+
clearInitialStateTimeout();
|
|
219
267
|
clearNoInitResultTimeout();
|
|
220
268
|
}
|
|
221
269
|
return {
|
|
@@ -3,6 +3,7 @@ import type { NotionDataSourceBindings } from "../protocol/dataSources/dataSourc
|
|
|
3
3
|
import type { NotionBlockId } from "../protocol/ids.js";
|
|
4
4
|
import type { CustomBlockManifest } from "../protocol/manifest.js";
|
|
5
5
|
import type { ConnectMessage } from "../protocol/messages/connect.js";
|
|
6
|
+
import type { CustomBlockInitErrorInfo } from "../protocol/messages/init.js";
|
|
6
7
|
import type { CustomBlockPage } from "../protocol/pages/page.js";
|
|
7
8
|
import type { NotionParent } from "../protocol/parent.js";
|
|
8
9
|
import type { NotionTheme } from "../protocol/theme.js";
|
|
@@ -14,10 +15,12 @@ export type ConnectSuccessMessage = Extract<ConnectMessage, {
|
|
|
14
15
|
export type InitStatus =
|
|
15
16
|
/** The host is waiting for the sandbox's first `connect` message. */
|
|
16
17
|
"waitingForConnect"
|
|
18
|
+
/** The host is waiting for `getInitialState` to prepare `init`. */
|
|
19
|
+
| "preparingInit"
|
|
17
20
|
/** The host is waiting for a matching `initResult` after it sends `init`. */
|
|
18
21
|
| "waitingForInitResult"
|
|
19
22
|
/** The host is initialized and can send live updates. */
|
|
20
|
-
| "
|
|
23
|
+
| "initialized"
|
|
21
24
|
/** The host is in an error state after an initialization failure or timeout. */
|
|
22
25
|
| "error"
|
|
23
26
|
/** The host is stopped and no longer sends or accepts sandbox messages. */
|
|
@@ -25,7 +28,6 @@ export type InitStatus =
|
|
|
25
28
|
export type CustomBlockHostDataSourcesPayload = {
|
|
26
29
|
bindings: NotionDataSourceBindings;
|
|
27
30
|
};
|
|
28
|
-
export type CustomBlockHostInitialDataSources = CustomBlockHostDataSourcesPayload | ((connect: ConnectSuccessMessage) => CustomBlockHostDataSourcesPayload);
|
|
29
31
|
export type CustomBlockHostInitialState = {
|
|
30
32
|
theme: NotionTheme;
|
|
31
33
|
/**
|
|
@@ -36,12 +38,16 @@ export type CustomBlockHostInitialState = {
|
|
|
36
38
|
blockId: NotionBlockId;
|
|
37
39
|
parent: NotionParent;
|
|
38
40
|
page: CustomBlockPage;
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
* requires the sandbox to provide a manifest in `connect`.
|
|
42
|
-
*/
|
|
43
|
-
manifest?: CustomBlockManifest;
|
|
44
|
-
dataSources: CustomBlockHostInitialDataSources;
|
|
41
|
+
manifest: CustomBlockManifest;
|
|
42
|
+
dataSources: CustomBlockHostDataSourcesPayload;
|
|
45
43
|
currentUser: NotionUser;
|
|
46
44
|
};
|
|
45
|
+
export type CustomBlockHostInitialStateResult = {
|
|
46
|
+
status: "success";
|
|
47
|
+
initialState: CustomBlockHostInitialState;
|
|
48
|
+
} | {
|
|
49
|
+
status: "error";
|
|
50
|
+
error: CustomBlockInitErrorInfo;
|
|
51
|
+
};
|
|
52
|
+
export type CustomBlockHostGetInitialState = (connect: ConnectSuccessMessage) => Promise<CustomBlockHostInitialStateResult>;
|
|
47
53
|
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/lifecycle/types.ts"],"names":[],"mappings":"AAAA,OAAO,K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Message } from "../protocol/messages/createPage.js";
|
|
3
3
|
import type { GetPageMessage } from "../protocol/messages/getPage.js";
|
|
4
4
|
import type { GetUserMessage } from "../protocol/messages/getUser.js";
|
|
5
|
-
import type { HostToSandboxMessage } from "../protocol/messages/hostToSandbox.js";
|
|
6
5
|
import type { InitResultMessage } from "../protocol/messages/initResult.js";
|
|
6
|
+
import type { InvalidSandboxMessage } from "../protocol/messages/invalidSandboxMessage.js";
|
|
7
7
|
import type { ListUsersMessage } from "../protocol/messages/listUsers.js";
|
|
8
8
|
import type { OpenPageMessage } from "../protocol/messages/openPage.js";
|
|
9
9
|
import type { QueryDataSourceMessage } from "../protocol/messages/queryDataSource.js";
|
|
@@ -18,7 +18,7 @@ export type SandboxMessageRouterOptions = {
|
|
|
18
18
|
sandboxOrigin: string;
|
|
19
19
|
getInitStatus: () => InitStatus;
|
|
20
20
|
emit: (direction: CustomBlockHostLogDirection, payload: unknown) => void;
|
|
21
|
-
|
|
21
|
+
sendNack: (message: InvalidSandboxMessage) => void;
|
|
22
22
|
onInvalidConnect: InitializationController["handleInvalidConnect"];
|
|
23
23
|
onInvalidInitResult: InitializationController["handleInvalidInitResult"];
|
|
24
24
|
onConnect: (message: ConnectMessage) => void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sandboxMessageRouter.d.ts","sourceRoot":"","sources":["../../src/messages/sandboxMessageRouter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sDAAsD,CAAA;AAC1F,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,yDAAyD,CAAA;AAChG,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sDAAsD,CAAA;AAC1F,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sDAAsD,CAAA;AAC1F,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"sandboxMessageRouter.d.ts","sourceRoot":"","sources":["../../src/messages/sandboxMessageRouter.ts"],"names":[],"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AAmB,EAAE,wBAAwB,CAAC,yBAAyB,CAAC,CAAA;IACxE,SAAS,EAAE,CAAC,OAAO,EAAE,cAAc,KAAK,IAAI,CAAA;IAC5C,YAAY,EAAE,CAAC,OAAO,EAAE,iBAAiB,KAAK,IAAI,CAAA;IAClD,iBAAiB,EAAE,CAAC,OAAO,EAAE,sBAAsB,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAC5E,4BAA4B,EAAE,CAC7B,OAAO,EAAE,iCAAiC,KACtC,IAAI,CAAA;IACT,YAAY,EAAE,CAAC,OAAO,EAAE,iBAAiB,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAClE,UAAU,EAAE,CAAC,OAAO,EAAE,eAAe,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAC9D,SAAS,EAAE,CAAC,OAAO,EAAE,cAAc,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAC5D,SAAS,EAAE,CAAC,OAAO,EAAE,cAAc,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAC5D,WAAW,EAAE,CAAC,OAAO,EAAE,gBAAgB,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAChE,YAAY,EAAE,CAAC,OAAO,EAAE,iBAAiB,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAClE,QAAQ,EAAE,CAAC,OAAO,EAAE,aAAa,KAAK,IAAI,CAAA;CAC1C,CAAA;AAED,wBAAgB,0BAA0B,CACzC,OAAO,EAAE,2BAA2B,GAClC,CAAC,KAAK,EAAE,YAAY,KAAK,IAAI,CAsI/B"}
|
|
@@ -13,7 +13,7 @@ const bridgeProtocolVersionSchema = v.object({
|
|
|
13
13
|
bridgeProtocolVersion: v.union([v.number(), v.nan()]),
|
|
14
14
|
});
|
|
15
15
|
export function createSandboxMessageRouter(options) {
|
|
16
|
-
const { iframe, sandboxOrigin, getInitStatus, emit,
|
|
16
|
+
const { iframe, sandboxOrigin, getInitStatus, emit, sendNack, onInvalidConnect, onInvalidInitResult, onConnect, onInitResult, onQueryDataSource, onUnsubscribeDataSourceQuery, onCreatePage, onGetPage, onOpenPage, onGetUser, onListUsers, onUpdatePage, onResize, } = options;
|
|
17
17
|
function onInvalidSandboxMessage(data, issues) {
|
|
18
18
|
emit("warn", {
|
|
19
19
|
message: "Ignored unsupported sandbox message",
|
|
@@ -47,9 +47,9 @@ export function createSandboxMessageRouter(options) {
|
|
|
47
47
|
error: initErrorForFailureReason("invalid_init_result_payload"),
|
|
48
48
|
});
|
|
49
49
|
}
|
|
50
|
-
// Other messages get NACKed indicating they were invalid and skipped
|
|
51
|
-
if (response.nack !== undefined) {
|
|
52
|
-
|
|
50
|
+
// Other messages get NACKed indicating they were invalid and skipped.
|
|
51
|
+
if (response.nack !== undefined && getInitStatus() !== "stopped") {
|
|
52
|
+
sendNack(response.nack);
|
|
53
53
|
}
|
|
54
54
|
}
|
|
55
55
|
return function onMessage(event) {
|
|
@@ -67,7 +67,7 @@ export function createSandboxMessageRouter(options) {
|
|
|
67
67
|
return;
|
|
68
68
|
}
|
|
69
69
|
const message = parsed.output;
|
|
70
|
-
if (!isInitHandshakeMessage(message) && getInitStatus() !== "
|
|
70
|
+
if (!isInitHandshakeMessage(message) && getInitStatus() !== "initialized") {
|
|
71
71
|
emit("warn", {
|
|
72
72
|
message: `Ignored ${message.type} before initialization completed`,
|
|
73
73
|
payload: message,
|
|
@@ -2,7 +2,7 @@ import * as v from "valibot";
|
|
|
2
2
|
import type { CustomBlockErrorCode, CustomBlockErrorInfo } from "../errors.js";
|
|
3
3
|
import type { CustomBlockInitResultErrorCode } from "./initResult.js";
|
|
4
4
|
export declare const customBlockInitErrorCodeSchema: v.StringSchema<undefined>;
|
|
5
|
-
export type CustomBlockInitErrorCode = CustomBlockErrorCode<"manifest_unavailable" | "manifest_invalid" | "invalid_connect_payload" | "invalid_protocol_version" | "unsupported_protocol_version" | "init_timeout" | "context_unavailable" | "current_user_unavailable" | "missing_data_source_binding" | "data_source_unavailable" | "missing_property_binding" | "invalid_property_binding">;
|
|
5
|
+
export type CustomBlockInitErrorCode = CustomBlockErrorCode<"manifest_unavailable" | "manifest_invalid" | "invalid_connect_payload" | "invalid_protocol_version" | "unsupported_protocol_version" | "init_timeout" | "context_unavailable" | "current_user_unavailable" | "initial_state_timeout" | "missing_data_source_binding" | "data_source_unavailable" | "missing_property_binding" | "invalid_property_binding">;
|
|
6
6
|
export type CustomBlockInitErrorInfo = CustomBlockErrorInfo<CustomBlockInitErrorCode>;
|
|
7
7
|
/**
|
|
8
8
|
* Every initialization failure visible to block code. This combines errors
|