diff --git a/packages/neovim/src/api/Base.ts b/packages/neovim/src/api/Base.ts index 4f29c631..b88eaf29 100644 --- a/packages/neovim/src/api/Base.ts +++ b/packages/neovim/src/api/Base.ts @@ -118,6 +118,7 @@ export class BaseApi extends EventEmitter { }); } + /** Sends a request to Nvim (the peer). */ request(name: string, args: any[] = []): Promise { return this.asyncRequest(name, args); } diff --git a/packages/neovim/src/api/client.ts b/packages/neovim/src/api/client.ts index 672dff9e..15a9acb5 100644 --- a/packages/neovim/src/api/client.ts +++ b/packages/neovim/src/api/client.ts @@ -63,6 +63,7 @@ export class NeovimClient extends Neovim { return this.attachedBuffers.has(key); } + /** Handles incoming request (from the peer). */ handleRequest( method: string, args: VimValue[], @@ -85,6 +86,7 @@ export class NeovimClient extends Neovim { } } + /** Publishes to (local) subscribers of this `EventEmitter`. */ emitNotification(method: string, args: any[]) { if (method.endsWith('_event')) { if (!method.startsWith('nvim_buf_')) { @@ -114,6 +116,7 @@ export class NeovimClient extends Neovim { } } + /** Handles incoming notification (from the peer). */ handleNotification(method: string, args: VimValue[], ...restArgs: any[]) { this.logger.info('handleNotification: %s', method); // If neovim API is not generated yet then queue up requests diff --git a/packages/neovim/src/attach/attach.test.ts b/packages/neovim/src/attach/attach.test.ts index 81a721fe..deb10786 100644 --- a/packages/neovim/src/attach/attach.test.ts +++ b/packages/neovim/src/attach/attach.test.ts @@ -11,30 +11,33 @@ describe('Nvim API', () => { let proc: ReturnType[0]; let nvim: ReturnType[1]; + /** Incoming requests (from Nvim). */ + const requests: { method: string; args: number[] }[] = []; + /** Incoming notifications (from Nvim). */ + const notifications: { method: string; args: number[] }[] = []; + before(async () => { [proc, nvim] = testUtil.startNvim(); - }); - - after(() => { - testUtil.stopNvim(); - }); - let requests: { method: string; args: number[] }[]; - let notifications: { method: string; args: number[] }[]; - - before(async () => { + // Incoming requests (from Nvim). nvim.on('request', (method, args, resp) => { requests.push({ method, args }); resp.send(`received ${method}(${args})`); }); + + // Incoming notifications (from Nvim). nvim.on('notification', (method, args) => { notifications.push({ method, args }); }); }); + after(() => { + testUtil.stopNvim(); + }); + beforeEach(() => { - requests = []; - notifications = []; + requests.length = 0; + notifications.length = 0; }); it('failure modes', async () => { @@ -101,6 +104,30 @@ describe('Nvim API', () => { testUtil.stopNvim(nvim2); }); + it('noisy RPC traffic', async () => { + let requestCount = 0; + const oldRequest = nvim.request; + nvim.request = function ( + this: any, + name: string, + args: any[] = [] + ): Promise { + requestCount = requestCount + 1; + return oldRequest.call(this, name, args); + }; + + for (let i = 0; i < 99; i = i + 1) { + nvim.command('noswapfile edit test-node-client.lua'); + nvim.command('bwipeout!'); + } + + expect(requestCount).toEqual(99 * 2); + // Still alive? + expect(await nvim.eval('1+1')).toEqual(2); + + nvim.request = oldRequest; + }); + it('can send requests and receive response', async () => { const result = await nvim.eval('{"k1": "v1", "k2": 2}'); expect(result).toEqual({ k1: 'v1', k2: 2 }); @@ -149,7 +176,7 @@ describe('Nvim API', () => { end: -1, strictIndexing: true, }); - expect(lines).toEqual([]); + expect(lines).toEqual(['']); buf.setLines(['line1', 'line2'], { start: 0, end: 1 }); const newLines = await buf.getLines({