-
Notifications
You must be signed in to change notification settings - Fork 6
feat(reminder): wire createAppServices and AppRuntime #181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gac0812
wants to merge
4
commits into
1024XEngineer:main
Choose a base branch
from
gac0812:feat/reminder-app-wiring
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+161
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a72ce0a
feat(reminder): wire createAppServices and AppRuntime
2bb289c
chore(reminder): merge main into app wiring branch
1d027e2
ci(reminder): retrigger checks after marking PR ready
b8abf49
fix(app): serialize AppRuntime lifecycle and best-effort stop
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { AppRuntime } from '../orchestration/AppRuntime'; | ||
| import type { | ||
| ReminderApplicationDependencies, | ||
| ReminderApplicationPort, | ||
| } from '../../features/reminder/application/interfaces'; | ||
| import { | ||
| MockLocalScheduleReader, | ||
| MockReminderApplication, | ||
| MockReminderDispositionSync, | ||
| MockReminderStateStore, | ||
| } from '../../features/reminder/data/local'; | ||
| import { MockAudioPlayback } from '../../infrastructure/audio'; | ||
| import { MockLocationMonitor } from '../../infrastructure/location'; | ||
| import { | ||
| MockAlarmScheduler, | ||
| MockDeviceCapability, | ||
| MockPopup, | ||
| MockReminderRecovery, | ||
| MockReminderDelivery, | ||
| MockSystemNotification, | ||
| MockVibration, | ||
| } from '../../infrastructure/notifications'; | ||
| import { MockTimeListener } from '../../shared/time'; | ||
| import { MockReminderPresenter } from '../../features/reminder/presentation'; | ||
|
|
||
| export type AppServices = { | ||
| runtime: AppRuntime; | ||
| reminder: ReminderApplicationPort; | ||
| reminderPorts: ReminderApplicationDependencies; | ||
| }; | ||
|
|
||
| /** 提醒端口的组合根;当前所有具体适配器都是确定性的模拟实现。 */ | ||
| export function createAppServices(): AppServices { | ||
| const reminderPorts: ReminderApplicationDependencies = { | ||
| schedules: new MockLocalScheduleReader(), | ||
| time: new MockTimeListener(), | ||
| location: new MockLocationMonitor(), | ||
| alarms: new MockAlarmScheduler(), | ||
| delivery: new MockReminderDelivery(), | ||
| audio: new MockAudioPlayback(), | ||
| device: new MockDeviceCapability(), | ||
| presenter: new MockReminderPresenter(), | ||
| systemNotification: new MockSystemNotification(), | ||
| popup: new MockPopup(), | ||
| vibration: new MockVibration(), | ||
| recovery: new MockReminderRecovery(), | ||
| state: new MockReminderStateStore(), | ||
| dispositionSync: new MockReminderDispositionSync(), | ||
| }; | ||
| const reminder = new MockReminderApplication(reminderPorts); | ||
|
|
||
| return { | ||
| runtime: new AppRuntime([ | ||
| { | ||
| start: () => reminder.start(), | ||
| stop: () => reminder.stop(), | ||
| }, | ||
| ]), | ||
| reminder, | ||
| reminderPorts, | ||
| }; | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| export type RuntimeModule = { | ||
| start(): Promise<void> | void; | ||
| stop(): Promise<void> | void; | ||
| }; | ||
|
|
||
| /** 协调应用生命周期模块,不在应用层放置业务规则。 */ | ||
| export class AppRuntime { | ||
| private started = false; | ||
| /** 串行化 start/stop,避免并发重入与半开状态。 */ | ||
| private lifecycle: Promise<void> = Promise.resolve(); | ||
|
|
||
| constructor(private readonly modules: readonly RuntimeModule[] = []) {} | ||
|
|
||
| async start(): Promise<void> { | ||
| const run = this.lifecycle.then(() => this.startInternal()); | ||
| this.lifecycle = run.then( | ||
| () => undefined, | ||
| () => undefined, | ||
| ); | ||
| return run; | ||
| } | ||
|
|
||
| async stop(): Promise<void> { | ||
| const run = this.lifecycle.then(() => this.stopInternal()); | ||
| this.lifecycle = run.then( | ||
| () => undefined, | ||
| () => undefined, | ||
| ); | ||
| return run; | ||
| } | ||
|
|
||
| private async startInternal(): Promise<void> { | ||
| if (this.started) return; | ||
|
|
||
| const startedModules: RuntimeModule[] = []; | ||
| try { | ||
| for (const module of this.modules) { | ||
| await module.start(); | ||
| startedModules.push(module); | ||
| } | ||
| this.started = true; | ||
| } catch (error) { | ||
| const cleanupErrors = await stopAllBestEffort([...startedModules].reverse()); | ||
| throw withCleanupErrors(error, cleanupErrors, 'AppRuntime start failed'); | ||
| } | ||
| } | ||
|
|
||
| private async stopInternal(): Promise<void> { | ||
| if (!this.started) return; | ||
|
|
||
| const stopErrors = await stopAllBestEffort([...this.modules].reverse()); | ||
| // 无论个别 stop 是否失败,都视为已退出 started,避免半关闭后重试重复 stop。 | ||
| this.started = false; | ||
| if (stopErrors.length > 0) { | ||
| throw withCleanupErrors(undefined, stopErrors, 'AppRuntime stop failed'); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| async function stopAllBestEffort(modules: readonly RuntimeModule[]): Promise<unknown[]> { | ||
| const errors: unknown[] = []; | ||
| for (const module of modules) { | ||
| try { | ||
| await module.stop(); | ||
|
gac0812 marked this conversation as resolved.
|
||
| } catch (error) { | ||
| errors.push(error); | ||
| } | ||
| } | ||
| return errors; | ||
| } | ||
|
|
||
| function withCleanupErrors( | ||
| primary: unknown | undefined, | ||
| cleanupErrors: readonly unknown[], | ||
| message: string, | ||
| ): Error { | ||
| if (cleanupErrors.length === 0) { | ||
| return primary instanceof Error ? primary : new Error(String(primary)); | ||
| } | ||
|
|
||
| const parts: unknown[] = []; | ||
| if (primary !== undefined) { | ||
| parts.push(primary); | ||
| } | ||
| parts.push(...cleanupErrors); | ||
|
|
||
| if (typeof AggregateError === 'function') { | ||
| return new AggregateError(parts, message); | ||
| } | ||
|
|
||
| const detail = parts | ||
| .map((error) => (error instanceof Error ? error.message : String(error))) | ||
| .join('; '); | ||
| const fallback = new Error(`${message}: ${detail}`); | ||
| if (primary instanceof Error) { | ||
| fallback.cause = primary; | ||
| } | ||
| return fallback; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.