Skip to content

Commit

Permalink
fix: retain invocation context (#135)
Browse files Browse the repository at this point in the history
Ensure `this` has the expected value when invoking a method/generator.
  • Loading branch information
achingbrain authored Jun 19, 2024
1 parent e5d4a02 commit 595aa93
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
8 changes: 4 additions & 4 deletions packages/it-rpc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ class DuplexRPC implements Duplex<AsyncGenerator<Uint8Array, void, unknown>> {
const target = this.lookupInvocationTarget(message.path)

// invoke the method
const val = await target.apply(context, message.args.map(arg => this.values.fromValue(arg, this.output, invocation)))
const val = await target.fn.apply(target.context, message.args.map(arg => this.values.fromValue(arg, this.output, invocation)))

this.output.push(RPCMessage.encode({
type: MessageType.methodResolved,
Expand Down Expand Up @@ -550,7 +550,7 @@ class DuplexRPC implements Duplex<AsyncGenerator<Uint8Array, void, unknown>> {
const target = this.lookupInvocationTarget(message.path)

// invoke the method
const gen = target.apply(context, message.args.map(arg => this.values.fromValue(arg, this.output, invocation)))
const gen = target.fn.apply(target.context, message.args.map(arg => this.values.fromValue(arg, this.output, invocation)))

if (typeof gen.next !== 'function') {
throw new InvalidReturnTypeError(`${message.path} did not return an async generator`)
Expand Down Expand Up @@ -601,7 +601,7 @@ class DuplexRPC implements Duplex<AsyncGenerator<Uint8Array, void, unknown>> {
}))
}

private lookupInvocationTarget (path: string): (...args: any[]) => any {
private lookupInvocationTarget (path: string): { context: any, fn(...args: any[]): any } {
// look up the requested method on the target
const pathParts = path.split('.')
let target = this.targets.get(pathParts[0])
Expand All @@ -627,7 +627,7 @@ class DuplexRPC implements Duplex<AsyncGenerator<Uint8Array, void, unknown>> {
throw new InvalidInvocationTypeError('Invocation target was not a function')
}

return target
return { context, fn: target }
}

private async handleMethodResolved (message: MethodResolvedMessage, invocation: Invocation): Promise<void> {
Expand Down
15 changes: 15 additions & 0 deletions packages/it-rpc/test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from 'aegir/chai'
import all from 'it-all'
import { rpc } from '../src/index.js'
import type { RPC } from '../src/index.js'

Expand All @@ -14,6 +15,12 @@ const target = {
},
async supportSimpleArguments (...args: any[]): Promise<any[]> {
return args
},
async contextAccess (): Promise<boolean> {
return this.prop
},
async * generatorContextAccess (): AsyncGenerator<boolean> {
yield this.prop
}
}

Expand Down Expand Up @@ -78,4 +85,12 @@ describe('basics', () => {

await expect(sender.supportSimpleArguments(...input)).to.eventually.deep.equal(input)
})

it('should retain object context when invoking a method', async () => {
await expect(sender.contextAccess()).to.eventually.be.true()
})

it('should retain object context when invoking a generator', async () => {
await expect(all(sender.generatorContextAccess())).to.eventually.deep.equal([true])
})
})

0 comments on commit 595aa93

Please sign in to comment.