Commit ef73b952 authored by nanahira's avatar nanahira

rework with factory

parent e92712c3
...@@ -42,20 +42,5 @@ export class SchedulePluginConfig { ...@@ -42,20 +42,5 @@ export class SchedulePluginConfig {
description: '图片发送目标。', description: '图片发送目标。',
}) })
targets: SendTarget[]; targets: SendTarget[];
initializeTasks(ctx: Context, callback: () => void) {
if (this.immediate) {
callback();
}
if (this.cron) {
const job = scheduleJob(this.cron, callback);
ctx.on('dispose', () => {
job.cancel()
});
}
if (this.interval) {
ctx.setInterval(callback, this.interval);
}
}
} }
``` ```
import { Context } from 'koishi'; import { Context } from 'koishi';
import { DefinePlugin, SchemaProperty } from 'koishi-thirdeye'; import {
DefinePlugin,
MultiInstancePlugin,
SchemaProperty,
} from 'koishi-thirdeye';
import { SchedulePlugin } from '../src'; import { SchedulePlugin } from '../src';
class Config { class Config {
...@@ -14,17 +18,24 @@ class TestSendPlugin extends SchedulePlugin(Config) { ...@@ -14,17 +18,24 @@ class TestSendPlugin extends SchedulePlugin(Config) {
} }
} }
@DefinePlugin()
class MultiTestSendPlugin extends MultiInstancePlugin(TestSendPlugin) {}
export default class ExtrasInDev { export default class ExtrasInDev {
constructor(ctx: Context) { constructor(ctx: Context) {
ctx.plugin(TestSendPlugin, { ctx.plugin(MultiTestSendPlugin, {
message: 'dress', instances: [
interval: 5000,
immediate: true,
targets: [
{ {
bot: 'sandbox:koishi', message: 'dress',
users: ['Alice', 'Bob'], interval: 5000,
channels: [{ channelId: '#' }], immediate: true,
targets: [
{
bot: 'sandbox:koishi',
users: ['Alice', 'Bob'],
channels: [{ channelId: '#' }],
},
],
}, },
], ],
}); });
......
{ {
"name": "koishi-plugin-SchedulePlugin", "name": "koishi-schedule-send",
"version": "1.0.2", "version": "1.0.2",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "koishi-plugin-SchedulePlugin", "name": "koishi-schedule-send",
"version": "1.0.2", "version": "1.0.2",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"koishi-target-def": "^1.0.5", "koishi-target-def": "^1.0.5",
"koishi-thirdeye": "^10.0.12", "koishi-thirdeye": "^10.0.20",
"node-schedule": "^2.1.0" "node-schedule": "^2.1.0"
}, },
"devDependencies": { "devDependencies": {
...@@ -5338,9 +5338,9 @@ ...@@ -5338,9 +5338,9 @@
} }
}, },
"node_modules/koishi-thirdeye": { "node_modules/koishi-thirdeye": {
"version": "10.0.12", "version": "10.0.20",
"resolved": "https://registry.npmjs.org/koishi-thirdeye/-/koishi-thirdeye-10.0.12.tgz", "resolved": "https://registry.npmjs.org/koishi-thirdeye/-/koishi-thirdeye-10.0.20.tgz",
"integrity": "sha512-ErvIbWU+tsLIroKAs6ErldrfCQSqyVhewZXVV3j1cOrZCWlsp+HElYXWqFiFqSs7xm5IcfoNa4cN6RFCOHk8PA==", "integrity": "sha512-aYBiQ6R4A8VcZ7xw0qfECIJvWsuEoW3MWdBH+SuWi0dcrwhvfF3naEtkC0ZGEnWOkauyW1YyOzTK/Fieikmv4w==",
"dependencies": { "dependencies": {
"@types/koa": "^2.13.4", "@types/koa": "^2.13.4",
"@types/koa__router": "^8.0.11", "@types/koa__router": "^8.0.11",
...@@ -5348,7 +5348,7 @@ ...@@ -5348,7 +5348,7 @@
"koishi-decorators": "^2.0.1", "koishi-decorators": "^2.0.1",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
"schemastery-gen": "^3.1.7", "schemastery-gen": "^3.1.10",
"typed-reflector": "^1.0.10" "typed-reflector": "^1.0.10"
}, },
"peerDependencies": { "peerDependencies": {
...@@ -6512,9 +6512,9 @@ ...@@ -6512,9 +6512,9 @@
} }
}, },
"node_modules/schemastery-gen": { "node_modules/schemastery-gen": {
"version": "3.1.7", "version": "3.1.10",
"resolved": "https://registry.npmjs.org/schemastery-gen/-/schemastery-gen-3.1.7.tgz", "resolved": "https://registry.npmjs.org/schemastery-gen/-/schemastery-gen-3.1.10.tgz",
"integrity": "sha512-0M/9QLRQ2yuDxMVkwkcW0g3Lf+2De+dPlll8CEgzKJ4ow1zDd/H/k+gDPLawpAccAooHoGgV2++8RGWecXf7MQ==", "integrity": "sha512-hwCLOICGEodCcQt+eLoEEvNhm7iSEwJB37JQ+27cHbggTry/Rq07ivXVsPw63Et74EobEaOGDVOTzJ4kIQa01w==",
"dependencies": { "dependencies": {
"lodash": "^4.17.21", "lodash": "^4.17.21",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
...@@ -11951,9 +11951,9 @@ ...@@ -11951,9 +11951,9 @@
} }
}, },
"koishi-thirdeye": { "koishi-thirdeye": {
"version": "10.0.12", "version": "10.0.20",
"resolved": "https://registry.npmjs.org/koishi-thirdeye/-/koishi-thirdeye-10.0.12.tgz", "resolved": "https://registry.npmjs.org/koishi-thirdeye/-/koishi-thirdeye-10.0.20.tgz",
"integrity": "sha512-ErvIbWU+tsLIroKAs6ErldrfCQSqyVhewZXVV3j1cOrZCWlsp+HElYXWqFiFqSs7xm5IcfoNa4cN6RFCOHk8PA==", "integrity": "sha512-aYBiQ6R4A8VcZ7xw0qfECIJvWsuEoW3MWdBH+SuWi0dcrwhvfF3naEtkC0ZGEnWOkauyW1YyOzTK/Fieikmv4w==",
"requires": { "requires": {
"@types/koa": "^2.13.4", "@types/koa": "^2.13.4",
"@types/koa__router": "^8.0.11", "@types/koa__router": "^8.0.11",
...@@ -11961,7 +11961,7 @@ ...@@ -11961,7 +11961,7 @@
"koishi-decorators": "^2.0.1", "koishi-decorators": "^2.0.1",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
"schemastery-gen": "^3.1.7", "schemastery-gen": "^3.1.10",
"typed-reflector": "^1.0.10" "typed-reflector": "^1.0.10"
} }
}, },
...@@ -12811,9 +12811,9 @@ ...@@ -12811,9 +12811,9 @@
} }
}, },
"schemastery-gen": { "schemastery-gen": {
"version": "3.1.7", "version": "3.1.10",
"resolved": "https://registry.npmjs.org/schemastery-gen/-/schemastery-gen-3.1.7.tgz", "resolved": "https://registry.npmjs.org/schemastery-gen/-/schemastery-gen-3.1.10.tgz",
"integrity": "sha512-0M/9QLRQ2yuDxMVkwkcW0g3Lf+2De+dPlll8CEgzKJ4ow1zDd/H/k+gDPLawpAccAooHoGgV2++8RGWecXf7MQ==", "integrity": "sha512-hwCLOICGEodCcQt+eLoEEvNhm7iSEwJB37JQ+27cHbggTry/Rq07ivXVsPw63Et74EobEaOGDVOTzJ4kIQa01w==",
"requires": { "requires": {
"lodash": "^4.17.21", "lodash": "^4.17.21",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
......
...@@ -45,7 +45,7 @@ ...@@ -45,7 +45,7 @@
}, },
"dependencies": { "dependencies": {
"koishi-target-def": "^1.0.5", "koishi-target-def": "^1.0.5",
"koishi-thirdeye": "^10.0.12", "koishi-thirdeye": "^10.0.20",
"node-schedule": "^2.1.0" "node-schedule": "^2.1.0"
}, },
"peerDependencies": { "peerDependencies": {
......
...@@ -2,20 +2,16 @@ ...@@ -2,20 +2,16 @@
import { SchedulePluginConfig } from './config'; import { SchedulePluginConfig } from './config';
import { import {
BasePlugin, BasePlugin,
ClassType, CreatePluginFactory,
Inject, Inject,
InjectLogger, InjectLogger,
Mixin,
PluginSchema,
} from 'koishi-thirdeye'; } from 'koishi-thirdeye';
import { Logger } from 'koishi'; import { Logger } from 'koishi';
import { Adapter } from 'koishi'; import { Adapter } from 'koishi';
import { Bot } from 'koishi'; import { Bot } from 'koishi';
export * from './config'; export * from './config';
export class BaseSchedulePlugin<C> extends BasePlugin< export class BaseSchedulePlugin extends BasePlugin<SchedulePluginConfig> {
SchedulePluginConfig & C
> {
@InjectLogger() @InjectLogger()
logger: Logger; logger: Logger;
...@@ -73,8 +69,7 @@ export class BaseSchedulePlugin<C> extends BasePlugin< ...@@ -73,8 +69,7 @@ export class BaseSchedulePlugin<C> extends BasePlugin<
} }
} }
export function SchedulePlugin<C>(config?: ClassType<C>) { export const SchedulePlugin = CreatePluginFactory(
const plugin = class SpecificSchedulePlugin extends BaseSchedulePlugin<C> {}; BaseSchedulePlugin,
PluginSchema(Mixin(config, SchedulePluginConfig))(plugin); SchedulePluginConfig,
return plugin; );
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment