-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: impl Application and Agent extend with Class (#65)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Release Notes - **New Features** - Introduced new lifecycle method `beforeClose` to manage application shutdown. - Enhanced asynchronous handling in various classes, including `Schedule`, `BaseStrategy`, and `TimerStrategy`. - **Bug Fixes** - Corrected documentation in the `init` method of the `Schedule` class. - **Refactor** - Updated class structures to use explicit class declarations instead of object exports. - Streamlined property access in several classes by transitioning to getter methods. - **Chores** - Updated import paths for `Agent` and `Application` types across multiple files. - Expanded exports in `src/index.ts` to include new modules. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
- Loading branch information
Showing
11 changed files
with
61 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,33 @@ | ||
import { Agent as EggAgent } from 'egg'; | ||
import { BaseStrategy } from '../../lib/strategy/base.js'; | ||
import { TimerStrategy } from '../../lib/strategy/timer.js'; | ||
import { Schedule } from '../../lib/schedule.js'; | ||
|
||
const SCHEDULE = Symbol('agent#schedule'); | ||
const SCHEDULE = Symbol('agent schedule'); | ||
|
||
export default { | ||
export default class Agent extends EggAgent { | ||
/** | ||
* @member agent#ScheduleStrategy | ||
*/ | ||
ScheduleStrategy: BaseStrategy, | ||
get ScheduleStrategy() { | ||
return BaseStrategy; | ||
} | ||
|
||
/** | ||
* @member agent#TimerScheduleStrategy | ||
*/ | ||
TimerScheduleStrategy: TimerStrategy, | ||
get TimerScheduleStrategy() { | ||
return TimerStrategy; | ||
} | ||
|
||
/** | ||
* @member agent#schedule | ||
*/ | ||
get schedule() { | ||
if (!this[SCHEDULE]) { | ||
this[SCHEDULE] = new Schedule(this); | ||
this.lifecycle.registerBeforeClose(() => { | ||
return this[SCHEDULE].close(); | ||
}); | ||
let schedule = this[SCHEDULE] as Schedule; | ||
if (!schedule) { | ||
this[SCHEDULE] = schedule = new Schedule(this); | ||
} | ||
return this[SCHEDULE]; | ||
}, | ||
} as any; | ||
return schedule; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,18 @@ | ||
import { Application as EggApplication } from 'egg'; | ||
import { ScheduleWorker } from '../../lib/schedule_worker.js'; | ||
|
||
const SCHEDULE_WORKER = Symbol('application#scheduleWorker'); | ||
const SCHEDULE_WORKER = Symbol('application scheduleWorker'); | ||
|
||
export default { | ||
export default class Application extends EggApplication { | ||
/** | ||
* @member app#schedule | ||
*/ | ||
get scheduleWorker() { | ||
if (!this[SCHEDULE_WORKER]) { | ||
this[SCHEDULE_WORKER] = new ScheduleWorker(this); | ||
let scheduleWorker = this[SCHEDULE_WORKER] as ScheduleWorker; | ||
if (!scheduleWorker) { | ||
this[SCHEDULE_WORKER] = scheduleWorker = new ScheduleWorker(this); | ||
} | ||
return this[SCHEDULE_WORKER]; | ||
}, | ||
} as any; | ||
return scheduleWorker; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,9 @@ | ||
import Agent from './app/extend/agent.js'; | ||
import Application from './app/extend/application.js'; | ||
import ApplicationUnittest from './app/extend/application.unittest.js'; | ||
|
||
export { Agent, Application, ApplicationUnittest }; | ||
export { ScheduleWorker } from './lib/schedule_worker.js'; | ||
export { Schedule } from './lib/schedule.js'; | ||
|
||
export * from './lib/types.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters