Skip to content

Commit 2016beb

Browse files
committed
Register class methods as action handlers
1 parent a2965b2 commit 2016beb

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

packages/assets-controllers/src/AssetsContractController.ts

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -192,27 +192,36 @@ export class AssetsContractController {
192192
this.ipfsGateway = IPFS_DEFAULT_GATEWAY_URL;
193193
this.chainId = initialChainId;
194194

195-
this.messagingSystem.registerActionHandler(
196-
'AssetsContractController:getERC20Standard',
197-
this.getERC20Standard.bind(this),
198-
);
199-
this.messagingSystem.registerActionHandler(
200-
'AssetsContractController:getERC721Standard',
201-
this.getERC721Standard.bind(this),
202-
);
203-
this.messagingSystem.registerActionHandler(
204-
'AssetsContractController:getERC1155Standard',
205-
this.getERC1155Standard.bind(this),
206-
);
207-
this.messagingSystem.registerActionHandler(
208-
'AssetsContractController:getTokenStandardAndDetails',
209-
this.getTokenStandardAndDetails.bind(this),
210-
);
211-
this.messagingSystem.registerActionHandler(
212-
'AssetsContractController:getBalancesInSingleCall',
213-
this.getBalancesInSingleCall.bind(this),
214-
);
195+
this.#registerActionHandlers();
196+
this.#registerEventSubscriptions();
197+
}
215198

199+
// TODO: Expand into base-controller utility function that batch registers action handlers.
200+
#registerActionHandlers() {
201+
for (const method of Object.getOwnPropertyNames(
202+
Object.getPrototypeOf(this),
203+
) as (keyof this)[]) {
204+
if (
205+
((key: keyof this): key is AssetsContractControllerMethodName =>
206+
![
207+
'constructor',
208+
'messagingSystem',
209+
'provider',
210+
'ipfsGateway',
211+
'chainId',
212+
].find((e) => e === key) && typeof this[key] === 'function')(method)
213+
) {
214+
this.messagingSystem.registerActionHandler(
215+
`${name}:${method}`,
216+
// TODO: Write a for-loop function that iterates over an input union type in tandem with the input array.
217+
// @ts-expect-error Both assigned argument and assignee parameter are using the entire union type for `method`
218+
this[method].bind(this),
219+
);
220+
}
221+
}
222+
}
223+
224+
#registerEventSubscriptions() {
216225
this.messagingSystem.subscribe(
217226
`PreferencesController:stateChange`,
218227
({ ipfsGateway }) => {
@@ -232,7 +241,6 @@ export class AssetsContractController {
232241

233242
if (this.chainId !== chainId) {
234243
this.chainId = chainId;
235-
236244
// @ts-expect-error TODO: remove this annotation once the `Eip1193Provider` class is released
237245
this.#provider = this.#getCorrectProvider();
238246
}

0 commit comments

Comments
 (0)