Skip to content

Commit b832066

Browse files
committed
Make timers integrated into kernel
1 parent bab7603 commit b832066

4 files changed

Lines changed: 24 additions & 18 deletions

File tree

.changeset/quiet-timers-share.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@fraqjs/fraq": minor
3+
"@fraqjs/kernel": minor
4+
---
5+
6+
`timeout``interval` 集成到 `@fraqjs/kernel`

packages/fraq/src/core/context/index.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import type { Filter } from '../filter';
1212
import type { LogHandler } from '../logging';
1313
import { ApiHookRegistry } from './api-hooks';
1414
import { EventSourceRegistry } from './event-sources';
15-
import { TimerRegistry } from './timers';
1615

1716
export interface ContextOptions {
1817
reconnect?: {
@@ -42,7 +41,6 @@ interface Subsystems {
4241
readonly detachParentEvents: (() => void) | undefined;
4342
readonly apiHooks: ApiHookRegistry;
4443
readonly eventSources: EventSourceRegistry;
45-
readonly timers: TimerRegistry;
4644
}
4745

4846
interface Builtins {
@@ -55,8 +53,6 @@ interface Builtins {
5553
<E extends ApiEndpointName>(endpoint: E, hook: ApiHook<E>): () => void;
5654
(hook: AnyApiHook): () => void;
5755
};
58-
timeout(delayMs: number, callback: () => void | Promise<void>): NodeJS.Timeout;
59-
interval(intervalMs: number, callback: () => void | Promise<void>): NodeJS.Timeout;
6056
createSession(selfId: number, message: IncomingMessage): Session;
6157
}
6258

@@ -134,11 +130,6 @@ const ContextRuntime = defineContext<RootOptions, Filter>()
134130
create: () => new ApiHookRegistry(baseClient, parent?.systems.apiHooks, name, getState),
135131
stop: (registry) => registry.clear(),
136132
});
137-
const timers = subsystem({
138-
name: 'timers',
139-
create: () => new TimerRegistry(name, logger, getState),
140-
suspend: (registry) => registry.clear(),
141-
});
142133
const eventSources = subsystem({
143134
name: 'eventSources',
144135
create: () =>
@@ -155,7 +146,6 @@ const ContextRuntime = defineContext<RootOptions, Filter>()
155146
detachParentEvents,
156147
apiHooks,
157148
eventSources,
158-
timers,
159149
};
160150
})
161151
.builtins<Builtins>(({ logger, rootOptions, parent, systems, getState }) => {
@@ -185,8 +175,6 @@ const ContextRuntime = defineContext<RootOptions, Filter>()
185175
installEventSource: (eventSource) => systems.eventSources.install(eventSource),
186176
hookApi: (endpointOrHook: ApiEndpointName | AnyApiHook, hook?: ApiHook<ApiEndpointName>) =>
187177
systems.apiHooks.register(endpointOrHook, hook),
188-
timeout: (delayMs, callback) => systems.timers.timeout(delayMs, callback),
189-
interval: (intervalMs, callback) => systems.timers.interval(intervalMs, callback),
190178
createSession: (selfId, message) => createSession(systems.apiHooks.client, selfId, message),
191179
};
192180
})

packages/kernel/src/context/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { type ContextState, LifecycleManager } from './lifecycle';
88
import { PluginRegistry, type PluginRegistryOptions } from './plugins';
99
import { ServiceRegistry, type ServiceResolutionScope } from './services';
1010
import { type SubsystemDefinition, SubsystemRegistry } from './subsystems';
11+
import { TimerRegistry } from './timers';
1112

1213
export type { ContextState } from './lifecycle';
1314
export type { SubsystemCleanupResult, SubsystemDefinition, SubsystemHooks } from './subsystems';
@@ -18,6 +19,8 @@ export interface KernelContext<C extends object, ForkOptions> {
1819
readonly state: ContextState;
1920
readonly logger: Logger;
2021
readonly logBus: LogEmitter;
22+
timeout(delayMs: number, callback: () => void | Promise<void>): NodeJS.Timeout;
23+
interval(intervalMs: number, callback: () => void | Promise<void>): NodeJS.Timeout;
2124

2225
install<T extends ParameterList>(plugin: PluginDefinition<C, T>, ...args: T): void;
2326

@@ -169,6 +172,7 @@ export class ContextBuilder<RootOptions, ForkOptions, Subsystems = never, Builti
169172
private readonly services: ServiceRegistry<Context>;
170173
private readonly serviceScope: ServiceResolutionScope<Context>;
171174
private readonly subsystems = new SubsystemRegistry();
175+
private readonly timers: TimerRegistry;
172176
private readonly systems: Subsystems;
173177
private readonly plugins: PluginRegistry<Context>;
174178
private readonly lifecycle: LifecycleManager<Context>;
@@ -191,6 +195,11 @@ export class ContextBuilder<RootOptions, ForkOptions, Subsystems = never, Builti
191195
this.services = new ServiceRegistry(parent?.services);
192196

193197
const getState = () => this.lifecycle.state;
198+
this.timers = this.subsystems.register({
199+
name: 'timers',
200+
create: () => new TimerRegistry(name, this.logger, getState),
201+
suspend: (timers) => timers.clear(),
202+
});
194203
this.systems = createSubsystems({
195204
name,
196205
path: this.path,
@@ -248,6 +257,14 @@ export class ContextBuilder<RootOptions, ForkOptions, Subsystems = never, Builti
248257
return this.lifecycle.state;
249258
}
250259

260+
timeout(delayMs: number, callback: () => void | Promise<void>): NodeJS.Timeout {
261+
return this.timers.timeout(delayMs, callback);
262+
}
263+
264+
interval(intervalMs: number, callback: () => void | Promise<void>): NodeJS.Timeout {
265+
return this.timers.interval(intervalMs, callback);
266+
}
267+
251268
install<T extends ParameterList>(plugin: PluginDefinition<Context, T>, ...args: T): void {
252269
this.plugins.install(plugin, ...args);
253270
}
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import type { ContextState } from '@fraqjs/kernel';
2-
31
import type { Logger } from '../logging';
2+
import type { ContextState } from './lifecycle';
43

54
export class TimerRegistry {
65
private readonly timers = new Set<NodeJS.Timeout>();
@@ -11,10 +10,6 @@ export class TimerRegistry {
1110
private readonly getState: () => ContextState,
1211
) {}
1312

14-
get hasTimers(): boolean {
15-
return this.timers.size > 0;
16-
}
17-
1813
timeout(delayMs: number, callback: () => void | Promise<void>): NodeJS.Timeout {
1914
this.assertCanScheduleTimer();
2015
const timeout = setTimeout(() => {

0 commit comments

Comments
 (0)