Git Product home page Git Product logo

Comments (7)

shigma avatar shigma commented on May 17, 2024

@arily 试试 ctx.removeMiddleware

from koishi.

arily avatar arily commented on May 17, 2024

@shigma 抱歉!!好像我写的有些问题,上面想要被卸载掉的其实是插件

try {
  const pluginToRemove = require(target)
  ctx.remove(pluginToRemove)
}

大概是这样.
通过ctx.removeMiddleware的话应该需要拿到具体的中间件?

from koishi.

shigma avatar shigma commented on May 17, 2024

插件在 Koishi 的设计中,本身就不应该在运行时加载,自然也就不存在运行时卸载的概念。

如果你想要手动实现一个运行时卸载的功能,不妨考虑使用监听器:

// 定义一个插件
module.exports = (ctx, options) => {
  ctx.middleware(callback1)
  ctx.receiver.on(event, callback2)

  ctx.on('remove-my-plugin', () => {
    ctx.removeMiddleware(callback1)
    ctx.receiver.off(event, callback2)
  })
}
// 在你想要移除插件的地方
ctx.emit('remove-my-plugin')

from koishi.

undefined-moe avatar undefined-moe commented on May 17, 2024

是否支持停用指令 ctx.removeCommand()(?

from koishi.

shigma avatar shigma commented on May 17, 2024

@masnn 目前没有这个接口,下个大版本会加上。在此之前可以使用暴力方式移除:

Context.prototype.removeCommand = function (cmd) {
    cmd.shortcuts.forEach(name => delete ctx.app._shortcutMap[name])
    ctx.app._shortcuts = difference(ctx.app._shortcuts, cmd.shortcuts)
    cmd._aliases.forEach(name => delete ctx.app._commandMap[name])
    ctx.app._commands = difference(ctx.app._commands, [cmd])
    if (cmd.parent) cmd.parent.children = difference(cmd.parent.children, [cmd])
}

得写这么多我很惭愧……

from koishi.

shigma avatar shigma commented on May 17, 2024

在 next 分支的最新一次更新 bd46fe8 中已经实装了下列方法:

command.dispose() // 移除指令及其子指令
context.dispose() // 移除当前插件中注册的监听器、中间件和指令

from koishi.

shigma avatar shigma commented on May 17, 2024

已经实装于 koishi v2 beta 版本。

from koishi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.