Skip to content

Commit

Permalink
refactor: remove DefineTrigger (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyj1991 authored Sep 6, 2022
1 parent 81cd280 commit b4e85fc
Show file tree
Hide file tree
Showing 23 changed files with 67 additions and 76 deletions.
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
],
"scripts": {
"build": "tsc -p ./tsconfig.build.json",
"test": "jest --coverage --detectOpenHandles --testTimeout=15000",
"ci": "npm run lint && npm run test",
"test": "jest --detectOpenHandles --testTimeout=15000",
"cov": "jest --coverage --detectOpenHandles --testTimeout=15000",
"ci": "npm run lint && npm run cov",
"lint:fix": "eslint . --ext .ts --fix",
"lint": "eslint . --ext .ts"
},
Expand Down Expand Up @@ -61,10 +62,10 @@
"reflect-metadata": "^0.1.13",
"ts-jest": "^27.1.3",
"tslib": "^2.4.0",
"typescript": "~4.7.2"
"typescript": "^4.8.0"
},
"dependencies": {
"@artus/injection": "^0.4.1",
"@artus/injection": "^0.5.1",
"@artus/pipeline": "^0.2.2",
"deepmerge": "^4.2.2",
"minimatch": "^5.0.1"
Expand Down
6 changes: 1 addition & 5 deletions src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ArtusInjectEnum } from './constant';
import { ArtusStdError, ExceptionHandler } from './exception';
import { HookFunction, LifecycleManager } from './lifecycle';
import { LoaderFactory, Manifest } from './loader';
import { Application, ApplicationInitOptions, TriggerType } from './types';
import { Application, ApplicationInitOptions } from './types';
import Trigger from './trigger';
import ConfigurationHandler from './configuration';
import { ArtusLogger, Logger } from './logger';
Expand Down Expand Up @@ -44,10 +44,6 @@ export class ArtusApplication implements Application {
return this.container.get(ArtusInjectEnum.Packages);
}

get trigger(): TriggerType {
return this.container.get(ArtusInjectEnum.Trigger);
}

get exceptionHandler(): ExceptionHandler {
return this.container.get(ExceptionHandler);
}
Expand Down
1 change: 0 additions & 1 deletion src/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export enum ArtusInjectEnum {
LifecycleManager = 'artus#lifecycle-manager',
Logger = 'artus#logger',
Packages = 'artus#packages',
Trigger = 'artus#trigger',
}

