Skip to content

Commit 5c46180

Browse files
chore: lint
1 parent f561338 commit 5c46180

6 files changed

Lines changed: 32 additions & 52 deletions

File tree

packages/snap/snap.manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/MetaMask/snap-solana-wallet.git"
88
},
99
"source": {
10-
"shasum": "TDb2+6FRoGHtfLs8uWuYAaP3A4ytstkcqvCCHxwc6gc=",
10+
"shasum": "eNQ7za5XZSa1Qieblt1oEkM9P4PfG3tItWqp5EUdbus=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

packages/snap/src/core/services/wallet/WalletService.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import {
2222
} from '@solana/kit';
2323

2424
import type { SolanaKeyringAccount } from '../../../entities';
25-
import { eventEmitter } from '../../../snapContext';
2625
import type { Caip10Address, Network } from '../../constants/solana';
2726
import { ScheduleBackgroundEventMethod } from '../../handlers/onCronjob/backgroundEvents/ScheduleBackgroundEventMethod';
2827
import type { DecompileTransactionMessageFetchingLookupTablesConfig } from '../../sdk-extensions/codecs';
@@ -153,8 +152,6 @@ export class WalletService {
153152
account: SolanaKeyringAccount,
154153
request: KeyringRequest,
155154
): Promise<SolanaSignTransactionResponse> {
156-
eventEmitter.emitSync('onStart', request);
157-
158155
assert(request.request, SolanaSignTransactionRequestStruct);
159156
assert(request.scope, NetworkStruct);
160157

@@ -378,8 +375,6 @@ export class WalletService {
378375
account: SolanaKeyringAccount,
379376
request: KeyringRequest,
380377
): Promise<SolanaSignMessageResponse> {
381-
eventEmitter.emitSync('onTestSubscription', request);
382-
383378
assert(request.request, SolanaSignMessageRequestStruct);
384379

385380
// message is base64 encoded

packages/snap/src/infrastructure/event-emitter/EventEmitter.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ describe('EventEmitter', () => {
5959
eventEmitter.on('someEvent', listener2);
6060
eventEmitter.on('someEvent', listener3);
6161

62-
eventEmitter.emitSync('someEvent');
62+
await eventEmitter.emitSync('someEvent');
6363

6464
expect(listener1).toHaveBeenCalled();
6565
expect(listener2).toHaveBeenCalled();
@@ -72,7 +72,7 @@ describe('EventEmitter', () => {
7272
eventEmitter.on('someEvent', listener1);
7373
eventEmitter.on('someOtherEvent', listener2);
7474

75-
eventEmitter.emitSync('someEvent');
75+
await eventEmitter.emitSync('someEvent');
7676

7777
expect(listener1).toHaveBeenCalled();
7878
expect(listener2).not.toHaveBeenCalled();

packages/snap/src/infrastructure/event-emitter/EventEmitter.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
/* eslint-disable no-void */
21
import type { ILogger } from '../../core/utils/logger';
32

3+
type Listener = (data?: any) => Promise<void>;
4+
45
/**
56
* The EventEmitter class is a simple event emitter that allows to register listeners / callbacks for events and emit events.
67
*
@@ -19,8 +20,7 @@ export class EventEmitter {
1920

2021
readonly #loggerPrefix = '[⚡ EventEmitter]';
2122

22-
readonly #listeners: Map<string, Set<(data?: any) => Promise<void>>> =
23-
new Map();
23+
readonly #listeners: Map<string, Set<Listener>> = new Map();
2424

2525
constructor(logger: ILogger) {
2626
this.#logger = logger;
@@ -31,7 +31,7 @@ export class EventEmitter {
3131
* @param event - The event to listen to.
3232
* @param listener - The listener to call when the event is emitted.
3333
*/
34-
on(event: string, listener: (data?: any) => Promise<void>) {
34+
on(event: string, listener: Listener) {
3535
this.#logger.info(this.#loggerPrefix, `Adding listener for event ${event}`);
3636

3737
if (!this.#listeners.has(event)) {
@@ -46,7 +46,7 @@ export class EventEmitter {
4646
* @param event - The event to remove the listener for.
4747
* @param listener - The listener to remove.
4848
*/
49-
off(event: string, listener: (data?: any) => Promise<void>) {
49+
off(event: string, listener: Listener) {
5050
this.#logger.info(
5151
this.#loggerPrefix,
5252
`Removing listener for event ${event}`,
@@ -75,9 +75,8 @@ export class EventEmitter {
7575
const listeners = this.#listeners.get(event);
7676

7777
if (listeners) {
78-
// Execute all listeners concurrently
79-
const promises = Array.from(listeners).map(async (listener) =>
80-
listener(data),
78+
const promises = Array.from(listeners).map(
79+
async (listener) => await listener(data),
8180
);
8281
await Promise.allSettled(promises);
8382
}

packages/snap/src/infrastructure/subscriptions/ConnectionManagerAdapter.test.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,11 @@ describe('ConnectionManagerAdapter', () => {
220220
);
221221

222222
// Send the connect event
223-
mockEventEmitter.emitSync('onWebSocketEvent', {
223+
await mockEventEmitter.emitSync('onWebSocketEvent', {
224224
id: mockConnectionId,
225225
type: 'open',
226226
});
227227

228-
// Wait for async operations to complete
229-
await new Promise((resolve) => setTimeout(resolve, 0));
230-
231228
expect(recoveryCallback0).toHaveBeenCalled();
232229
expect(recoveryCallback1).toHaveBeenCalled();
233230
});
@@ -248,14 +245,11 @@ describe('ConnectionManagerAdapter', () => {
248245

249246
it('attempts to reconnect', async () => {
250247
// Send the connect event
251-
mockEventEmitter.emitSync('onWebSocketEvent', {
248+
await mockEventEmitter.emitSync('onWebSocketEvent', {
252249
id: mockConnectionId,
253250
type: 'close',
254251
});
255252

256-
// Wait for async operations to complete
257-
await new Promise((resolve) => setTimeout(resolve, 0));
258-
259253
expect(mockConnectionRepository.save).toHaveBeenCalledTimes(1);
260254
});
261255
});

packages/snap/src/infrastructure/subscriptions/SubscriberAdapter.test.ts

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -92,26 +92,24 @@ const simulateDisconnection = async (
9292
eventEmitter: EventEmitter,
9393
connectionId: string,
9494
) => {
95-
eventEmitter.emitSync('onWebSocketEvent', {
95+
await eventEmitter.emitSync('onWebSocketEvent', {
9696
event: {
9797
type: 'close',
9898
id: connectionId,
9999
},
100100
});
101-
await new Promise((resolve) => setTimeout(resolve, 0)); // Emitting the event is async. We need to wait for it to complete.
102101
};
103102

104103
const simulateReconnection = async (
105104
eventEmitter: EventEmitter,
106105
connectionId: string,
107106
) => {
108-
eventEmitter.emitSync('onWebSocketEvent', {
107+
await eventEmitter.emitSync('onWebSocketEvent', {
109108
event: {
110109
type: 'open',
111110
id: connectionId,
112111
},
113112
});
114-
await new Promise((resolve) => setTimeout(resolve, 0)); // Emitting the event is async. We need to wait for it to complete.
115113
};
116114

117115
const triggerConnectionRecoveryCallbacks = async (
@@ -274,8 +272,7 @@ describe('SubscriberAdapter', () => {
274272
.mockResolvedValue(undefined);
275273
const notification = createMockNotification();
276274

277-
mockEventEmitter.emitSync('onWebSocketEvent', notification);
278-
await new Promise((resolve) => setTimeout(resolve, 0)); // Emitting the event is async. We need to wait for it to complete.
275+
await mockEventEmitter.emitSync('onWebSocketEvent', notification);
279276

280277
expect(mockLogger.warn).toHaveBeenCalledWith(
281278
loggerScope,
@@ -302,10 +299,9 @@ describe('SubscriberAdapter', () => {
302299
98765,
303300
);
304301

305-
mockEventEmitter.emitSync('onWebSocketEvent', {
302+
await mockEventEmitter.emitSync('onWebSocketEvent', {
306303
event: confirmationMessage,
307304
});
308-
await new Promise((resolve) => setTimeout(resolve, 0)); // Emitting the event is async. We need to wait for it to complete.
309305

310306
const confirmedSubscription: ConfirmedSubscription = {
311307
...request,
@@ -328,8 +324,7 @@ describe('SubscriberAdapter', () => {
328324
value: { lamports: 116044436802 },
329325
});
330326

331-
mockEventEmitter.emitSync('onWebSocketEvent', notification);
332-
await new Promise((resolve) => setTimeout(resolve, 0)); // Emitting the event is async. We need to wait for it to complete.
327+
await mockEventEmitter.emitSync('onWebSocketEvent', notification);
333328

334329
expect(callbacks.onNotification).toHaveBeenCalledWith({
335330
context: { Slot: 348893275 },
@@ -348,8 +343,7 @@ describe('SubscriberAdapter', () => {
348343
value: { lamports: 116044436802 },
349344
});
350345

351-
mockEventEmitter.emitSync('onWebSocketEvent', notification);
352-
await new Promise((resolve) => setTimeout(resolve, 0)); // Emitting the event is async. We need to wait for it to complete.
346+
await mockEventEmitter.emitSync('onWebSocketEvent', notification);
353347

354348
expect(mockLogger.error).toHaveBeenCalledWith(
355349
loggerScope,
@@ -365,8 +359,7 @@ describe('SubscriberAdapter', () => {
365359
it('logs a warning and does nothing', async () => {
366360
const message = createMockConfirmationMessage('some-subscription-id');
367361

368-
mockEventEmitter.emitSync('onWebSocketEvent', message);
369-
await new Promise((resolve) => setTimeout(resolve, 0)); // Emitting the event is async. We need to wait for it to complete.
362+
await mockEventEmitter.emitSync('onWebSocketEvent', message);
370363

371364
expect(mockLogger.warn).toHaveBeenCalledWith(
372365
loggerScope,
@@ -405,8 +398,10 @@ describe('SubscriberAdapter', () => {
405398
it('confirms the subscription', async () => {
406399
const confirmationMessage = createMockConfirmationMessage();
407400

408-
mockEventEmitter.emitSync('onWebSocketEvent', confirmationMessage);
409-
await new Promise((resolve) => setTimeout(resolve, 0)); // Emitting the event is async. We need to wait for it to complete.
401+
await mockEventEmitter.emitSync(
402+
'onWebSocketEvent',
403+
confirmationMessage,
404+
);
410405

411406
// Verify the confirmation was updated to 'confirmed'
412407
const confirmedSubscription: ConfirmedSubscription = {
@@ -435,8 +430,7 @@ describe('SubscriberAdapter', () => {
435430
},
436431
);
437432

438-
mockEventEmitter.emitSync('onWebSocketEvent', notification);
439-
await new Promise((resolve) => setTimeout(resolve, 0)); // Emitting the event is async. We need to wait for it to complete.
433+
await mockEventEmitter.emitSync('onWebSocketEvent', notification);
440434

441435
expect(callbacks.onNotification).toHaveBeenCalledWith({
442436
context: { Slot: 348893275 },
@@ -480,8 +474,7 @@ describe('SubscriberAdapter', () => {
480474
});
481475

482476
it('logs the error', async () => {
483-
mockEventEmitter.emitSync('onWebSocketEvent', message);
484-
await new Promise((resolve) => setTimeout(resolve, 0)); // Emitting the event is async. We need to wait for it to complete.
477+
await mockEventEmitter.emitSync('onWebSocketEvent', message);
485478

486479
expect(mockLogger.error).toHaveBeenCalledWith(
487480
loggerScope,
@@ -494,8 +487,7 @@ describe('SubscriberAdapter', () => {
494487
});
495488

496489
it('calls the subscription callback with the error', async () => {
497-
mockEventEmitter.emitSync('onWebSocketEvent', message);
498-
await new Promise((resolve) => setTimeout(resolve, 0)); // Emitting the event is async. We need to wait for it to complete.
490+
await mockEventEmitter.emitSync('onWebSocketEvent', message);
499491

500492
expect(callbacks.onSubscriptionFailed).toHaveBeenCalledWith({
501493
code: -32000,
@@ -506,8 +498,7 @@ describe('SubscriberAdapter', () => {
506498

507499
describe('when there is no subscription for the message', () => {
508500
it('logs an error and does nothing', async () => {
509-
mockEventEmitter.emitSync('onWebSocketEvent', message);
510-
await new Promise((resolve) => setTimeout(resolve, 0)); // Emitting the event is async. We need to wait for it to complete.
501+
await mockEventEmitter.emitSync('onWebSocketEvent', message);
511502

512503
expect(mockLogger.error).toHaveBeenCalledWith(
513504
loggerScope,
@@ -539,8 +530,7 @@ describe('SubscriberAdapter', () => {
539530
};
540531

541532
it('logs an error and does nothing', async () => {
542-
mockEventEmitter.emitSync('onWebSocketEvent', message);
543-
await new Promise((resolve) => setTimeout(resolve, 0)); // Emitting the event is async. We need to wait for it to complete.
533+
await mockEventEmitter.emitSync('onWebSocketEvent', message);
544534

545535
expect(mockLogger.error).toHaveBeenCalledWith(
546536
loggerScope,
@@ -769,8 +759,10 @@ describe('SubscriberAdapter', () => {
769759
subscriptionId,
770760
98765,
771761
);
772-
mockEventEmitter.emitSync('onWebSocketEvent', confirmationMessage);
773-
await new Promise((resolve) => setTimeout(resolve, 0)); // Emitting the event is async. We need to wait for it to complete.
762+
await mockEventEmitter.emitSync(
763+
'onWebSocketEvent',
764+
confirmationMessage,
765+
);
774766

775767
jest.spyOn(mockSubscriptionRepository, 'findBy').mockResolvedValue({
776768
...pendingSubscription,

0 commit comments

Comments
 (0)