@fiyuu/runtime 0.1.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/service.ts DELETED
@@ -1,97 +0,0 @@
1
- import type { FiyuuDB } from "@fiyuu/db";
2
- import type { FiyuuRealtime } from "@fiyuu/realtime";
3
- import type { FiyuuConfig } from "@fiyuu/core";
4
-
5
- export interface ServiceContext {
6
- db: FiyuuDB;
7
- realtime: FiyuuRealtime;
8
- config: FiyuuConfig;
9
- log: (level: "info" | "warn" | "error", msg: string, data?: unknown) => void;
10
- }
11
-
12
- export interface FiyuuService {
13
- name: string;
14
- start(ctx: ServiceContext): Promise<void> | void;
15
- stop?(ctx: ServiceContext): Promise<void> | void;
16
- }
17
-
18
- export function defineService(service: FiyuuService): FiyuuService {
19
- if (!service.name || service.name.trim().length === 0) {
20
- throw new Error("Service must have a non-empty name.");
21
- }
22
- if (typeof service.start !== "function") {
23
- throw new Error(`Service "${service.name}" must have a start() function.`);
24
- }
25
- return service;
26
- }
27
-
28
- export class ServiceManager {
29
- private services: FiyuuService[] = [];
30
- private context: ServiceContext | null = null;
31
- private started = false;
32
-
33
- setContext(ctx: ServiceContext): void {
34
- this.context = ctx;
35
- }
36
-
37
- register(service: FiyuuService): void {
38
- if (this.services.some((s) => s.name === service.name)) {
39
- throw new Error(`Service "${service.name}" is already registered.`);
40
- }
41
- this.services.push(service);
42
- }
43
-
44
- async startAll(): Promise<void> {
45
- if (!this.context) throw new Error("ServiceManager context not set. Call setContext() first.");
46
-
47
- this.started = true;
48
-
49
- for (const service of this.services) {
50
- try {
51
- this.context.log("info", `service.start`, { name: service.name });
52
- await service.start(this.context);
53
- this.context.log("info", `service.started`, { name: service.name });
54
- } catch (err) {
55
- const msg = err instanceof Error ? err.message : String(err);
56
- this.context.log("error", `service.start.error`, { name: service.name, error: msg });
57
- }
58
- }
59
- }
60
-
61
- async stopAll(): Promise<void> {
62
- if (!this.context || !this.started) return;
63
-
64
- // Stop in reverse order
65
- for (let i = this.services.length - 1; i >= 0; i--) {
66
- const service = this.services[i];
67
- if (service.stop) {
68
- try {
69
- this.context.log("info", `service.stop`, { name: service.name });
70
- await service.stop(this.context);
71
- this.context.log("info", `service.stopped`, { name: service.name });
72
- } catch (err) {
73
- const msg = err instanceof Error ? err.message : String(err);
74
- this.context.log("error", `service.stop.error`, { name: service.name, error: msg });
75
- }
76
- }
77
- }
78
-
79
- this.started = false;
80
- }
81
-
82
- list(): string[] {
83
- return this.services.map((s) => s.name);
84
- }
85
-
86
- get count(): number {
87
- return this.services.length;
88
- }
89
-
90
- get isRunning(): boolean {
91
- return this.started;
92
- }
93
- }
94
-
95
- export function createServiceManager(): ServiceManager {
96
- return new ServiceManager();
97
- }