Skip to content

Commit

Permalink
fixup init
Browse files Browse the repository at this point in the history
  • Loading branch information
dlants committed Dec 8, 2024
1 parent 27940f0 commit 3864159
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions rplugin/node/magenta/src/magenta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,11 @@ class Magenta {
private anthropicClient: AnthropicClient;
private sidebar: Sidebar;

constructor(private context: Context, plugin: NvimPlugin, private chat: Chat) {
constructor(private context: Context, private chat: Chat) {
this.context.logger.debug(`Initializing plugin`)
this.anthropicClient = new AnthropicClient(this.context.logger);
this.sidebar = new Sidebar(this.context.nvim, this.context.logger);

plugin.registerCommand('Magenta', (args: string[]) => this.command(args).catch((err: Error) => {
this.context.logger.error(err)
}), {
nargs: '1'
})
}

async command(args: string[]): Promise<void> {
Expand Down Expand Up @@ -59,25 +54,41 @@ class Magenta {

static async init(plugin: NvimPlugin, logger: Logger) {
const chat = await Chat.init({ nvim: plugin.nvim, logger })
return new Magenta({ nvim: plugin.nvim, logger }, plugin, chat)
return new Magenta({ nvim: plugin.nvim, logger }, chat)
}
}

let singletonPromise: Promise<Magenta> | undefined = undefined;
let init: { magenta: Promise<Magenta>, logger: Logger } | undefined = undefined;

module.exports = (plugin: NvimPlugin) => {
plugin.setOptions({
// dev: true,
// alwaysInit: true
})

if (!singletonPromise) {
if (!init) {
const logger = new Logger(plugin.nvim, { level: 'trace' });
process.on('uncaughtException', (error) => {
logger.error(error);
process.exit(1);
});

singletonPromise = Magenta.init(plugin, logger)
init = {
magenta: Magenta.init(plugin, logger),
logger
}
}

plugin.registerCommand('Magenta', async (args: string[]) => {
try {
const magenta = await init!.magenta
await magenta.command(args)
} catch (err) {
init!.logger.error(err as Error)
}
}, {

nargs: '1'
})

}

0 comments on commit 3864159

Please sign in to comment.