export enum ARTUS_DEFAULT_CONFIG_ENV {
Expand Down
1 change: 0 additions & 1 deletion src/decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,3 @@ export function LifecycleHook(hookName?: string): PropertyDecorator {
}

export * from './loader/decorator';
export * from './trigger/decorator';
1 change: 0 additions & 1 deletion src/loader/impl/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class ModuleLoader implements Loader {
for (const name of state.exportNames) {
const moduleClazz = origin[name];
const opts: Partial<InjectableDefinition> = {
path: item.path,
type: moduleClazz,
scope: ScopeEnum.EXECUTION, // The class used with @artus/core will have default scope EXECUTION, can be overwritten by Injectable decorator
};
Expand Down
12 changes: 0 additions & 12 deletions src/trigger/decorator.ts

This file was deleted.

5 changes: 2 additions & 3 deletions src/trigger/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { ExecutionContainer, Inject } from '@artus/injection';
import { ExecutionContainer, Inject, Injectable, ScopeEnum } from '@artus/injection';
import { Input, Context, MiddlewareInput, Pipeline, Output } from '@artus/pipeline';
import { ArtusInjectEnum } from '../constant';
import { Application, TriggerType } from '../types';
import { DefineTrigger } from './decorator';

@DefineTrigger()
@Injectable({ scope: ScopeEnum.SINGLETON })
export default class Trigger implements TriggerType {
private pipeline: Pipeline;

Expand Down
2 changes: 0 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ export interface Application {
manifest?: Manifest;
config?: Record<string, any>;

get trigger(): TriggerType;

load(manifest: Manifest): Promise<this>;
run(): Promise<void>;
registerHook(hookName: string, hookFn: HookFunction): void;
Expand Down
4 changes: 0 additions & 4 deletions test/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'reflect-metadata';
import axios from 'axios';
import assert from 'assert';
import { ArtusInjectEnum, ConfigurationHandler, ExceptionHandler } from '../src';
import HttpTrigger from './fixtures/app_koa_with_ts/src/http_trigger';

describe('test/app.test.ts', () => {
describe('app koa with ts', () => {
Expand All @@ -27,13 +26,10 @@ describe('test/app.test.ts', () => {
expect(() => app.container.get(ArtusInjectEnum.Application)).not.toThrow();
expect(() => app.container.get(ArtusInjectEnum.LifecycleManager)).not.toThrow();
expect(() => app.container.get(ArtusInjectEnum.Logger)).not.toThrow();
expect(() => app.container.get(ArtusInjectEnum.Trigger)).not.toThrow();
expect(() => app.container.get(ExceptionHandler)).not.toThrow();
expect(() => app.container.get(ConfigurationHandler)).not.toThrow();

await main();

expect(app.container.get(ArtusInjectEnum.Trigger)).toBeInstanceOf(HttpTrigger);

const testResponse = await axios.get('http://127.0.0.1:3000', {
headers: {
Expand Down
9 changes: 6 additions & 3 deletions test/fixtures/app_koa_with_ts/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ApplicationLifecycle } from '../../../../src/types';

import KoaApplication from './koa_app';
import HelloController from './controllers/hello';
import HttpTrigger from './http_trigger';

export let server: Server;

Expand All @@ -18,14 +19,16 @@ export default class MyLifecycle implements ApplicationLifecycle {
app: ArtusApplication;
@Inject()
container: Container;
@Inject()
trigger: HttpTrigger;

get koaApp(): KoaApplication {
return this.container.get(KoaApplication);
}

@LifecycleHook()
async didLoad() {
this.app.trigger.use(async (ctx: Context) => {
this.trigger.use(async (ctx: Context) => {
const { koaCtx } = ctx.input.params;
ctx.container.set({ id: 'headers', value: koaCtx.headers });
ctx.output.data = await ctx.container.get(HelloController).index();
Expand All @@ -38,8 +41,8 @@ export default class MyLifecycle implements ApplicationLifecycle {
const input = new Input();
const { req, res } = koaCtx;
input.params = { koaCtx, req, res };
const ctx = await this.app.trigger.initContext(input);
await this.app.trigger.startPipeline(ctx);
const ctx = await this.trigger.initContext(input);
await this.trigger.startPipeline(ctx);
});
}

Expand Down
5 changes: 2 additions & 3 deletions test/fixtures/app_koa_with_ts/src/http_trigger.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Context, Next } from '@artus/pipeline';
import { Trigger } from '../../../../src';
import { DefineTrigger } from '../../../../src/decorator';
import { Injectable, ScopeEnum, Trigger } from '../../../../src';

@DefineTrigger()
@Injectable({ scope: ScopeEnum.SINGLETON })
export default class HttpTrigger extends Trigger {
constructor() {
super();
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/frameworks/abstract/foo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ export interface AbstractFoo {
isListening: () => boolean
}

export interface HttpTrigger extends Trigger {}
export class HttpTrigger extends Trigger { }
4 changes: 3 additions & 1 deletion test/fixtures/frameworks/bar/src/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ export let server: Server;
export default class MyLifecycle implements ApplicationLifecycle {
@Inject(ArtusInjectEnum.Application)
app: ArtusApplication;
@Inject()
trigger: HttpTrigger;

@LifecycleHook()
async didLoad() {
// register controller
registerController(this.app.trigger as HttpTrigger);
registerController(this.trigger as HttpTrigger);
}
}
7 changes: 5 additions & 2 deletions test/fixtures/frameworks/layer/foo/foo1/src/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ import { LifecycleHookUnit, LifecycleHook } from '../../../../../../../src/decor
import { ApplicationLifecycle } from '../../../../../../../src/types';
import { Input } from '@artus/pipeline';
import { ArtusApplication, Inject, ArtusInjectEnum } from '../../../../../../../src';
import { HttpTrigger } from '../../../../abstract/foo';

export let server: Server;

@LifecycleHookUnit()
export default class MyLifecycle implements ApplicationLifecycle {
@Inject(ArtusInjectEnum.Application)
app: ArtusApplication;
@Inject()
trigger: HttpTrigger;

@LifecycleHook()
willReady() {
Expand All @@ -19,8 +22,8 @@ export default class MyLifecycle implements ApplicationLifecycle {
.createServer(async (req: http.IncomingMessage, res: http.ServerResponse) => {
const input = new Input();
input.params = { req, res, config, app: this.app };
const ctx = await this.app.trigger.initContext(input);
await this.app.trigger.startPipeline(ctx);
const ctx = await this.trigger.initContext(input);
await this.trigger.startPipeline(ctx);
})
.listen(port);
}
Expand Down
8 changes: 4 additions & 4 deletions test/fixtures/frameworks/layer/foo/foo1/src/trigger/http.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Stream } from 'stream';
import { Context, Next } from '@artus/pipeline';
import { Trigger } from '../../../../../../../../src';
import { DefineTrigger } from '../../../../../../../../src/decorator';
import { Injectable, ScopeEnum, Trigger } from '../../../../../../../../src';
import { HttpTrigger } from '../../../../../abstract/foo';

@DefineTrigger()
export default class HttpTrigger extends Trigger {
@Injectable({ id: HttpTrigger, scope: ScopeEnum.SINGLETON })
export default class HttpTriggerImpl extends Trigger {
constructor() {
super();
// first of all
Expand Down
7 changes: 5 additions & 2 deletions test/fixtures/frameworks/layer/foo/foo2/src/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ import { LifecycleHookUnit, LifecycleHook } from '../../../../../../../src/decor
import { ApplicationLifecycle } from '../../../../../../../src/types';
import { Input } from '@artus/pipeline';
import { ArtusApplication, Inject, ArtusInjectEnum } from '../../../../../../../src';
import { HttpTrigger } from '../../../../abstract/foo';

export let server: Server;

@LifecycleHookUnit()
export default class MyLifecycle implements ApplicationLifecycle {
@Inject(ArtusInjectEnum.Application)
app: ArtusApplication;
@Inject()
trigger: HttpTrigger;

@LifecycleHook()
willReady() {
Expand All @@ -19,8 +22,8 @@ export default class MyLifecycle implements ApplicationLifecycle {
.createServer(async (req: http.IncomingMessage, res: http.ServerResponse) => {
const input = new Input();
input.params = { req, res, config, app: this.app };
const ctx = await this.app.trigger.initContext(input);
await this.app.trigger.startPipeline(ctx);
const ctx = await this.trigger.initContext(input);
await this.trigger.startPipeline(ctx);
})
.listen(port);
}
Expand Down
8 changes: 4 additions & 4 deletions test/fixtures/frameworks/layer/foo/foo2/src/trigger/http.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Stream } from 'stream';
import { Context, Next } from '@artus/pipeline';
import { Trigger } from '../../../../../../../../src';
import { DefineTrigger } from '../../../../../../../../src/decorator';
import { Injectable, ScopeEnum, Trigger } from '../../../../../../../../src';
import { HttpTrigger } from '../../../../../abstract/foo';

@DefineTrigger()
export default class HttpTrigger extends Trigger {
@Injectable({ id: HttpTrigger, scope: ScopeEnum.SINGLETON })
export default class HttpTriggerImpl extends Trigger {
constructor() {
super();
// first of all
Expand Down
15 changes: 9 additions & 6 deletions test/fixtures/trigger/event/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ import { ArtusApplication, Inject, ArtusInjectEnum } from '../../../../src';
import { LifecycleHookUnit, LifecycleHook } from '../../../../src/decorator';
import { Context, Input, Next } from '@artus/pipeline';
import { ApplicationLifecycle } from '../../../../src/types';
import EventTrigger from './event_trigger';

export const event = new EventEmitter();

@LifecycleHookUnit()
export default class MyLifecycle implements ApplicationLifecycle {
@Inject(ArtusInjectEnum.Application)
app: ArtusApplication;
@Inject()
trigger: EventTrigger;

@LifecycleHook()
async didLoad() {
this.app.trigger.use(async (ctx: Context, next: Next) => {
this.trigger.use(async (ctx: Context, next: Next) => {
const { input: { params: { type, payload } } } = ctx;
if (type !== 'e1') {
await next();
Expand All @@ -24,7 +27,7 @@ export default class MyLifecycle implements ApplicationLifecycle {
data.payload = payload;
});

this.app.trigger.use(async (ctx: Context, next: Next) => {
this.trigger.use(async (ctx: Context, next: Next) => {
const { input: { params: { type, payload } } } = ctx;
if (type !== 'e2') {
await next();
Expand All @@ -42,16 +45,16 @@ export default class MyLifecycle implements ApplicationLifecycle {
const input = new Input();
input.params.type = 'e1';
input.params.payload = payload;
const ctx = await this.app.trigger.initContext(input);
await this.app.trigger.startPipeline(ctx);
const ctx = await this.trigger.initContext(input);
await this.trigger.startPipeline(ctx);
});

event.on('e2', async payload => {
const input = new Input();
input.params.type = 'e2';
input.params.payload = payload;
const ctx = await this.app.trigger.initContext(input);
await this.app.trigger.startPipeline(ctx);
const ctx = await this.trigger.initContext(input);
await this.trigger.startPipeline(ctx);
});
}

Expand Down
5 changes: 2 additions & 3 deletions test/fixtures/trigger/event/event_trigger.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Context, Next } from '@artus/pipeline';
import { Trigger } from '../../../../src';
import { DefineTrigger } from '../../../../src/decorator';
import { Injectable, ScopeEnum, Trigger } from '../../../../src';

@DefineTrigger()
@Injectable({ scope: ScopeEnum.SINGLETON })
export default class EventTrigger extends Trigger {
constructor() {
super();
Expand Down
9 changes: 6 additions & 3 deletions test/fixtures/trigger/http/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ import { Context, Input } from '@artus/pipeline';
import { ArtusApplication, Inject, ArtusInjectEnum } from '../../../../src';
import { LifecycleHookUnit, LifecycleHook } from '../../../../src/decorator';
import { ApplicationLifecycle } from '../../../../src/types';
import HttpTrigger from './http_trigger';

export let server: Server;

@LifecycleHookUnit()
export default class MyLifecycle implements ApplicationLifecycle {
@Inject(ArtusInjectEnum.Application)
app: ArtusApplication;
@Inject()
trigger: HttpTrigger;

@LifecycleHook()
async didLoad() {
this.app.trigger.use(async (ctx: Context) => {
this.trigger.use(async (ctx: Context) => {
const { data } = ctx.output;
data.content = { title: 'Hello Artus' };
});
Expand All @@ -25,8 +28,8 @@ export default class MyLifecycle implements ApplicationLifecycle {
.createServer(async (req: http.IncomingMessage, res: http.ServerResponse) => {
const input = new Input();
input.params = { req, res };
const ctx = await this.app.trigger.initContext(input);
await this.app.trigger.startPipeline(ctx);
const ctx = await this.trigger.initContext(input);
await this.trigger.startPipeline(ctx);
})
.listen(3001);
}
Expand Down
5 changes: 2 additions & 3 deletions test/fixtures/trigger/http/http_trigger.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Stream } from 'stream';
import { Context, Next } from '@artus/pipeline';
import { Trigger } from '../../../../src';
import { DefineTrigger } from '../../../../src/decorator';
import { Injectable, ScopeEnum, Trigger } from '../../../../src';

@DefineTrigger()
@Injectable({ scope: ScopeEnum.SINGLETON })
export default class HttpTrigger extends Trigger {
constructor() {
super();
Expand Down
Loading

0 comments on commit b4e85fc

Please sign in to comment.