@fraqjs/fraq 0.6.0 → 0.6.2

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/dist/index.d.mts CHANGED
@@ -5624,6 +5624,7 @@ interface Disposable {
5624
5624
  dispose(): void | Promise<void>;
5625
5625
  }
5626
5626
  declare function isDisposable(service: object): service is Disposable;
5627
+ declare function implementsESNextDisposable(service: object): boolean;
5627
5628
  //#endregion
5628
5629
  //#region src/core/plugin.d.ts
5629
5630
  type ParameterList = Array<any>;
@@ -5646,6 +5647,7 @@ interface Plugin<
5646
5647
  optionalInject?: OI;
5647
5648
  provides?: readonly ServiceClass[];
5648
5649
  apply(ctx: Context & InjectedServices<I> & OptionalInjectedServices<OI>, ...args: T): void | Promise<void>;
5650
+ start?(ctx: Context & InjectedServices<I> & OptionalInjectedServices<OI>): void | Promise<void>;
5649
5651
  }
5650
5652
  declare function definePlugin<
5651
5653
  T extends ParameterList,
@@ -5706,12 +5708,14 @@ declare class Context {
5706
5708
  private stopEventStream;
5707
5709
  private acceptsParentEvent;
5708
5710
  private applyPlugins;
5711
+ private startPlugins;
5709
5712
  private sortPlugins;
5710
5713
  private collectPendingProvidedServices;
5711
5714
  private areRequiredServicesAvailable;
5712
5715
  private areOptionalServicesReady;
5713
5716
  private collectAvailableServices;
5714
5717
  private createUnresolvablePluginError;
5718
+ private getPluginContext;
5715
5719
  private createProxyContextForPlugin;
5716
5720
  private runEventStream;
5717
5721
  private waitForReconnectDelay;
@@ -5794,6 +5798,7 @@ export {
5794
5798
  createMilkyClient,
5795
5799
  definePlugin,
5796
5800
  filter,
5801
+ implementsESNextDisposable,
5797
5802
  isDisposable,
5798
5803
  type types_d_exports as milky,
5799
5804
  milkyPackageVersion,
package/dist/index.mjs CHANGED
@@ -363,6 +363,9 @@ function combineLogHandlers(...handlers) {
363
363
  function isDisposable(service) {
364
364
  return "dispose" in service && typeof service.dispose === "function";
365
365
  }
366
+ function implementsESNextDisposable(service) {
367
+ return Symbol.dispose in service && typeof service[Symbol.dispose] === "function";
368
+ }
366
369
  //#endregion
367
370
  //#region src/core/context.ts
368
371
  const DEFAULT_INITIAL_RECONNECT_DELAY_MS = 1e3;
@@ -436,6 +439,14 @@ var Context = class Context {
436
439
  }
437
440
  provide(service, instance) {
438
441
  if (this.services.has(service)) throw new Error(`Service ${service.name} has already been provided in this context.`);
442
+ if (implementsESNextDisposable(instance) && !isDisposable(instance)) throw new Error(`
443
+ Service ${service.name} implements ESNext Disposable but not Fraq Disposable.
444
+ Please explicitly import the interface like this:
445
+
446
+ import type { Disposable } from '@fraqjs/fraq';
447
+
448
+ and implement the dispose method to clean up resources when the context stops.
449
+ `.trim());
439
450
  this.services.set(service, instance);
440
451
  }
441
452
  resolve(service) {
@@ -516,7 +527,9 @@ var Context = class Context {
516
527
  }
517
528
  async startInternal() {
518
529
  try {
519
- await this.applyPlugins();
530
+ const sortedPlugins = this.sortPlugins();
531
+ await this.applyPlugins(sortedPlugins);
532
+ await this.startPlugins(sortedPlugins);
520
533
  } catch (error) {
521
534
  this.state = "idle";
522
535
  throw error;
@@ -569,13 +582,21 @@ var Context = class Context {
569
582
  const predicate = this.filter[type];
570
583
  return predicate?.(event) === true;
571
584
  }
572
- async applyPlugins() {
573
- for (const { plugin, args } of this.sortPlugins()) {
585
+ async applyPlugins(sortedPlugins) {
586
+ for (const installedPlugin of sortedPlugins) {
587
+ const { plugin, args } = installedPlugin;
574
588
  const providedBeforeApply = new Set(this.services.keys());
575
- this.logger.info(`Applying plugin ${plugin.name}`);
576
- await plugin.apply(this.createProxyContextForPlugin(plugin), ...args);
589
+ this.logger.debug(`Applying plugin ${plugin.name}`);
590
+ await plugin.apply(this.getPluginContext(installedPlugin), ...args);
577
591
  for (const service of plugin.provides ?? []) if (!this.services.has(service) || providedBeforeApply.has(service)) throw new Error(`${plugin.name} declares service ${service.name} but did not provide it.`);
578
- this.logger.debug(`Applied plugin ${plugin.name}`);
592
+ }
593
+ }
594
+ async startPlugins(sortedPlugins) {
595
+ for (const installedPlugin of sortedPlugins) {
596
+ const { plugin } = installedPlugin;
597
+ if (!plugin.start) continue;
598
+ this.logger.debug(`Plugin ${plugin.name} is starting...`);
599
+ await plugin.start(this.getPluginContext(installedPlugin));
579
600
  }
580
601
  }
581
602
  sortPlugins() {
@@ -637,6 +658,10 @@ var Context = class Context {
637
658
  });
638
659
  return /* @__PURE__ */ new Error(`Unable to resolve plugin service dependencies: ${lines.join("; ")}.`);
639
660
  }
661
+ getPluginContext(installedPlugin) {
662
+ installedPlugin.proxy ??= this.createProxyContextForPlugin(installedPlugin.plugin);
663
+ return installedPlugin.proxy;
664
+ }
640
665
  createProxyContextForPlugin(plugin) {
641
666
  const proxyLogger = new Logger((message) => this.logHandler?.(message), plugin.name);
642
667
  let proxyInjections;
@@ -1088,4 +1113,4 @@ let param;
1088
1113
  _param.segment = segment;
1089
1114
  })(param || (param = {}));
1090
1115
  //#endregion
1091
- export { Context, Logger, Parameter, Router, combineLogHandlers, createMilkyClient, definePlugin, filter, isDisposable, milkyPackageVersion, milkyVersion, msg, param, seg };
1116
+ export { Context, Logger, Parameter, Router, combineLogHandlers, createMilkyClient, definePlugin, filter, implementsESNextDisposable, isDisposable, milkyPackageVersion, milkyVersion, msg, param, seg };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@fraqjs/fraq",
3
3
  "type": "module",
4
- "version": "0.6.0",
4
+ "version": "0.6.2",
5
5
  "description": "Milky Bot framework",
6
6
  "files": [
7
7
  "dist"