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

[base-controller] Implement utility function registerActionHandlers for batch registering all internal actions of a controller class #4582

Open
MajorLift opened this issue Aug 2, 2024 · 0 comments
Labels

Comments

@MajorLift
Copy link
Contributor

MajorLift commented Aug 2, 2024

Motivation

  • Currently, we manually enumerate each individual registerActionHandler call in controller constructors (example). We should have a DRY-er way of doing this.
  • Most handlers for controller internal actions are simply public controller class methods bound to the controller instance.

Proof of concept

  // TODO: Expand into base-controller utility function that batch registers action handlers.
  #registerActionHandlers() {
    const methodsExcludedFromMessenger = [
      'constructor',
      'messagingSystem',
      'setProvider',
      'provider',
      'ipfsGateway',
      'chainId',
    ];

    getKnownPropertyNames<keyof this>(Object.getPrototypeOf(this)).forEach(
      (method) => {
        if (
          ((key: keyof this): key is AssetsContractControllerMethodName =>
            !methodsExcludedFromMessenger.find((e) => e === key) &&
            typeof this[key] === 'function')(method)
        ) {
          this.messagingSystem.registerActionHandler(
            `${name}:${method}`,
            // TODO: Write a generic for-loop implementation that iterates over an input union type in tandem with the input array.
            // @ts-expect-error Both assigned argument and assignee parameter are using the entire union type for `method` instead of the type for the current element
            this[method].bind(this),
          );
        }
      },
    );
  }

https://github.com/MetaMask/core/pull/4397/files#diff-7ada7b099f6bcc6f3d4acd71f94b604636abd22cc556a76dbe7b3e610b1d9233R235-R262

Acceptance Criteria

  • MVP: Write a function that...
    • a) Accepts a list of public controller class fields and methods that should be excluded from being registered as action handlers.
      • constructor and messagingSystem should be included by default.
    • b) Iterates over a list of public controller methods, and binds them to the controller instance.
    • c) Registers these bound methods as action handlers for the corresponding internal messenger actions.
      • This iteration is correctly typed.
  • Stretch: Better handling for exceptions.
    • a) Accepts a map of action names and handlers which are exceptions to the general rule that each class method corresponds to an action handler.
    • b) Register these exceptions to the messagingSystem as well.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant