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

[Suggestion]: Support for "Middlewares" to inject repetitive code massively #3

Open
LuanHimmlisch opened this issue Sep 11, 2024 · 0 comments

Comments

@LuanHimmlisch
Copy link
Contributor

The idea is to be able to inject repetitive code inside all ACEs in the compilation step.

You could define an object of middlewares, where each key is a RegEx pattern to match the ACE function signature, and the value is a callback of the injected code.

The idea of the sintaxis is meant to be simple as you were writing normal code, however I suspect it will be a pain to implement, as this code will probably need to be transformed to an AST and probably recursively walked through without actually evaluating it:

@AceClass({
  middlewares: {
    ".*": (self, name, type, next) => {
      if (self._check()) return;

      next(self, name, type, next);
    }
  }
})

The following parameters can be used on the function:

  • self: The instance reference. This should be replaced to this in the compilation step.
  • next: A reference of the next middleware callback. This will need to get the values of each parameter passed through AST, this allows more flexibility to mutate for the developer.
  • name: The function signature of the ACE.
  • type: The type of the ACE to check.

An alternative to AST, and replacing values, is to have this on runtime. Injecting the callbacks inside the code. If so, the way to do it would be:

  • Convert string values from @AceClass.middlewares to AST.
  • Then validate the node is a function.
  • Then instantiate a new Function(...) with it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant