@oxog/utils 1.0.0 โ 1.0.1
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/CHANGELOG.md +38 -0
- package/README.md +50 -5
- package/dist/index.cjs +344 -23
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +257 -5
- package/dist/index.d.ts +257 -5
- package/dist/index.js +331 -24
- package/dist/index.js.map +1 -1
- package/dist/plugins/async.cjs.map +1 -1
- package/dist/plugins/async.js.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,44 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to @oxog/utils will be documented in this file.
|
|
4
4
|
|
|
5
|
+
## [1.0.1] - 2026-01-25
|
|
6
|
+
|
|
7
|
+
### Added
|
|
8
|
+
- **Plugin State Tracking**: New `PluginState` type (`registered`, `initializing`, `active`, `error`, `destroyed`)
|
|
9
|
+
- **Scoped Event Bus**: Automatic listener cleanup when plugins are unregistered
|
|
10
|
+
- `ScopedEventBus` class for plugin-specific event handling
|
|
11
|
+
- `EventBus.onScoped()`, `EventBus.removeScope()`, `EventBus.hasScope()` methods
|
|
12
|
+
- **Kernel Options**: Constructor now accepts `KernelOptions<TContext>` for initial context
|
|
13
|
+
- **New API Methods**:
|
|
14
|
+
- `kernel.getPluginState(name)` - Get plugin lifecycle state
|
|
15
|
+
- `kernel.getState()` - Get kernel state (`idle`, `initializing`, `initialized`, `destroyed`)
|
|
16
|
+
- `kernel.isInitialized()` - Check if kernel is initialized
|
|
17
|
+
- `kernel.getScopedEventBus(name)` - Get scoped event bus for a plugin
|
|
18
|
+
- **New Error Classes**:
|
|
19
|
+
- `CircularDependencyError` - Thrown when circular dependency detected
|
|
20
|
+
- `PluginInitializationError` - Thrown with cause when plugin init fails
|
|
21
|
+
- `KernelAlreadyInitializedError` - Thrown when kernel already initialized
|
|
22
|
+
- `KernelInitializingError` - Thrown for race condition prevention
|
|
23
|
+
|
|
24
|
+
### Changed
|
|
25
|
+
- **BREAKING**: `kernel.unregister()` is now async and returns `Promise<void>`
|
|
26
|
+
- **Topological Sort**: Plugins now initialize in dependency order
|
|
27
|
+
- **Rollback Mechanism**: Failed initialization triggers rollback of already-initialized plugins
|
|
28
|
+
- **Race Condition Prevention**: Concurrent `init()` calls are now properly handled
|
|
29
|
+
- **Late Registration**: Plugins registered after `kernel.init()` are automatically initialized
|
|
30
|
+
|
|
31
|
+
### Fixed
|
|
32
|
+
- Plugins registered after `kernel.init()` now have their `onInit` called
|
|
33
|
+
- Event listeners are automatically cleaned up when plugins are unregistered
|
|
34
|
+
- Circular dependencies are now detected and throw clear errors
|
|
35
|
+
- Partial initialization failures now properly rollback
|
|
36
|
+
|
|
37
|
+
### Testing
|
|
38
|
+
- Added 23 new test cases for Kernel improvements
|
|
39
|
+
- Added 8 new test cases for ScopedEventBus
|
|
40
|
+
- Added 6 new test cases for new error classes
|
|
41
|
+
- Test coverage: 98.36% statements, 100% functions
|
|
42
|
+
|
|
5
43
|
## [1.0.0] - 2026-01-15
|
|
6
44
|
|
|
7
45
|
### Added
|
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
[](https://opensource.org/licenses/MIT)
|
|
7
7
|
[](https://www.typescriptlang.org/)
|
|
8
8
|
[](https://nodejs.org/)
|
|
9
|
-
[](https://github.com/ersinkoc/utils)
|
|
10
10
|
|
|
11
11
|
Modern, tree-shakeable utility library for Node.js backend development with zero dependencies. Full TypeScript support with automatic type inference.
|
|
12
12
|
|
|
@@ -17,7 +17,7 @@ Modern, tree-shakeable utility library for Node.js backend development with zero
|
|
|
17
17
|
- ๐ณ **Tree-shakeable** - Import only what you need
|
|
18
18
|
- ๐ก๏ธ **Secure** - Prototype pollution protection built-in
|
|
19
19
|
- โก **Performant** - Immutable by default, optimized for Node.js backends
|
|
20
|
-
- ๐งช **Well-tested** -
|
|
20
|
+
- ๐งช **Well-tested** - 247 tests with 100% success rate
|
|
21
21
|
- ๐ **LLM-native** - Designed for AI/LLM code generation
|
|
22
22
|
|
|
23
23
|
## ๐ฆ Installation
|
|
@@ -166,6 +166,51 @@ import { compose, pipe, flip } from '@oxog/utils/transform';
|
|
|
166
166
|
| `compose(...fns)` | Compose functions right-to-left |
|
|
167
167
|
| `pipe(...fns)` | Compose functions left-to-right |
|
|
168
168
|
|
|
169
|
+
### Kernel (Plugin System)
|
|
170
|
+
|
|
171
|
+
| Function/Class | Description |
|
|
172
|
+
|----------------|-------------|
|
|
173
|
+
| `Kernel<TContext>` | Micro-kernel for plugin management |
|
|
174
|
+
| `kernel.register(plugin)` | Register a plugin |
|
|
175
|
+
| `kernel.init()` | Initialize all plugins (async) |
|
|
176
|
+
| `kernel.unregister(name)` | Unregister a plugin (async) |
|
|
177
|
+
| `kernel.destroy()` | Destroy kernel and all plugins (async) |
|
|
178
|
+
| `kernel.getPluginState(name)` | Get plugin lifecycle state |
|
|
179
|
+
| `kernel.getScopedEventBus(name)` | Get scoped event bus for plugin |
|
|
180
|
+
| `ScopedEventBus` | Auto-cleanup event bus for plugins |
|
|
181
|
+
|
|
182
|
+
```typescript
|
|
183
|
+
import { Kernel, type Plugin } from '@oxog/utils';
|
|
184
|
+
|
|
185
|
+
interface AppContext {
|
|
186
|
+
db: Database;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const kernel = new Kernel<AppContext>({
|
|
190
|
+
context: { db: myDatabase }
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
const loggerPlugin: Plugin<AppContext> = {
|
|
194
|
+
name: 'logger',
|
|
195
|
+
version: '1.0.0',
|
|
196
|
+
install: (kernel) => {
|
|
197
|
+
// Setup during registration
|
|
198
|
+
},
|
|
199
|
+
onInit: async (ctx) => {
|
|
200
|
+
// Async initialization
|
|
201
|
+
},
|
|
202
|
+
onDestroy: async () => {
|
|
203
|
+
// Cleanup on unregister
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
kernel.register(loggerPlugin);
|
|
208
|
+
await kernel.init();
|
|
209
|
+
|
|
210
|
+
// Plugin states: 'registered' | 'initializing' | 'active' | 'error' | 'destroyed'
|
|
211
|
+
console.log(kernel.getPluginState('logger')); // 'active'
|
|
212
|
+
```
|
|
213
|
+
|
|
169
214
|
## ๐ก Usage Examples
|
|
170
215
|
|
|
171
216
|
### Type-Safe Property Access
|
|
@@ -238,10 +283,10 @@ npm run test:watch
|
|
|
238
283
|
npm run test:coverage
|
|
239
284
|
```
|
|
240
285
|
|
|
241
|
-
- **
|
|
242
|
-
- **
|
|
286
|
+
- **247 tests** with 100% success rate
|
|
287
|
+
- **98.36% statement coverage**
|
|
243
288
|
- **100% function coverage**
|
|
244
|
-
- **
|
|
289
|
+
- **94.74% branch coverage**
|
|
245
290
|
|
|
246
291
|
## ๐ง Scripts
|
|
247
292
|
|
package/dist/index.cjs
CHANGED
|
@@ -366,6 +366,7 @@ function template(str, data) {
|
|
|
366
366
|
// src/utils/eventBus.ts
|
|
367
367
|
var EventBus = class {
|
|
368
368
|
listeners = /* @__PURE__ */ new Map();
|
|
369
|
+
scopedHandlers = /* @__PURE__ */ new Map();
|
|
369
370
|
/**
|
|
370
371
|
* Register an event listener.
|
|
371
372
|
*
|
|
@@ -432,6 +433,80 @@ var EventBus = class {
|
|
|
432
433
|
listenerCount(event) {
|
|
433
434
|
return this.listeners.get(event)?.size ?? 0;
|
|
434
435
|
}
|
|
436
|
+
/**
|
|
437
|
+
* Register an event listener with a scope (e.g., plugin name).
|
|
438
|
+
* Scoped listeners can be removed all at once using removeScope().
|
|
439
|
+
*
|
|
440
|
+
* @param scope - Scope identifier (e.g., plugin name)
|
|
441
|
+
* @param event - Event name to listen for
|
|
442
|
+
* @param handler - Function to call when event is emitted
|
|
443
|
+
*/
|
|
444
|
+
onScoped(scope, event, handler) {
|
|
445
|
+
this.on(event, handler);
|
|
446
|
+
if (!this.scopedHandlers.has(scope)) {
|
|
447
|
+
this.scopedHandlers.set(scope, /* @__PURE__ */ new Map());
|
|
448
|
+
}
|
|
449
|
+
const scopeMap = this.scopedHandlers.get(scope);
|
|
450
|
+
if (!scopeMap.has(event)) {
|
|
451
|
+
scopeMap.set(event, /* @__PURE__ */ new Set());
|
|
452
|
+
}
|
|
453
|
+
scopeMap.get(event).add(handler);
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* Remove all listeners registered under a specific scope.
|
|
457
|
+
* Useful for cleanup when a plugin is unregistered.
|
|
458
|
+
*
|
|
459
|
+
* @param scope - Scope identifier to remove
|
|
460
|
+
*/
|
|
461
|
+
removeScope(scope) {
|
|
462
|
+
const scopeMap = this.scopedHandlers.get(scope);
|
|
463
|
+
if (!scopeMap) return;
|
|
464
|
+
for (const [event, handlers] of scopeMap) {
|
|
465
|
+
for (const handler of handlers) {
|
|
466
|
+
this.off(event, handler);
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
this.scopedHandlers.delete(scope);
|
|
470
|
+
}
|
|
471
|
+
/**
|
|
472
|
+
* Check if a scope has any registered listeners.
|
|
473
|
+
*
|
|
474
|
+
* @param scope - Scope identifier
|
|
475
|
+
* @returns True if scope has listeners
|
|
476
|
+
*/
|
|
477
|
+
hasScope(scope) {
|
|
478
|
+
return this.scopedHandlers.has(scope);
|
|
479
|
+
}
|
|
480
|
+
};
|
|
481
|
+
var ScopedEventBus = class {
|
|
482
|
+
constructor(eventBus, scope) {
|
|
483
|
+
this.eventBus = eventBus;
|
|
484
|
+
this.scope = scope;
|
|
485
|
+
}
|
|
486
|
+
/**
|
|
487
|
+
* Register an event listener (automatically scoped).
|
|
488
|
+
*/
|
|
489
|
+
on(event, handler) {
|
|
490
|
+
this.eventBus.onScoped(this.scope, event, handler);
|
|
491
|
+
}
|
|
492
|
+
/**
|
|
493
|
+
* Unregister an event listener.
|
|
494
|
+
*/
|
|
495
|
+
off(event, handler) {
|
|
496
|
+
this.eventBus.off(event, handler);
|
|
497
|
+
}
|
|
498
|
+
/**
|
|
499
|
+
* Emit an event.
|
|
500
|
+
*/
|
|
501
|
+
emit(event, ...args) {
|
|
502
|
+
this.eventBus.emit(event, ...args);
|
|
503
|
+
}
|
|
504
|
+
/**
|
|
505
|
+
* Remove all listeners registered by this scoped bus.
|
|
506
|
+
*/
|
|
507
|
+
destroy() {
|
|
508
|
+
this.eventBus.removeScope(this.scope);
|
|
509
|
+
}
|
|
435
510
|
};
|
|
436
511
|
|
|
437
512
|
// src/errors.ts
|
|
@@ -444,6 +519,24 @@ var UtilsError = class extends Error {
|
|
|
444
519
|
Error.captureStackTrace(this, this.constructor);
|
|
445
520
|
}
|
|
446
521
|
};
|
|
522
|
+
var InvalidPathError = class extends UtilsError {
|
|
523
|
+
constructor(path) {
|
|
524
|
+
super(`Invalid path: ${path}`, "INVALID_PATH");
|
|
525
|
+
this.name = "InvalidPathError";
|
|
526
|
+
}
|
|
527
|
+
};
|
|
528
|
+
var CircularReferenceError = class extends UtilsError {
|
|
529
|
+
constructor() {
|
|
530
|
+
super("Circular reference detected", "CIRCULAR_REF");
|
|
531
|
+
this.name = "CircularReferenceError";
|
|
532
|
+
}
|
|
533
|
+
};
|
|
534
|
+
var TimeoutError = class extends UtilsError {
|
|
535
|
+
constructor(ms) {
|
|
536
|
+
super(`Operation timed out after ${ms}ms`, "TIMEOUT");
|
|
537
|
+
this.name = "TimeoutError";
|
|
538
|
+
}
|
|
539
|
+
};
|
|
447
540
|
var PluginAlreadyRegisteredError = class extends UtilsError {
|
|
448
541
|
constructor(name) {
|
|
449
542
|
super(`Plugin "${name}" already registered`, "PLUGIN_ALREADY_REGISTERED");
|
|
@@ -462,15 +555,57 @@ var PluginDependencyMissingError = class extends UtilsError {
|
|
|
462
555
|
this.name = "PluginDependencyMissingError";
|
|
463
556
|
}
|
|
464
557
|
};
|
|
558
|
+
var MaxRetriesExceededError = class extends UtilsError {
|
|
559
|
+
constructor(attempts) {
|
|
560
|
+
super(`Maximum retry attempts (${attempts}) exceeded`, "MAX_RETRIES_EXCEEDED");
|
|
561
|
+
this.name = "MaxRetriesExceededError";
|
|
562
|
+
}
|
|
563
|
+
};
|
|
564
|
+
var CircularDependencyError = class extends UtilsError {
|
|
565
|
+
constructor(cycle) {
|
|
566
|
+
super(`Circular dependency detected: ${cycle.join(" -> ")}`, "CIRCULAR_DEPENDENCY");
|
|
567
|
+
this.name = "CircularDependencyError";
|
|
568
|
+
}
|
|
569
|
+
};
|
|
570
|
+
var PluginInitializationError = class extends UtilsError {
|
|
571
|
+
pluginName;
|
|
572
|
+
cause;
|
|
573
|
+
constructor(pluginName, cause) {
|
|
574
|
+
super(`Plugin "${pluginName}" failed to initialize: ${cause.message}`, "PLUGIN_INIT_FAILED");
|
|
575
|
+
this.name = "PluginInitializationError";
|
|
576
|
+
this.pluginName = pluginName;
|
|
577
|
+
this.cause = cause;
|
|
578
|
+
}
|
|
579
|
+
};
|
|
580
|
+
var KernelAlreadyInitializedError = class extends UtilsError {
|
|
581
|
+
constructor() {
|
|
582
|
+
super("Kernel is already initialized", "KERNEL_ALREADY_INITIALIZED");
|
|
583
|
+
this.name = "KernelAlreadyInitializedError";
|
|
584
|
+
}
|
|
585
|
+
};
|
|
586
|
+
var KernelInitializingError = class extends UtilsError {
|
|
587
|
+
constructor() {
|
|
588
|
+
super("Kernel is currently initializing", "KERNEL_INITIALIZING");
|
|
589
|
+
this.name = "KernelInitializingError";
|
|
590
|
+
}
|
|
591
|
+
};
|
|
465
592
|
|
|
466
593
|
// src/kernel.ts
|
|
467
594
|
var Kernel = class {
|
|
468
595
|
plugins = /* @__PURE__ */ new Map();
|
|
469
|
-
context
|
|
596
|
+
context;
|
|
470
597
|
eventBus = new EventBus();
|
|
598
|
+
state = "idle";
|
|
599
|
+
initPromise = null;
|
|
600
|
+
constructor(options) {
|
|
601
|
+
this.context = options?.context ?? {};
|
|
602
|
+
}
|
|
471
603
|
/**
|
|
472
604
|
* Register a plugin with the kernel.
|
|
473
605
|
*
|
|
606
|
+
* If the kernel is already initialized, the plugin will be
|
|
607
|
+
* automatically initialized after registration.
|
|
608
|
+
*
|
|
474
609
|
* @param plugin - Plugin to register
|
|
475
610
|
* @throws {PluginAlreadyRegisteredError} If plugin name already exists
|
|
476
611
|
* @throws {PluginDependencyMissingError} If plugin dependencies are not met
|
|
@@ -486,51 +621,177 @@ var Kernel = class {
|
|
|
486
621
|
}
|
|
487
622
|
}
|
|
488
623
|
}
|
|
489
|
-
this.
|
|
624
|
+
const scopedBus = new ScopedEventBus(this.eventBus, plugin.name);
|
|
625
|
+
const entry = {
|
|
626
|
+
plugin,
|
|
627
|
+
state: "registered",
|
|
628
|
+
scopedBus
|
|
629
|
+
};
|
|
630
|
+
this.plugins.set(plugin.name, entry);
|
|
490
631
|
try {
|
|
491
632
|
plugin.install(this);
|
|
492
633
|
} catch (error) {
|
|
493
634
|
this.plugins.delete(plugin.name);
|
|
635
|
+
scopedBus.destroy();
|
|
494
636
|
throw error;
|
|
495
637
|
}
|
|
496
638
|
this.eventBus.emit("plugin:registered", { name: plugin.name, version: plugin.version });
|
|
639
|
+
if (this.state === "initialized") {
|
|
640
|
+
this.initPlugin(entry).catch((error) => {
|
|
641
|
+
entry.state = "error";
|
|
642
|
+
this.eventBus.emit("plugin:error", { name: plugin.name, error });
|
|
643
|
+
});
|
|
644
|
+
}
|
|
497
645
|
}
|
|
498
646
|
/**
|
|
499
647
|
* Initialize all registered plugins.
|
|
500
648
|
*
|
|
501
|
-
*
|
|
502
|
-
*
|
|
649
|
+
* Plugins are initialized in topological order based on dependencies.
|
|
650
|
+
* If initialization fails, already initialized plugins are rolled back.
|
|
651
|
+
*
|
|
652
|
+
* @throws {CircularDependencyError} If circular dependency detected
|
|
653
|
+
* @throws {PluginInitializationError} If a plugin fails to initialize
|
|
503
654
|
*/
|
|
504
655
|
async init() {
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
656
|
+
if (this.state === "initialized") {
|
|
657
|
+
return;
|
|
658
|
+
}
|
|
659
|
+
if (this.state === "initializing") {
|
|
660
|
+
return this.initPromise;
|
|
661
|
+
}
|
|
662
|
+
if (this.state === "destroyed") {
|
|
663
|
+
throw new Error("Cannot initialize destroyed kernel");
|
|
664
|
+
}
|
|
665
|
+
this.state = "initializing";
|
|
666
|
+
this.initPromise = this.doInit();
|
|
667
|
+
try {
|
|
668
|
+
await this.initPromise;
|
|
669
|
+
} finally {
|
|
670
|
+
this.initPromise = null;
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
async doInit() {
|
|
674
|
+
const sortedEntries = this.topologicalSort();
|
|
675
|
+
const initializedStack = [];
|
|
676
|
+
for (const entry of sortedEntries) {
|
|
677
|
+
try {
|
|
678
|
+
await this.initPlugin(entry);
|
|
679
|
+
initializedStack.push(entry);
|
|
680
|
+
} catch (error) {
|
|
681
|
+
entry.state = "error";
|
|
682
|
+
for (const initialized of initializedStack.reverse()) {
|
|
683
|
+
try {
|
|
684
|
+
await this.destroyPlugin(initialized);
|
|
685
|
+
} catch (destroyError) {
|
|
686
|
+
console.error(
|
|
687
|
+
`Error during rollback of plugin "${initialized.plugin.name}":`,
|
|
688
|
+
destroyError
|
|
689
|
+
);
|
|
512
690
|
}
|
|
513
|
-
throw error;
|
|
514
691
|
}
|
|
692
|
+
this.state = "idle";
|
|
693
|
+
throw new PluginInitializationError(
|
|
694
|
+
entry.plugin.name,
|
|
695
|
+
error instanceof Error ? error : new Error(String(error))
|
|
696
|
+
);
|
|
515
697
|
}
|
|
516
698
|
}
|
|
699
|
+
this.state = "initialized";
|
|
517
700
|
this.eventBus.emit("kernel:initialized");
|
|
518
701
|
}
|
|
702
|
+
/**
|
|
703
|
+
* Initialize a single plugin.
|
|
704
|
+
*/
|
|
705
|
+
async initPlugin(entry) {
|
|
706
|
+
if (entry.state !== "registered") {
|
|
707
|
+
return;
|
|
708
|
+
}
|
|
709
|
+
entry.state = "initializing";
|
|
710
|
+
this.eventBus.emit("plugin:initializing", { name: entry.plugin.name });
|
|
711
|
+
if (entry.plugin.onInit) {
|
|
712
|
+
try {
|
|
713
|
+
await entry.plugin.onInit(this.context);
|
|
714
|
+
} catch (error) {
|
|
715
|
+
if (entry.plugin.onError) {
|
|
716
|
+
entry.plugin.onError(error);
|
|
717
|
+
}
|
|
718
|
+
throw error;
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
entry.state = "active";
|
|
722
|
+
this.eventBus.emit("plugin:initialized", { name: entry.plugin.name });
|
|
723
|
+
}
|
|
724
|
+
/**
|
|
725
|
+
* Destroy a single plugin.
|
|
726
|
+
*/
|
|
727
|
+
async destroyPlugin(entry) {
|
|
728
|
+
if (entry.state === "destroyed") {
|
|
729
|
+
return;
|
|
730
|
+
}
|
|
731
|
+
entry.scopedBus.destroy();
|
|
732
|
+
if (entry.plugin.onDestroy) {
|
|
733
|
+
await entry.plugin.onDestroy();
|
|
734
|
+
}
|
|
735
|
+
entry.state = "destroyed";
|
|
736
|
+
}
|
|
737
|
+
/**
|
|
738
|
+
* Topological sort of plugins based on dependencies.
|
|
739
|
+
* Detects circular dependencies.
|
|
740
|
+
*
|
|
741
|
+
* @returns Plugins sorted in dependency order
|
|
742
|
+
* @throws {CircularDependencyError} If circular dependency detected
|
|
743
|
+
*/
|
|
744
|
+
topologicalSort() {
|
|
745
|
+
const result = [];
|
|
746
|
+
const visited = /* @__PURE__ */ new Set();
|
|
747
|
+
const visiting = /* @__PURE__ */ new Set();
|
|
748
|
+
const visit = (name, path) => {
|
|
749
|
+
if (visited.has(name)) return;
|
|
750
|
+
if (visiting.has(name)) {
|
|
751
|
+
const cycleStart = path.indexOf(name);
|
|
752
|
+
const cycle = [...path.slice(cycleStart), name];
|
|
753
|
+
throw new CircularDependencyError(cycle);
|
|
754
|
+
}
|
|
755
|
+
const entry = this.plugins.get(name);
|
|
756
|
+
if (!entry) return;
|
|
757
|
+
visiting.add(name);
|
|
758
|
+
const currentPath = [...path, name];
|
|
759
|
+
if (entry.plugin.dependencies) {
|
|
760
|
+
for (const dep of entry.plugin.dependencies) {
|
|
761
|
+
visit(dep, currentPath);
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
visiting.delete(name);
|
|
765
|
+
visited.add(name);
|
|
766
|
+
result.push(entry);
|
|
767
|
+
};
|
|
768
|
+
for (const name of this.plugins.keys()) {
|
|
769
|
+
visit(name, []);
|
|
770
|
+
}
|
|
771
|
+
return result;
|
|
772
|
+
}
|
|
519
773
|
/**
|
|
520
774
|
* Unregister a plugin.
|
|
521
775
|
*
|
|
776
|
+
* Automatically cleans up event listeners registered by this plugin.
|
|
777
|
+
*
|
|
522
778
|
* @param name - Plugin name to unregister
|
|
523
779
|
* @throws {PluginNotFoundError} If plugin doesn't exist
|
|
524
780
|
*/
|
|
525
|
-
unregister(name) {
|
|
526
|
-
const
|
|
527
|
-
if (!
|
|
781
|
+
async unregister(name) {
|
|
782
|
+
const entry = this.plugins.get(name);
|
|
783
|
+
if (!entry) {
|
|
528
784
|
throw new PluginNotFoundError(name);
|
|
529
785
|
}
|
|
530
|
-
this.plugins
|
|
531
|
-
|
|
532
|
-
|
|
786
|
+
for (const [otherName, otherEntry] of this.plugins) {
|
|
787
|
+
if (otherName !== name && otherEntry.plugin.dependencies?.includes(name)) {
|
|
788
|
+
throw new Error(
|
|
789
|
+
`Cannot unregister plugin "${name}": plugin "${otherName}" depends on it`
|
|
790
|
+
);
|
|
791
|
+
}
|
|
533
792
|
}
|
|
793
|
+
await this.destroyPlugin(entry);
|
|
794
|
+
this.plugins.delete(name);
|
|
534
795
|
this.eventBus.emit("plugin:unregistered", { name });
|
|
535
796
|
}
|
|
536
797
|
/**
|
|
@@ -548,7 +809,16 @@ var Kernel = class {
|
|
|
548
809
|
* @returns Plugin instance or undefined
|
|
549
810
|
*/
|
|
550
811
|
getPlugin(name) {
|
|
551
|
-
return this.plugins.get(name);
|
|
812
|
+
return this.plugins.get(name)?.plugin;
|
|
813
|
+
}
|
|
814
|
+
/**
|
|
815
|
+
* Get the state of a registered plugin.
|
|
816
|
+
*
|
|
817
|
+
* @param name - Plugin name
|
|
818
|
+
* @returns Plugin state or undefined if not found
|
|
819
|
+
*/
|
|
820
|
+
getPluginState(name) {
|
|
821
|
+
return this.plugins.get(name)?.state;
|
|
552
822
|
}
|
|
553
823
|
/**
|
|
554
824
|
* Check if a plugin is registered.
|
|
@@ -567,6 +837,33 @@ var Kernel = class {
|
|
|
567
837
|
listPlugins() {
|
|
568
838
|
return Array.from(this.plugins.keys());
|
|
569
839
|
}
|
|
840
|
+
/**
|
|
841
|
+
* Get the kernel's current state.
|
|
842
|
+
*
|
|
843
|
+
* @returns Current kernel state
|
|
844
|
+
*/
|
|
845
|
+
getState() {
|
|
846
|
+
return this.state;
|
|
847
|
+
}
|
|
848
|
+
/**
|
|
849
|
+
* Check if kernel is initialized.
|
|
850
|
+
*
|
|
851
|
+
* @returns True if kernel is initialized
|
|
852
|
+
*/
|
|
853
|
+
isInitialized() {
|
|
854
|
+
return this.state === "initialized";
|
|
855
|
+
}
|
|
856
|
+
/**
|
|
857
|
+
* Get the scoped event bus for a plugin.
|
|
858
|
+
* Listeners registered through this bus are automatically
|
|
859
|
+
* cleaned up when the plugin is unregistered.
|
|
860
|
+
*
|
|
861
|
+
* @param pluginName - Plugin name
|
|
862
|
+
* @returns Scoped event bus or undefined
|
|
863
|
+
*/
|
|
864
|
+
getScopedEventBus(pluginName) {
|
|
865
|
+
return this.plugins.get(pluginName)?.scopedBus;
|
|
866
|
+
}
|
|
570
867
|
/**
|
|
571
868
|
* Register an event listener.
|
|
572
869
|
*
|
|
@@ -597,18 +894,42 @@ var Kernel = class {
|
|
|
597
894
|
/**
|
|
598
895
|
* Destroy the kernel and all plugins.
|
|
599
896
|
*
|
|
600
|
-
* Unregisters all plugins and cleans up resources.
|
|
897
|
+
* Unregisters all plugins in reverse dependency order and cleans up resources.
|
|
601
898
|
*/
|
|
602
899
|
async destroy() {
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
900
|
+
if (this.state === "destroyed") {
|
|
901
|
+
return;
|
|
902
|
+
}
|
|
903
|
+
const sortedEntries = this.topologicalSort().reverse();
|
|
904
|
+
for (const entry of sortedEntries) {
|
|
905
|
+
try {
|
|
906
|
+
await this.destroyPlugin(entry);
|
|
907
|
+
} catch (error) {
|
|
908
|
+
console.error(`Error destroying plugin "${entry.plugin.name}":`, error);
|
|
909
|
+
}
|
|
606
910
|
}
|
|
911
|
+
this.plugins.clear();
|
|
607
912
|
this.eventBus.removeAllListeners();
|
|
913
|
+
this.state = "destroyed";
|
|
914
|
+
this.eventBus.emit("kernel:destroyed");
|
|
608
915
|
}
|
|
609
916
|
};
|
|
610
917
|
|
|
918
|
+
exports.CircularDependencyError = CircularDependencyError;
|
|
919
|
+
exports.CircularReferenceError = CircularReferenceError;
|
|
920
|
+
exports.EventBus = EventBus;
|
|
921
|
+
exports.InvalidPathError = InvalidPathError;
|
|
611
922
|
exports.Kernel = Kernel;
|
|
923
|
+
exports.KernelAlreadyInitializedError = KernelAlreadyInitializedError;
|
|
924
|
+
exports.KernelInitializingError = KernelInitializingError;
|
|
925
|
+
exports.MaxRetriesExceededError = MaxRetriesExceededError;
|
|
926
|
+
exports.PluginAlreadyRegisteredError = PluginAlreadyRegisteredError;
|
|
927
|
+
exports.PluginDependencyMissingError = PluginDependencyMissingError;
|
|
928
|
+
exports.PluginInitializationError = PluginInitializationError;
|
|
929
|
+
exports.PluginNotFoundError = PluginNotFoundError;
|
|
930
|
+
exports.ScopedEventBus = ScopedEventBus;
|
|
931
|
+
exports.TimeoutError = TimeoutError;
|
|
932
|
+
exports.UtilsError = UtilsError;
|
|
612
933
|
exports.array = array_exports;
|
|
613
934
|
exports.camelCase = camelCase;
|
|
614
935
|
exports.capitalize = capitalize;
|