feat: midway注解方式编写插件

This commit is contained in:
xiaojunnuo
2022-12-29 23:52:51 +08:00
parent 717d203622
commit e4ec4e1404
30 changed files with 506 additions and 446 deletions
+1 -3
View File
@@ -1,7 +1,5 @@
import { Registrable } from "../registry";
import { accessRegistry } from "./registry";
import { FormItemProps } from "../d.ts";
import { AbstractAccess } from "./abstract-access";
export type AccessInputDefine = FormItemProps & {
title: string;
@@ -13,5 +11,5 @@ export type AccessDefine = Registrable & {
};
};
export interface IAccessService {
getById(id: any): Promise<AbstractAccess>;
getById(id: any): Promise<any>;
}
@@ -9,7 +9,7 @@ import {
saveModule,
} from "@midwayjs/decorator";
import { AccessDefine, AccessInputDefine } from "./api";
import _ from "lodash-es";
import _ from "lodash";
import { accessRegistry } from "./registry";
// 提供一个唯一 key
+7 -4
View File
@@ -1,14 +1,15 @@
import { ConcurrencyStrategy, Pipeline, ResultType, Runnable, RunStrategy, Stage, Step, Task } from "../d.ts";
import _ from "lodash";
import { RunHistory } from "./run-history";
import { pluginRegistry, ITaskPlugin, PluginDefine } from "../plugin";
import { PluginDefine, pluginRegistry } from "../plugin";
import { ContextFactory, IContext } from "./context";
import { IStorage } from "./storage";
import { logger } from "../utils/util.log";
import { Logger } from "log4js";
import { request } from "../utils/util.request";
import { IAccessService } from "../access";
import { Registrable, RegistryItem } from "../registry";
import { RegistryItem } from "../registry";
export class Executor {
userId: any;
pipeline: Pipeline;
@@ -136,7 +137,7 @@ export class Executor {
private async runStep(step: Step) {
//执行任务
const plugin: RegistryItem = pluginRegistry.get(step.type);
const context = {
const context: any = {
logger: this.runtime.loggers[step.id],
accessService: this.accessService,
pipelineContext: this.pipelineContext,
@@ -159,7 +160,9 @@ export class Executor {
}
});
_.forEach(define.autowire, (item, key: string) => {
instance[key] = context[key];
});
const res = await instance.execute();
@@ -1,18 +1,5 @@
// src/decorator/memoryCache.decorator.ts
import {
attachClassMetadata,
attachPropertyDataToClass,
createCustomPropertyDecorator,
getClassMetadata,
listModule,
listPropertyDataFromClass,
Provide,
saveClassMetadata,
saveModule,
Scope,
ScopeEnum,
} from "@midwayjs/decorator";
import _ from "lodash-es";
import { getClassMetadata, listModule, Provide, saveClassMetadata, saveModule, Scope, ScopeEnum } from "@midwayjs/decorator";
import { dnsProviderRegistry } from "./registry";
import { DnsProviderDefine } from "./api";
-1
View File
@@ -5,6 +5,5 @@ export * from "./registry";
export * from "./dns-provider";
export * from "./plugin";
export * from "./utils";
export * from "./decorator";
export * from "./midway";
export * from "./context";
@@ -1,9 +1,7 @@
import { Config, Configuration, getClassMetadata, Inject, listModule, listPropertyDataFromClass, Logger } from "@midwayjs/decorator";
import _ from "lodash-es";
import { Config, Configuration, Inject, Logger } from "@midwayjs/decorator";
// @ts-ignore
import { ILogger } from "@midwayjs/logger";
import { IMidwayContainer, MidwayDecoratorService } from "@midwayjs/core";
import { pluginRegistry } from "../plugin";
import { registerPlugins } from "../plugin/decorator";
import { registerAccess } from "../access/decorator";
import { registerDnsProviders } from "../dns-provider";
+1 -1
View File
@@ -1,5 +1,4 @@
import { Registrable } from "../registry";
import { pluginRegistry } from "./registry";
import { FormItemProps } from "../d.ts";
export enum ContextScope {
@@ -33,6 +32,7 @@ export type PluginDefine = Registrable & {
};
export interface ITaskPlugin {
onInit(): Promise<void>;
execute(): Promise<void>;
}
@@ -1,18 +1,13 @@
// src/decorator/memoryCache.decorator.ts
import {
attachClassMetadata,
attachPropertyDataToClass,
createCustomPropertyDecorator,
getClassMetadata,
listModule,
listPropertyDataFromClass,
Provide,
saveClassMetadata,
saveModule,
Scope,
ScopeEnum,
} from "@midwayjs/decorator";
import _ from "lodash-es";
import _ from "lodash";
import { pluginRegistry } from "./registry";
import { PluginDefine, TaskInputDefine, TaskOutputDefine } from "./api";
@@ -33,11 +28,11 @@ export function IsTaskPlugin(define: PluginDefine): ClassDecorator {
},
target
);
// 指定 IoC 容器创建实例的作用域,这里注册为请求作用域,这样能取到 ctx
Scope(ScopeEnum.Prototype)(target);
// // 指定 IoC 容器创建实例的作用域,这里注册为请求作用域,这样能取到 ctx
// Scope(ScopeEnum.Prototype)(target);
// 调用一下 Provide 装饰器,这样用户的 class 可以省略写 @Provide() 装饰器了
Provide()(target);
// Provide()(target);
};
}
+2 -1
View File
@@ -1,4 +1,4 @@
import log4js, { LoggingEvent } from "log4js";
import log4js, { LoggingEvent, Logger } from "log4js";
const OutputAppender = {
configure: (config: any, layouts: any, findAppender: any, levels: any) => {
@@ -32,3 +32,4 @@ export function buildLogger(write: (text: string) => void) {
});
return logger;
}
export type LOGGER = Logger;