Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 | 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 8x 2x 2x 2x 2x 1x 1x 4x 4x 4x 1x 1x 1x 21x 21x 2x 1x 1x 1x 11x 11x 2x 8x 8x 8x 8x 4x 4x 4x 4x 2x 4x 1x 4x 4x 4x 1x 4x 4x 3x 2x 2x 4x 4x 4x 1x 4x 4x 4x 4x 4x 4x 1x 1x 3x 4x 10x 1x 1x 1x 1x 1x 1x 7x 33x 2x 2x 2x 1x 2x 1x 1x 4x 15x | import {
BridgeState,
type ICoreAction,
type ICoreAnyModule,
type ICoreBridge,
type ICoreCache,
type ICoreClient,
type ICoreKernel,
type ICoreKernelModule,
type ICorePresenter,
type ICoreService,
type IDataBase,
type KernelEvent,
} from '../lib';
import CoreAction from './CoreAction';
import CoreService from './CoreService';
import CoreLogChannel from './CoreLogChannel';
import CoreLogger from './CoreLogger';
/**
* Core kernel module
*
* ```typescript
* // Default use
*
* class ExampleModulen extends CoreKernelModule<any, any, any, any, any> {
* constructor(module: ICoreKernel<any>) {
* super('example', module);
* }
*
* beforeServiceStart(): Promise<void> {
* return Promise.resolve(undefined);
* }
*
* final(): Promise<void> {
* return Promise.resolve(undefined);
* }
*
* initModule(): Promise<void> {
* return Promise.resolve(undefined);
* }
*
* startup(): Promise<void> {
* return Promise.resolve(undefined);
* }
* }
*
* class Kernel extends CoreKernel<any> {
* constructor() {
* super({ appName: 'TestName', appCode: 'testcode' });
* // OPTION 01
* this.addModule(new ExampleModulen(this));
* }
* }
* const kernel = new Kernel();
* // OPTION 02
* kernel.setTriggerFunction('pre', async (ik) => {
* ik.addModule(new ExampleModulen(ik));
* });
*
* kernel.start();
* ```
*/
export default abstract class CoreKernelModule<
K extends ICoreKernel<any>,
T extends IDataBase<any, any> | null,
P extends ICoreClient | null,
C extends ICoreCache | null,
E extends ICorePresenter<any> | null,
>
extends CoreLogChannel
implements ICoreKernelModule<K, T, P, C, E>
{
/**
* Optional trigger function to be executed during registration.
*/
protected trigger?: () => Promise<void>;
/**
* List of actions registered in this module.
*/
private actionlist: CoreAction[];
/**
* Map of service name to CoreService instances.
*/
private readonly servicelist: Map<string, CoreService>;
/**
* List of dependency names.
*/
private readonly deps: string[];
/**
* List of source bridges.
*/
private readonly srcBridges: ICoreBridge[];
/**
* List of target bridges.
*/
private tarBridges: ICoreBridge[];
/**
* Reference to the kernel instance.
*/
private readonly kernel: K;
/**
* Database instance.
*/
private db: T | null;
/**
* Client instance.
*/
private client: P | null;
/**
* Cache instance.
*/
private cache: C | null;
/**
* Presenter instance.
*/
private presenter: E | null;
/**
* Name of the module.
*/
private readonly name: string;
/**
* Constructs a new CoreKernelModule.
* @param name Name of the module
* @param kernel Kernel instance
* @param deps List of dependency names
*/
constructor(name: string, kernel: K, ...deps: string[]) {
super(`${name}Module`, kernel);
this.name = name;
this.actionlist = [];
this.servicelist = new Map();
this.kernel = kernel;
this.db = null;
this.client = null;
this.cache = null;
this.presenter = null;
this.srcBridges = [];
this.tarBridges = [];
this.deps = deps;
}
/**
* Returns the list of actions.
*/
getActionList(): ICoreAction[] {
return this.actionlist;
}
/**
* Returns true if a database is set.
*/
hasDb(): boolean {
return !!this.db;
}
/**
* Returns true if a client is set.
*/
hasClient(): boolean {
return !!this.client;
}
/**
* Returns true if a presenter is set.
*/
hasPresenter(): boolean {
return !!this.presenter;
}
/**
* Returns true if a cache is set.
*/
hasCache(): boolean {
return !!this.cache;
}
/**
* Adds a source bridge.
* @param bridge The bridge to add
*/
addSrcBridge(bridge: ICoreBridge): void {
this.srcBridges.push(bridge);
}
/**
* Adds a target bridge.
* @param bridge The bridge to add
*/
addTarBridge(bridge: ICoreBridge): void {
this.tarBridges.push(bridge);
}
/**
* Startup logic for the module.
*/
async startup(): Promise<void> {
this.verbose('run-startup');
}
/**
* Starts the presenter and the module.
*/
async start(): Promise<void> {
await this.presenter?.start();
await this.startup();
}
/**
* Returns the presenter instance.
* @throws Error if presenter is not set
*/
getPresenter(): E {
Iif (!this.presenter) {
throw this.lError('no endpoint');
}
return this.presenter;
}
/**
* Sets the presenter instance.
* @param presenter The presenter to set
*/
setPresenter(presenter: E | null): void {
this.presenter = presenter;
}
/**
* Returns the database instance.
* @throws Error if database is not set
*/
getDb(): T {
Iif (!this.db) {
throw this.lError('no db');
}
return this.db;
}
/**
* Sets the database instance.
* @param db The database to set
*/
setDb(db: T): void {
this.db = db;
}
/**
* Returns the client instance.
* @throws Error if client is not set
*/
getClient(): P {
Iif (!this.client) {
throw this.lError('no client');
}
return this.client;
}
/**
* Sets the client instance.
* @param client The client to set
*/
setClient(client: P): void {
this.client = client;
}
/**
* Returns the cache instance.
* @throws Error if cache is not set
*/
getCache(): C {
Iif (!this.cache) {
throw this.lError('no cache');
}
return this.cache;
}
/**
* Sets the cache instance.
* @param cache The cache to set
*/
setCache(cache: C): void {
this.cache = cache;
}
/**
* Waits for all source bridges to reach a specific state.
* @param state The bridge state to wait for
*/
async waitForBridgeState(state: BridgeState): Promise<void> {
const waitList: Promise<any>[] = [];
this.srcBridges.forEach((b) => waitList.push(b.waitForState(state)));
await Promise.all(waitList);
}
/**
* Notifies all target bridges of a state change.
* @param state The state to set
*/
notifyBridges(state: BridgeState): void {
this.tarBridges.forEach((b) => b.setState(state));
}
/**
* Abstract method to initialize the module.
*/
abstract initModule(): Promise<void>;
/**
* Hook called before services are started.
*/
async beforeServiceStart(): Promise<void> {
this.verbose('run-before-service');
}
/**
* Registers the module, actions, and services.
* @param action Optional kernel event to trigger
*/
async register(action?: KernelEvent): Promise<void> {
await this.waitForBridgeState(BridgeState.ready);
await this.initModule();
if (action) {
await this.getKernel().triggerEvent(action);
}
if (this.trigger) {
await this.trigger();
}
await this.db?.start();
await this.cache?.start();
this.actionlist.forEach((el) => {
el.register();
});
await this.beforeServiceStart();
this.servicelist.forEach((service) => {
if (!service.skipAutoStart) {
service.debug('Starting');
service.start();
}
});
this.notifyBridges(BridgeState.ready);
}
/**
* Shuts down the module and its services.
*/
async shutdown(): Promise<void> {
await this.waitForBridgeState(BridgeState.end);
if (this.presenter) {
this.presenter.stop();
}
const workload: Promise<any>[] = [];
this.servicelist.forEach((el) => workload.push(el.stop()));
await Promise.all(workload);
await this.db?.disconnect();
await this.cache?.stop();
this.notifyBridges(BridgeState.end);
}
/**
* Adds one or more actions to the module.
* @param action Actions to add
*/
addAction(...action: CoreAction[]): void {
action.forEach((value) => {
this.actionlist.push(value);
});
}
/**
* Adds one or more services to the module.
* @param service Services to add
*/
addService(...service: CoreService[]): void {
service.forEach((value) => {
this.servicelist.set(value.getName(), value);
});
}
/**
* Returns the list of services.
*/
getServiceList(): ICoreService[] {
return Array.from(this.servicelist.values());
}
/**
* Stops a service by name.
* @param name Name of the service
* @returns The stopped service or null
*/
async stopService(name: string): Promise<ICoreService | null> {
const s = this.servicelist.get(name);
Eif (!s?.isRunning()) {
return null;
}
s.stop();
s.state = 'SLEEPING';
return s;
}
/**
* Starts a service by name.
* @param name Name of the service
* @returns The started service or null
*/
async startService(name: string): Promise<ICoreService | null> {
const s = this.servicelist.get(name);
Eif (!s || s.isRunning()) {
return null;
}
s.start();
s.state = 'RUNNING';
return s;
}
/**
* Returns the kernel instance.
*/
getKernel(): K {
return this.kernel;
}
/**
* Returns the name of the module.
*/
getName(): string {
return this.name;
}
/**
* Returns the list of dependency names.
*/
getDependencyList(): string[] {
return this.deps;
}
/**
* Returns the list of source bridges.
*/
getBridges(): ICoreBridge[] {
return this.srcBridges;
}
/**
* Returns a bridge module by name.
* @param name Name of the module
* @returns The bridge module
*/
getBridgeModule<M extends ICoreAnyModule>(name: string): M {
const br = this.srcBridges.find(
(bridge) => bridge.getTarget().getName() === name,
);
if (!br) {
throw this.lError(`no bridge module found: ${name}`);
}
return br.getTarget() as M;
}
/**
* Finalization logic for the module.
*/
async final(): Promise<void> {
this.verbose('run-final');
}
/**
* Returns the logger instance from the kernel.
*/
getLogger(): CoreLogger {
return this.kernel.getLogger();
}
}
|