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

Inferring protocol message value as function without ProtocolWithReturn #20

Closed NoamLoewenstern closed 1 year ago

NoamLoewenstern commented 1 year ago

Currently, if a message wants to get response, the way to do that is to use ProtocolWithReturn<data, responseValue>. But it's possible to infer the type passing a regular function type, and inferring the data-argument and response-value from the type.

Instead of:

interface ProtocolMap {
  getStringLength: ProtocolWithReturn<string, number>;
}

To infer like this:

interface ProtocolMap {
  getStringLength: (str: string) => number;
  messageWithoutResponse: (num: number) => void;
  messageWithoutDataWithResponse: () => number;
}
aklinker1 commented 1 year ago

Oh wow that's so much cleaner and easier to understand...

Great recommendation! I'll see if I can support this without a breaking change, but I doubt that will be possible. I'll try and release v2 sometime today with this change.

NoamLoewenstern commented 1 year ago

I've added to this pull request. Seems there was an error in the pull-requests' bot deploying to vercel. Odd, since I've changed only a type check.

aklinker1 commented 1 year ago

Thanks for the PR, I've got it merged and published to v1.3.0. I also updated the docs: https://webext-core.aklinker1.io/messaging/protocol-maps.html