Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: allow passing kit functions by reference
What ---- Define methods using `methodName = async () => {…}` syntax, rather than `async methodName() {…}` Why --- This allows for better APIs when using StellarWalletsKit. For example, given a `stellar-wallets-kit.ts` file in my own project with: ```ts const kit: StellarWalletsKit = new StellarWalletsKit({…}); export const signTransaction = kit.signTransaction; ``` And I then use this in my app: ```ts const tx = await incrementor.increment(); const { result } = await tx.signAndSend({ signTransaction }) ``` Today, the `signAndSend` will throw a runtime error: ``` TypeError: Cannot read properties of undefined (reading 'selectedModule') ``` This is because JavaScript's default behavior is coo coo bananas, no one understands it, and `this` ends up getting bound to `undefined` if you use the `async methodName() {…}` syntax and then pass `methodName` as a reference the way I did. Today, in order to make my code work, I would need to export the whole `kit` and then change my `signAndSend` line to: ```ts const { result } = await tx.signAndSend({ signTransaction: async (xdr) => { return await kit.signTransaction(xdr); }, }); ``` I don't like this because A) it's ugly and B) I don't think it's good practice to export the whole `kit`. Within my app, I want to have the ability to wrap interfaces like `signTransaction`, so that I can always make sure app-specific logic gets taken care of. Exporting all of `kit` adds more room for error. The Fix ------- Using the arrow syntax with `methodName = async (…) => {…}` makes JS use similar `this`-binding logic to every other language, and makes my pass-by-reference use-case possible.
- Loading branch information