Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: retain invocation context #135

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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])
})
})
Loading