aklinker1 / webext-core

Collection of essential libraries and tools for building web extensions
https://webext-core.aklinker1.io
MIT License
96 stars 11 forks source link

`proxy-service`: Add class shorthand #14

Closed aklinker1 closed 1 year ago

aklinker1 commented 1 year ago

For a class, you should just be able to pass it into defineProxyService without a name or constructor.

import { defineProxyService } from '@webext-core/proxy-service';

class DatabaseService {
  construtor(db) { ... }
}

const [registerDatabaseService, getDatabaseService] = defineProxyService(DatabaseService);

registerDatabaseService(db);
const service = getDatabaseService();

In this case, it should use the class name for the message, and the constructor when registering it.

tlarkin011 commented 1 year ago

I would love to help out!

aklinker1 commented 1 year ago

@tlarkin011 that'd be great, thanks! I'm not a big fan of classes in JS, so this is super low priority for me 😂 That's why I added the "good first issue" and "help wanted" labels lol.

I just created docs to help contributors get started. Let me know if you run into any issues or it doesn't make sense.

https://webext-core.aklinker1.io/guide/contributing.html

Otherwise I'll let you handle it! Thanks again 😄

tlarkin011 commented 1 year ago

@aklinker1 Thank you! The docs are great, I will reach out if I have any questions!

tlarkin011 commented 1 year ago

@aklinker1 Hello, I may need some help with the specific class updates. Thank you!

aklinker1 commented 1 year ago

@tlarkin011 Are you familiar with function overloading in TypeScript?

All we need to do is add a new override for defineProxyService that accepts a single argument, and class.

We'll need to replace this:

https://github.com/aklinker1/webext-core/blob/ade768187966a7aaf6eaf9adb7942bc2274006e6/packages/proxy-service/src/defineProxyService.ts#L6-L11

With something like this:

+export function defineProxyService<TService extends Service, TArgs extends any[]>(
+  classDefinition: { new (...arg: TArgs): TService },
+  config?: ProxyServiceConfig,
+): [registerService: RegisterService<TService, TArgs>, getService: GetService<TService>];
+
export function defineProxyService<TService extends Service, TArgs extends any[]>(
  name: string,
  init: (...args: TArgs) => TService,
  config?: ProxyServiceConfig,
-): [registerService: RegisterService<TService, TArgs>, getService: GetService<TService>] {
+): [registerService: RegisterService<TService, TArgs>, getService: GetService<TService>];
+
+export function defineProxyService<TService extends Service, TArgs extends any[]>(
+  arg1: any,
+  arg2: any,
+  arg3?: any,
+): [registerService: RegisterService<TService, TArgs>, getService: GetService<TService>] {
+  // Based on the overrides, create variables for all the old args
+  const isClassOverride = typeof arg1 !== "string";
+  let name: string = isClassOverride ? arg1.name : arg1;
+  let init: (...args: TArgs) => TService  = isClassOverride ? (...args: TArgs) => new arg1(...args) : arg2;
+  let config: ProxyServiceConfig | undefined = isClassOverride ? arg2 : arg3;
+
  let service: TService | undefined;
  ...
tlarkin011 commented 1 year ago

@aklinker1 I am familiar with them now. great learning experience! thank you for your help!

aklinker1 commented 1 year ago

No problem, let me know if you have any other questions!

aklinker1 commented 1 year ago

I started on this today, and after some testing, have decided to NOT support a class shorthand.

When an extension is minified, it's name is not unique, and can cause problems.