Skip to content

Commit

Permalink
feat(express): support lifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
hua-bang committed Aug 28, 2024
1 parent 0ca1a73 commit 7232bd4
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
"typescript": "^5.5.4"
},
"dependencies": {
"awesome_mvp_tapable": "0.0.1-beta.0"
"awesome_mvp_tapable": "0.0.1-beta.2"
}
}
6 changes: 5 additions & 1 deletion packages/express/src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import http from 'http';
import Router from './router';
import { Handler, HttpMethod } from './typings';
import { defaultHandler } from './default-handler';
import Lifecycle from './lifecycle';

class Application {
private httpServer: http.Server | null = null;
private router: Router = new Router();
lifeCycle: Lifecycle = new Lifecycle();

handle(
req: http.IncomingMessage,
Expand All @@ -22,7 +24,9 @@ class Application {
this.httpServer = http.createServer((req, res) => {
appInstance.handle.call(appInstance, req, res);
});
this.httpServer.listen(port);
this.httpServer.listen(port, () => {
this.lifeCycle.listened.call(port);
});
}

get(path: string, handler: Handler) {
Expand Down
7 changes: 7 additions & 0 deletions packages/express/src/lifecycle/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { SyncHook } from 'awesome_mvp_tapable';

class Lifecycle {
listened: SyncHook = new SyncHook([3000]);
}

export default Lifecycle;
6 changes: 6 additions & 0 deletions packages/express/test/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import { express } from '../src/index';

const app = express();

app.lifeCycle.listened.tap('event', (port) => {
console.log(`
Http Server is start on ${port}
`);
});

app.get('/user/name', (req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
Expand Down
12 changes: 5 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7232bd4

Please sign in to comment.