Skip to content

Commit 604e87b

Browse files
chore!: Make interface actions synchronous (#3361)
Interface actions used to be required to be asynchronous, this is no longer the case. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Convert Snap interface create/update flows from async to sync across controllers, RPC handlers, simulation hooks, and tests. > > - **Controllers**: > - Make `SnapInterfaceController#createInterface`, `updateInterface`, and private `#validateContent` synchronous; remove `await` usage. > - **RPC Methods**: > - Update `snap_createInterface` and `snap_updateInterface` implementations and hook types to return synchronously (no `Promise`). > - **Simulation**: > - Make permitted hooks `createInterface`/`updateInterface` synchronous; adjust `getInterfaceFromResult` to call controller without `await`. > - **Tests**: > - Remove `await` from synchronous calls; switch async rejection assertions to synchronous `.toThrow` where applicable; update usages across controller, simulation, and request tests. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit c8e8a1b. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 8b4369d commit 604e87b

File tree

10 files changed

+125
-133
lines changed

10 files changed

+125
-133
lines changed

packages/snaps-controllers/src/interface/SnapInterfaceController.test.tsx

Lines changed: 62 additions & 64 deletions
Large diffs are not rendered by default.

packages/snaps-controllers/src/interface/SnapInterfaceController.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,14 +277,14 @@ export class SnapInterfaceController extends BaseController<
277277
* @param contentType - The type of content.
278278
* @returns The newly interface id.
279279
*/
280-
async createInterface(
280+
createInterface(
281281
snapId: SnapId,
282282
content: ComponentOrElement,
283283
context?: InterfaceContext,
284284
contentType?: ContentType,
285285
) {
286286
const element = getJsxInterface(content);
287-
await this.#validateContent(element);
287+
this.#validateContent(element);
288288
validateInterfaceContext(context);
289289

290290
const id = nanoid();
@@ -333,15 +333,15 @@ export class SnapInterfaceController extends BaseController<
333333
* @param content - The new content.
334334
* @param context - An optional interface context object.
335335
*/
336-
async updateInterface(
336+
updateInterface(
337337
snapId: SnapId,
338338
id: string,
339339
content: ComponentOrElement,
340340
context?: InterfaceContext,
341341
) {
342342
this.#validateArgs(snapId, id);
343343
const element = getJsxInterface(content);
344-
await this.#validateContent(element);
344+
this.#validateContent(element);
345345
validateInterfaceContext(context);
346346

347347
const oldState = this.state.interfaces[id].state;
@@ -543,7 +543,7 @@ export class SnapInterfaceController extends BaseController<
543543
*
544544
* @param element - The JSX element to verify.
545545
*/
546-
async #validateContent(element: JSXElement) {
546+
#validateContent(element: JSXElement) {
547547
// We assume the validity of this JSON to be validated by the caller.
548548
// E.g., in the RPC method implementation.
549549
const size = getJsonSizeUnsafe(element);

packages/snaps-rpc-methods/src/permitted/createInterface.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export type CreateInterfaceMethodHooks = {
3232
ui: ComponentOrElement,
3333
context?: InterfaceContext,
3434
contentType?: ContentType,
35-
) => Promise<string>;
35+
) => string;
3636
};
3737

3838
export const createInterfaceHandler: PermittedHandlerExport<
@@ -67,21 +67,21 @@ export type CreateInterfaceParameters = InferMatching<
6767
* @param hooks.createInterface - The function to create the interface.
6868
* @returns Nothing.
6969
*/
70-
async function getCreateInterfaceImplementation(
70+
function getCreateInterfaceImplementation(
7171
req: JsonRpcRequest<CreateInterfaceParameters>,
7272
res: PendingJsonRpcResponse<CreateInterfaceResult>,
7373
_next: unknown,
7474
end: JsonRpcEngineEndCallback,
7575
{ createInterface }: CreateInterfaceMethodHooks,
76-
): Promise<void> {
76+
): void {
7777
const { params } = req;
7878

7979
try {
8080
const validatedParams = getValidatedParams(params);
8181

8282
const { ui, context } = validatedParams;
8383

84-
res.result = await createInterface(ui, context);
84+
res.result = createInterface(ui, context);
8585
} catch (error) {
8686
return end(error);
8787
}

packages/snaps-rpc-methods/src/permitted/updateInterface.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export type UpdateInterfaceMethodHooks = {
3838
id: string,
3939
ui: ComponentOrElement,
4040
context?: InterfaceContext,
41-
) => Promise<void>;
41+
) => void;
4242
};
4343

4444
export const updateInterfaceHandler: PermittedHandlerExport<
@@ -74,21 +74,21 @@ export type UpdateInterfaceParameters = InferMatching<
7474
* @param hooks.updateInterface - The function to update the interface.
7575
* @returns Nothing.
7676
*/
77-
async function getUpdateInterfaceImplementation(
77+
function getUpdateInterfaceImplementation(
7878
req: JsonRpcRequest<UpdateInterfaceParameters>,
7979
res: PendingJsonRpcResponse<UpdateInterfaceResult>,
8080
_next: unknown,
8181
end: JsonRpcEngineEndCallback,
8282
{ updateInterface }: UpdateInterfaceMethodHooks,
83-
): Promise<void> {
83+
): void {
8484
const { params } = req;
8585

8686
try {
8787
const validatedParams = getValidatedParams(params);
8888

8989
const { id, ui, context } = validatedParams;
9090

91-
await updateInterface(id, ui, context);
91+
updateInterface(id, ui, context);
9292
res.result = null;
9393
} catch (error) {
9494
return end(error);

0 commit comments

Comments
 (0)