tomassasovsky / WeAppe.ar

Open-Source time logger backend made in Dart
MIT License
5 stars 0 forks source link

feat: change mirrors/reflectable with a `newInstance` getter #26

Closed tomassasovsky closed 2 years ago

tomassasovsky commented 2 years ago

So I had the idea to replace the use of the reflectable package fully.

The concept here would be adding a getter to the RouteHandler class that can be called to create a new instance of the class itself.

Would look like this:

abstract class RouteHandler<T extends RouteHandler<T>> {
  FutureOr<dynamic> defineVars(HttpRequest req, HttpResponse res) async {}

  // this is the method that is called when the route is called
  FutureOr<dynamic> call(HttpRequest req, HttpResponse res) async {
    final instance = newInstance;
    // this handles the request
    await instance.defineVars(req, res);
    req.validate();
    await instance.run(req, res);
  }

  FutureOr<dynamic> run(HttpRequest req, HttpResponse res);

  // to create a new instance internally
  T get newInstance;
}

and an implementation of this:

class SomeMiddleware extends Middleware<SomeMiddleware> {
  @override
  FutureOr run(HttpRequest req, HttpResponse res) {}

  @override
  SomeMiddleware get newInstance => SomeMiddleware();
}