Skip to content

Commit

Permalink
👍 Add denops#interrupt() function
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdalisue committed May 14, 2024
1 parent 639ed41 commit 4b9d832
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 3 deletions.
8 changes: 8 additions & 0 deletions autoload/denops.vim
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ function! denops#request_async(name, method, params, success, failure) abort
\)
endfunction

function! denops#interrupt(...) abort
let l:args = a:0 ? [a:1] : []
call denops#_internal#server#chan#notify(
\ 'invoke',
\ ['interrupt', l:args],
\)
endfunction

" Configuration
call denops#_internal#conf#define('denops#disabled', 0)
call denops#_internal#conf#define('denops#deno', 'deno')
Expand Down
9 changes: 8 additions & 1 deletion denops/@denops-private/denops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ const isBatchReturn = is.TupleOf([is.Array, is.String] as const);

export type Host = Pick<HostOrigin, "redraw" | "call" | "batch">;

export type Service = Pick<ServiceOrigin, "dispatch" | "waitLoaded">;
export type Service = Pick<
ServiceOrigin,
"dispatch" | "waitLoaded" | "interrupted"
>;

export class DenopsImpl implements Denops {
readonly name: string;
Expand All @@ -37,6 +40,10 @@ export class DenopsImpl implements Denops {
this.#service = service;
}

get interrupted(): AbortSignal {
return this.#service.interrupted;
}

Check warning on line 45 in denops/@denops-private/denops.ts

View check run for this annotation

Codecov / codecov/patch

denops/@denops-private/denops.ts#L43-L45

Added lines #L43 - L45 were not covered by tests

redraw(force?: boolean): Promise<void> {
return this.#host.redraw(force);
}
Expand Down
1 change: 1 addition & 0 deletions denops/@denops-private/denops_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Deno.test("DenopsImpl", async (t) => {
const service: Service = {
dispatch: () => unimplemented(),
waitLoaded: () => unimplemented(),
interrupted: new AbortController().signal,
};
const denops = new DenopsImpl("dummy", meta, host, service);

Expand Down
6 changes: 6 additions & 0 deletions denops/@denops-private/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export type Service = {
bind(host: Host): void;
load(name: string, script: string): Promise<void>;
reload(name: string): Promise<void>;
interrupt(reason?: unknown): void;
dispatch(name: string, fn: string, args: unknown[]): Promise<unknown>;
dispatchAsync(
name: string,
Expand All @@ -74,6 +75,11 @@ export function invoke(
return service.reload(
...ensure(args, is.TupleOf([is.String] as const)),
);
case "interrupt":
service.interrupt(
...ensure(args, is.ParametersOf([is.OptionalOf(is.Unknown)] as const)),

Check warning on line 80 in denops/@denops-private/host.ts

View check run for this annotation

Codecov / codecov/patch

denops/@denops-private/host.ts#L79-L80

Added lines #L79 - L80 were not covered by tests
);
return Promise.resolve();

Check warning on line 82 in denops/@denops-private/host.ts

View check run for this annotation

Codecov / codecov/patch

denops/@denops-private/host.ts#L82

Added line #L82 was not covered by tests
case "dispatch":
return service.dispatch(
...ensure(args, is.TupleOf([is.String, is.String, is.Array] as const)),
Expand Down
1 change: 1 addition & 0 deletions denops/@denops-private/host/nvim_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Deno.test("Neovim", async (t) => {
bind: () => unimplemented(),
load: () => unimplemented(),
reload: () => unimplemented(),
interrupt: () => unimplemented(),
dispatch: () => unimplemented(),
dispatchAsync: () => unimplemented(),
};
Expand Down
1 change: 1 addition & 0 deletions denops/@denops-private/host/vim_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Deno.test("Vim", async (t) => {
bind: () => unimplemented(),
load: () => unimplemented(),
reload: () => unimplemented(),
interrupt: () => unimplemented(),
dispatch: () => unimplemented(),
dispatchAsync: () => unimplemented(),
};
Expand Down
1 change: 1 addition & 0 deletions denops/@denops-private/host_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Deno.test("invoke", async (t) => {
const service: Omit<Service, "bind"> = {
load: () => unimplemented(),
reload: () => unimplemented(),
interrupt: () => unimplemented(),
dispatch: () => unimplemented(),
dispatchAsync: () => unimplemented(),
};
Expand Down
14 changes: 12 additions & 2 deletions denops/@denops-private/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ type Waiter = {
* Service manage plugins and is visible from the host (Vim/Neovim) through `invoke()` function.
*/
export class Service implements Disposable {
#plugins: Map<string, Plugin> = new Map();
#waiters: Map<string, Waiter> = new Map();
#interruptController = new AbortController();
#plugins = new Map<string, Plugin>();
#waiters = new Map<string, Waiter>();
#meta: Meta;
#host?: Host;

Expand All @@ -32,6 +33,10 @@ export class Service implements Disposable {
return this.#waiters.get(name)!;
}

get interrupted(): AbortSignal {
return this.#interruptController.signal;
}

Check warning on line 38 in denops/@denops-private/service.ts

View check run for this annotation

Codecov / codecov/patch

denops/@denops-private/service.ts#L36-L38

Added lines #L36 - L38 were not covered by tests

bind(host: Host): void {
this.#host = host;
}
Expand Down Expand Up @@ -80,6 +85,11 @@ export class Service implements Disposable {
return this.#getWaiter(name).promise;
}

interrupt(reason?: unknown): void {
this.#interruptController.abort(reason);
this.#interruptController = new AbortController();
}

Check warning on line 91 in denops/@denops-private/service.ts

View check run for this annotation

Codecov / codecov/patch

denops/@denops-private/service.ts#L88-L91

Added lines #L88 - L91 were not covered by tests

async #dispatch(name: string, fn: string, args: unknown[]): Promise<unknown> {
const plugin = this.#plugins.get(name);
if (!plugin) {
Expand Down

0 comments on commit 4b9d832

Please sign in to comment.