!176 add lib/plugins/index.js.

* add lib/plugins/index.js.
This commit is contained in:
柠檬冲水 2024-06-06 18:55:19 +00:00 committed by Kokomi
parent dc29946618
commit 6a5200e734
1 changed files with 85 additions and 0 deletions

85
lib/plugins/index.js Normal file
View File

@ -0,0 +1,85 @@
import plugin from "./plugin.js";
/**
* ********************
* - 2024-6-4 @柠檬冲水
* https://gitee.com/ningmengchongshui
* ********************
* 函数式编程语法糖 - 使用示例
* ********************
* - apps.js
*
* import { Messages } from "../../lib/plugins/index.js";
* const message = new Messages()
* message.response(/^(#|\/)?你好/, async (e) => {
* e.reply('你好')
* return false
* })
* export default message
* ********************
* - index.js
*
* // 导入
* import hello from './apps.js'
* const event = new Events()
* event.use(hello.ok)
*
* // 导出
* const apps = event.ok
* export { apps }
* ********************
*/
export const define = {
name: Date.now,
dsc: 'plugin',
event: 'message',
priority: 999
}
//
export class Messages {
count = 0
response(reg, fnc) {
this.count++
const propName = `prop_${this.count}`
this[propName] = fnc
this.rule.push({
reg,
fnc: propName
})
}
get ok() {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const App = this
class Children extends plugin {
constructor() {
super({
...define,
rule: App.rule
})
for (const key of App.rule) {
if (App[key.fnc] instanceof Function) {
this[key.fnc] = App[key.fnc].bind(App)
}
}
}
}
return Children
}
}
//
export class Events {
count = 0
use(val) {
this.count++
this.data[this.count] = val
}
get ok() {
return this.data
}
}
//
export { plugin };