Skip to content

Commit

Permalink
💪 Get meta on denops cli
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdalisue committed Nov 26, 2023
1 parent 1b755c7 commit f677826
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
5 changes: 4 additions & 1 deletion denops/@denops-private/cli.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { ensure } from "https://deno.land/x/[email protected]/mod.ts";
import { parseArgs } from "https://deno.land/[email protected]/cli/mod.ts";
import { pop } from "https://deno.land/x/[email protected]/mod.ts";
import { usingResource } from "https://deno.land/x/[email protected]/mod.ts";
import { Service } from "./service.ts";
import { Vim } from "./host/vim.ts";
import { Neovim } from "./host/nvim.ts";
import { isMeta } from "./util.ts";

type Host = typeof Vim | typeof Neovim;

Expand Down Expand Up @@ -39,7 +41,8 @@ async function handleConn(conn: Deno.Conn): Promise<void> {

// Create host and service
await usingResource(new hostClass(r2, writer), async (host) => {
await usingResource(new Service(host), async (_service) => {
const meta = ensure(await host.call("denops#_internal#meta#get"), isMeta);
await usingResource(new Service(host, meta), async (_service) => {
await host.call("execute", "doautocmd <nomodeline> User DenopsReady");
await host.waitClosed();
if (!quiet) {
Expand Down
4 changes: 2 additions & 2 deletions denops/@denops-private/host/invoker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ export class Invoker {
this.#service = service;
}

register(name: string, script: string): Promise<void> {
register(name: string, script: string): void {
return this.#service.register(name, script);
}

reload(name: string): Promise<void> {
reload(name: string): void {
return this.#service.reload(name);
}

Expand Down
35 changes: 14 additions & 21 deletions denops/@denops-private/service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { toFileUrl } from "https://deno.land/[email protected]/path/mod.ts";
import {
assert,
ensure,
is,
} from "https://deno.land/x/[email protected]/mod.ts";
import { assert, is } from "https://deno.land/x/[email protected]/mod.ts";
import {
Client,
Session,
Expand All @@ -17,7 +13,6 @@ import type { Host } from "./host/base.ts";
import { Invoker } from "./host/invoker.ts";
import { errorDeserializer, errorSerializer } from "./error.ts";
import type { Meta } from "../@denops/mod.ts";
import { isMeta } from "./util.ts";

const workerScript = "./worker/script.ts";

Expand All @@ -33,26 +28,23 @@ type Plugin = {
*/
export class Service implements Disposable {
#plugins: Map<string, Plugin>;
host: Host;
meta: Promise<Meta>;
readonly host: Host;
readonly meta: Meta;

constructor(host: Host) {
constructor(host: Host, meta: Meta) {
this.#plugins = new Map();
this.host = host;
this.host.register(new Invoker(this));
this.meta = host.call("denops#_internal#meta#get").then((m) =>
ensure(m, isMeta)
);
this.meta = meta;
}

async register(
register(
name: string,
script: string,
): Promise<void> {
const meta = await this.meta;
): void {
const plugin = this.#plugins.get(name);
if (plugin) {
if (meta.mode === "debug") {
if (this.meta.mode === "debug") {
console.log(`A denops plugin '${name}' is already registered. Skip`);
}
return;
Expand All @@ -68,7 +60,7 @@ export class Service implements Disposable {
// https://github.com/vim-denops/denops.vim/issues/227
const suffix = `#${performance.now()}`;
const scriptUrl = resolveScriptUrl(script);
worker.postMessage({ scriptUrl: `${scriptUrl}${suffix}`, meta });
worker.postMessage({ scriptUrl: `${scriptUrl}${suffix}`, meta: this.meta });
const session = buildServiceSession(
name,
readableStreamFromWorker(worker),
Expand All @@ -83,16 +75,17 @@ export class Service implements Disposable {
});
}

async reload(name: string): Promise<void> {
const meta = await this.meta;
reload(name: string): void {
const plugin = this.#plugins.get(name);
if (!plugin) {
if (meta.mode === "debug") {
if (this.meta.mode === "debug") {
console.log(`A denops plugin '${name}' is not registered yet. Skip`);
}
return;
}
await disposePlugin(plugin);
disposePlugin(plugin).catch(() => {
// Do nothing
});
this.#plugins.delete(name);
this.register(name, plugin.script);
}
Expand Down

0 comments on commit f677826

Please sign in to comment.