Skip to content

Commit

Permalink
refactor: decoupling ctx & pipeline (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyj1991 authored Jun 23, 2022
1 parent 5d82d3b commit 38602f4
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 13 deletions.
6 changes: 2 additions & 4 deletions src/trigger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ export default class Trigger implements TriggerType {
this.pipeline.use(middleware);
}

async initContext(input: Input, output: Output): Promise<Context> {
async initContext(input: Input = new Input(), output = new Output()): Promise<Context> {
const ctx = new Context(input, output);
ctx.container = new ExecutionContainer(ctx, this.app.getContainer());
return ctx;
}

async startPipeline(input: Input = new Input(), output = new Output()): Promise<Context> {
const ctx = await this.initContext(input, output);
async startPipeline(ctx: Context): Promise<void> {
await this.pipeline.run(ctx);
return ctx;
}
}
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export interface Application {
export interface TriggerType {
use(...args): Promise<void>;
initContext(...args): Promise<BaseContext>;
startPipeline(...args): Promise<BaseContext>;
startPipeline(...args): Promise<void>;
}

export * from './loader/types';
3 changes: 2 additions & 1 deletion test/fixtures/app_koa_with_ts/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ export default class MyLifecycle implements ApplicationLifecycle {
const input = new Input();
const { req, res } = koaCtx;
input.params = { koaCtx, req, res };
await this.app.trigger.startPipeline(input);
const ctx = await this.app.trigger.initContext(input);
await this.app.trigger.startPipeline(ctx);
});
}

Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/frameworks/layer/foo/foo1/src/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,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 };
await this.app.trigger.startPipeline(input);
const ctx = await this.app.trigger.initContext(input);
await this.app.trigger.startPipeline(ctx);
})
.listen(port);
}
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/frameworks/layer/foo/foo2/src/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,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 };
await this.app.trigger.startPipeline(input);
const ctx = await this.app.trigger.initContext(input);
await this.app.trigger.startPipeline(ctx);
})
.listen(port);
}
Expand Down
6 changes: 4 additions & 2 deletions test/fixtures/trigger/event/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@ export default class MyLifecycle implements ApplicationLifecycle {
const input = new Input();
input.params.type = 'e1';
input.params.payload = payload;
await this.app.trigger.startPipeline(input);
const ctx = await this.app.trigger.initContext(input);
await this.app.trigger.startPipeline(ctx);
});

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

Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/trigger/http/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export default class MyLifecycle implements ApplicationLifecycle {
.createServer(async (req: http.IncomingMessage, res: http.ServerResponse) => {
const input = new Input();
input.params = { req, res };
await this.app.trigger.startPipeline(input);
const ctx = await this.app.trigger.initContext(input);
await this.app.trigger.startPipeline(ctx);
})
.listen(3001)
}
Expand Down
6 changes: 4 additions & 2 deletions test/fixtures/trigger/timer/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ export default class MyLifecycle implements ApplicationLifecycle {
timers.push(setInterval(async () => {
const input = new Input();
input.params = { task: '1', execution };
await this.app.trigger.startPipeline(input);
const ctx = await this.app.trigger.initContext(input);
await this.app.trigger.startPipeline(ctx);
}, 100));

timers.push(setInterval(async () => {
const input = new Input();
input.params = { task: '2', execution };
await this.app.trigger.startPipeline(input);
const ctx = await this.app.trigger.initContext(input);
await this.app.trigger.startPipeline(ctx);
}, 200));
}

Expand Down

0 comments on commit 38602f4

Please sign in to comment.