Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override subscriptionManager.middleware.destroy to also remove sub entry #4984

Merged
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import createSubscriptionManager from '@metamask/eth-json-rpc-filters/subscriptionManager';
import type SafeEventEmitter from '@metamask/safe-event-emitter';

import { MultichainSubscriptionManager } from './MultichainSubscriptionManager';

Expand Down Expand Up @@ -51,15 +52,21 @@ const createMultichainSubscriptionManager = () => {
return { multichainSubscriptionManager };
};

describe('MultichainSubscriptionManager', () => {
const mockSubscriptionManager = {
events: {
on: jest.fn(),
},
const createMockSubscriptionManager = () => ({
events: {
on: jest.fn(),
} as unknown as jest.Mocked<SafeEventEmitter>,
destroy: jest.fn(),
middleware: {
destroy: jest.fn(),
};
},
});

describe('MultichainSubscriptionManager', () => {
let mockSubscriptionManager = createMockSubscriptionManager();

beforeEach(() => {
mockSubscriptionManager = createMockSubscriptionManager();
MockCreateSubscriptionManager.mockReturnValue(mockSubscriptionManager);
});

Expand Down Expand Up @@ -118,10 +125,6 @@ describe('MultichainSubscriptionManager', () => {
multichainSubscriptionManager.subscribe({ scope, origin, tabId });
multichainSubscriptionManager.unsubscribeByScopeAndOrigin(scope, origin);

mockSubscriptionManager.events.on.mock.calls[0][1](
newHeadsNotificationMock,
);

expect(mockSubscriptionManager.destroy).toHaveBeenCalled();
});

Expand All @@ -138,9 +141,6 @@ describe('MultichainSubscriptionManager', () => {
'other-origin',
123,
);
mockSubscriptionManager.events.on.mock.calls[0][1](
newHeadsNotificationMock,
);

expect(mockSubscriptionManager.destroy).not.toHaveBeenCalled();
});
Expand All @@ -151,9 +151,14 @@ describe('MultichainSubscriptionManager', () => {
multichainSubscriptionManager.subscribe({ scope, origin, tabId });
multichainSubscriptionManager.unsubscribeByOriginAndTabId(origin, tabId);

mockSubscriptionManager.events.on.mock.calls[0][1](
newHeadsNotificationMock,
);
expect(mockSubscriptionManager.destroy).toHaveBeenCalled();
});

it('should unsubscribe when the middleware is destroyed', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be an assertion in this test about the state of subscriptions? Or I suppose you can't access this 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like you said, it's private so you can't actually check the internal state

const { multichainSubscriptionManager } =
createMultichainSubscriptionManager();
multichainSubscriptionManager.subscribe({ scope, origin, tabId });
mockSubscriptionManager.middleware.destroy();

expect(mockSubscriptionManager.destroy).toHaveBeenCalled();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import type { NetworkController } from '@metamask/network-controller';
import SafeEventEmitter from '@metamask/safe-event-emitter';
import type { CaipChainId, Hex } from '@metamask/utils';
import { parseCaipChainId } from '@metamask/utils';
import type EventEmitter from 'events';

import type { ExternalScopeString } from '../scope/types';
import type { ExtendedJsonRpcMiddleware } from './MultichainMiddlewareManager';

export type SubscriptionManager = {
events: EventEmitter;
events: SafeEventEmitter;
destroy?: () => void;
middleware: ExtendedJsonRpcMiddleware;
};

type SubscriptionNotificationEvent = {
Expand Down Expand Up @@ -108,10 +109,17 @@ export class MultichainSubscriptionManager extends SafeEventEmitter {
},
);

this.#subscriptions.push({
const newSubscriptionManagerEntry = {
...subscriptionKey,
subscriptionManager,
});
};
subscriptionManager.destroy = subscriptionManager.middleware.destroy;
subscriptionManager.middleware.destroy = this.#unsubscribe.bind(
this,
newSubscriptionManagerEntry,
);
adonesky1 marked this conversation as resolved.
Show resolved Hide resolved

this.#subscriptions.push(newSubscriptionManagerEntry);

return subscriptionManager;
}
Expand Down
Loading