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`: Support functions and nested objects #15

Closed aklinker1 closed 1 year ago

aklinker1 commented 1 year ago

I'd like to support executing functions directly, or executing functions in deeply nested in objects.

async function doWork() {
  ...
}

function createSomeService() {
 return { doWork };
}

function createBackgroundAPI() {
  return {
    someService: createSomeService(),
  },
}
// ❌ This doesn't work today
const [registerDoWork, getDoWork] = defineProxyService("doWork", () => doWork);

// ✅ This works today
const [registerSomeService, getSomeService] = defineProxyService("SomeService", () => createSomeService());

// ❌ This doesn't work today
const [registerAPI, getAPI] = defineProxyService("API", () => createBackgroundAPI());

They could be used like so:

const doWork = getDoWork();
await doWork();

const someService = getSomeService();
await someService.doWork();

const api = getAPI();
api.someService.doWork();