Skip to content

Commit

Permalink
use weakmap to maintain function reference
Browse files Browse the repository at this point in the history
  • Loading branch information
hmalik88 committed Nov 1, 2024
1 parent 47545fc commit a90208a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,13 @@ describe('SnapInterfaceController', () => {
[],
);

// defer
setTimeout(() => {
expect(controller.state).toStrictEqual({
interfaces: {
'2': {
contentType: ContentType.Dialog,
},
expect(controller.state).toStrictEqual({
interfaces: {
'2': {
contentType: ContentType.Dialog,
},
});
}, 1);
},
});

controller.destroy();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ export type SnapInterfaceControllerArgs = {
state?: SnapInterfaceControllerState;
};

const subscriptions = new WeakMap();

/**
* Use this controller to manage snaps UI interfaces using RPC method hooks.
*/
Expand Down Expand Up @@ -161,14 +163,14 @@ export class SnapInterfaceController extends BaseController<
state: { interfaces: {}, ...state },
});

this._onNotificationsListUpdated =
this._onNotificationsListUpdated.bind(this);
/* eslint-disable @typescript-eslint/unbound-method */
subscriptions.set(this, this.#_onNotificationsListUpdated.bind(this));

this.messagingSystem.subscribe(
'NotificationServicesController:notificationsListUpdated',
/* eslint-disable @typescript-eslint/unbound-method */
this._onNotificationsListUpdated,
subscriptions.get(this),
);

this.#registerMessageHandlers();
}

Expand Down Expand Up @@ -428,7 +430,7 @@ export class SnapInterfaceController extends BaseController<
);
}

_onNotificationsListUpdated(notificationsList: Record<string, any>[]) {
#_onNotificationsListUpdated(notificationsList: Record<string, any>[]) {
const snapNotificationsWithInterface = notificationsList.filter(
(notification) => {
return notification.type === 'snap' && notification.data?.detailedView;
Expand Down Expand Up @@ -470,7 +472,7 @@ export class SnapInterfaceController extends BaseController<
/* eslint-disable @typescript-eslint/unbound-method */
this.messagingSystem.unsubscribe(
'NotificationServicesController:notificationsListUpdated',
this._onNotificationsListUpdated,
subscriptions.get(this),
);
}
}

0 comments on commit a90208a

Please sign in to comment.